Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for CI_COMMIT_PULL_REQUEST_TITLE #4157

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/20-usage/50-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ This is the reference list of all environment variables available to your pipeli
| `CI_COMMIT_TAG` | commit tag name (empty if event is not `tag`) | `v1.10.3` |
| `CI_COMMIT_PULL_REQUEST` | commit pull request number (empty if event is not `pull_request` or `pull_request_closed`) | `1` |
| `CI_COMMIT_PULL_REQUEST_LABELS` | labels assigned to pull request (empty if event is not `pull_request` or `pull_request_closed`) | `server` |
| `CI_COMMIT_PULL_REQUEST_TITLE` | commit pull request title (empty if event is not `pull_request` or `pull_request_closed`) | `server` |
| `CI_COMMIT_MESSAGE` | commit message | `Initial commit` |
| `CI_COMMIT_AUTHOR` | commit author username | `john-doe` |
| `CI_COMMIT_AUTHOR_EMAIL` | commit author email address | `[email protected]` |
Expand Down
2 changes: 2 additions & 0 deletions pipeline/frontend/metadata/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (m *Metadata) Environ() map[string]string {
"CI_COMMIT_AUTHOR_AVATAR": m.Curr.Commit.Author.Avatar,
"CI_COMMIT_TAG": "", // will be set if event is tag
"CI_COMMIT_PULL_REQUEST": "", // will be set if event is pull_request or pull_request_closed
"CI_COMMIT_PULL_REQUEST_TITLE": "", // will be set if event is pull_request or pull_request_closed
"CI_COMMIT_PULL_REQUEST_LABELS": "", // will be set if event is pull_request or pull_request_closed

"CI_PIPELINE_NUMBER": strconv.FormatInt(m.Curr.Number, 10),
Expand Down Expand Up @@ -137,6 +138,7 @@ func (m *Metadata) Environ() map[string]string {
}
if m.Curr.Event == EventPull || m.Curr.Event == EventPullClosed {
params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref)
params["CI_COMMIT_PULL_REQUEST_TITLE"] = pullRegexp.FindString(m.Curr.Commit.PullRequestTitle)
params["CI_COMMIT_PULL_REQUEST_LABELS"] = strings.Join(m.Curr.Commit.PullRequestLabels, ",")
}

Expand Down
1 change: 1 addition & 0 deletions pipeline/frontend/metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type (
Author Author `json:"author,omitempty"`
ChangedFiles []string `json:"changed_files,omitempty"`
PullRequestLabels []string `json:"labels,omitempty"`
PullRequestTitle string `json:"title,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty"`
}

Expand Down
15 changes: 8 additions & 7 deletions server/forge/bitbucket/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ func convertPullHook(from *internal.PullRequestHook) *model.Pipeline {
from.PullRequest.Source.Branch.Name,
from.PullRequest.Dest.Branch.Name,
),
ForgeURL: from.PullRequest.Links.HTML.Href,
Branch: from.PullRequest.Source.Branch.Name,
Message: from.PullRequest.Title,
Avatar: from.Actor.Links.Avatar.Href,
Author: from.Actor.Login,
Sender: from.Actor.Login,
Timestamp: from.PullRequest.Updated.UTC().Unix(),
ForgeURL: from.PullRequest.Links.HTML.Href,
Branch: from.PullRequest.Source.Branch.Name,
Message: from.PullRequest.Title,
PullRequestTitle: from.PullRequest.Title,
Avatar: from.Actor.Links.Avatar.Href,
Author: from.Actor.Login,
Sender: from.Actor.Login,
Timestamp: from.PullRequest.Updated.UTC().Unix(),
}

if from.PullRequest.State == stateClosed {
Expand Down
1 change: 1 addition & 0 deletions server/forge/bitbucket/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func Test_helper(t *testing.T) {
g.Assert(pipeline.Ref).Equal("refs/pull-requests/1/from")
g.Assert(pipeline.Refspec).Equal("change:main")
g.Assert(pipeline.Message).Equal(hook.PullRequest.Title)
g.Assert(pipeline.PullRequestTitle).Equal(hook.PullRequest.Title)
g.Assert(pipeline.Timestamp).Equal(hook.PullRequest.Updated.Unix())
})

