Skip to content

Commit

Permalink
feat(appset): add key title to PR generator (#16447) (#17907)
Browse files Browse the repository at this point in the history
Signed-off-by: ggjulio <[email protected]>
  • Loading branch information
ggjulio authored Jul 22, 2024
1 parent a7cb6ed commit f9078f7
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions applicationset/controllers/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func TestGenerateAppsUsingPullRequestGenerator(t *testing.T) {
params: []map[string]interface{}{
{
"number": "1",
"title": "title1",
"branch": "branch1",
"branch_slug": "branchSlug1",
"head_sha": "089d92cbf9ff857a39e6feccd32798ca700fb958",
Expand Down
1 change: 1 addition & 0 deletions applicationset/generators/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (g *PullRequestGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha

paramMap := map[string]interface{}{
"number": strconv.Itoa(pull.Number),
"title": pull.Title,
"branch": pull.Branch,
"branch_slug": slug.Make(pull.Branch),
"target_branch": pull.TargetBranch,
Expand Down
10 changes: 10 additions & 0 deletions applicationset/generators/pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
[]*pullrequest.PullRequest{
{
Number: 1,
Title: "title1",
Branch: "branch1",
TargetBranch: "master",
HeadSHA: "089d92cbf9ff857a39e6feccd32798ca700fb958",
Expand All @@ -41,6 +42,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
expected: []map[string]interface{}{
{
"number": "1",
"title": "title1",
"branch": "branch1",
"branch_slug": "branch1",
"target_branch": "master",
Expand All @@ -59,6 +61,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
[]*pullrequest.PullRequest{
{
Number: 2,
Title: "title2",
Branch: "feat/areally+long_pull_request_name_to_test_argo_slugification_and_branch_name_shortening_feature",
TargetBranch: "feat/anotherreally+long_pull_request_name_to_test_argo_slugification_and_branch_name_shortening_feature",
HeadSHA: "9b34ff5bd418e57d58891eb0aa0728043ca1e8be",
Expand All @@ -70,6 +73,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
expected: []map[string]interface{}{
{
"number": "2",
"title": "title2",
"branch": "feat/areally+long_pull_request_name_to_test_argo_slugification_and_branch_name_shortening_feature",
"branch_slug": "feat-areally-long-pull-request-name-to-test-argo",
"target_branch": "feat/anotherreally+long_pull_request_name_to_test_argo_slugification_and_branch_name_shortening_feature",
Expand All @@ -88,6 +92,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
[]*pullrequest.PullRequest{
{
Number: 1,
Title: "title1",
Branch: "a-very-short-sha",
TargetBranch: "master",
HeadSHA: "abcd",
Expand All @@ -99,6 +104,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
expected: []map[string]interface{}{
{
"number": "1",
"title": "title1",
"branch": "a-very-short-sha",
"branch_slug": "a-very-short-sha",
"target_branch": "master",
Expand Down Expand Up @@ -128,6 +134,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
[]*pullrequest.PullRequest{
{
Number: 1,
Title: "title1",
Branch: "branch1",
TargetBranch: "master",
HeadSHA: "089d92cbf9ff857a39e6feccd32798ca700fb958",
Expand All @@ -140,6 +147,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
expected: []map[string]interface{}{
{
"number": "1",
"title": "title1",
"branch": "branch1",
"branch_slug": "branch1",
"target_branch": "master",
Expand All @@ -165,6 +173,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
[]*pullrequest.PullRequest{
{
Number: 1,
Title: "title1",
Branch: "branch1",
TargetBranch: "master",
HeadSHA: "089d92cbf9ff857a39e6feccd32798ca700fb958",
Expand All @@ -177,6 +186,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
expected: []map[string]interface{}{
{
"number": "1",
"title": "title1",
"branch": "branch1",
"branch_slug": "branch1",
"target_branch": "master",
Expand Down
1 change: 1 addition & 0 deletions applicationset/services/pull_request/azure_devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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,
Expand Down
3 changes: 3 additions & 0 deletions applicationset/services/pull_request/azure_devops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,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),
Expand Down Expand Up @@ -95,6 +97,7 @@ func TestListPullRequest(t *testing.T) {
assert.Len(t, list, 1)
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)
}

Expand Down
2 changes: 2 additions & 0 deletions applicationset/services/pull_request/bitbucket_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type BitbucketCloudService struct {

type BitbucketCloudPullRequest struct {
ID int `json:"id"`
Title string `json:"title"`
Source BitbucketCloudPullRequestSource `json:"source"`
}

Expand Down Expand Up @@ -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,
})
Expand Down
16 changes: 16 additions & 0 deletions applicationset/services/pull_request/bitbucket_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func defaultHandlerCloud(t *testing.T) func(http.ResponseWriter, *http.Request)
"values": [
{
"id": 101,
"title": "feat(foo-bar)",
"source": {
"branch": {
"name": "feature/foo-bar"
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestListPullRequestBearerTokenCloud(t *testing.T) {
require.NoError(t, err)
assert.Len(t, pullRequests, 1)
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)
}
Expand All @@ -102,6 +104,7 @@ func TestListPullRequestNoAuthCloud(t *testing.T) {
require.NoError(t, err)
assert.Len(t, pullRequests, 1)
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)
}
Expand All @@ -118,6 +121,7 @@ func TestListPullRequestBasicAuthCloud(t *testing.T) {
require.NoError(t, err)
assert.Len(t, pullRequests, 1)
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)
}
Expand All @@ -136,6 +140,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) {
"values": [
{
"id": 101,
"title": "feat(101)",
"source": {
"branch": {
"name": "feature-101"
Expand All @@ -148,6 +153,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) {
},
{
"id": 102,
"title": "feat(102)",
"source": {
"branch": {
"name": "feature-102"
Expand All @@ -169,6 +175,7 @@ func TestListPullRequestPaginationCloud(t *testing.T) {
"values": [
{
"id": 103,
"title": "feat(103)",
"source": {
"branch": {
"name": "feature-103"
Expand Down Expand Up @@ -196,16 +203,19 @@ func TestListPullRequestPaginationCloud(t *testing.T) {
assert.Len(t, pullRequests, 3)
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])
Expand Down Expand Up @@ -309,6 +319,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) {
"values": [
{
"id": 101,
"title": "feat(101)",
"source": {
"branch": {
"name": "feature-101"
Expand All @@ -321,6 +332,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) {
},
{
"id": 200,
"title": "feat(200)",
"source": {
"branch": {
"name": "feature-200"
Expand All @@ -342,6 +354,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) {
"values": [
{
"id": 102,
"title": "feat(102)",
"source": {
"branch": {
"name": "feature-102"
Expand Down Expand Up @@ -374,11 +387,13 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) {
assert.Len(t, pullRequests, 2)
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])
Expand All @@ -395,6 +410,7 @@ func TestListPullRequestBranchMatchCloud(t *testing.T) {
assert.Len(t, pullRequests, 1)
assert.Equal(t, PullRequest{
Number: 102,
Title: "feat(102)",
Branch: "feature-102",
HeadSHA: "6344d9623e3b",
}, *pullRequests[0])
Expand Down
1 change: 1 addition & 0 deletions applicationset/services/pull_request/bitbucket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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
Expand Down
15 changes: 15 additions & 0 deletions applicationset/services/pull_request/bitbucket_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func defaultHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
"values": [
{
"id": 101,
"title": "feat(ABC) : 123",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "master",
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestListPullRequestNoAuth(t *testing.T) {
require.NoError(t, err)
assert.Len(t, pullRequests, 1)
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)
Expand All @@ -79,6 +81,7 @@ func TestListPullRequestPagination(t *testing.T) {
"values": [
{
"id": 101,
"title": "feat(101)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "master",
Expand All @@ -92,6 +95,7 @@ func TestListPullRequestPagination(t *testing.T) {
},
{
"id": 102,
"title": "feat(102)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "branch",
Expand All @@ -114,6 +118,7 @@ func TestListPullRequestPagination(t *testing.T) {
"values": [
{
"id": 200,
"title": "feat(200)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "master",
Expand Down Expand Up @@ -143,20 +148,23 @@ func TestListPullRequestPagination(t *testing.T) {
assert.Len(t, pullRequests, 3)
assert.Equal(t, PullRequest{
Number: 101,
Title: "feat(101)",
Branch: "feature-101",
TargetBranch: "master",
HeadSHA: "ab3cf2e4d1517c83e720d2585b9402dbef71f992",
Labels: []string{},
}, *pullRequests[0])
assert.Equal(t, PullRequest{
Number: 102,
Title: "feat(102)",
Branch: "feature-102",
TargetBranch: "branch",
HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
Labels: []string{},
}, *pullRequests[1])
assert.Equal(t, PullRequest{
Number: 200,
Title: "feat(200)",
Branch: "feature-200",
TargetBranch: "master",
HeadSHA: "cb3cf2e4d1517c83e720d2585b9402dbef71f992",
Expand Down Expand Up @@ -195,6 +203,7 @@ func TestListPullRequestBearerAuth(t *testing.T) {
require.NoError(t, err)
assert.Len(t, pullRequests, 1)
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)
}
Expand Down Expand Up @@ -274,6 +283,7 @@ func TestListPullRequestBranchMatch(t *testing.T) {
"values": [
{
"id": 101,
"title": "feat(101)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "master",
Expand All @@ -287,6 +297,7 @@ func TestListPullRequestBranchMatch(t *testing.T) {
},
{
"id": 102,
"title": "feat(102)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "branch",
Expand All @@ -309,6 +320,7 @@ func TestListPullRequestBranchMatch(t *testing.T) {
"values": [
{
"id": 200,
"title": "feat(200)",
"toRef": {
"latestCommit": "5b766e3564a3453808f3cd3dd3f2e5fad8ef0e7a",
"displayId": "master",
Expand Down Expand Up @@ -343,13 +355,15 @@ func TestListPullRequestBranchMatch(t *testing.T) {
assert.Len(t, pullRequests, 2)
assert.Equal(t, PullRequest{
Number: 101,
Title: "feat(101)",
Branch: "feature-101",
TargetBranch: "master",
HeadSHA: "ab3cf2e4d1517c83e720d2585b9402dbef71f992",
Labels: []string{},
}, *pullRequests[0])
assert.Equal(t, PullRequest{
Number: 102,
Title: "feat(102)",
Branch: "feature-102",
TargetBranch: "branch",
HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
Expand All @@ -368,6 +382,7 @@ func TestListPullRequestBranchMatch(t *testing.T) {
assert.Len(t, pullRequests, 1)
assert.Equal(t, PullRequest{
Number: 102,
Title: "feat(102)",
Branch: "feature-102",
TargetBranch: "branch",
HeadSHA: "bb3cf2e4d1517c83e720d2585b9402dbef71f992",
Expand Down
Loading

0 comments on commit f9078f7

Please sign in to comment.