@@ -2,11 +2,12 @@ package controllers
22
33import (
44 "errors"
5- "github.com/samber/lo"
6- "log"
5+ "log/slog"
76 "net/http"
87 "os"
98
9+ "github.com/samber/lo"
10+
1011 "github.com/diggerhq/digger/backend/utils"
1112 "gorm.io/gorm"
1213
@@ -22,15 +23,15 @@ func ListVCSConnectionsApi(c *gin.Context) {
2223 var org models.Organisation
2324 err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
2425 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 )
2627 c .JSON (http .StatusNotFound , gin.H {"error" : "Could not fetch organisation" })
2728 return
2829 }
2930
3031 var connections []models.VCSConnection
3132 err = models .DB .GormDB .Where ("organisation_id = ?" , org .ID ).Find (& connections ).Error
3233 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 )
3435 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not fetch VCS connections" })
3536 return
3637 }
@@ -54,7 +55,7 @@ func CreateVCSConnectionApi(c *gin.Context) {
5455 var org models.Organisation
5556 err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
5657 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 )
5859 c .JSON (http .StatusNotFound , gin.H {"error" : "Could not fetch organisation" })
5960 return
6061 }
@@ -68,33 +69,34 @@ func CreateVCSConnectionApi(c *gin.Context) {
6869
6970 var request CreateVCSConnectionRequest
7071 if err := c .BindJSON (& request ); err != nil {
72+ slog .Error ("Invalid request body" , "error" , err )
7173 c .JSON (http .StatusBadRequest , gin.H {"error" : "Invalid request body" })
7274 return
7375 }
7476
7577 if request .VCS != "bitbucket" {
76- log . Printf ("VCS type not supported: %v " , request .VCS )
78+ slog . Error ("VCS type not supported" , "type " , request .VCS )
7779 c .JSON (http .StatusBadRequest , gin.H {"error" : "VCS type not supported" })
7880 return
7981 }
8082
8183 secret := os .Getenv ("DIGGER_ENCRYPTION_SECRET" )
8284 if secret == "" {
83- log . Printf ( "ERROR: no encryption secret specified" )
85+ slog . Error ( "No encryption secret specified" )
8486 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt access token" })
8587 return
8688 }
8789
8890 bitbucketAccessTokenEncrypted , err := utils .AESEncrypt ([]byte (secret ), request .BitbucketAccessToken )
8991 if err != nil {
90- log . Printf ( "could not encrypt access token: %v " , err )
92+ slog . Error ( "Could not encrypt access token" , "error " , err )
9193 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt access token" })
9294 return
9395 }
9496
9597 bitbucketWebhookSecretEncrypted , err := utils .AESEncrypt ([]byte (secret ), request .BitbucketWebhookSecret )
9698 if err != nil {
97- log . Printf ( "could not encrypt webhook secret: %v " , err )
99+ slog . Error ( "Could not encrypt webhook secret" , "error " , err )
98100 c .JSON (http .StatusInternalServerError , gin.H {"error" : "Could not encrypt webhook secret" })
99101 return
100102 }
@@ -114,9 +116,12 @@ func CreateVCSConnectionApi(c *gin.Context) {
114116 org .ID ,
115117 )
116118 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
118122 }
119123
124+ slog .Info ("Created VCS connection" , "connectionId" , connection .ID , "organisationId" , org .ID )
120125 c .JSON (http .StatusCreated , gin.H {
121126 "connection" : connection .ID ,
122127 })
@@ -131,9 +136,10 @@ func GetVCSConnection(c *gin.Context) {
131136 err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
132137 if err != nil {
133138 if errors .Is (err , gorm .ErrRecordNotFound ) {
139+ slog .Info ("Organisation not found" , "organisationId" , organisationId )
134140 c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
135141 } else {
136- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
142+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
137143 c .String (http .StatusNotFound , "Could not fetch organisation: " + organisationId )
138144 }
139145 return
@@ -143,9 +149,10 @@ func GetVCSConnection(c *gin.Context) {
143149 err = models .DB .GormDB .Where ("id = ? AND organisation_id = ?" , connectionId , org .ID ).First (& connection ).Error
144150 if err != nil {
145151 if errors .Is (err , gorm .ErrRecordNotFound ) {
152+ slog .Info ("Connection not found" , "connectionId" , connectionId , "organisationId" , org .ID )
146153 c .String (http .StatusNotFound , "Could not find connection: " + connectionId )
147154 } else {
148- log . Printf ( "could not fetch connection: %v err: %v" , connectionId , err )
155+ slog . Error ( "Could not fetch connection" , "connectionId" , connectionId , "error" , err )
149156 c .String (http .StatusInternalServerError , "Could not fetch connection" )
150157 }
151158 return
@@ -166,9 +173,10 @@ func DeleteVCSConnection(c *gin.Context) {
166173 err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
167174 if err != nil {
168175 if errors .Is (err , gorm .ErrRecordNotFound ) {
176+ slog .Info ("Organisation not found" , "organisationId" , organisationId )
169177 c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
170178 } else {
171- log . Printf ( "could not fetch organisation: %v err: %v" , organisationId , err )
179+ slog . Error ( "Could not fetch organisation" , "organisationId" , organisationId , "error" , err )
172180 c .String (http .StatusNotFound , "Could not fetch organisation: " + organisationId )
173181 }
174182 return
@@ -178,21 +186,23 @@ func DeleteVCSConnection(c *gin.Context) {
178186 err = models .DB .GormDB .Where ("id = ? AND organisation_id = ?" , connectionId , org .ID ).First (& connection ).Error
179187 if err != nil {
180188 if errors .Is (err , gorm .ErrRecordNotFound ) {
189+ slog .Info ("Connection not found" , "connectionId" , connectionId , "organisationId" , org .ID )
181190 c .String (http .StatusNotFound , "Could not find connection: " + connectionId )
182191 } else {
183- log . Printf ( "could not fetch connection: %v err: %v" , connectionId , err )
192+ slog . Error ( "Could not fetch connection" , "connectionId" , connectionId , "error" , err )
184193 c .String (http .StatusInternalServerError , "Could not fetch connection" )
185194 }
186195 return
187196 }
188197
189198 err = models .DB .GormDB .Delete (& connection ).Error
190199 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 )
192201 c .String (http .StatusInternalServerError , "Could not delete connection" )
193202 return
194203 }
195204
205+ slog .Info ("Successfully deleted VCS connection" , "connectionId" , connectionId , "organisationId" , org .ID )
196206 c .JSON (http .StatusOK , gin.H {
197207 "status" : "success" ,
198208 })
0 commit comments