@@ -6,10 +6,14 @@ import (
66 "github.com/diggerhq/digger/backend/config"
77 "github.com/diggerhq/digger/backend/models"
88 "github.com/diggerhq/digger/backend/utils"
9+ comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary"
910 orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
11+ "github.com/diggerhq/digger/libs/spec"
1012 "github.com/google/go-github/v61/github"
1113 "github.com/google/uuid"
1214 "log"
15+ "runtime/debug"
16+ "time"
1317)
1418
1519func DiggerJobCompleted (client * github.Client , batchId * uuid.UUID , parentJob * models.DiggerJob , repoFullName string , repoOwner string , repoName string , workflowFileName string , gh utils.GithubClientProvider ) error {
@@ -131,5 +135,68 @@ func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend,
131135 return err
132136 }
133137
138+ go UpdateWorkflowUrlForJob (job , ciBackend , spec , gh )
139+
134140 return nil
135141}
142+
143+ // This is meant to run asyncronously since it queries for job url
144+ // in case of github we don't get it immediately but with some delay
145+ func UpdateWorkflowUrlForJob (job * models.DiggerJob , ciBackend ci_backends.CiBackend , spec * spec.Spec , gh utils.GithubClientProvider ) {
146+ defer func () {
147+ if r := recover (); r != nil {
148+ log .Printf ("Recovered from panic in UpdateWorkflowUrlForJob handler: %v" , r )
149+ log .Printf ("\n === PANIC RECOVERED ===\n " )
150+ log .Printf ("Error: %v\n " , r )
151+ log .Printf ("Stack Trace:\n %s" , string (debug .Stack ()))
152+ log .Printf ("=== END PANIC ===\n " )
153+ }
154+ }()
155+
156+ batch := job .Batch
157+ // for now we only perform this update for github
158+ if batch .VCS != models .DiggerVCSGithub {
159+ return
160+ }
161+ for n := 0 ; n < 30 ; n ++ {
162+ time .Sleep (1 * time .Second )
163+ workflowUrl , err := ciBackend .GetWorkflowUrl (* spec )
164+ if err != nil {
165+ log .Printf ("DiggerJobId %v: error while attempting to fetch workflow url: %v" , job .DiggerJobID , err )
166+ } else {
167+ if workflowUrl == "#" || workflowUrl == "" {
168+ log .Printf ("DiggerJobId %v: got blank workflow url as response, ignoring" , job .DiggerJobID )
169+ } else {
170+ job .WorkflowRunUrl = & workflowUrl
171+ err = models .DB .UpdateDiggerJob (job )
172+ if err != nil {
173+ log .Printf ("DiggerJobId %v: Error updating digger job: %v" , job .DiggerJobID , err )
174+ continue
175+ } else {
176+ log .Printf ("DiggerJobId %v: successfully updated workflow run url to: %v for DiggerJobID: %v" , job .DiggerJobID , workflowUrl , job .DiggerJobID )
177+ }
178+
179+ // refresh the batch from DB to get accurate results
180+ batch , err = models .DB .GetDiggerBatch (& job .Batch .ID )
181+ if err != nil {
182+ log .Printf ("DiggerJobId %v: Error getting batch: %v" , job .DiggerJobID , err )
183+ continue
184+ }
185+ res , err := batch .MapToJsonStruct ()
186+ if err != nil {
187+ log .Printf ("DiggerJobId %v: Error getting batch details: %v" , job .DiggerJobID , err )
188+ continue
189+ }
190+ // TODO: make this abstract and extracting the right "prService" based on VCS
191+ client , _ , err := utils .GetGithubService (gh , batch .GithubInstallationId , spec .VCS .RepoFullname , spec .VCS .RepoOwner , spec .VCS .RepoName )
192+ err = comment_updater.BasicCommentUpdater {}.UpdateComment (res .Jobs , * spec .Job .PullRequestNumber , client , spec .CommentId )
193+ if err != nil {
194+ log .Printf ("diggerJobId: %v error whilst updating comment %v" , job .DiggerJobID , err )
195+ continue
196+ }
197+ return
198+ }
199+ }
200+ }
201+
202+ }
0 commit comments