Skip to content

Commit d306bef

Browse files
authored
Fix/use right GitHub provider (#1623)
* pass the right github providers in functions
1 parent 8cafcba commit d306bef

File tree

9 files changed

+25
-22
lines changed

9 files changed

+25
-22
lines changed

backend/controllers/github.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
642642
return fmt.Errorf("error fetching ci backed %v", err)
643643
}
644644

645-
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, prNumber, ghService)
645+
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, prNumber, ghService, gh)
646646
if err != nil {
647647
log.Printf("TriggerDiggerJobs error: %v", err)
648648
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
@@ -947,7 +947,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
947947
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: GetCiBackend error: %v", err))
948948
return fmt.Errorf("error fetching ci backed %v", err)
949949
}
950-
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, ghService)
950+
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, ghService, gh)
951951
if err != nil {
952952
log.Printf("TriggerDiggerJobs error: %v", err)
953953
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
@@ -956,7 +956,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
956956
return nil
957957
}
958958

959-
func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, repoOwner string, repoName string, batchId *uuid.UUID, prNumber int, prService ci.PullRequestService) error {
959+
func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, repoOwner string, repoName string, batchId *uuid.UUID, prNumber int, prService ci.PullRequestService, gh utils.GithubClientProvider) error {
960960
_, err := models.DB.GetDiggerBatch(batchId)
961961
if err != nil {
962962
log.Printf("failed to get digger batch, %v\n", err)
@@ -979,7 +979,7 @@ func TriggerDiggerJobs(ciBackend ci_backends.CiBackend, repoFullName string, rep
979979
log.Printf("jobString: %v \n", jobString)
980980

981981
// TODO: make workflow file name configurable
982-
err = services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, &job)
982+
err = services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, &job, gh)
983983
if err != nil {
984984
log.Printf("failed to trigger CI workflow, %v\n", err)
985985
return fmt.Errorf("failed to trigger CI workflow, %v\n", err)

backend/controllers/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
435435

436436
repoFullNameSplit := strings.Split(jobLink.RepoFullName, "/")
437437
client, _, err := ghClientProvider.Get(installations[0].GithubAppId, installationLink.GithubInstallationId)
438-
err = services.DiggerJobCompleted(client, &job.Batch.ID, job, jobLink.RepoFullName, repoFullNameSplit[0], repoFullNameSplit[1], workflowFileName)
438+
err = services.DiggerJobCompleted(client, &job.Batch.ID, job, jobLink.RepoFullName, repoFullNameSplit[0], repoFullNameSplit[1], workflowFileName, d.GithubClientProvider)
439439
if err != nil {
440440
log.Printf("Error triggering job: %v", err)
441441
return

backend/services/scheduler.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"github.com/diggerhq/digger/backend/ci_backends"
66
"github.com/diggerhq/digger/backend/config"
77
"github.com/diggerhq/digger/backend/models"
8+
"github.com/diggerhq/digger/backend/utils"
89
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
910
"github.com/google/go-github/v61/github"
1011
"github.com/google/uuid"
1112
"log"
1213
)
1314

14-
func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *models.DiggerJob, repoFullName string, repoOwner string, repoName string, workflowFileName string) error {
15+
func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *models.DiggerJob, repoFullName string, repoOwner string, repoName string, workflowFileName string, gh utils.GithubClientProvider) error {
1516
log.Printf("DiggerJobCompleted parentJobId: %v", parentJob.DiggerJobID)
1617

1718
jobLinksForParent, err := models.DB.GetDiggerJobParentLinksByParentId(&parentJob.DiggerJobID)
@@ -45,18 +46,18 @@ func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *mo
4546
return err
4647
}
4748
ciBackend := ci_backends.GithubActionCi{Client: client}
48-
ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, job)
49+
ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, job, gh)
4950
}
5051

5152
}
5253
return nil
5354
}
5455

