diff --git a/pipeline/const.go b/pipeline/const.go index 845e81cb3..3979eb01e 100644 --- a/pipeline/const.go +++ b/pipeline/const.go @@ -29,6 +29,8 @@ const ( LabelRepoFullName string = InternalLabelPrefix + "/repo-full-name" LabelBranch string = InternalLabelPrefix + "/branch" LabelOrgID string = InternalLabelPrefix + "/org-id" + LabelCommitBranch string = InternalLabelPrefix + "/commit-branch" + LabelEvent string = InternalLabelPrefix + "/event" LabelFilterOrg string = "org-id" LabelFilterRepo string = "repo" LabelFilterPlatform string = "platform" diff --git a/pipeline/frontend/builder/builder.go b/pipeline/frontend/builder/builder.go index ae1d50023..d083e1180 100644 --- a/pipeline/frontend/builder/builder.go +++ b/pipeline/frontend/builder/builder.go @@ -202,6 +202,8 @@ func (b *PipelineBuilder) genItemForWorkflow(workflow *Workflow, axis matrix.Axi item.Labels[pipeline.LabelRepoFullName] = workflowMetadata.Repo.Owner + "/" + workflowMetadata.Repo.Name item.Labels[pipeline.LabelBranch] = workflowMetadata.Repo.Branch item.Labels[pipeline.LabelOrgID] = strconv.FormatInt(workflowMetadata.Repo.OrgID, 10) + item.Labels[pipeline.LabelCommitBranch] = workflowMetadata.Curr.Commit.Branch + item.Labels[pipeline.LabelEvent] = string(workflowMetadata.Curr.Event) return item, errorsAndWarnings } diff --git a/pipeline/frontend/builder/builder_test.go b/pipeline/frontend/builder/builder_test.go index 3dd63bf3a..5abe79aa8 100644 --- a/pipeline/frontend/builder/builder_test.go +++ b/pipeline/frontend/builder/builder_test.go @@ -20,6 +20,7 @@ import ( "github.com/stretchr/testify/assert" + "go.woodpecker-ci.org/woodpecker/v3/pipeline" "go.woodpecker-ci.org/woodpecker/v3/pipeline/errors" "go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/metadata" "go.woodpecker-ci.org/woodpecker/v3/pipeline/frontend/yaml/compiler" @@ -902,3 +903,33 @@ func (t *testMetadata) GetWorkflowMetadata(w *Workflow) metadata.Metadata { }, } } + +func TestInternalCommitBranchAndEventLabels(t *testing.T) { + t.Parallel() + + m := &testMetadata{ + pipelineEvent: "push", + branch: "main", + } + + b := PipelineBuilder{ + GetWorkflowMetadata: m.GetWorkflowMetadata, + RepoTrusted: &metadata.TrustedConfiguration{}, + Yamls: []*YamlFile{ + {Data: []byte(` +when: + event: push +steps: + - name: build + image: scratch +`)}, + }, + } + + items, err := b.Build() + assert.NoError(t, err) + assert.Len(t, items, 1) + + assert.Equal(t, "main", items[0].Labels[pipeline.LabelCommitBranch]) + assert.Equal(t, "push", items[0].Labels[pipeline.LabelEvent]) +}