Expand Down
1 change: 1 addition & 0 deletions server/forge/forgejo/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref,
),
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestTitle: hook.PullRequest.Title,
}

return pipeline
Expand Down
4 changes: 4 additions & 0 deletions server/forge/forgejo/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func TestForgejoParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "http://forgejo.golang.org/gordon/hello-world/pull/1",
PullRequestLabels: []string{},
PullRequestTitle: "Update the README with new information",
},
},
{
Expand Down Expand Up @@ -259,6 +260,7 @@ func TestForgejoParser(t *testing.T) {
"Kind/Bug",
"Kind/Security",
},
PullRequestTitle: "New Pull",
},
},
{
Expand Down Expand Up @@ -297,6 +299,7 @@ func TestForgejoParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
PullRequestTitle: "Adjust file",
},
},
{
Expand Down Expand Up @@ -335,6 +338,7 @@ func TestForgejoParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
PullRequestTitle: "Adjust file",
},
},
{
Expand Down
1 change: 1 addition & 0 deletions server/forge/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
hook.PullRequest.Base.Ref,
),
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
PullRequestTitle: hook.PullRequest.Title,
}

return pipeline
Expand Down
4 changes: 4 additions & 0 deletions server/forge/gitea/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func TestGiteaParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "http://gitea.golang.org/gordon/hello-world/pull/1",
PullRequestLabels: []string{},
PullRequestTitle: "Update the README with new information",
},
},
{
Expand Down Expand Up @@ -260,6 +261,7 @@ func TestGiteaParser(t *testing.T) {
"Kind/Bug",
"Kind/Security",
},
PullRequestTitle: "New Pull",
},
},
{
Expand Down Expand Up @@ -298,6 +300,7 @@ func TestGiteaParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
PullRequestTitle: "Adjust file",
},
},
{
Expand Down Expand Up @@ -336,6 +339,7 @@ func TestGiteaParser(t *testing.T) {
Email: "[email protected]",
ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1",
PullRequestLabels: []string{},
PullRequestTitle: "Adjust file",
},
},
{
Expand Down
1 change: 1 addition & 0 deletions server/forge/github/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullReque
hook.GetPullRequest().GetBase().GetRef(),
),
PullRequestLabels: convertLabels(hook.GetPullRequest().Labels),
PullRequestTitle: hook.GetPullRequest().GetTitle(),
}
if merge {
pipeline.Ref = fmt.Sprintf(mergeRefs, hook.GetPullRequest().GetNumber())
Expand Down
1 change: 1 addition & 0 deletions server/forge/github/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func Test_parser(t *testing.T) {
g.Assert(b).IsNotNil()
g.Assert(p).IsNotNil()
g.Assert(b.Event).Equal(model.EventPull)
g.Assert(b.PullRequestTitle).Equal(b.Message)
})
g.It("should handle a PR closed hook when PR got closed", func() {
req := testHookRequest([]byte(fixtures.HookPullRequestClosed), hookPull)
Expand Down
1 change: 1 addition & 0 deletions server/forge/gitlab/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
pipeline.Title = obj.Title
pipeline.ForgeURL = obj.URL
pipeline.PullRequestLabels = convertLabels(hook.Labels)
pipeline.PullRequestTitle = obj.Title

return obj.IID, repo, pipeline, nil
}
Expand Down
1 change: 1 addition & 0 deletions server/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Pipeline struct {
ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"`
AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"`
PullRequestLabels []string `json:"pr_labels,omitempty" xorm:"json 'pr_labels'"`
PullRequestTitle string `json:"pr_title,omitempty" xorm:"json 'pr_title'"`
IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
} // @name Pipeline

Expand Down
1 change: 1 addition & 0 deletions server/pipeline/stepbuilder/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func metadataPipelineFromModelPipeline(pipeline *model.Pipeline, includeParent b
},
ChangedFiles: pipeline.ChangedFiles,
PullRequestLabels: pipeline.PullRequestLabels,
PullRequestTitle: pipeline.PullRequestTitle,
IsPrerelease: pipeline.IsPrerelease,
},
Cron: cron,
Expand Down
4 changes: 2 additions & 2 deletions server/pipeline/stepbuilder/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestMetadataFromStruct(t *testing.T) {
expectedEnviron: map[string]string{
"CI": "woodpecker",
"CI_COMMIT_AUTHOR": "", "CI_COMMIT_AUTHOR_AVATAR": "", "CI_COMMIT_AUTHOR_EMAIL": "", "CI_COMMIT_BRANCH": "",
"CI_COMMIT_MESSAGE": "", "CI_COMMIT_PULL_REQUEST": "", "CI_COMMIT_PULL_REQUEST_LABELS": "", "CI_COMMIT_REF": "", "CI_COMMIT_REFSPEC": "", "CI_COMMIT_SHA": "", "CI_COMMIT_SOURCE_BRANCH": "",
"CI_COMMIT_MESSAGE": "", "CI_COMMIT_PULL_REQUEST": "", "CI_COMMIT_PULL_REQUEST_TITLE": "", "CI_COMMIT_PULL_REQUEST_LABELS": "", "CI_COMMIT_REF": "", "CI_COMMIT_REFSPEC": "", "CI_COMMIT_SHA": "", "CI_COMMIT_SOURCE_BRANCH": "",
"CI_COMMIT_TAG": "", "CI_COMMIT_TARGET_BRANCH": "", "CI_FORGE_TYPE": "", "CI_FORGE_URL": "",
"CI_PIPELINE_CREATED": "0", "CI_PIPELINE_DEPLOY_TARGET": "", "CI_PIPELINE_DEPLOY_TASK": "", "CI_PIPELINE_EVENT": "", "CI_PIPELINE_FILES": "[]", "CI_PIPELINE_NUMBER": "0",
"CI_PIPELINE_PARENT": "0", "CI_PIPELINE_STARTED": "0", "CI_PIPELINE_URL": "/repos/0/pipeline/0", "CI_PIPELINE_FORGE_URL": "",
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestMetadataFromStruct(t *testing.T) {
expectedEnviron: map[string]string{
"CI": "woodpecker",
"CI_COMMIT_AUTHOR": "", "CI_COMMIT_AUTHOR_AVATAR": "", "CI_COMMIT_AUTHOR_EMAIL": "", "CI_COMMIT_BRANCH": "",
"CI_COMMIT_MESSAGE": "", "CI_COMMIT_PULL_REQUEST": "", "CI_COMMIT_PULL_REQUEST_LABELS": "", "CI_COMMIT_REF": "", "CI_COMMIT_REFSPEC": "", "CI_COMMIT_SHA": "", "CI_COMMIT_SOURCE_BRANCH": "",
"CI_COMMIT_MESSAGE": "", "CI_COMMIT_PULL_REQUEST": "", "CI_COMMIT_PULL_REQUEST_TITLE": "", "CI_COMMIT_PULL_REQUEST_LABELS": "", "CI_COMMIT_REF": "", "CI_COMMIT_REFSPEC": "", "CI_COMMIT_SHA": "", "CI_COMMIT_SOURCE_BRANCH": "",
"CI_COMMIT_TAG": "", "CI_COMMIT_TARGET_BRANCH": "", "CI_FORGE_TYPE": "gitea", "CI_FORGE_URL": "https://gitea.com",
"CI_PIPELINE_CREATED": "0", "CI_PIPELINE_DEPLOY_TARGET": "", "CI_PIPELINE_DEPLOY_TASK": "", "CI_PIPELINE_EVENT": "", "CI_PIPELINE_FILES": `["test.go","markdown file.md"]`,
"CI_PIPELINE_NUMBER": "3", "CI_PIPELINE_PARENT": "0", "CI_PIPELINE_STARTED": "0", "CI_PIPELINE_URL": "https://example.com/repos/0/pipeline/3", "CI_PIPELINE_FORGE_URL": "",
Expand Down