-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathconnections.go
199 lines (173 loc) · 6.21 KB
/
connections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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",
})
}