Skip to content

Commit

Permalink
set gitlab ref for using a conditional template
Browse files Browse the repository at this point in the history
Signed-off-by: Javan lacerda <[email protected]>
  • Loading branch information
javanlacerda committed Jul 24, 2024
1 parent 9d4a9bc commit fc94115
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/identity/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ ci-issuer-metadata:
runner-environment: "runner_environment"
source-repository-uri: "{{ .url }}/{{ .repository }}"
source-repository-digest: "sha"
source-repository-ref: "ref"
source-repository-ref: refs/{{if eq .ref_type "branch"}}heads/{{ else }}tags/{{end}}/{{ .ref }}
source-repository-identifier: "project_id"
source-repository-owner-uri: "{{ .url }}/{{ .namespace_path }}"
source-repository-owner-identifier: "namespace_id"
Expand Down
22 changes: 18 additions & 4 deletions pkg/identity/ciprovider/principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ func TestApplyTemplateOrReplace(t *testing.T) {
"workflow": "foo",
"workflow_ref": "sigstore/other/.github/workflows/foo.yaml@refs/heads/main",
"workflow_sha": "example-sha-other",
"ref_type": "branch",
"ref_gitlab": "main",
"ref_type_tag": "tag",
"ref_tag": "1.0.0",
}
issuerMetadata := map[string]string{
"url": "https://github.com",
Expand Down Expand Up @@ -269,18 +273,28 @@ func TestApplyTemplateOrReplace(t *testing.T) {
ExpectedResult: "",
ExpectErr: true,
},
`If else template`: {
Template: `refs/{{if eq .ref_type "branch"}}heads/{{ else }}tags/{{end}}{{ .ref_gitlab }}`,
ExpectedResult: "refs/heads/main",
ExpectErr: false,
},
`If else template using else condition`: {
Template: `refs/{{if eq .ref_type_tag "branch"}}heads/{{ else }}tags/{{end}}{{ .ref_tag }}`,
ExpectedResult: "refs/tags/1.0.0",
ExpectErr: false,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
res, err := applyTemplateOrReplace(test.Template, tokenClaims, issuerMetadata)
if res != test.ExpectedResult {
t.Errorf("expected result don't matches: Expected %s, received: %s",
test.ExpectedResult, res)
t.Errorf("expected result don't matches: Expected %s, received: %s, error: %v",
test.ExpectedResult, res, err)
}
if (err != nil) != test.ExpectErr {
t.Errorf("should raise an error don't matches: Expected %v, received: %v",
test.ExpectErr, err != nil)
t.Errorf("should raise an error don't matches: Expected %v, received: %v, error: %v",
test.ExpectErr, err != nil, err)
}
})
}
Expand Down

0 comments on commit fc94115

Please sign in to comment.