From 800e2226ba3180696fc54e83ddfb683cc2679ec8 Mon Sep 17 00:00:00 2001 From: Julio Date: Sat, 20 Apr 2024 00:02:25 +0000 Subject: [PATCH] feat:(appset): add title key PR generator (#16447) Signed-off-by: ggjulio --- .../services/pull_request/azure_devops.go | 1 + .../pull_request/azure_devops_test.go | 3 +++ .../services/pull_request/bitbucket_cloud.go | 2 ++ .../pull_request/bitbucket_cloud_test.go | 16 ++++++++++++++++ .../services/pull_request/bitbucket_server.go | 1 + .../pull_request/bitbucket_server_test.go | 15 +++++++++++++++ applicationset/services/pull_request/gitea.go | 1 + .../services/pull_request/gitea_test.go | 1 + .../services/pull_request/github.go | 1 + .../services/pull_request/gitlab.go | 1 + .../services/pull_request/gitlab_test.go | 1 + .../services/pull_request/interface.go | 2 ++ .../services/pull_request/utils_test.go | 19 +++++++++++++++++++ .../applicationset/Generators-Pull-Request.md | 1 + 14 files changed, 65 insertions(+) diff --git a/applicationset/services/pull_request/azure_devops.go b/applicationset/services/pull_request/azure_devops.go index 9090b829ca0c2..94fa09789b58a 100644 --- a/applicationset/services/pull_request/azure_devops.go +++ b/applicationset/services/pull_request/azure_devops.go @@ -93,6 +93,7 @@ func (a *AzureDevOpsService) List(ctx context.Context) ([]*PullRequest, error) { if *pr.Repository.Name == a.repo { pullRequests = append(pullRequests, &PullRequest{ Number: *pr.PullRequestId, + Title: *pr.Title, Branch: strings.Replace(*pr.SourceRefName, "refs/heads/", "", 1), HeadSHA: *pr.LastMergeSourceCommit.CommitId, Labels: azureDevOpsLabels, diff --git a/applicationset/services/pull_request/azure_devops_test.go b/applicationset/services/pull_request/azure_devops_test.go index 5ed8f4de78b9d..214d309e461d5 100644 --- a/applicationset/services/pull_request/azure_devops_test.go +++ b/applicationset/services/pull_request/azure_devops_test.go @@ -55,12 +55,14 @@ func TestListPullRequest(t *testing.T) { teamProject := "myorg_project" repoName := "myorg_project_repo" pr_id := 123 + pr_title := "feat(123)" pr_head_sha := "cd4973d9d14a08ffe6b641a89a68891d6aac8056" ctx := context.Background() pullRequestMock := []git.GitPullRequest{ { PullRequestId: createIntPtr(pr_id), + Title: createStringPtr(pr_title), SourceRefName: createStringPtr("refs/heads/feature-branch"), LastMergeSourceCommit: &git.GitCommitRef{ CommitId: createStringPtr(pr_head_sha), @@ -94,6 +96,7 @@ func TestListPullRequest(t *testing.T) { assert.Equal(t, 1, len(list)) assert.Equal(t, "feature-branch", list[0].Branch) assert.Equal(t, pr_head_sha, list[0].HeadSHA) + assert.Equal(t, "feat(123)", list[0].Title) assert.Equal(t, pr_id, list[0].Number) } diff --git a/applicationset/services/pull_request/bitbucket_cloud.go b/applicationset/services/pull_request/bitbucket_cloud.go index 5d5f8208f9b06..c7dff767e37aa 100644 --- a/applicationset/services/pull_request/bitbucket_cloud.go +++ b/applicationset/services/pull_request/bitbucket_cloud.go @@ -17,6 +17,7 @@ type BitbucketCloudService struct { type BitbucketCloudPullRequest struct { ID int `json:"id"` + Title string `json:"title"` Source BitbucketCloudPullRequestSource `json:"source"` } @@ -129,6 +130,7 @@ func (b *BitbucketCloudService) List(_ context.Context) ([]*PullRequest, error) for _, pull := range pulls { pullRequests = append(pullRequests, &PullRequest{ Number: pull.ID, + Title: pull.Title, Branch: pull.Source.Branch.Name, HeadSHA: pull.Source.Commit.Hash, }) diff --git a/applicationset/services/pull_request/bitbucket_cloud_test.go b/applicationset/services/pull_request/bitbucket_cloud_test.go index 2f604c1fa9ccf..a046f73279aa8 100644 --- a/applicationset/services/pull_request/bitbucket_cloud_test.go +++ b/applicationset/services/pull_request/bitbucket_cloud_test.go @@ -26,6 +26,7 @@ func defaultHandlerCloud(t *testing.T) func(http.ResponseWriter, *http.Request) "values": [ { "id": 101, + "title": "feat(foo-bar)", "source": { "branch": { "name": "feature/foo-bar" @@ -85,6 +86,7 @@ func TestListPullRequestBearerTokenCloud(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, 101, pullRequests[0].Number) + assert.Equal(t, "feat(foo-bar)", pullRequests[0].Title) assert.Equal(t, "feature/foo-bar", pullRequests[0].Branch) assert.Equal(t, "1a8dd249c04a", pullRequests[0].HeadSHA) } @@ -101,6 +103,7 @@ func TestListPullRequestNoAuthCloud(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, 101, pullRequests[0].Number) + assert.Equal(t, "feat(foo-bar)", pullRequests[0].Title) assert.Equal(t, "feature/foo-bar", pullRequests[0].Branch) assert.Equal(t, "1a8dd249c04a", pullRequests[0].HeadSHA) } @@ -117,6 +120,7 @@ func TestListPullRequestBasicAuthCloud(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, 101, pullRequests[0].Number) + assert.Equal(t, "feat(foo-bar)", pullRequests[0].Title) assert.Equal(t, "feature/foo-bar", pullRequests[0].Branch) assert.Equal(t, "1a8dd249c04a", pullRequests[0].HeadSHA) } @@ -135,6 +139,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) { "values": [ { "id": 101, + "title": "feat(101)", "source": { "branch": { "name": "feature-101" @@ -147,6 +152,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) { }, { "id": 102, + "title": "feat(102)", "source": { "branch": { "name": "feature-102" @@ -168,6 +174,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) { "values": [ { "id": 103, + "title": "feat(103)", "source": { "branch": { "name": "feature-103" @@ -195,16 +202,19 @@ func TestListPullRequestPaginationCloud(t *testing.T) { assert.Equal(t, 3, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 101, + Title: "feat(101)", Branch: "feature-101", HeadSHA: "1a8dd249c04a", }, *pullRequests[0]) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", HeadSHA: "4cf807e67a6d", }, *pullRequests[1]) assert.Equal(t, PullRequest{ Number: 103, + Title: "feat(103)", Branch: "feature-103", HeadSHA: "6344d9623e3b", }, *pullRequests[2]) @@ -308,6 +318,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { "values": [ { "id": 101, + "title": "feat(101)", "source": { "branch": { "name": "feature-101" @@ -320,6 +331,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { }, { "id": 200, + "title": "feat(200)", "source": { "branch": { "name": "feature-200" @@ -341,6 +353,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { "values": [ { "id": 102, + "title": "feat(102)", "source": { "branch": { "name": "feature-102" @@ -373,11 +386,13 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { assert.Equal(t, 2, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 101, + Title: "feat(101)", Branch: "feature-101", HeadSHA: "1a8dd249c04a", }, *pullRequests[0]) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", HeadSHA: "6344d9623e3b", }, *pullRequests[1]) @@ -394,6 +409,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) { assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", HeadSHA: "6344d9623e3b", }, *pullRequests[0]) diff --git a/applicationset/services/pull_request/bitbucket_server.go b/applicationset/services/pull_request/bitbucket_server.go index 99665d163e1bc..5bf82d2c537bb 100644 --- a/applicationset/services/pull_request/bitbucket_server.go +++ b/applicationset/services/pull_request/bitbucket_server.go @@ -67,6 +67,7 @@ func (b *BitbucketService) List(_ context.Context) ([]*PullRequest, error) { for _, pull := range pulls { pullRequests = append(pullRequests, &PullRequest{ Number: pull.ID, + Title: pull.Title, Branch: pull.FromRef.DisplayID, // ID: refs/heads/main DisplayID: main TargetBranch: pull.ToRef.DisplayID, HeadSHA: pull.FromRef.LatestCommit, // This is not defined in the official docs, but works in practice diff --git a/applicationset/services/pull_request/bitbucket_server_test.go b/applicationset/services/pull_request/bitbucket_server_test.go index 911e3e7e0ccd0..e5b0d34b1e3b0 100644 --- a/applicationset/services/pull_request/bitbucket_server_test.go +++ b/applicationset/services/pull_request/bitbucket_server_test.go @@ -24,6 +24,7 @@ func defaultHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { "values": [ { "id": 101, + "title": "feat(ABC) : 123", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "master", @@ -59,6 +60,7 @@ func TestListPullRequestNoAuth(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, 101, pullRequests[0].Number) + assert.Equal(t, "feat(ABC) : 123", pullRequests[0].Title) assert.Equal(t, "feature-ABC-123", pullRequests[0].Branch) assert.Equal(t, "master", pullRequests[0].TargetBranch) assert.Equal(t, "cb3cf2e4d1517c83e720d2585b9402dbef71f992", pullRequests[0].HeadSHA) @@ -77,6 +79,7 @@ func TestListPullRequestPagination(t *testing.T) { "values": [ { "id": 101, + "title": "feat(101)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "master", @@ -90,6 +93,7 @@ func TestListPullRequestPagination(t *testing.T) { }, { "id": 102, + "title": "feat(102)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "branch", @@ -112,6 +116,7 @@ func TestListPullRequestPagination(t *testing.T) { "values": [ { "id": 200, + "title": "feat(200)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "master", @@ -141,6 +146,7 @@ func TestListPullRequestPagination(t *testing.T) { assert.Equal(t, 3, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 101, + Title: "feat(101)", Branch: "feature-101", TargetBranch: "master", HeadSHA: "ab3cf2e4d1517c83e720d2585b9402dbef71f992", @@ -148,6 +154,7 @@ func TestListPullRequestPagination(t *testing.T) { }, *pullRequests[0]) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", TargetBranch: "branch", HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992", @@ -155,6 +162,7 @@ func TestListPullRequestPagination(t *testing.T) { }, *pullRequests[1]) assert.Equal(t, PullRequest{ Number: 200, + Title: "feat(200)", Branch: "feature-200", TargetBranch: "master", HeadSHA: "cb3cf2e4d1517c83e720d2585b9402dbef71f992", @@ -176,6 +184,7 @@ func TestListPullRequestBasicAuth(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, 101, pullRequests[0].Number) + assert.Equal(t, "feat(ABC) : 123", pullRequests[0].Title) assert.Equal(t, "feature-ABC-123", pullRequests[0].Branch) assert.Equal(t, "cb3cf2e4d1517c83e720d2585b9402dbef71f992", pullRequests[0].HeadSHA) } @@ -255,6 +264,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { "values": [ { "id": 101, + "title": "feat(101)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "master", @@ -268,6 +278,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { }, { "id": 102, + "title": "feat(102)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "branch", @@ -290,6 +301,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { "values": [ { "id": 200, + "title": "feat(200)", "toRef": { "latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a", "displayId": "master", @@ -324,6 +336,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { assert.Equal(t, 2, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 101, + Title: "feat(101)", Branch: "feature-101", TargetBranch: "master", HeadSHA: "ab3cf2e4d1517c83e720d2585b9402dbef71f992", @@ -331,6 +344,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { }, *pullRequests[0]) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", TargetBranch: "branch", HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992", @@ -349,6 +363,7 @@ func TestListPullRequestBranchMatch(t *testing.T) { assert.Equal(t, 1, len(pullRequests)) assert.Equal(t, PullRequest{ Number: 102, + Title: "feat(102)", Branch: "feature-102", TargetBranch: "branch", HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992", diff --git a/applicationset/services/pull_request/gitea.go b/applicationset/services/pull_request/gitea.go index ff385ff281c6d..f68ae2ce9a574 100644 --- a/applicationset/services/pull_request/gitea.go +++ b/applicationset/services/pull_request/gitea.go @@ -57,6 +57,7 @@ func (g *GiteaService) List(ctx context.Context) ([]*PullRequest, error) { for _, pr := range prs { list = append(list, &PullRequest{ Number: int(pr.Index), + Title: pr.Title, Branch: pr.Head.Ref, TargetBranch: pr.Base.Ref, HeadSHA: pr.Head.Sha, diff --git a/applicationset/services/pull_request/gitea_test.go b/applicationset/services/pull_request/gitea_test.go index 125c8ee481b3a..7e65b39542aaf 100644 --- a/applicationset/services/pull_request/gitea_test.go +++ b/applicationset/services/pull_request/gitea_test.go @@ -255,6 +255,7 @@ func TestGiteaList(t *testing.T) { assert.Nil(t, err) assert.Equal(t, len(prs), 1) assert.Equal(t, prs[0].Number, 1) + assert.Equal(t, prs[0].Title, "add an empty file") assert.Equal(t, prs[0].Branch, "test") assert.Equal(t, prs[0].TargetBranch, "main") assert.Equal(t, prs[0].HeadSHA, "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f") diff --git a/applicationset/services/pull_request/github.go b/applicationset/services/pull_request/github.go index 7c801e7370f53..692146632a78c 100644 --- a/applicationset/services/pull_request/github.go +++ b/applicationset/services/pull_request/github.go @@ -66,6 +66,7 @@ func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) { } pullRequests = append(pullRequests, &PullRequest{ Number: *pull.Number, + Title: *pull.Title, Branch: *pull.Head.Ref, TargetBranch: *pull.Base.Ref, HeadSHA: *pull.Head.SHA, diff --git a/applicationset/services/pull_request/gitlab.go b/applicationset/services/pull_request/gitlab.go index 04a4f3464f6f0..f53d053c45227 100644 --- a/applicationset/services/pull_request/gitlab.go +++ b/applicationset/services/pull_request/gitlab.go @@ -81,6 +81,7 @@ func (g *GitLabService) List(ctx context.Context) ([]*PullRequest, error) { for _, mr := range mrs { pullRequests = append(pullRequests, &PullRequest{ Number: mr.IID, + Title: mr.Title, Branch: mr.SourceBranch, TargetBranch: mr.TargetBranch, HeadSHA: mr.SHA, diff --git a/applicationset/services/pull_request/gitlab_test.go b/applicationset/services/pull_request/gitlab_test.go index 59c476fcd713a..c44e3af77f74c 100644 --- a/applicationset/services/pull_request/gitlab_test.go +++ b/applicationset/services/pull_request/gitlab_test.go @@ -79,6 +79,7 @@ func TestList(t *testing.T) { assert.NoError(t, err) assert.Len(t, prs, 1) assert.Equal(t, prs[0].Number, 15442) + assert.Equal(t, prs[0].Title, "Draft: Use structured logging for DB load balancer") assert.Equal(t, prs[0].Branch, "use-structured-logging-for-db-load-balancer") assert.Equal(t, prs[0].TargetBranch, "master") assert.Equal(t, prs[0].HeadSHA, "2fc4e8b972ff3208ec63b6143e34ad67ff343ad7") diff --git a/applicationset/services/pull_request/interface.go b/applicationset/services/pull_request/interface.go index 0015cfe5eafa6..28e40a575362b 100644 --- a/applicationset/services/pull_request/interface.go +++ b/applicationset/services/pull_request/interface.go @@ -8,6 +8,8 @@ import ( type PullRequest struct { // Number is a number that will be the ID of the pull request. Number int + // Title of the pull request. + Title string // Branch is the name of the branch from which the pull request originated. Branch string // TargetBranch is the name of the target branch of the pull request. diff --git a/applicationset/services/pull_request/utils_test.go b/applicationset/services/pull_request/utils_test.go index 3f813127edab7..137b8097ff7a1 100644 --- a/applicationset/services/pull_request/utils_test.go +++ b/applicationset/services/pull_request/utils_test.go @@ -17,6 +17,7 @@ func TestFilterBranchMatchBadRegexp(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR branch1", Branch: "branch1", TargetBranch: "master", HeadSHA: "089d92cbf9ff857a39e6feccd32798ca700fb958", @@ -39,24 +40,28 @@ func TestFilterBranchMatch(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR one", Branch: "one", TargetBranch: "master", HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 2, + Title: "PR two", Branch: "two", TargetBranch: "master", HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 3, + Title: "PR three", Branch: "three", TargetBranch: "master", HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 4, + Title: "PR four", Branch: "four", TargetBranch: "master", HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958", @@ -81,24 +86,28 @@ func TestFilterTargetBranchMatch(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR one", Branch: "one", TargetBranch: "master", HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 2, + Title: "PR two", Branch: "two", TargetBranch: "branch1", HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 3, + Title: "PR three", Branch: "three", TargetBranch: "branch2", HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 4, + Title: "PR four", Branch: "four", TargetBranch: "branch3", HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958", @@ -123,24 +132,28 @@ func TestMultiFilterOr(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR one", Branch: "one", TargetBranch: "master", HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 2, + Title: "PR two", Branch: "two", TargetBranch: "master", HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 3, + Title: "PR three", Branch: "three", TargetBranch: "master", HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 4, + Title: "PR four", Branch: "four", TargetBranch: "master", HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958", @@ -170,24 +183,28 @@ func TestMultiFilterOrWithTargetBranchFilter(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR one", Branch: "one", TargetBranch: "master", HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 2, + Title: "PR two", Branch: "two", TargetBranch: "branch1", HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 3, + Title: "PR three", Branch: "three", TargetBranch: "branch2", HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 4, + Title: "PR four", Branch: "four", TargetBranch: "branch3", HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958", @@ -218,12 +235,14 @@ func TestNoFilters(t *testing.T) { []*PullRequest{ { Number: 1, + Title: "PR one", Branch: "one", TargetBranch: "master", HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958", }, { Number: 2, + Title: "PR two", Branch: "two", TargetBranch: "master", HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958", diff --git a/docs/operator-manual/applicationset/Generators-Pull-Request.md b/docs/operator-manual/applicationset/Generators-Pull-Request.md index a213c1dbb23bb..dc0c4859d9e34 100644 --- a/docs/operator-manual/applicationset/Generators-Pull-Request.md +++ b/docs/operator-manual/applicationset/Generators-Pull-Request.md @@ -392,6 +392,7 @@ spec: ``` * `number`: The ID number of the pull request. +* `title`: The title of the pull request. * `branch`: The name of the branch of the pull request head. * `branch_slug`: The branch name will be cleaned to be conform to the DNS label standard as defined in [RFC 1123](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names), and truncated to 50 characters to give room to append/suffix-ing it with 13 more characters. * `target_branch`: The name of the target branch of the pull request.