Skip to content

Commit

Permalink
implementation of backend based locks (#1524)
Browse files Browse the repository at this point in the history
* implementation of backend based locks
  • Loading branch information
motatoes authored May 30, 2024
1 parent 40a31d0 commit e43db33
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 62 deletions.
95 changes: 89 additions & 6 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/diggerhq/digger/backend/locking"
"github.com/diggerhq/digger/backend/segment"
"github.com/diggerhq/digger/backend/services"
comment_updater "github.com/diggerhq/digger/libs/comment_utils/reporting"
dg_locking "github.com/diggerhq/digger/libs/locking"
orchestrator_scheduler "github.com/diggerhq/digger/libs/orchestrator/scheduler"
"github.com/google/uuid"
"log"
Expand All @@ -24,7 +26,7 @@ import (
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
dg_configuration "github.com/diggerhq/digger/libs/digger_config"
orchestrator "github.com/diggerhq/digger/libs/orchestrator"
"github.com/diggerhq/digger/libs/orchestrator"
dg_github "github.com/diggerhq/digger/libs/orchestrator/github"
"github.com/dominikbraun/graph"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -469,12 +471,45 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
return nil
}

diggerCommand, err := orchestrator.GetCommandFromJob(jobsForImpactedProjects[0])
if err != nil {
log.Printf("could not determine digger command from job: %v", jobsForImpactedProjects[0].Commands)
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: could not determine digger command from job: %v", err))
return fmt.Errorf("unkown digger command in comment %v", err)
}

commentReporter, err := utils.InitCommentReporter(ghService, prNumber, ":construction_worker: Digger starting...")
if err != nil {
log.Printf("Error initializing comment reporter: %v", err)
return fmt.Errorf("error initializing comment reporter")
}

// perform locking/unlocking in backend
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
InternalLock: locking.BackendDBLock{
OrgId: organisationId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: prNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed to perform lock action on project: %v, %v", project.Name, err)
}
}

// if commands are locking or unlocking we don't need to trigger any jobs
if *diggerCommand == orchestrator.DiggerCommandUnlock ||
*diggerCommand == orchestrator.DiggerCommandLock {
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":white_check_mark: Command %v completed successfully", *diggerCommand))
return nil
}

err = utils.ReportInitialJobsStatus(commentReporter, jobsForImpactedProjects)
if err != nil {
log.Printf("Failed to comment initial status for jobs: %v", err)
Expand All @@ -499,7 +534,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
impactedJobsMap[j.ProjectName] = j
}

batchId, _, err := utils.ConvertJobsToDiggerJobs(orchestrator.DiggerCommandPlan, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, *branch, prNumber, repoOwner, repoName, repoFullName, commentReporter.CommentId, diggerYmlStr)
batchId, _, err := utils.ConvertJobsToDiggerJobs(*diggerCommand, organisationId, impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, *branch, prNumber, repoOwner, repoName, repoFullName, commentReporter.CommentId, diggerYmlStr)
if err != nil {
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))
Expand Down Expand Up @@ -664,7 +699,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
}

if !config.AllowDraftPRs && isDraft {
log.Printf("AllowDraftPRs is enabled, skipping PR: %v", issueNumber)
log.Printf("AllowDraftPRs is disabled, skipping PR: %v", issueNumber)
return nil
}

Expand Down Expand Up @@ -696,9 +731,30 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
}
log.Printf("GitHub IssueComment event processed successfully\n")

if err != nil {
log.Printf("GetGithubService error: %v", err)
return fmt.Errorf("error getting github prservice")
// perform unlocking in backend
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
InternalLock: locking.BackendDBLock{
OrgId: orgId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: issueNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed perform lock action on project: %v %v", project.Name, err)
}
}

// if commands are locking or unlocking we don't need to trigger any jobs
if *diggerCommand == orchestrator.DiggerCommandUnlock ||
*diggerCommand == orchestrator.DiggerCommandLock {
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":white_check_mark: Command %v completed successfully", *diggerCommand))
return nil
}

jobs, _, err := dg_github.ConvertGithubIssueCommentEventToJobs(payload, impactedProjects, requestedProject, config.Workflows, prBranchName)
Expand Down Expand Up @@ -787,6 +843,33 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
return nil
}

func PerformLockingActionFromCommand(prLock dg_locking.PullRequestLock, command orchestrator.DiggerCommand) error {
var err error
switch command {
case orchestrator.DiggerCommandUnlock:
_, err = prLock.Unlock()
if err != nil {
err = fmt.Errorf("failed to unlock project: %v", err)
}
case orchestrator.DiggerCommandPlan:
_, err = prLock.Lock()
if err != nil {
err = fmt.Errorf("failed to lock project: %v", err)
}
case orchestrator.DiggerCommandApply:
_, err = prLock.Lock()
if err != nil {
err = fmt.Errorf("failed to lock project: %v", err)
}
case orchestrator.DiggerCommandLock:
_, err = prLock.Lock()
if err != nil {
err = fmt.Errorf("failed to lock project: %v", err)
}
}
return err
}

func TriggerDiggerJobs(client *github.Client, repoOwner string, repoName string, batchId *uuid.UUID, prNumber int, prService *dg_github.GithubService) error {
_, err := models.DB.GetDiggerBatch(batchId)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions backend/controllers/locking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package controllers
38 changes: 25 additions & 13 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/go-git/go-git/v5 v5.12.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/go-github/v61 v61.0.0
github.com/google/go-github/v62 v62.0.0
github.com/google/uuid v1.6.0
github.com/migueleliasweb/go-github-mock v0.0.23
github.com/robfig/cron v1.2.0
Expand All @@ -43,6 +42,10 @@ require (
dario.cat/mergo v1.0.0 // indirect
filippo.io/age v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go v63.3.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/data/aztables v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.26 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.23 // indirect
Expand All @@ -53,6 +56,7 @@ require (
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
Expand All @@ -63,18 +67,23 @@ require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.51.21 // indirect
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.12 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.12 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.16 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.16 // indirect
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.15 // indirect
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.7.15 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.32.1 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.9 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.10 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
Expand All @@ -91,7 +100,6 @@ require (
github.com/creack/pty v1.1.17 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/diggerhq/digger/cli v0.0.0-20240524095424-de040e7a4485 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/dineshba/tf-summarize v0.3.10 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
Expand All @@ -111,6 +119,7 @@ require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -154,7 +163,7 @@ require (
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform v0.15.3 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20231204233900-a34142ec2a72 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20240509232506-4708120f8f30 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-registry-address v0.2.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.1 // indirect
Expand All @@ -176,6 +185,7 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/m1gwings/treedrawer v0.3.3-beta // indirect
Expand All @@ -202,6 +212,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/posener/complete v1.2.3 // indirect
Expand Down Expand Up @@ -231,6 +242,7 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wader/gormstore/v2 v2.0.3 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
github.com/zclconf/go-cty-yaml v1.0.3 // indirect
Expand Down
Loading

0 comments on commit e43db33

Please sign in to comment.