From b7caae6163126c861d74fa01129de7435d5ddb47 Mon Sep 17 00:00:00 2001 From: pat-s Date: Thu, 3 Oct 2024 23:34:42 +0200 Subject: [PATCH] add support for CI_COMMIT_PULLREQUEST_TITLE --- docs/docs/20-usage/50-environment.md | 1 + pipeline/frontend/metadata/environment.go | 2 ++ pipeline/frontend/metadata/types.go | 1 + server/forge/bitbucket/convert.go | 15 ++++++++------- server/forge/bitbucket/convert_test.go | 1 + server/forge/forgejo/helper.go | 1 + server/forge/forgejo/parse_test.go | 4 ++++ server/forge/gitea/helper.go | 1 + server/forge/gitea/parse_test.go | 4 ++++ server/forge/github/parse.go | 1 + server/forge/github/parse_test.go | 1 + server/forge/gitlab/convert.go | 1 + server/model/pipeline.go | 1 + server/pipeline/stepbuilder/metadata.go | 1 + server/pipeline/stepbuilder/metadata_test.go | 4 ++-- 15 files changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/docs/20-usage/50-environment.md b/docs/docs/20-usage/50-environment.md index 0aa0a988d..bf74fc0db 100644 --- a/docs/docs/20-usage/50-environment.md +++ b/docs/docs/20-usage/50-environment.md @@ -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 | `john-doe@example.com` | diff --git a/pipeline/frontend/metadata/environment.go b/pipeline/frontend/metadata/environment.go index 1a018867f..70bd7fed7 100644 --- a/pipeline/frontend/metadata/environment.go +++ b/pipeline/frontend/metadata/environment.go @@ -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), @@ -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, ",") } diff --git a/pipeline/frontend/metadata/types.go b/pipeline/frontend/metadata/types.go index 9d3529fd9..da84f05d3 100644 --- a/pipeline/frontend/metadata/types.go +++ b/pipeline/frontend/metadata/types.go @@ -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"` } diff --git a/server/forge/bitbucket/convert.go b/server/forge/bitbucket/convert.go index 6dc8f1fb1..70299a29a 100644 --- a/server/forge/bitbucket/convert.go +++ b/server/forge/bitbucket/convert.go @@ -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 { diff --git a/server/forge/bitbucket/convert_test.go b/server/forge/bitbucket/convert_test.go index 70dac2bae..f56d229fb 100644 --- a/server/forge/bitbucket/convert_test.go +++ b/server/forge/bitbucket/convert_test.go @@ -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()) }) diff --git a/server/forge/forgejo/helper.go b/server/forge/forgejo/helper.go index 0ab2b24e0..afcb44f4b 100644 --- a/server/forge/forgejo/helper.go +++ b/server/forge/forgejo/helper.go @@ -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 diff --git a/server/forge/forgejo/parse_test.go b/server/forge/forgejo/parse_test.go index 9415d9975..8f56c6d56 100644 --- a/server/forge/forgejo/parse_test.go +++ b/server/forge/forgejo/parse_test.go @@ -217,6 +217,7 @@ func TestForgejoParser(t *testing.T) { Email: "gordon@golang.org", ForgeURL: "http://forgejo.golang.org/gordon/hello-world/pull/1", PullRequestLabels: []string{}, + PullRequestTitle: "Update the README with new information", }, }, { @@ -259,6 +260,7 @@ func TestForgejoParser(t *testing.T) { "Kind/Bug", "Kind/Security", }, + PullRequestTitle: "New Pull", }, }, { @@ -297,6 +299,7 @@ func TestForgejoParser(t *testing.T) { Email: "anbraten@sender.forgejo.com", ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", PullRequestLabels: []string{}, + PullRequestTitle: "Adjust file", }, }, { @@ -335,6 +338,7 @@ func TestForgejoParser(t *testing.T) { Email: "anbraten@noreply.forgejo.com", ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", PullRequestLabels: []string{}, + PullRequestTitle: "Adjust file", }, }, { diff --git a/server/forge/gitea/helper.go b/server/forge/gitea/helper.go index 69b2a885b..e20b8e882 100644 --- a/server/forge/gitea/helper.go +++ b/server/forge/gitea/helper.go @@ -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 diff --git a/server/forge/gitea/parse_test.go b/server/forge/gitea/parse_test.go index a37384464..24cb381c9 100644 --- a/server/forge/gitea/parse_test.go +++ b/server/forge/gitea/parse_test.go @@ -218,6 +218,7 @@ func TestGiteaParser(t *testing.T) { Email: "gordon@golang.org", ForgeURL: "http://gitea.golang.org/gordon/hello-world/pull/1", PullRequestLabels: []string{}, + PullRequestTitle: "Update the README with new information", }, }, { @@ -260,6 +261,7 @@ func TestGiteaParser(t *testing.T) { "Kind/Bug", "Kind/Security", }, + PullRequestTitle: "New Pull", }, }, { @@ -298,6 +300,7 @@ func TestGiteaParser(t *testing.T) { Email: "anbraten@sender.gitea.com", ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", PullRequestLabels: []string{}, + PullRequestTitle: "Adjust file", }, }, { @@ -336,6 +339,7 @@ func TestGiteaParser(t *testing.T) { Email: "anbraten@noreply.gitea.com", ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", PullRequestLabels: []string{}, + PullRequestTitle: "Adjust file", }, }, { diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index f47e91751..ac6e78024 100644 --- a/server/forge/github/parse.go +++ b/server/forge/github/parse.go @@ -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()) diff --git a/server/forge/github/parse_test.go b/server/forge/github/parse_test.go index 225bfd3c9..7f243e10d 100644 --- a/server/forge/github/parse_test.go +++ b/server/forge/github/parse_test.go @@ -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) diff --git a/server/forge/gitlab/convert.go b/server/forge/gitlab/convert.go index ad6dbea58..531d4a506 100644 --- a/server/forge/gitlab/convert.go +++ b/server/forge/gitlab/convert.go @@ -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 } diff --git a/server/model/pipeline.go b/server/model/pipeline.go index a073ad15f..ca027a91b 100644 --- a/server/model/pipeline.go +++ b/server/model/pipeline.go @@ -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 diff --git a/server/pipeline/stepbuilder/metadata.go b/server/pipeline/stepbuilder/metadata.go index 24ba90e96..0ca20c2e1 100644 --- a/server/pipeline/stepbuilder/metadata.go +++ b/server/pipeline/stepbuilder/metadata.go @@ -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, diff --git a/server/pipeline/stepbuilder/metadata_test.go b/server/pipeline/stepbuilder/metadata_test.go index 53cf388e6..4e8adcc7b 100644 --- a/server/pipeline/stepbuilder/metadata_test.go +++ b/server/pipeline/stepbuilder/metadata_test.go @@ -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": "", @@ -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": "",