Skip to content

Commit 6e6c1b8

Browse files
authored
add support for target branch of projects (#2125)
* add support for target branch of projects
1 parent 578a153 commit 6e6c1b8

File tree

22 files changed

+68
-32
lines changed

22 files changed

+68
-32
lines changed

backend/controllers/github_comment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
210210
"command", *diggerCommand,
211211
)
212212

213-
prBranchName, _, err := ghService.GetBranchName(issueNumber)
213+
prBranchName, _, targetBranch, _, err := ghService.GetBranchName(issueNumber)
214214
if err != nil {
215215
slog.Error("Error getting branch name",
216216
"issueNumber", issueNumber,
@@ -247,6 +247,8 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
247247
return fmt.Errorf("error filtering out projects from comment")
248248
}
249249

250+
impactedProjectsForComment = generic.FilterTargetBranchForImpactedProjects(impactedProjectsForComment, defaultBranch, targetBranch)
251+
250252
slog.Info("Issue comment event processed successfully",
251253
"issueNumber", issueNumber,
252254
"impactedProjectCount", len(impactedProjectsForComment),

backend/controllers/github_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ func getDiggerConfigForPR(gh utils.GithubClientProvider, orgId uint, prLabels []
616616
}
617617

618618
var prBranch string
619-
prBranch, prCommitSha, err := ghService.GetBranchName(prNumber)
619+
prBranch, prCommitSha, _, _, err := ghService.GetBranchName(prNumber)
620620
if err != nil {
621621
slog.Error("Error getting branch name for PR",
622622
"prNumber", prNumber,

backend/controllers/github_pull_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
9595
// we sleep for 1 second to give github time to delete the branch
9696
time.Sleep(1 * time.Second)
9797

98-
branchName, _, err := ghService.GetBranchName(prNumber)
98+
branchName, _, _, _, err := ghService.GetBranchName(prNumber)
9999
if err != nil {
100100
slog.Error("Could not retrieve PR details", "prNumber", prNumber, "error", err)
101101
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: Could not retrieve PR details, error: %v", err))

cli/pkg/digger/digger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ func (m *MockPRManager) CreateCommentReaction(id string, reaction string) error
164164
return nil
165165
}
166166

167-
func (m *MockPRManager) GetBranchName(prNumber int) (string, string, error) {
167+
func (m *MockPRManager) GetBranchName(prNumber int) (string, string, string, string, error) {
168168
m.Commands = append(m.Commands, RunInfo{"GetBranchName", strconv.Itoa(prNumber), time.Now()})
169-
return "", "", nil
169+
return "", "", "", "", nil
170170
}
171171

172172
func (m *MockPRManager) SetOutput(prNumber int, key string, value string) error {

cli/pkg/github/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh
300300
if prEvent, ok := ghEvent.(github.PullRequestEvent); ok {
301301
jobs, coversAllImpactedProjects, err = dg_github.ConvertGithubPullRequestEventToJobs(&prEvent, impactedProjects, requestedProject, *diggerConfig, true)
302302
} else if commentEvent, ok := ghEvent.(github.IssueCommentEvent); ok {
303-
prBranchName, _, err := githubPrService.GetBranchName(*commentEvent.Issue.Number)
303+
prBranchName, _, _, _, err := githubPrService.GetBranchName(*commentEvent.Issue.Number)
304304

305305
if err != nil {
306306
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Error while retrieving default branch from Issue: %v", err), 6)

docs/ce/reference/digger.yml.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ pr_locks: true
2626
projects:
2727
- name: prod
2828
dir: prod
29+
branch: main
2930
workspace: default
3031
terragrunt: false
3132
workflow: prod
3233
include_patterns: ["../modules/**"]
3334
exclude_patterns: []
3435
- name: staging
3536
dir: staging
37+
branch: main
3638
workflow: staging
3739
include_patterns: ["../modules/**"]
3840
exclude_patterns: []
@@ -115,6 +117,7 @@ workflows:
115117
| Key | Type | Default | Required | Description | Notes |
116118
| ------------------------ | ---------------------------------------------------- | ------- | -------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------- |
117119
| name | string | | yes | name of the project | must be unique |
120+
| branch | string | | yes | the target branch to match this project on | This field is optional and defaults to the repository's default branch when not set |
118121
| dir | string | | yes | directory containing the project | |
119122
| workspace | string | default | no | terraform workspace to use | |
120123
| opentofu | boolean | false | no | whether to use opentofu | |

ee/backend/controllers/bitbucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func handleIssueCommentEventBB(bitbucketProvider utils.BitbucketProvider, payloa
208208
return fmt.Errorf("unknown digger command in comment %v", err)
209209
}
210210

211-
prBranchName, _, err := bbService.GetBranchName(issueNumber)
211+
prBranchName, _, _, _, err := bbService.GetBranchName(issueNumber)
212212
if err != nil {
213213
log.Printf("GetBranchName error: %v", err)
214214
utils.InitCommentReporter(bbService, issueNumber, fmt.Sprintf(":x: GetBranchName error: %v", err))

ee/backend/controllers/gitlab.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
142142
// here we check if pr was merged and automatic deletion is enabled, to avoid errors when
143143
// pr is merged and the branch does not exist we handle that gracefully
144144
if action == "merge" {
145-
sourceBranch, _, err := glService.GetBranchName(prNumber)
145+
sourceBranch, _, _, _, err := glService.GetBranchName(prNumber)
146146
if err != nil {
147147
utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: Could not retrieve PR details, error: %v", err))
148148
log.Printf("Could not retrieve PR details error: %v", err)
@@ -393,7 +393,7 @@ func handleIssueCommentEvent(gitlabProvider utils.GitlabProvider, payload *gitla
393393
return fmt.Errorf("unknown digger command in comment %v", err)
394394
}
395395

396-
prBranchName, _, err := glService.GetBranchName(issueNumber)
396+
prBranchName, _, _, _, err := glService.GetBranchName(issueNumber)
397397
if err != nil {
398398
log.Printf("GetBranchName error: %v", err)
399399
utils.InitCommentReporter(glService, issueNumber, fmt.Sprintf(":x: GetBranchName error: %v", err))

libs/ci/azure/azure.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ func (a *AzureReposService) CreateCommentReaction(id string, reaction string) er
356356
return nil
357357
}
358358

359-
func (a *AzureReposService) GetBranchName(prNumber int) (string, string, error) {
359+
func (a *AzureReposService) GetBranchName(prNumber int) (string, string, string, string, error) {
360360
//TODO implement me
361-
return "", "", nil
361+
return "", "", "", "", nil
362362
}
363363

364364
func (svc *AzureReposService) SetOutput(prNumber int, key string, value string) error {

libs/ci/bitbucket/bitbucket.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,17 @@ func (b BitbucketAPI) IsClosed(prNumber int) (bool, error) {
477477
return pullRequest.State != "OPEN", nil
478478
}
479479

480-
func (b BitbucketAPI) GetBranchName(prNumber int) (string, string, error) {
480+
func (b BitbucketAPI) GetBranchName(prNumber int) (string, string, string, string, error) {
481481
url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber)
482482

483483
resp, err := b.sendRequest("GET", url, nil)
484484
if err != nil {
485-
return "", "", err
485+
return "", "", "", "", err
486486
}
487487
defer resp.Body.Close()
488488

489489
if resp.StatusCode != http.StatusOK {
490-
return "", "", fmt.Errorf("failed to get pull request. Status code: %d", resp.StatusCode)
490+
return "", "", "", "", fmt.Errorf("failed to get pull request. Status code: %d", resp.StatusCode)
491491
}
492492

493493
var pullRequest struct {
@@ -500,10 +500,10 @@ func (b BitbucketAPI) GetBranchName(prNumber int) (string, string, error) {
500500

501501
err = json.NewDecoder(resp.Body).Decode(&pullRequest)
502502
if err != nil {
503-
return "", "", err
503+
return "", "", "", "", err
504504
}
505505

506-
return pullRequest.Source.Branch.Name, "", nil
506+
return pullRequest.Source.Branch.Name, "", "", "", nil
507507
}
508508

509509
func (svc BitbucketAPI) SetOutput(prNumber int, key string, value string) error {

0 commit comments

Comments
 (0)