Skip to content

Commit be4f65a

Browse files
authored
add policies api endpoints (#1897)
1 parent 84f1c0e commit be4f65a

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

backend/bootstrap/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
231231
vcsApiGroup.GET("/", controllers.ListVCSConnectionsApi)
232232
vcsApiGroup.POST("/", controllers.CreateVCSConnectionApi)
233233
vcsApiGroup.DELETE("/:id", controllers.DeleteVCSConnection)
234+
235+
policyApiGroup := apiGroup.Group("/policies")
236+
policyApiGroup.GET("/:policy_type", controllers.PolicyOrgGetApi)
237+
policyApiGroup.PUT("/", controllers.PolicyOrgUpsertApi)
234238
}
235239

236240
return r

backend/controllers/policies_api.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package controllers
2+
3+
import (
4+
"errors"
5+
"github.com/diggerhq/digger/backend/middleware"
6+
"github.com/diggerhq/digger/backend/models"
7+
"github.com/gin-gonic/gin"
8+
"gorm.io/gorm"
9+
"log"
10+
"net/http"
11+
)
12+
13+
func PolicyOrgGetApi(c *gin.Context) {
14+
policyType := c.Param("policy_type")
15+
16+
if policyType != "plan" && policyType != "access" {
17+
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid policy type requested: " + policyType})
18+
return
19+
}
20+
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
21+
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)
22+
23+
var org models.Organisation
24+
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
25+
if err != nil {
26+
if errors.Is(err, gorm.ErrRecordNotFound) {
27+
log.Printf("could not find organisation: %v", err)
28+
c.JSON(http.StatusNotFound, gin.H{"status": "Could not find organisation: " + organisationId})
29+
} else {
30+
log.Printf("database error while finding organisation: %v", err)
31+
c.JSON(http.StatusInternalServerError, gin.H{"status": "Internal server error"})
32+
}
33+
return
34+
}
35+
36+
var policy models.Policy
37+
query := JoinedOrganisationRepoProjectQuery()
38+
err = query.
39+
Where("organisations.id = ? AND (repos.id IS NULL AND projects.id IS NULL) AND policies.type = ? ", org.ID, policyType).
40+
First(&policy).Error
41+
if err != nil {
42+
if errors.Is(err, gorm.ErrRecordNotFound) {
43+
c.JSON(http.StatusNotFound, gin.H{"error": "Could not find policy for organisation ext ID: " + organisationId})
44+
} else {
45+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown error occurred while fetching database"})
46+
}
47+
return
48+
}
49+
50+
c.JSON(http.StatusOK, gin.H{"result": policy.Policy})
51+
}
52+
53+
func PolicyOrgUpsertApi(c *gin.Context) {
54+
type PolicyUpsertRequest struct {
55+
PolicyType string `json:"policy_type"`
56+
PolicyText string `json:"policy_text"`
57+
}
58+
59+
var request PolicyUpsertRequest
60+
err := c.BindJSON(&request)
61+
if err != nil {
62+
log.Printf("Error binding JSON: %v", err)
63+
c.JSON(http.StatusBadRequest, gin.H{"status": "Invalid request format"})
64+
return
65+
}
66+
67+
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
68+
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)
69+
70+
var org models.Organisation
71+
err = models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
72+
if err != nil {
73+
if errors.Is(err, gorm.ErrRecordNotFound) {
74+
log.Printf("could not find organisation: %v", err)
75+
c.JSON(http.StatusNotFound, gin.H{"status": "Could not find organisation: " + organisationId})
76+
} else {
77+
log.Printf("database error while finding organisation: %v", err)
78+
c.JSON(http.StatusInternalServerError, gin.H{"status": "Internal server error"})
79+
}
80+
return
81+
}
82+
83+
policyType := request.PolicyType
84+
policyData := request.PolicyText
85+
86+
policy := models.Policy{}
87+
88+
policyResult := models.DB.GormDB.Where("organisation_id = ? AND (repo_id IS NULL AND project_id IS NULL) AND type = ?", org.ID, policyType).Take(&policy)
89+
90+
if policyResult.RowsAffected == 0 {
91+
err := models.DB.GormDB.Create(&models.Policy{
92+
OrganisationID: org.ID,
93+
Type: policyType,
94+
Policy: policyData,
95+
}).Error
96+
97+
if err != nil {
98+
log.Printf("Error creating policy: %v", err)
99+
c.String(http.StatusInternalServerError, "Error creating policy")
100+
return
101+
}
102+
} else {
103+
policy.Policy = policyData
104+
err := models.DB.GormDB.Save(policy).Error
105+
if err != nil {
106+
log.Printf("Error updating policy: %v", err)
107+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating policy"})
108+
return
109+
}
110+
}
111+
112+
c.JSON(http.StatusOK, gin.H{"success": true})
113+
114+
}

0 commit comments

Comments
 (0)