Skip to content

Commit ff27c5d

Browse files
committed
support workflow url
1 parent 1ce5e9c commit ff27c5d

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

backend/controllers/github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func TriggerDiggerJobs(client *github.Client, repoOwner string, repoName string,
700700
return fmt.Errorf("failed to trigger github workflow, %v\n", err)
701701
} else {
702702

703-
_, workflowRunUrl, err := utils.GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, job)
703+
_, workflowRunUrl, err := utils.GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, job.DiggerJobID)
704704
if err != nil {
705705
log.Printf("failed to find workflow url: %v\n", err)
706706
}

backend/controllers/projects.go

+16
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ func SetJobStatusForProject(c *gin.Context) {
291291
switch request.Status {
292292
case "started":
293293
job.Status = orchestrator_scheduler.DiggerJobStarted
294+
client, _, err := utils.GetGithubClient(&utils.DiggerGithubRealClientProvider{}, job.Batch.GithubInstallationId, job.Batch.RepoFullName)
295+
if err != nil {
296+
log.Printf("Error Creating github client: %v", err)
297+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error Creating github client"})
298+
return
299+
}
300+
_, workflowRunUrl, err := utils.GetWorkflowIdAndUrlFromDiggerJobId(client, job.Batch.RepoOwner, job.Batch.RepoName, job.DiggerJobID)
301+
if err != nil {
302+
log.Printf("Error getting workflow ID from job: %v", err)
303+
} else {
304+
job.WorkflowRunUrl = &workflowRunUrl
305+
err = models.DB.UpdateDiggerJob(job)
306+
if err != nil {
307+
log.Printf("Error updating digger job: %v", err)
308+
}
309+
}
294310
case "succeeded":
295311
job.Status = orchestrator_scheduler.DiggerJobSucceeded
296312
go func() {

backend/services/scheduler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TriggerJob(client *github.Client, repoOwner string, repoName string, batchI
7070
return
7171
}
7272

73-
_, workflowRunUrl, err := utils.GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, *job)
73+
_, workflowRunUrl, err := utils.GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, job.DiggerJobID)
7474
if err != nil {
7575
log.Printf("failed to find workflow url: %v\n", err)
7676
}

backend/utils/github.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (gh *DiggerGithubClientMockProvider) Get(githubAppId int64, installationId
9292
return ghClient, &token, nil
9393
}
9494

95-
func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) {
95+
func GetGithubClient(gh GithubClientProvider, installationId int64, repoFullName string) (*github.Client, *string, error) {
9696
installation, err := models.DB.GetGithubAppInstallationByIdAndRepo(installationId, repoFullName)
9797
if err != nil {
9898
log.Printf("Error getting installation: %v", err)
@@ -106,6 +106,10 @@ func GetGithubService(gh GithubClientProvider, installationId int64, repoFullNam
106106
}
107107

108108
ghClient, token, err := gh.Get(installation.GithubAppId, installation.GithubInstallationId)
109+
return ghClient, token, err
110+
}
111+
func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) {
112+
ghClient, token, err := GetGithubClient(gh, installationId, repoFullName)
109113
if err != nil {
110114
log.Printf("Error creating github app client: %v", err)
111115
return nil, nil, fmt.Errorf("Error creating github app client: %v", err)
@@ -139,7 +143,7 @@ func SetPRStatusForJobs(prService *github2.GithubService, prNumber int, jobs []o
139143
return nil
140144
}
141145

142-
func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, job models.DiggerJob) (int64, string, error) {
146+
func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (int64, string, error) {
143147
timeFilter := time.Now().Add(-5 * time.Minute)
144148
runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), repoOwner, repoName, &github.ListWorkflowRunsOptions{
145149
Created: ">= " + timeFilter.Format(time.RFC3339),
@@ -149,23 +153,24 @@ func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string,
149153
}
150154

151155
for _, workflowRun := range runs.WorkflowRuns {
156+
println(*workflowRun.ID)
152157
workflowjobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), repoOwner, repoName, *workflowRun.ID, nil)
153158
if err != nil {
154159
return 0, "#", fmt.Errorf("error listing workflow jobs for run %v %v", workflowRun.ID, err)
155160
}
156161

157162
for _, workflowjob := range workflowjobs.Jobs {
158163
for _, step := range workflowjob.Steps {
159-
if strings.Contains(*step.Name, job.DiggerJobID) {
164+
if strings.Contains(*step.Name, diggerJobID) {
160165
return *workflowRun.ID, fmt.Sprintf("https://github.com/%v/%v/actions/runs/%v", repoOwner, repoName, *workflowRun.ID), nil
161166
}
162167
}
163168

164169
}
165170
}
166-
167171
return 0, "#", fmt.Errorf("workflow not found")
168172
}
173+
169174
func TriggerGithubWorkflow(client *github.Client, repoOwner string, repoName string, job models.DiggerJob, jobString string, commentId int64) error {
170175
log.Printf("TriggerGithubWorkflow: repoOwner: %v, repoName: %v, commentId: %v", repoOwner, repoName, commentId)
171176
_, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), repoOwner, repoName, "digger_workflow.yml", github.CreateWorkflowDispatchEventRequest{

0 commit comments

Comments
 (0)