Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add policies api endpoints #1897

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
vcsApiGroup.GET("/", controllers.ListVCSConnectionsApi)
vcsApiGroup.POST("/", controllers.CreateVCSConnectionApi)
vcsApiGroup.DELETE("/:id", controllers.DeleteVCSConnection)

policyApiGroup := apiGroup.Group("/policies")
policyApiGroup.GET("/:policy_type", controllers.PolicyOrgGetApi)
policyApiGroup.PUT("/", controllers.PolicyOrgUpsertApi)
}

return r
Expand Down
114 changes: 114 additions & 0 deletions backend/controllers/policies_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package controllers

import (
"errors"
"github.com/diggerhq/digger/backend/middleware"
"github.com/diggerhq/digger/backend/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"log"
"net/http"
)

func PolicyOrgGetApi(c *gin.Context) {
policyType := c.Param("policy_type")

if policyType != "plan" && policyType != "access" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid policy type requested: " + policyType})
return
}
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)

var org models.Organisation
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("could not find organisation: %v", err)
c.JSON(http.StatusNotFound, gin.H{"status": "Could not find organisation: " + organisationId})
} else {
log.Printf("database error while finding organisation: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"status": "Internal server error"})
}
return
}

var policy models.Policy
query := JoinedOrganisationRepoProjectQuery()
err = query.
Where("organisations.id = ? AND (repos.id IS NULL AND projects.id IS NULL) AND policies.type = ? ", org.ID, policyType).
First(&policy).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Could not find policy for organisation ext ID: " + organisationId})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown error occurred while fetching database"})
}
return
}

c.JSON(http.StatusOK, gin.H{"result": policy.Policy})
}

func PolicyOrgUpsertApi(c *gin.Context) {
type PolicyUpsertRequest struct {
PolicyType string `json:"policy_type"`
PolicyText string `json:"policy_text"`
}

var request PolicyUpsertRequest
err := c.BindJSON(&request)
if err != nil {
log.Printf("Error binding JSON: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"status": "Invalid request format"})
return
}

organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)

var org models.Organisation
err = models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("could not find organisation: %v", err)
c.JSON(http.StatusNotFound, gin.H{"status": "Could not find organisation: " + organisationId})
} else {
log.Printf("database error while finding organisation: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"status": "Internal server error"})
}
return
}

policyType := request.PolicyType
policyData := request.PolicyText

policy := models.Policy{}

policyResult := models.DB.GormDB.Where("organisation_id = ? AND (repo_id IS NULL AND project_id IS NULL) AND type = ?", org.ID, policyType).Take(&policy)

if policyResult.RowsAffected == 0 {
err := models.DB.GormDB.Create(&models.Policy{
OrganisationID: org.ID,
Type: policyType,
Policy: policyData,
}).Error

if err != nil {
log.Printf("Error creating policy: %v", err)
c.String(http.StatusInternalServerError, "Error creating policy")
return
}
} else {
policy.Policy = policyData
err := models.DB.GormDB.Save(policy).Error
if err != nil {
log.Printf("Error updating policy: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error updating policy"})
return
}
}
Comment on lines +90 to +110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Prevent race conditions or duplicates for upserted policies.
Two parallel requests with different payloads might cause conflicting updates. Ensure there's a unique constraint on (organisation_id, repo_id, project_id, type) or wrap this logic into a transactional approach to avoid inconsistent states.


c.JSON(http.StatusOK, gin.H{"success": true})

}
Loading