Skip to content

Commit

Permalink
Fix/use right GitHub provider (#1623)
Browse files Browse the repository at this point in the history
* pass the right github providers in functions
  • Loading branch information
motatoes authored Jul 16, 2024
1 parent 8cafcba commit d306bef
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 22 deletions.
8 changes: 4 additions & 4 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
return fmt.Errorf("error fetching ci backed %v", err)
}

err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, prNumber, ghService)
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, prNumber, ghService, gh)
if err != nil {
log.Printf("TriggerDiggerJobs error: %v", err)
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
Expand Down Expand Up @@ -947,7 +947,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: GetCiBackend error: %v", err))
return fmt.Errorf("error fetching ci backed %v", err)
}
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, ghService)
err = TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, ghService, gh)
if err != nil {
log.Printf("TriggerDiggerJobs error: %v", err)
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
Expand All @@ -956,7 +956,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
return nil
}

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

// TODO: make workflow file name configurable
err = services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, &job)
err = services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, &job, gh)
if err != nil {
log.Printf("failed to trigger CI workflow, %v\n", err)
return fmt.Errorf("failed to trigger CI workflow, %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {

repoFullNameSplit := strings.Split(jobLink.RepoFullName, "/")
client, _, err := ghClientProvider.Get(installations[0].GithubAppId, installationLink.GithubInstallationId)
err = services.DiggerJobCompleted(client, &job.Batch.ID, job, jobLink.RepoFullName, repoFullNameSplit[0], repoFullNameSplit[1], workflowFileName)
err = services.DiggerJobCompleted(client, &job.Batch.ID, job, jobLink.RepoFullName, repoFullNameSplit[0], repoFullNameSplit[1], workflowFileName, d.GithubClientProvider)
if err != nil {
log.Printf("Error triggering job: %v", err)
return
Expand Down
15 changes: 8 additions & 7 deletions backend/services/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"github.com/diggerhq/digger/backend/ci_backends"
"github.com/diggerhq/digger/backend/config"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/google/go-github/v61/github"
"github.com/google/uuid"
"log"
)

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

jobLinksForParent, err := models.DB.GetDiggerJobParentLinksByParentId(&parentJob.DiggerJobID)
Expand Down Expand Up @@ -45,18 +46,18 @@ func DiggerJobCompleted(client *github.Client, batchId *uuid.UUID, parentJob *mo
return err
}
ciBackend := ci_backends.GithubActionCi{Client: client}
ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, job)
ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, batchId, job, gh)
}

}
return nil
}