55-
func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error {
56+
func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob, gh utils.GithubClientProvider) error {
5657
maxConcurrencyForBatch := config.DiggerConfig.GetInt("max_concurrency_per_batch")
5758
if maxConcurrencyForBatch == 0 {
5859
// concurrency limits not set
59-
err := TriggerJob(ciBackend, repoFullname, repoOwner, repoName, batchId, job)
60+
err := TriggerJob(gh, ciBackend, repoFullname, repoOwner, repoName, batchId, job)
6061
if err != nil {
6162
log.Printf("Could not trigger job: %v", err)
6263
return err
@@ -79,7 +80,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner
7980
models.DB.UpdateDiggerJob(job)
8081
return nil
8182
} else {
82-
err := TriggerJob(ciBackend, repoFullname, repoOwner, repoName, batchId, job)
83+
err := TriggerJob(gh, ciBackend, repoFullname, repoOwner, repoName, batchId, job)
8384
if err != nil {
8485
log.Printf("Could not trigger job: %v", err)
8586
return err
@@ -89,7 +90,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner
8990
return nil
9091
}
9192

92-
func TriggerJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error {
93+
func TriggerJob(gh utils.GithubClientProvider, ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error {
9394
log.Printf("TriggerJob jobId: %v", job.DiggerJobID)
9495

9596
if job.SerializedJobSpec == nil {
@@ -111,7 +112,7 @@ func TriggerJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner
111112
return fmt.Errorf("coult not get spec %v", err)
112113
}
113114

114-
vcsToken, err := GetVCSTokenFromJob(*job)
115+
vcsToken, err := GetVCSTokenFromJob(*job, gh)
115116
if err != nil {
116117
log.Printf("could not get vcs token: %v", err)
117118
return fmt.Errorf("coult not get vcs token: %v", err)

backend/services/spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
"strconv"
1313
)
1414

15-
func GetVCSTokenFromJob(job models.DiggerJob) (*string, error) {
15+
func GetVCSTokenFromJob(job models.DiggerJob, gh utils.GithubClientProvider) (*string, error) {
1616
// TODO: make it VCS generic
1717
batch := job.Batch
1818
var token string
1919
switch batch.VCS {
2020
case models.DiggerVCSGithub:
2121
_, ghToken, err := utils.GetGithubService(
22-
utils.DiggerGithubRealClientProvider{},
22+
gh,
2323
job.Batch.GithubInstallationId,
2424
job.Batch.RepoFullName,
2525
job.Batch.RepoOwner,

backend/tasks/runs.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"github.com/diggerhq/digger/backend/ci_backends"
55
"github.com/diggerhq/digger/backend/models"
66
"github.com/diggerhq/digger/backend/services"
7+
"github.com/diggerhq/digger/backend/utils"
78
"github.com/diggerhq/digger/libs/ci"
89
"github.com/diggerhq/digger/libs/ci/github"
910
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
1011
"log"
1112
)
1213

13-
func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.PullRequestService) {
14+
func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.PullRequestService, gh utils.GithubClientProvider) {
1415
dr := queueItem.DiggerRun
1516
switch queueItem.DiggerRun.Status {
1617
case models.RunQueued:
@@ -30,7 +31,7 @@ func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.Pull
3031
return
3132
}
3233

33-
vcsToken, err := services.GetVCSTokenFromJob(*job)
34+
vcsToken, err := services.GetVCSTokenFromJob(*job, gh)
3435
if err != nil {
3536
log.Printf("could not get vcs token: %v", err)
3637
return
@@ -103,7 +104,7 @@ func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.Pull
103104
return
104105
}
105106

106-
vcsToken, err := services.GetVCSTokenFromJob(*job)
107+
vcsToken, err := services.GetVCSTokenFromJob(*job, gh)
107108
if err != nil {
108109
log.Printf("could not get vcs token: %v", err)
109110
return

backend/tasks/runs_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/diggerhq/digger/backend/models"
6+
"github.com/diggerhq/digger/backend/utils"
67
github2 "github.com/diggerhq/digger/libs/ci/github"
78
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
89
"github.com/diggerhq/digger/libs/spec"
@@ -152,7 +153,7 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) {
152153
models.DB.UpdateDiggerBatch(batch)
153154
queueItem, _ = models.DB.GetDiggerRunQueueItem(queueItem.ID)
154155

155-
RunQueuesStateMachine(queueItem, ciService)
156+
RunQueuesStateMachine(queueItem, ciService, utils.DiggerGithubClientMockProvider{})
156157
diggerRunRefreshed, _ := models.DB.GetDiggerRun(diggerRun.ID)
157158
assert.Equal(t, testParam.NextExpectedStatus, diggerRunRefreshed.Status)
158159
}

backend/tasks/tasks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func main() {
4242
log.Printf("failed to get github service for DiggerRun ID: %v: %v", dr.ID, err)
4343
continue
4444
}
45-
RunQueuesStateMachine(&queueItem, service)
45+
RunQueuesStateMachine(&queueItem, service, &utils.DiggerGithubRealClientProvider{})
4646
}
4747
})
4848

@@ -64,7 +64,7 @@ func main() {
6464
}
6565

6666
ciBackend := ci_backends.GithubActionCi{Client: service.Client}
67-
services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, &batch.ID, &job)
67+
services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, &batch.ID, &job, &utils.DiggerGithubRealClientProvider{})
6868
}
6969
})
7070

backend/utils/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (gh DiggerGithubClientMockProvider) NewClient(netClient *net.Client) (*gith
122122
return ghClient, nil
123123
}
124124

125-
func (gh *DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) {
125+
func (gh DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) {
126126
ghClient, _ := gh.NewClient(gh.MockedHTTPClient)
127127
token := "token"
128128
return ghClient, &token, nil

ee/backend/controllers/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla
515515
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: GetCiBackend error: %v", err))
516516
return fmt.Errorf("error fetching ci backed %v", err)
517517
}
518-
err = controllers.TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, glService)
518+
err = controllers.TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, glService, nil)
519519
if err != nil {
520520
log.Printf("TriggerDiggerJobs error: %v", err)
521521
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))

0 commit comments

Comments
 (0)