Skip to content

Commit

Permalink
feat: add short_sha_7 to AppSet generators (#11976) (#13199)
Browse files Browse the repository at this point in the history
* feat: add shortSHALength7 variable to AppSet generator PR, SCM

Signed-off-by: Marco Lecheler <marco.lecheler@mercedes-benz.com>

* feat(test): add test for shortSHALength7 in AppSet

Signed-off-by: Marco Lecheler <marco.lecheler@mercedes-benz.com>

* docs: add short_sha_7 to AppSet generator docu

Signed-off-by: Marco Lecheler <marco.lecheler@mercedes-benz.com>

---------

Signed-off-by: Marco Lecheler <marco.lecheler@mercedes-benz.com>
  • Loading branch information
fty4 authored Apr 16, 2023
1 parent acadb62 commit d4fc854
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
17 changes: 12 additions & 5 deletions applicationset/generators/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,25 @@ func (g *PullRequestGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha
}

var shortSHALength int
var shortSHALength7 int
for _, pull := range pulls {
shortSHALength = 8
if len(pull.HeadSHA) < 8 {
shortSHALength = len(pull.HeadSHA)
}

shortSHALength7 = 7
if len(pull.HeadSHA) < 7 {
shortSHALength7 = len(pull.HeadSHA)
}

paramMap := map[string]interface{}{
"number": strconv.Itoa(pull.Number),
"branch": pull.Branch,
"branch_slug": slug.Make(pull.Branch),
"head_sha": pull.HeadSHA,
"head_short_sha": pull.HeadSHA[:shortSHALength],
"number": strconv.Itoa(pull.Number),
"branch": pull.Branch,
"branch_slug": slug.Make(pull.Branch),
"head_sha": pull.HeadSHA,
"head_short_sha": pull.HeadSHA[:shortSHALength],
"head_short_sha_7": pull.HeadSHA[:shortSHALength7],
}

// PR lables will only be supported for Go Template appsets, since fasttemplate will be deprecated.
Expand Down
5 changes: 5 additions & 0 deletions applicationset/generators/pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
"branch_slug": "branch1",
"head_sha": "089d92cbf9ff857a39e6feccd32798ca700fb958",
"head_short_sha": "089d92cb",
"head_short_sha_7": "089d92c",
},
},
expectedErr: nil,
Expand All @@ -68,6 +69,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
"branch_slug": "feat-areally-long-pull-request-name-to-test-argo",
"head_sha": "9b34ff5bd418e57d58891eb0aa0728043ca1e8be",
"head_short_sha": "9b34ff5b",
"head_short_sha_7": "9b34ff5",
},
},
expectedErr: nil,
Expand All @@ -93,6 +95,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
"branch_slug": "a-very-short-sha",
"head_sha": "abcd",
"head_short_sha": "abcd",
"head_short_sha_7": "abcd",
},
},
expectedErr: nil,
Expand Down Expand Up @@ -130,6 +133,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
"branch_slug": "branch1",
"head_sha": "089d92cbf9ff857a39e6feccd32798ca700fb958",
"head_short_sha": "089d92cb",
"head_short_sha_7": "089d92c",
"labels": []string{"preview"},
},
},
Expand Down Expand Up @@ -163,6 +167,7 @@ func TestPullRequestGithubGenerateParams(t *testing.T) {
"branch_slug": "branch1",
"head_sha": "089d92cbf9ff857a39e6feccd32798ca700fb958",
"head_short_sha": "089d92cb",
"head_short_sha_7": "089d92c",
},
},
expectedErr: nil,
Expand Down
7 changes: 7 additions & 0 deletions applicationset/generators/scm_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,26 @@ func (g *SCMProviderGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha
}
params := make([]map[string]interface{}, 0, len(repos))
var shortSHALength int
var shortSHALength7 int
for _, repo := range repos {
shortSHALength = 8
if len(repo.SHA) < 8 {
shortSHALength = len(repo.SHA)
}

shortSHALength7 = 7
if len(repo.SHA) < 7 {
shortSHALength7 = len(repo.SHA)
}

params = append(params, map[string]interface{}{
"organization": repo.Organization,
"repository": repo.Repository,
"url": repo.URL,
"branch": repo.Branch,
"sha": repo.SHA,
"short_sha": repo.SHA[:shortSHALength],
"short_sha_7": repo.SHA[:shortSHALength7],
"labels": strings.Join(repo.Labels, ","),
"branchNormalized": utils.SanitizeName(repo.Branch),
})
Expand Down
2 changes: 2 additions & 0 deletions applicationset/generators/scm_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func TestSCMProviderGenerateParams(t *testing.T) {
assert.Equal(t, "0bc57212c3cbbec69d20b34c507284bd300def5b", params[0]["sha"])
assert.Equal(t, "0bc57212", params[0]["short_sha"])
assert.Equal(t, "59d0", params[1]["short_sha"])
assert.Equal(t, "0bc5721", params[0]["short_sha_7"])
assert.Equal(t, "59d0", params[1]["short_sha_7"])
assert.Equal(t, "prod,staging", params[0]["labels"])
assert.Equal(t, "repo2", params[1]["repository"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ spec:
* `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.
* `head_sha`: This is the SHA of the head of the pull request.
* `head_short_sha`: This is the short SHA of the head of the pull request (8 characters long or the length of the head SHA if it's shorter).
* `head_short_sha_7`: This is the short SHA of the head of the pull request (7 characters long or the length of the head SHA if it's shorter).
* `labels`: The array of pull request labels. (Supported only for Go Template ApplicationSet manifests.)

## Webhook Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,6 @@ spec:
* `branch`: The default branch of the repository.
* `sha`: The Git commit SHA for the branch.
* `short_sha`: The abbreviated Git commit SHA for the branch (8 chars or the length of the `sha` if it's shorter).
* `short_sha_7`: The abbreviated Git commit SHA for the branch (7 chars or the length of the `sha` if it's shorter).
* `labels`: A comma-separated list of repository labels.
* `branchNormalized`: The value of `branch` normalized to contain only lowercase alphanumeric characters, '-' or '.'.

0 comments on commit d4fc854

Please sign in to comment.