|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/samber/lo" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/diggerhq/digger/backend/utils" |
| 11 | + "gorm.io/gorm" |
| 12 | + |
| 13 | + "github.com/diggerhq/digger/backend/middleware" |
| 14 | + "github.com/diggerhq/digger/backend/models" |
| 15 | + "github.com/gin-gonic/gin" |
| 16 | +) |
| 17 | + |
| 18 | +func ListVCSConnectionsApi(c *gin.Context) { |
| 19 | + organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) |
| 20 | + organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) |
| 21 | + |
| 22 | + var org models.Organisation |
| 23 | + err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error |
| 24 | + if err != nil { |
| 25 | + log.Printf("could not fetch organisation: %v err: %v", organisationId, err) |
| 26 | + c.JSON(http.StatusNotFound, gin.H{"error": "Could not fetch organisation"}) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + var connections []models.VCSConnection |
| 31 | + err = models.DB.GormDB.Where("organisation_id = ?", org.ID).Find(&connections).Error |
| 32 | + if err != nil { |
| 33 | + log.Printf("could not fetch VCS connections: %v", err) |
| 34 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch VCS connections"}) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + connectionsSlim := lo.Map(connections, func(c models.VCSConnection, i int) gin.H { |
| 39 | + return gin.H{ |
| 40 | + "connection_id": c.ID, |
| 41 | + "vcs": "bitbucket", |
| 42 | + "connection_name": c.Name, |
| 43 | + } |
| 44 | + }) |
| 45 | + c.JSON(http.StatusOK, gin.H{ |
| 46 | + "result": connectionsSlim, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func CreateVCSConnectionApi(c *gin.Context) { |
| 51 | + organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) |
| 52 | + organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) |
| 53 | + |
| 54 | + var org models.Organisation |
| 55 | + err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error |
| 56 | + if err != nil { |
| 57 | + log.Printf("could not fetch organisation: %v err: %v", organisationId, err) |
| 58 | + c.JSON(http.StatusNotFound, gin.H{"error": "Could not fetch organisation"}) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + type CreateVCSConnectionRequest struct { |
| 63 | + VCS string `json:"type" binding:"required"` |
| 64 | + Name string `json:"connection_name"` |
| 65 | + BitbucketAccessToken string `json:"bitbucket_access_token"` |
| 66 | + BitbucketWebhookSecret string `json:"bitbucket_webhook_secret"` |
| 67 | + } |
| 68 | + |
| 69 | + var request CreateVCSConnectionRequest |
| 70 | + if err := c.BindJSON(&request); err != nil { |
| 71 | + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if request.VCS != "bitbucket" { |
| 76 | + log.Printf("VCS type not supported: %v", request.VCS) |
| 77 | + c.JSON(http.StatusBadRequest, gin.H{"error": "VCS type not supported"}) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + secret := os.Getenv("DIGGER_ENCRYPTION_SECRET") |
| 82 | + if secret == "" { |
| 83 | + log.Printf("ERROR: no encryption secret specified") |
| 84 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt access token"}) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + bitbucketAccessTokenEncrypted, err := utils.AESEncrypt([]byte(secret), request.BitbucketAccessToken) |
| 89 | + if err != nil { |
| 90 | + log.Printf("could not encrypt access token: %v", err) |
| 91 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt access token"}) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + bitbucketWebhookSecretEncrypted, err := utils.AESEncrypt([]byte(secret), request.BitbucketWebhookSecret) |
| 96 | + if err != nil { |
| 97 | + log.Printf("could not encrypt webhook secret: %v", err) |
| 98 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt webhook secret"}) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + connection, err := models.DB.CreateVCSConnection( |
| 103 | + request.Name, |
| 104 | + 0, |
| 105 | + "", |
| 106 | + "", |
| 107 | + "", |
| 108 | + "", |
| 109 | + "", |
| 110 | + "", |
| 111 | + "", |
| 112 | + bitbucketAccessTokenEncrypted, |
| 113 | + bitbucketWebhookSecretEncrypted, |
| 114 | + org.ID, |
| 115 | + ) |
| 116 | + if err != nil { |
| 117 | + log.Printf("") |
| 118 | + } |
| 119 | + |
| 120 | + c.JSON(http.StatusCreated, gin.H{ |
| 121 | + "connection": connection.ID, |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func GetVCSConnection(c *gin.Context) { |
| 126 | + organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) |
| 127 | + organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) |
| 128 | + connectionId := c.Param("id") |
| 129 | + |
| 130 | + var org models.Organisation |
| 131 | + err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error |
| 132 | + if err != nil { |
| 133 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 134 | + c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) |
| 135 | + } else { |
| 136 | + log.Printf("could not fetch organisation: %v err: %v", organisationId, err) |
| 137 | + c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) |
| 138 | + } |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + var connection models.VCSConnection |
| 143 | + err = models.DB.GormDB.Where("id = ? AND organisation_id = ?", connectionId, org.ID).First(&connection).Error |
| 144 | + if err != nil { |
| 145 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 146 | + c.String(http.StatusNotFound, "Could not find connection: "+connectionId) |
| 147 | + } else { |
| 148 | + log.Printf("could not fetch connection: %v err: %v", connectionId, err) |
| 149 | + c.String(http.StatusInternalServerError, "Could not fetch connection") |
| 150 | + } |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + c.JSON(http.StatusOK, gin.H{ |
| 155 | + "connection_name": connection.Name, |
| 156 | + "connection_id": connection.ID, |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +func DeleteVCSConnection(c *gin.Context) { |
| 161 | + organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) |
| 162 | + organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) |
| 163 | + connectionId := c.Param("id") |
| 164 | + |
| 165 | + var org models.Organisation |
| 166 | + err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error |
| 167 | + if err != nil { |
| 168 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 169 | + c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) |
| 170 | + } else { |
| 171 | + log.Printf("could not fetch organisation: %v err: %v", organisationId, err) |
| 172 | + c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) |
| 173 | + } |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + var connection models.VCSConnection |
| 178 | + err = models.DB.GormDB.Where("id = ? AND organisation_id = ?", connectionId, org.ID).First(&connection).Error |
| 179 | + if err != nil { |
| 180 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 181 | + c.String(http.StatusNotFound, "Could not find connection: "+connectionId) |
| 182 | + } else { |
| 183 | + log.Printf("could not fetch connection: %v err: %v", connectionId, err) |
| 184 | + c.String(http.StatusInternalServerError, "Could not fetch connection") |
| 185 | + } |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + err = models.DB.GormDB.Delete(&connection).Error |
| 190 | + if err != nil { |
| 191 | + log.Printf("could not delete connection: %v err: %v", connectionId, err) |
| 192 | + c.String(http.StatusInternalServerError, "Could not delete connection") |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + c.JSON(http.StatusOK, gin.H{ |
| 197 | + "status": "success", |
| 198 | + }) |
| 199 | +} |
0 commit comments