Skip to content

Commit 10df383

Browse files
authored
plan approval (#1327)
1 parent d9bc8f9 commit 10df383

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

backend/controllers/runs.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"net/http"
1212
"strconv"
13+
"time"
1314
)
1415

1516
func RunsForProject(c *gin.Context) {
@@ -127,3 +128,72 @@ func RunDetails(c *gin.Context) {
127128
}
128129
c.JSON(http.StatusOK, response)
129130
}
131+
132+
func ApproveRun(c *gin.Context) {
133+
134+
currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
135+
runIdStr := c.Param("run_id")
136+
137+
if runIdStr == "" {
138+
c.String(http.StatusBadRequest, "RunID not specified")
139+
return
140+
}
141+
142+
runId, err := strconv.Atoi(runIdStr)
143+
if err != nil {
144+
c.String(http.StatusBadRequest, "Invalid RunId")
145+
return
146+
}
147+
148+
if !exists {
149+
c.String(http.StatusForbidden, "Not allowed to access this resource")
150+
return
151+
}
152+
153+
var org models.Organisation
154+
err = models.DB.GormDB.Where("id = ?", currentOrg).First(&org).Error
155+
if err != nil {
156+
if errors.Is(err, gorm.ErrRecordNotFound) {
157+
c.String(http.StatusNotFound, fmt.Sprintf("Could not find organisation: %v", currentOrg))
158+
} else {
159+
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database")
160+
}
161+
return
162+
}
163+
164+
run, err := models.DB.GetDiggerRun(uint(runId))
165+
if err != nil {
166+
log.Printf("Could not fetch run: %v", err)
167+
c.String(http.StatusBadRequest, "Could not fetch run, please check that it exists")
168+
}
169+
if run.Repo.OrganisationID != org.ID {
170+
c.String(http.StatusForbidden, "Not allowed to access this resource")
171+
return
172+
}
173+
174+
if run.Status != models.RunPendingApproval {
175+
log.Printf("Run status not ready for approval: %v", run.ID)
176+
c.String(http.StatusBadRequest, "Approval not possible for run (%v) because status is %v", run.ID, run.Status)
177+
return
178+
}
179+
180+
if run.IsApproved == false {
181+
run.ApprovalAuthor = "a_user"
182+
run.IsApproved = true
183+
run.ApprovalDate = time.Now()
184+
err := models.DB.UpdateDiggerRun(run)
185+
if err != nil {
186+
log.Printf("Could update run: %v", err)
187+
c.String(http.StatusInternalServerError, "Could not update approval")
188+
}
189+
} else {
190+
log.Printf("Run has already been approved")
191+
}
192+
193+
response, err := run.MapToJsonStruct()
194+
if err != nil {
195+
c.String(http.StatusInternalServerError, "Could not unmarshall data")
196+
return
197+
}
198+
c.JSON(http.StatusOK, response)
199+
}

backend/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func main() {
170170
runsApiGroup := apiGroup.Group("/runs")
171171
runsApiGroup.Use(middleware.GetWebMiddleware())
172172
runsApiGroup.GET("/:run_id", controllers.RunDetails)
173+
runsApiGroup.POST("/:run_id/approve", controllers.ApproveRun)
173174

174175
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
175176

backend/migrations/20240405150942.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "digger_runs" table
2+
ALTER TABLE "public"."digger_runs" ADD COLUMN "is_approved" boolean NULL, ADD COLUMN "approval_author" text NULL, ADD COLUMN "approval_date" timestamptz NULL;

backend/migrations/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:xkkYzycO+Sb/rIvE5VwTvzTb6dA6kyuQQ1/ZT/Oy9no=
1+
h1:ksWZreAh93d05Lo6QT68Dm9v5UNw2aFgtP0wiLz+J34=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -15,3 +15,4 @@ h1:xkkYzycO+Sb/rIvE5VwTvzTb6dA6kyuQQ1/ZT/Oy9no=
1515
20240404161121.sql h1:ZbMLfHRom6Tws+2M3BcnMu1lcjz/YFwAI8kGmC+I+H4=
1616
20240404161723.sql h1:z3bJcKs0ZJSyTJewqgE0GSHpn33sX7zgc2rmCMF99Qo=
1717
20240404165910.sql h1:ofwrBzkvnxFz7sOrtaF3vb2xHsenPmUTSSBHvO1NEdI=
18+
20240405150942.sql h1:0JIQlXqQmfgfBcill47gAef3LnnfdwK6ry98eHraUbo=

backend/models/runs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type DiggerRun struct {
5151
PlanStageId *uint
5252
ApplyStage DiggerRunStage
5353
ApplyStageId *uint
54+
IsApproved bool
55+
ApprovalAuthor string
56+
ApprovalDate time.Time
5457
}
5558

5659
type DiggerRunStage struct {
@@ -89,18 +92,21 @@ func (r *DiggerRun) MapToJsonStruct() (interface{}, error) {
8992
Status string `json:"status"`
9093
Type string `json:"type"`
9194
ApprovalAuthor string `json:"approval_author"`
92-
ApprovalStatus string `json:"approval_status"`
9395
ApprovalDate string `json:"approval_date"`
9496
LastActivityTimeStamp string `json:"last_activity_time_stamp"`
9597
PlanStage SerializedRunStage `json:"plan_stage"`
9698
ApplyStage SerializedRunStage `json:"apply_stage"`
99+
IsApproved bool `json:"is_approved"`
97100
}{
98101
Id: r.ID,
99102
Status: string(r.Status),
100103
Type: string(r.RunType),
101104
LastActivityTimeStamp: r.UpdatedAt.String(),
102105
PlanStage: *planStage,
103106
ApplyStage: *applyStage,
107+
IsApproved: r.IsApproved,
108+
ApprovalAuthor: r.ApprovalAuthor,
109+
ApprovalDate: r.ApprovalDate.String(),
104110
}
105111

106112
return x, nil

backend/models/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status Dig
699699
RunType: RunType,
700700
PlanStageId: planStageId,
701701
ApplyStageId: applyStageId,
702+
IsApproved: false,
702703
}
703704
result := db.GormDB.Save(dr)
704705
if result.Error != nil {

0 commit comments

Comments
 (0)