@@ -8,10 +8,7 @@ import (
88
99 "github.com/diggerhq/digger/backend/middleware"
1010 "github.com/diggerhq/digger/backend/models"
11- "github.com/diggerhq/digger/backend/utils"
12- ci_github "github.com/diggerhq/digger/libs/ci/github"
1311 "github.com/gin-gonic/gin"
14- "github.com/google/go-github/v61/github"
1512 "gorm.io/gorm"
1613)
1714
@@ -88,82 +85,3 @@ func LinkGithubInstallationToOrgApi(c *gin.Context) {
8885 c .JSON (http .StatusOK , gin.H {"status" : "Successfully created Github installation link" })
8986 return
9087}
91-
92- func ResyncGithubInstallationApi (c * gin.Context ) {
93- type ResyncInstallationRequest struct {
94- InstallationId string `json:"installation_id"`
95- }
96-
97- var request ResyncInstallationRequest
98- if err := c .BindJSON (& request ); err != nil {
99- slog .Error ("Error binding JSON for resync" , "error" , err )
100- c .JSON (http .StatusBadRequest , gin.H {"status" : "Invalid request format" })
101- return
102- }
103-
104- installationId , err := strconv .ParseInt (request .InstallationId , 10 , 64 )
105- if err != nil {
106- slog .Error ("Failed to convert InstallationId to int64" , "installationId" , request .InstallationId , "error" , err )
107- c .JSON (http .StatusBadRequest , gin.H {"status" : "installationID should be a valid integer" })
108- return
109- }
110-
111- link , err := models .DB .GetGithubAppInstallationLink (installationId )
112- if err != nil {
113- slog .Error ("Could not get installation link for resync" , "installationId" , installationId , "error" , err )
114- c .JSON (http .StatusInternalServerError , gin.H {"status" : "Could not get installation link" })
115- return
116- }
117- if link == nil {
118- slog .Warn ("Installation link not found for resync" , "installationId" , installationId )
119- c .JSON (http .StatusNotFound , gin.H {"status" : "Installation link not found" })
120- return
121- }
122-
123- var installationRecord models.GithubAppInstallation
124- if err := models .DB .GormDB .Where ("github_installation_id = ?" , installationId ).Order ("updated_at desc" ).First (& installationRecord ).Error ; err != nil {
125- if errors .Is (err , gorm .ErrRecordNotFound ) {
126- slog .Warn ("No installation records found for resync" , "installationId" , installationId )
127- c .JSON (http .StatusNotFound , gin.H {"status" : "No installation records found" })
128- return
129- }
130- slog .Error ("Failed to fetch installation record for resync" , "installationId" , installationId , "error" , err )
131- c .JSON (http .StatusInternalServerError , gin.H {"status" : "Could not fetch installation records" })
132- return
133- }
134-
135- appId := installationRecord .GithubAppId
136- ghProvider := utils.DiggerGithubRealClientProvider {}
137-
138- client , _ , err := ghProvider .Get (appId , installationId )
139- if err != nil {
140- slog .Error ("Failed to create GitHub client for resync" , "installationId" , installationId , "appId" , appId , "error" , err )
141- c .JSON (http .StatusInternalServerError , gin.H {"status" : "Failed to create GitHub client" })
142- return
143- }
144-
145- repos , err := ci_github .ListGithubRepos (client )
146- if err != nil {
147- slog .Error ("Failed to list repos for resync" , "installationId" , installationId , "error" , err )
148- c .JSON (http .StatusInternalServerError , gin.H {"status" : "Failed to list repos for resync" })
149- return
150- }
151-
152- installationPayload := & github.Installation {
153- ID : github .Int64 (installationId ),
154- AppID : github .Int64 (appId ),
155- }
156- resyncEvent := & github.InstallationEvent {
157- Installation : installationPayload ,
158- Repositories : repos ,
159- }
160-
161- if err := handleInstallationUpsertEvent (c .Request .Context (), ghProvider , resyncEvent , appId ); err != nil {
162- slog .Error ("Resync failed" , "installationId" , installationId , "error" , err )
163- c .JSON (http .StatusInternalServerError , gin.H {"status" : "Resync failed" })
164- return
165- }
166-
167- slog .Info ("Resync completed" , "installationId" , installationId , "repoCount" , len (repos ))
168- c .JSON (http .StatusOK , gin.H {"status" : "Resync completed" , "repoCount" : len (repos )})
169- }
0 commit comments