func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob) error {
func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner string, repoName string, batchId *uuid.UUID, job *models.DiggerJob, gh utils.GithubClientProvider) error {
maxConcurrencyForBatch := config.DiggerConfig.GetInt("max_concurrency_per_batch")
if maxConcurrencyForBatch == 0 {
// concurrency limits not set
err := TriggerJob(ciBackend, repoFullname, repoOwner, repoName, batchId, job)
err := TriggerJob(gh, ciBackend, repoFullname, repoOwner, repoName, batchId, job)
if err != nil {
log.Printf("Could not trigger job: %v", err)
return err
Expand All @@ -79,7 +80,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner
models.DB.UpdateDiggerJob(job)
return nil
} else {
err := TriggerJob(ciBackend, repoFullname, repoOwner, repoName, batchId, job)
err := TriggerJob(gh, ciBackend, repoFullname, repoOwner, repoName, batchId, job)
if err != nil {
log.Printf("Could not trigger job: %v", err)
return err
Expand All @@ -89,7 +90,7 @@ func ScheduleJob(ciBackend ci_backends.CiBackend, repoFullname string, repoOwner
return nil
}

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

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

vcsToken, err := GetVCSTokenFromJob(*job)
vcsToken, err := GetVCSTokenFromJob(*job, gh)
if err != nil {
log.Printf("could not get vcs token: %v", err)
return fmt.Errorf("coult not get vcs token: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions backend/services/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"strconv"
)

func GetVCSTokenFromJob(job models.DiggerJob) (*string, error) {
func GetVCSTokenFromJob(job models.DiggerJob, gh utils.GithubClientProvider) (*string, error) {
// TODO: make it VCS generic
batch := job.Batch
var token string
switch batch.VCS {
case models.DiggerVCSGithub:
_, ghToken, err := utils.GetGithubService(
utils.DiggerGithubRealClientProvider{},
gh,
job.Batch.GithubInstallationId,
job.Batch.RepoFullName,
job.Batch.RepoOwner,
Expand Down
7 changes: 4 additions & 3 deletions backend/tasks/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"github.com/diggerhq/digger/backend/ci_backends"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/services"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/ci"
"github.com/diggerhq/digger/libs/ci/github"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"log"
)

func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.PullRequestService) {
func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.PullRequestService, gh utils.GithubClientProvider) {
dr := queueItem.DiggerRun
switch queueItem.DiggerRun.Status {
case models.RunQueued:
Expand All @@ -30,7 +31,7 @@ func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.Pull
return
}

vcsToken, err := services.GetVCSTokenFromJob(*job)
vcsToken, err := services.GetVCSTokenFromJob(*job, gh)
if err != nil {
log.Printf("could not get vcs token: %v", err)
return
Expand Down Expand Up @@ -103,7 +104,7 @@ func RunQueuesStateMachine(queueItem *models.DiggerRunQueueItem, service ci.Pull
return
}

vcsToken, err := services.GetVCSTokenFromJob(*job)
vcsToken, err := services.GetVCSTokenFromJob(*job, gh)
if err != nil {
log.Printf("could not get vcs token: %v", err)
return
Expand Down
3 changes: 2 additions & 1 deletion backend/tasks/runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
github2 "github.com/diggerhq/digger/libs/ci/github"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/diggerhq/digger/libs/spec"
Expand Down Expand Up @@ -152,7 +153,7 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) {
models.DB.UpdateDiggerBatch(batch)
queueItem, _ = models.DB.GetDiggerRunQueueItem(queueItem.ID)

RunQueuesStateMachine(queueItem, ciService)
RunQueuesStateMachine(queueItem, ciService, utils.DiggerGithubClientMockProvider{})
diggerRunRefreshed, _ := models.DB.GetDiggerRun(diggerRun.ID)
assert.Equal(t, testParam.NextExpectedStatus, diggerRunRefreshed.Status)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
log.Printf("failed to get github service for DiggerRun ID: %v: %v", dr.ID, err)
continue
}
RunQueuesStateMachine(&queueItem, service)
RunQueuesStateMachine(&queueItem, service, &utils.DiggerGithubRealClientProvider{})
}
})

Expand All @@ -64,7 +64,7 @@ func main() {
}

ciBackend := ci_backends.GithubActionCi{Client: service.Client}
services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, &batch.ID, &job)
services.ScheduleJob(ciBackend, repoFullName, repoOwner, repoName, &batch.ID, &job, &utils.DiggerGithubRealClientProvider{})
}
})

Expand Down
2 changes: 1 addition & 1 deletion backend/utils/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (gh DiggerGithubClientMockProvider) NewClient(netClient *net.Client) (*gith
return ghClient, nil
}

func (gh *DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) {
func (gh DiggerGithubClientMockProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) {
ghClient, _ := gh.NewClient(gh.MockedHTTPClient)
token := "token"
return ghClient, &token, nil
Expand Down
2 changes: 1 addition & 1 deletion ee/backend/controllers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: GetCiBackend error: %v", err))
return fmt.Errorf("error fetching ci backed %v", err)
}
err = controllers.TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, glService)
err = controllers.TriggerDiggerJobs(ciBackend, repoFullName, repoOwner, repoName, batchId, issueNumber, glService, nil)
if err != nil {
log.Printf("TriggerDiggerJobs error: %v", err)
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
Expand Down

0 comments on commit d306bef

Please sign in to comment.