-
Notifications
You must be signed in to change notification settings - Fork 563
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"success": true}) | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.