From 8d2d7858827c994d79f095f80d3d092ef6a55031 Mon Sep 17 00:00:00 2001 From: Michal Koval Date: Wed, 19 Mar 2025 10:10:38 +0100 Subject: [PATCH 1/4] fix: add target branch support for bitbucket cloud pull request generator Signed-off-by: Michal Koval --- .../services/pull_request/bitbucket_cloud.go | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/applicationset/services/pull_request/bitbucket_cloud.go b/applicationset/services/pull_request/bitbucket_cloud.go index 2abd89dce22aa..af2b0ae60a1a1 100644 --- a/applicationset/services/pull_request/bitbucket_cloud.go +++ b/applicationset/services/pull_request/bitbucket_cloud.go @@ -17,10 +17,19 @@ type BitbucketCloudService struct { } type BitbucketCloudPullRequest struct { - ID int `json:"id"` - Title string `json:"title"` - Source BitbucketCloudPullRequestSource `json:"source"` - Author BitbucketCloudPullRequestAuthor `json:"author"` + ID int `json:"id"` + Title string `json:"title"` + Source BitbucketCloudPullRequestSource `json:"source"` + Author BitbucketCloudPullRequestAuthor `json:"author"` + Destination BitbucketCloudPullRequestDestination `json:"destination"` +} + +type BitbucketCloudPullRequestDestination struct { + Branch BitbucketCloudPullRequestDestinationBranch `json:"branch"` +} + +type BitbucketCloudPullRequestDestinationBranch struct { + BitbucketCloudPullRequestSourceBranch } type BitbucketCloudPullRequestSource struct { @@ -136,11 +145,12 @@ func (b *BitbucketCloudService) List(_ context.Context) ([]*PullRequest, error) pullRequests := []*PullRequest{} for _, pull := range pulls { pullRequests = append(pullRequests, &PullRequest{ - Number: pull.ID, - Title: pull.Title, - Branch: pull.Source.Branch.Name, - HeadSHA: pull.Source.Commit.Hash, - Author: pull.Author.Nickname, + Number: pull.ID, + Title: pull.Title, + Branch: pull.Source.Branch.Name, + TargetBranch: pull.Destination.Branch.Name, + HeadSHA: pull.Source.Commit.Hash, + Author: pull.Author.Nickname, }) } From a8e039867388dbbae19d25c6383f321e0b5dd4ae Mon Sep 17 00:00:00 2001 From: Michal Koval Date: Wed, 19 Mar 2025 21:48:40 +0100 Subject: [PATCH 2/4] fix: add units tests bitbucket cloud pull request generator Signed-off-by: Michal Koval --- .../pull_request/bitbucket_cloud_test.go | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/applicationset/services/pull_request/bitbucket_cloud_test.go b/applicationset/services/pull_request/bitbucket_cloud_test.go index b9d5228c60993..62a949bf80c22 100644 --- a/applicationset/services/pull_request/bitbucket_cloud_test.go +++ b/applicationset/services/pull_request/bitbucket_cloud_test.go @@ -349,6 +349,11 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { }, "author": { "nickname": "testName" + }, + "destination": { + "branch": { + "name": "master" + } } }, { @@ -365,6 +370,11 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { }, "author": { "nickname": "testName" + }, + "destination": { + "branch": { + "name": "branch-200" + } } } ] @@ -390,6 +400,11 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { }, "author": { "nickname": "testName" + }, + "destination": { + "branch": { + "name": "master" + } } } ] @@ -413,18 +428,20 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { require.NoError(t, err) assert.Len(t, pullRequests, 2) assert.Equal(t, PullRequest{ - Number: 101, - Title: "feat(101)", - Branch: "feature-101", - HeadSHA: "1a8dd249c04a", - Author: "testName", + Number: 101, + Title: "feat(101)", + Branch: "feature-101", + HeadSHA: "1a8dd249c04a", + Author: "testName", + TargetBranch: "master", }, *pullRequests[0]) assert.Equal(t, PullRequest{ - Number: 102, - Title: "feat(102)", - Branch: "feature-102", - HeadSHA: "6344d9623e3b", - Author: "testName", + Number: 102, + Title: "feat(102)", + Branch: "feature-102", + HeadSHA: "6344d9623e3b", + Author: "testName", + TargetBranch: "master", }, *pullRequests[1]) regexp = `.*2$` @@ -438,11 +455,12 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { require.NoError(t, err) assert.Len(t, pullRequests, 1) assert.Equal(t, PullRequest{ - Number: 102, - Title: "feat(102)", - Branch: "feature-102", - HeadSHA: "6344d9623e3b", - Author: "testName", + Number: 102, + Title: "feat(102)", + Branch: "feature-102", + HeadSHA: "6344d9623e3b", + Author: "testName", + TargetBranch: "master", }, *pullRequests[0]) regexp = `[\d{2}` @@ -454,4 +472,23 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { }, }) require.Error(t, err) + + regexp = `feature-2[\d]{2}` + targetRegexp := `branch.*` + pullRequests, err = ListPullRequests(t.Context(), svc, []v1alpha1.PullRequestGeneratorFilter{ + { + BranchMatch: ®exp, + TargetBranchMatch: &targetRegexp, + }, + }) + require.NoError(t, err) + assert.Len(t, pullRequests, 1) + assert.Equal(t, PullRequest{ + Number: 200, + Title: "feat(200)", + Branch: "feature-200", + HeadSHA: "4cf807e67a6d", + Author: "testName", + TargetBranch: "branch-200", + }, *pullRequests[0]) } From dba7c93a203f758aed07a209d28f7341d93533aa Mon Sep 17 00:00:00 2001 From: Michal Koval Date: Mon, 24 Mar 2025 18:00:18 +0100 Subject: [PATCH 3/4] docs: add bitbucket cloud targetBranchMatch to Generators-Pull-Request.md Signed-off-by: Michal Koval --- .../applicationset/Generators-Pull-Request.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/operator-manual/applicationset/Generators-Pull-Request.md b/docs/operator-manual/applicationset/Generators-Pull-Request.md index 9ccd7a1960718..ab91fb48ed497 100644 --- a/docs/operator-manual/applicationset/Generators-Pull-Request.md +++ b/docs/operator-manual/applicationset/Generators-Pull-Request.md @@ -262,6 +262,14 @@ spec: # Filter PRs using the source branch name. (optional) filters: - branchMatch: ".*-argocd" + + # If you need to filter destination branch too, you can use this + - targetBranchMatch: "master" + + # Also you can combine source and target branch filters like + # This case will match any pull-request where source branch ends with "-argocd" and destination branch is master + - branchMatch: ".*-argocd" + targetBranchMatch: "master" template: # ... ``` @@ -269,7 +277,12 @@ spec: - `owner`: Required name of the Bitbucket workspace - `repo`: Required name of the Bitbucket repository. - `api`: Optional URL to access the Bitbucket REST API. For the example above, an API request would be made to `https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests`. If not set, defaults to `https://api.bitbucket.org/2.0` -- `branchMatch`: Optional regexp filter which should match the source branch name. This is an alternative to labels which are not supported by Bitbucket server. + +You can use branch `filters` like +- `branchMatch`: Optional regexp filter which should match the source branch name. +- `targetBranchMatch`: Optional regexp filter which should match destination branch name. + +> Note: Labels are not supported by Bitbucket. If you want to access a private repository, Argo CD will need credentials to access repository in Bitbucket Cloud. You can use Bitbucket App Password (generated per user, with access to whole workspace), or Bitbucket App Token (generated per repository, with access limited to repository scope only). If both App Password and App Token are defined, App Token will be used. From acd297d6a2c2d93396f47438ba3b20b73db7ff1c Mon Sep 17 00:00:00 2001 From: Michal Koval Date: Thu, 24 Apr 2025 14:36:11 +0200 Subject: [PATCH 4/4] fix: remove reuse of `source branch` struct type in `destination branch` Signed-off-by: Michal Koval --- applicationset/services/pull_request/bitbucket_cloud.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applicationset/services/pull_request/bitbucket_cloud.go b/applicationset/services/pull_request/bitbucket_cloud.go index af2b0ae60a1a1..1d03bb0249c6d 100644 --- a/applicationset/services/pull_request/bitbucket_cloud.go +++ b/applicationset/services/pull_request/bitbucket_cloud.go @@ -29,7 +29,7 @@ type BitbucketCloudPullRequestDestination struct { } type BitbucketCloudPullRequestDestinationBranch struct { - BitbucketCloudPullRequestSourceBranch + Name string `json:"name"` } type BitbucketCloudPullRequestSource struct {