-
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
Feat bitbucket support #1890
Merged
Merged
Feat bitbucket support #1890
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 was deleted.
Oops, something went wrong.
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,199 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
"github.com/samber/lo" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/diggerhq/digger/backend/utils" | ||
"gorm.io/gorm" | ||
|
||
"github.com/diggerhq/digger/backend/middleware" | ||
"github.com/diggerhq/digger/backend/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func ListVCSConnectionsApi(c *gin.Context) { | ||
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 { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Could not fetch organisation"}) | ||
return | ||
} | ||
|
||
var connections []models.VCSConnection | ||
err = models.DB.GormDB.Where("organisation_id = ?", org.ID).Find(&connections).Error | ||
if err != nil { | ||
log.Printf("could not fetch VCS connections: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch VCS connections"}) | ||
return | ||
} | ||
|
||
connectionsSlim := lo.Map(connections, func(c models.VCSConnection, i int) gin.H { | ||
return gin.H{ | ||
"connection_id": c.ID, | ||
"vcs": "bitbucket", | ||
"connection_name": c.Name, | ||
} | ||
}) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"result": connectionsSlim, | ||
}) | ||
} | ||
|
||
func CreateVCSConnectionApi(c *gin.Context) { | ||
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 { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Could not fetch organisation"}) | ||
return | ||
} | ||
|
||
type CreateVCSConnectionRequest struct { | ||
VCS string `json:"type" binding:"required"` | ||
Name string `json:"connection_name"` | ||
BitbucketAccessToken string `json:"bitbucket_access_token"` | ||
BitbucketWebhookSecret string `json:"bitbucket_webhook_secret"` | ||
} | ||
|
||
var request CreateVCSConnectionRequest | ||
if err := c.BindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) | ||
return | ||
} | ||
|
||
if request.VCS != "bitbucket" { | ||
log.Printf("VCS type not supported: %v", request.VCS) | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "VCS type not supported"}) | ||
return | ||
} | ||
|
||
secret := os.Getenv("DIGGER_ENCRYPTION_SECRET") | ||
if secret == "" { | ||
log.Printf("ERROR: no encryption secret specified") | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt access token"}) | ||
return | ||
} | ||
|
||
bitbucketAccessTokenEncrypted, err := utils.AESEncrypt([]byte(secret), request.BitbucketAccessToken) | ||
if err != nil { | ||
log.Printf("could not encrypt access token: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt access token"}) | ||
return | ||
} | ||
|
||
bitbucketWebhookSecretEncrypted, err := utils.AESEncrypt([]byte(secret), request.BitbucketWebhookSecret) | ||
if err != nil { | ||
log.Printf("could not encrypt webhook secret: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt webhook secret"}) | ||
return | ||
} | ||
|
||
connection, err := models.DB.CreateVCSConnection( | ||
request.Name, | ||
0, | ||
"", | ||
"", | ||
"", | ||
"", | ||
"", | ||
"", | ||
"", | ||
bitbucketAccessTokenEncrypted, | ||
bitbucketWebhookSecretEncrypted, | ||
org.ID, | ||
) | ||
if err != nil { | ||
log.Printf("") | ||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{ | ||
"connection": connection.ID, | ||
}) | ||
} | ||
|
||
func GetVCSConnection(c *gin.Context) { | ||
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) | ||
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) | ||
connectionId := c.Param("id") | ||
|
||
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) { | ||
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) | ||
} else { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) | ||
} | ||
return | ||
} | ||
|
||
var connection models.VCSConnection | ||
err = models.DB.GormDB.Where("id = ? AND organisation_id = ?", connectionId, org.ID).First(&connection).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
c.String(http.StatusNotFound, "Could not find connection: "+connectionId) | ||
} else { | ||
log.Printf("could not fetch connection: %v err: %v", connectionId, err) | ||
c.String(http.StatusInternalServerError, "Could not fetch connection") | ||
} | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"connection_name": connection.Name, | ||
"connection_id": connection.ID, | ||
}) | ||
} | ||
|
||
func DeleteVCSConnection(c *gin.Context) { | ||
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) | ||
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) | ||
connectionId := c.Param("id") | ||
|
||
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) { | ||
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) | ||
} else { | ||
log.Printf("could not fetch organisation: %v err: %v", organisationId, err) | ||
c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) | ||
} | ||
return | ||
} | ||
|
||
var connection models.VCSConnection | ||
err = models.DB.GormDB.Where("id = ? AND organisation_id = ?", connectionId, org.ID).First(&connection).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
c.String(http.StatusNotFound, "Could not find connection: "+connectionId) | ||
} else { | ||
log.Printf("could not fetch connection: %v err: %v", connectionId, err) | ||
c.String(http.StatusInternalServerError, "Could not fetch connection") | ||
} | ||
return | ||
} | ||
|
||
err = models.DB.GormDB.Delete(&connection).Error | ||
if err != nil { | ||
log.Printf("could not delete connection: %v err: %v", connectionId, err) | ||
c.String(http.StatusInternalServerError, "Could not delete connection") | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"status": "success", | ||
}) | ||
} |
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
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
Oops, something went wrong.
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.
Proper error handling for connection creation failures
If an error occurs while creating the VCS connection,
log.Printf("")
is empty and the code does not return a response. This silently fails and leaves the client hanging. Ensure you return a relevant error or message to the client.📝 Committable suggestion