@@ -2,11 +2,12 @@ package controllers
2
2
3
3
import (
4
4
"errors"
5
- "github.com/samber/lo"
6
- "log"
5
+ "log/slog"
7
6
"net/http"
8
7
"os"
9
8
9
+ "github.com/samber/lo"
10
+
10
11
"github.com/diggerhq/digger/backend/utils"
11
12
"gorm.io/gorm"
12
13
@@ -22,15 +23,15 @@ func ListVCSConnectionsApi(c *gin.Context) {
22
23
var org models.Organisation
23
24
err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
24
25
if err != nil {
25
- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
26
+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
26
27
c .JSON (http .StatusNotFound , gin.H {"error" : "Could not fetch organisation" })
27
28
return
28
29
}
29
30
30
31
var connections []models.VCSConnection
31
32
err = models .DB .GormDB .Where ("organisation_id = ?" , org .ID ).Find (& connections ).Error
32
33
if err != nil {
33
- log . Printf ( "could not fetch VCS connections: %v " , err )
34
+ slog . Error ( "Could not fetch VCS connections" , "organisationId" , organisationId , "error " , err )
34
35
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not fetch VCS connections" })
35
36
return
36
37
}
@@ -54,7 +55,7 @@ func CreateVCSConnectionApi(c *gin.Context) {
54
55
var org models.Organisation
55
56
err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
56
57
if err != nil {
57
- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
58
+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
58
59
c .JSON (http .StatusNotFound , gin.H {"error" : "Could not fetch organisation" })
59
60
return
60
61
}
@@ -68,33 +69,34 @@ func CreateVCSConnectionApi(c *gin.Context) {
68
69
69
70
var request CreateVCSConnectionRequest
70
71
if err := c .BindJSON (& request ); err != nil {
72
+ slog .Error ("Invalid request body" , "error" , err )
71
73
c .JSON (http .StatusBadRequest , gin.H {"error" : "Invalid request body" })
72
74
return
73
75
}
74
76
75
77
if request .VCS != "bitbucket" {
76
- log . Printf ("VCS type not supported: %v " , request .VCS )
78
+ slog . Error ("VCS type not supported" , "type " , request .VCS )
77
79
c .JSON (http .StatusBadRequest , gin.H {"error" : "VCS type not supported" })
78
80
return
79
81
}
80
82
81
83
secret := os .Getenv ("DIGGER_ENCRYPTION_SECRET" )
82
84
if secret == "" {
83
- log . Printf ( "ERROR: no encryption secret specified" )
85
+ slog . Error ( "No encryption secret specified" )
84
86
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt access token" })
85
87
return
86
88
}
87
89
88
90
bitbucketAccessTokenEncrypted , err := utils .AESEncrypt ([]byte (secret ), request .BitbucketAccessToken )
89
91
if err != nil {
90
- log . Printf ( "could not encrypt access token: %v " , err )
92
+ slog . Error ( "Could not encrypt access token" , "error " , err )
91
93
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt access token" })
92
94
return
93
95
}
94
96
95
97
bitbucketWebhookSecretEncrypted , err := utils .AESEncrypt ([]byte (secret ), request .BitbucketWebhookSecret )
96
98
if err != nil {
97
- log . Printf ( "could not encrypt webhook secret: %v " , err )
99
+ slog . Error ( "Could not encrypt webhook secret" , "error " , err )
98
100
c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt webhook secret" })
99
101
return
100
102
}
@@ -114,9 +116,12 @@ func CreateVCSConnectionApi(c *gin.Context) {
114
116
org .ID ,
115
117
)
116
118
if err != nil {
117
- log .Printf ("" )
119
+ slog .Error ("Could not create VCS connection" , "error" , err )
120
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not create VCS connection" })
121
+ return
118
122
}
119
123
124
+ slog .Info ("Created VCS connection" , "connectionId" , connection .ID , "organisationId" , org .ID )
120
125
c .JSON (http .StatusCreated , gin.H {
121
126
"connection" : connection .ID ,
122
127
})
@@ -131,9 +136,10 @@ func GetVCSConnection(c *gin.Context) {
131
136
err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
132
137
if err != nil {
133
138
if errors .Is (err , gorm .ErrRecordNotFound ) {
139
+ slog .Info ("Organisation not found" , "organisationId" , organisationId )
134
140
c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
135
141
} else {
136
- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
142
+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
137
143
c .String (http .StatusNotFound , "Could not fetch organisation: " + organisationId )
138
144
}
139
145
return
@@ -143,9 +149,10 @@ func GetVCSConnection(c *gin.Context) {
143
149
err = models .DB .GormDB .Where ("id = ? AND organisation_id = ?" , connectionId , org .ID ).First (& connection ).Error
144
150
if err != nil {
145
151
if errors .Is (err , gorm .ErrRecordNotFound ) {
152
+ slog .Info ("Connection not found" , "connectionId" , connectionId , "organisationId" , org .ID )
146
153
c .String (http .StatusNotFound , "Could not find connection: " + connectionId )
147
154
} else {
148
- log . Printf ( "could not fetch connection: %v err: %v" , connectionId , err )
155
+ slog . Error ( "Could not fetch connection" , "connectionId" , connectionId , "error" , err )
149
156
c .String (http .StatusInternalServerError , "Could not fetch connection" )
150
157
}
151
158
return
@@ -166,9 +173,10 @@ func DeleteVCSConnection(c *gin.Context) {
166
173
err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
167
174
if err != nil {
168
175
if errors .Is (err , gorm .ErrRecordNotFound ) {
176
+ slog .Info ("Organisation not found" , "organisationId" , organisationId )
169
177
c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
170
178
} else {
171
- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
179
+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
172
180
c .String (http .StatusNotFound , "Could not fetch organisation: " + organisationId )
173
181
}
174
182
return
@@ -178,21 +186,23 @@ func DeleteVCSConnection(c *gin.Context) {
178
186
err = models .DB .GormDB .Where ("id = ? AND organisation_id = ?" , connectionId , org .ID ).First (& connection ).Error
179
187
if err != nil {
180
188
if errors .Is (err , gorm .ErrRecordNotFound ) {
189
+ slog .Info ("Connection not found" , "connectionId" , connectionId , "organisationId" , org .ID )
181
190
c .String (http .StatusNotFound , "Could not find connection: " + connectionId )
182
191
} else {
183
- log . Printf ( "could not fetch connection: %v err: %v" , connectionId , err )
192
+ slog . Error ( "Could not fetch connection" , "connectionId" , connectionId , "error" , err )
184
193
c .String (http .StatusInternalServerError , "Could not fetch connection" )
185
194
}
186
195
return
187
196
}
188
197
189
198
err = models .DB .GormDB .Delete (& connection ).Error
190
199
if err != nil {
191
- log . Printf ( "could not delete connection: %v err: %v" , connectionId , err )
200
+ slog . Error ( "Could not delete connection" , "connectionId" , connectionId , "error" , err )
192
201
c .String (http .StatusInternalServerError , "Could not delete connection" )
193
202
return
194
203
}
195
204
205
+ slog .Info ("Successfully deleted VCS connection" , "connectionId" , connectionId , "organisationId" , org .ID )
196
206
c .JSON (http .StatusOK , gin.H {
197
207
"status" : "success" ,
198
208
})
0 commit comments