diff --git a/server/api/pipeline.go b/server/api/pipeline.go index a1b191ab4c2..14c5e8a3d80 100644 --- a/server/api/pipeline.go +++ b/server/api/pipeline.go @@ -90,7 +90,7 @@ func createTmpPipeline(event model.WebhookEvent, commit *model.Commit, user *mod Timestamp: time.Now().UTC().Unix(), Avatar: user.Avatar, - Message: "MANUAL PIPELINE @ " + opts.Branch, + Message: "manual pipeline @" + opts.Branch, Ref: opts.Branch, AdditionalVariables: opts.Variables, diff --git a/server/forge/bitbucket/convert.go b/server/forge/bitbucket/convert.go index 6dc8f1fb1da..77da2b2e975 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, + PRTitleDescription: from.PullRequest.Title + "\n" + from.PullRequest.Desc, + Message: "", // TODO: get last commit message + 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/bitbucketdatacenter/convert.go b/server/forge/bitbucketdatacenter/convert.go index bef0c30d55e..7d480fec6a5 100644 --- a/server/forge/bitbucketdatacenter/convert.go +++ b/server/forge/bitbucketdatacenter/convert.go @@ -89,10 +89,17 @@ func convertRepositoryPushEvent(ev *bb.RepositoryPushEvent, baseURL string) *mod return nil } + message := "" + if ev.ToCommit != nil { + message = ev.ToCommit.Message + } else if len(ev.Commits) > 0 { + message = ev.Commits[0].Message + } + pipeline := &model.Pipeline{ Commit: change.ToHash, Branch: change.Ref.DisplayID, - Message: "", + Message: message, Avatar: bitbucketAvatarURL(baseURL, ev.Actor.Slug), Author: authorLabel(ev.Actor.Name), Email: ev.Actor.Email, @@ -112,17 +119,17 @@ func convertRepositoryPushEvent(ev *bb.RepositoryPushEvent, baseURL string) *mod func convertPullRequestEvent(ev *bb.PullRequestEvent, baseURL string) *model.Pipeline { pipeline := &model.Pipeline{ - Commit: ev.PullRequest.Source.Latest, - Branch: ev.PullRequest.Source.DisplayID, - Title: ev.PullRequest.Title, - Message: "", - Avatar: bitbucketAvatarURL(baseURL, ev.Actor.Slug), - Author: authorLabel(ev.Actor.Name), - Email: ev.Actor.Email, - Timestamp: time.Time(ev.Date).UTC().Unix(), - Ref: fmt.Sprintf("refs/pull-requests/%d/from", ev.PullRequest.ID), - ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, ev.PullRequest.Source.Repository.Project.Key, ev.PullRequest.Source.Repository.Slug, ev.PullRequest.Source.Latest), - Refspec: fmt.Sprintf("%s:%s", ev.PullRequest.Source.DisplayID, ev.PullRequest.Target.DisplayID), + Commit: ev.PullRequest.Source.Latest, + Branch: ev.PullRequest.Source.DisplayID, + PRTitleDescription: ev.PullRequest.Title, + Message: "", // TODO: get message from last commit to pr + Avatar: bitbucketAvatarURL(baseURL, ev.Actor.Slug), + Author: authorLabel(ev.Actor.Name), + Email: ev.Actor.Email, + Timestamp: time.Time(ev.Date).UTC().Unix(), + Ref: fmt.Sprintf("refs/pull-requests/%d/from", ev.PullRequest.ID), + ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, ev.PullRequest.Source.Repository.Project.Key, ev.PullRequest.Source.Repository.Slug, ev.PullRequest.Source.Latest), + Refspec: fmt.Sprintf("%s:%s", ev.PullRequest.Source.DisplayID, ev.PullRequest.Target.DisplayID), } if ev.EventKey == bb.EventKeyPullRequestMerged || ev.EventKey == bb.EventKeyPullRequestDeclined || ev.EventKey == bb.EventKeyPullRequestDeleted { diff --git a/server/forge/forgejo/helper.go b/server/forge/forgejo/helper.go index 0ab2b24e0f8..aa8e5936df3 100644 --- a/server/forge/forgejo/helper.go +++ b/server/forge/forgejo/helper.go @@ -128,12 +128,21 @@ func pipelineFromTag(hook *pushHook) *model.Pipeline { ) ref := strings.TrimPrefix(hook.Ref, "refs/tags/") + var message string + if len(hook.Commits) > 0 { + message = hook.Commits[0].Message + if len(hook.Commits) == 1 { + } + } else { + message = hook.HeadCommit.Message + } + return &model.Pipeline{ Event: model.EventTag, Commit: hook.Sha, Ref: fmt.Sprintf("refs/tags/%s", ref), ForgeURL: fmt.Sprintf("%s/src/tag/%s", hook.Repo.HTMLURL, ref), - Message: fmt.Sprintf("created tag %s", ref), + Message: message, Avatar: avatar, Author: hook.Sender.UserName, Sender: hook.Sender.UserName, @@ -155,17 +164,17 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline { } pipeline := &model.Pipeline{ - Event: event, - Commit: hook.PullRequest.Head.Sha, - ForgeURL: hook.PullRequest.HTMLURL, - Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), - Branch: hook.PullRequest.Base.Ref, - Message: hook.PullRequest.Title, - Author: hook.PullRequest.Poster.UserName, - Avatar: avatar, - Sender: hook.Sender.UserName, - Email: hook.Sender.Email, - Title: hook.PullRequest.Title, + Event: event, + Commit: hook.PullRequest.Head.Sha, + ForgeURL: hook.PullRequest.HTMLURL, + Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), + Branch: hook.PullRequest.Base.Ref, + Author: hook.PullRequest.Poster.UserName, + Avatar: avatar, + Sender: hook.Sender.UserName, + Email: hook.Sender.Email, + PRTitleDescription: hook.PullRequest.Title + "\n" + hook.PullRequest.Body, + Message: "", // TODO: get message from last commit to pr Refspec: fmt.Sprintf("%s:%s", hook.PullRequest.Head.Ref, hook.PullRequest.Base.Ref, @@ -187,7 +196,7 @@ func pipelineFromRelease(hook *releaseHook) *model.Pipeline { Ref: fmt.Sprintf("refs/tags/%s", hook.Release.TagName), ForgeURL: hook.Release.HTMLURL, Branch: hook.Release.Target, - Message: fmt.Sprintf("created release %s", hook.Release.Title), + Message: hook.Release.Title + "\n" + hook.Release.Note, Avatar: avatar, Author: hook.Sender.UserName, Sender: hook.Sender.UserName, diff --git a/server/forge/forgejo/parse_test.go b/server/forge/forgejo/parse_test.go index 9415d997578..ca0c0d7bc62 100644 --- a/server/forge/forgejo/parse_test.go +++ b/server/forge/forgejo/parse_test.go @@ -204,19 +204,19 @@ func TestForgejoParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "gordon", - Event: "pull_request", - Commit: "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "feature/changes:main", - Title: "Update the README with new information", - Message: "Update the README with new information", - Sender: "gordon", - Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", - Email: "gordon@golang.org", - ForgeURL: "http://forgejo.golang.org/gordon/hello-world/pull/1", - PullRequestLabels: []string{}, + Author: "gordon", + Event: "pull_request", + Commit: "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "feature/changes:main", + PRTitleDescription: "Update the README with new information", + Message: "Update the README with new information", + Sender: "gordon", + Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + Email: "gordon@golang.org", + ForgeURL: "http://forgejo.golang.org/gordon/hello-world/pull/1", + PullRequestLabels: []string{}, }, }, { @@ -243,18 +243,18 @@ func TestForgejoParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "test", - Event: "pull_request", - Commit: "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25", - Branch: "main", - Ref: "refs/pull/2/head", - Refspec: "test-patch-1:main", - Title: "New Pull", - Message: "New Pull", - Sender: "test", - Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", - Email: "test@noreply.localhost", - ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2", + Author: "test", + Event: "pull_request", + Commit: "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25", + Branch: "main", + Ref: "refs/pull/2/head", + Refspec: "test-patch-1:main", + PRTitleDescription: "New Pull", + Message: "New Pull", + Sender: "test", + Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", + Email: "test@noreply.localhost", + ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2", PullRequestLabels: []string{ "Kind/Bug", "Kind/Security", @@ -284,19 +284,19 @@ func TestForgejoParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "anbraten", - Event: "pull_request_closed", - Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "anbraten-patch-1:main", - Title: "Adjust file", - Message: "Adjust file", - Sender: "anbraten", - Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", - Email: "anbraten@sender.forgejo.com", - ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", - PullRequestLabels: []string{}, + Author: "anbraten", + Event: "pull_request_closed", + Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "anbraten-patch-1:main", + PRTitleDescription: "Adjust file", + Message: "Adjust file", + Sender: "anbraten", + Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", + Email: "anbraten@sender.forgejo.com", + ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", + PullRequestLabels: []string{}, }, }, { @@ -322,19 +322,19 @@ func TestForgejoParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "anbraten", - Event: "pull_request_closed", - Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "anbraten-patch-1:main", - Title: "Adjust file", - Message: "Adjust file", - Sender: "anbraten", - Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", - Email: "anbraten@noreply.forgejo.com", - ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", - PullRequestLabels: []string{}, + Author: "anbraten", + Event: "pull_request_closed", + Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "anbraten-patch-1:main", + PRTitleDescription: "Adjust file", + Message: "Adjust file", + Sender: "anbraten", + Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", + Email: "anbraten@noreply.forgejo.com", + ForgeURL: "https://forgejo.com/anbraten/test-repo/pulls/1", + PullRequestLabels: []string{}, }, }, { diff --git a/server/forge/gitea/fixtures/hooks.go b/server/forge/gitea/fixtures/hooks.go index 2b151a62ce3..951af0d479d 100644 --- a/server/forge/gitea/fixtures/hooks.go +++ b/server/forge/gitea/fixtures/hooks.go @@ -1414,7 +1414,7 @@ const HookRelease = ` "tag_name": "0.0.5", "target_commitish": "main", "name": "Version 0.0.5", - "body": "", + "body": "release notes", "url": "https://git.xxx/api/v1/repos/anbraten/demo/releases/48", "html_url": "https://git.xxx/anbraten/demo/releases/tag/0.0.5", "tarball_url": "https://git.xxx/anbraten/demo/archive/0.0.5.tar.gz", diff --git a/server/forge/gitea/gitea.go b/server/forge/gitea/gitea.go index 546984cf1a6..9c8cb828d99 100644 --- a/server/forge/gitea/gitea.go +++ b/server/forge/gitea/gitea.go @@ -503,15 +503,24 @@ func (c *Gitea) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model. return nil, nil, err } + var tag *gitea.Tag + loadTag := func() error { + if tag == nil { + tagName := strings.Split(pipeline.Ref, "/")[2] + tag, err = c.getTag(ctx, repo, tagName) + return err + } + return nil + } + if pipeline != nil && pipeline.Event == model.EventRelease && pipeline.Commit == "" { - tagName := strings.Split(pipeline.Ref, "/")[2] - sha, err := c.getTagCommitSHA(ctx, repo, tagName) - if err != nil { + if err := loadTag(); err != nil { return nil, nil, err } - pipeline.Commit = sha + pipeline.Commit = tag.Commit.SHA } + // get changed files via api (and not from parsed webhook) if pipeline != nil && (pipeline.Event == model.EventPull || pipeline.Event == model.EventPullClosed) && len(pipeline.ChangedFiles) == 0 { index, err := strconv.ParseInt(strings.Split(pipeline.Ref, "/")[2], 10, 64) if err != nil { @@ -523,6 +532,14 @@ func (c *Gitea) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model. } } + // get tag message via api if empty + if pipeline != nil && pipeline.Event == model.EventTag && pipeline.Message == "" { + if err := loadTag(); err != nil { + return nil, nil, err + } + pipeline.Message = tag.Message + } + return repo, pipeline, nil } @@ -621,25 +638,9 @@ func getStatus(status model.StatusValue) gitea.StatusState { } func (c *Gitea) getChangedFilesForPR(ctx context.Context, repo *model.Repo, index int64) ([]string, error) { - _store, ok := store.TryFromContext(ctx) - if !ok { - log.Error().Msg("could not get store from context") - return []string{}, nil - } - - repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName) - if err != nil { - return nil, err - } - - user, err := _store.GetUser(repo.UserID) - if err != nil { - return nil, err - } - - client, err := c.newClientToken(ctx, user.Token) - if err != nil { - return nil, err + client, repo, err := c.prepareHookAPIClient(ctx, repo) + if err != nil || client == nil { + return []string{}, err } return shared_utils.Paginate(func(page int) ([]string, error) { @@ -657,34 +658,39 @@ func (c *Gitea) getChangedFilesForPR(ctx context.Context, repo *model.Repo, inde }) } -func (c *Gitea) getTagCommitSHA(ctx context.Context, repo *model.Repo, tagName string) (string, error) { - _store, ok := store.TryFromContext(ctx) - if !ok { - log.Error().Msg("could not get store from context") - return "", nil +func (c *Gitea) getTag(ctx context.Context, repo *model.Repo, tagName string) (*gitea.Tag, error) { + client, repo, err := c.prepareHookAPIClient(ctx, repo) + if err != nil || client == nil { + return nil, err } - repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName) + tag, _, err := client.GetTag(repo.Owner, repo.Name, tagName) if err != nil { - return "", err + return nil, err } - user, err := _store.GetUser(repo.UserID) - if err != nil { - return "", err + return tag, nil +} + +func (c *Gitea) prepareHookAPIClient(ctx context.Context, repo *model.Repo) (*gitea.Client, *model.Repo, error) { + _store, ok := store.TryFromContext(ctx) + if !ok { + log.Error().Msg("could not get store from context") + return nil, nil, nil } - client, err := c.newClientToken(ctx, user.Token) + repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName) if err != nil { - return "", err + return nil, nil, err } - tag, _, err := client.GetTag(repo.Owner, repo.Name, tagName) + repoOwner, err := _store.GetUser(repo.UserID) if err != nil { - return "", err + return nil, nil, err } - return tag.Commit.SHA, nil + client, err := c.newClientToken(ctx, repoOwner.Token) + return client, repo, err } func (c *Gitea) perPage(ctx context.Context) int { diff --git a/server/forge/gitea/helper.go b/server/forge/gitea/helper.go index 69b2a885b20..ac896f66477 100644 --- a/server/forge/gitea/helper.go +++ b/server/forge/gitea/helper.go @@ -129,12 +129,19 @@ func pipelineFromTag(hook *pushHook) *model.Pipeline { ) ref := strings.TrimPrefix(hook.Ref, "refs/tags/") + var message string + if len(hook.Commits) > 0 { + message = hook.Commits[0].Message + } else { + message = hook.HeadCommit.Message + } + return &model.Pipeline{ Event: model.EventTag, Commit: hook.Sha, Ref: fmt.Sprintf("refs/tags/%s", ref), ForgeURL: fmt.Sprintf("%s/src/tag/%s", hook.Repo.HTMLURL, ref), - Message: fmt.Sprintf("created tag %s", ref), + Message: message, Avatar: avatar, Author: hook.Sender.UserName, Sender: hook.Sender.UserName, @@ -156,17 +163,17 @@ func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline { } pipeline := &model.Pipeline{ - Event: event, - Commit: hook.PullRequest.Head.Sha, - ForgeURL: hook.PullRequest.HTMLURL, - Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), - Branch: hook.PullRequest.Base.Ref, - Message: hook.PullRequest.Title, - Author: hook.PullRequest.Poster.UserName, - Avatar: avatar, - Sender: hook.Sender.UserName, - Email: hook.Sender.Email, - Title: hook.PullRequest.Title, + Event: event, + Commit: hook.PullRequest.Head.Sha, + ForgeURL: hook.PullRequest.HTMLURL, + Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), + Branch: hook.PullRequest.Base.Ref, + PRTitleDescription: hook.PullRequest.Title + "\n" + hook.PullRequest.Body, + Message: "", // TODO: get commit message from last commit + Author: hook.PullRequest.Poster.UserName, + Avatar: avatar, + Sender: hook.Sender.UserName, + Email: hook.Sender.Email, Refspec: fmt.Sprintf("%s:%s", hook.PullRequest.Head.Ref, hook.PullRequest.Base.Ref, @@ -188,7 +195,7 @@ func pipelineFromRelease(hook *releaseHook) *model.Pipeline { Ref: fmt.Sprintf("refs/tags/%s", hook.Release.TagName), ForgeURL: hook.Release.HTMLURL, Branch: hook.Release.Target, - Message: fmt.Sprintf("created release %s", hook.Release.Title), + Message: hook.Release.Title + "\n" + hook.Release.Note, Avatar: avatar, Author: hook.Sender.UserName, Sender: hook.Sender.UserName, diff --git a/server/forge/gitea/helper_test.go b/server/forge/gitea/helper_test.go index f8d36f37148..8b6f5249884 100644 --- a/server/forge/gitea/helper_test.go +++ b/server/forge/gitea/helper_test.go @@ -27,7 +27,7 @@ import ( "go.woodpecker-ci.org/woodpecker/v2/shared/utils" ) -func Test_parse(t *testing.T) { +func TestGiteaHelper(t *testing.T) { g := goblin.Goblin(t) g.Describe("Gitea", func() { g.It("Should parse push hook payload", func() { @@ -126,7 +126,7 @@ func Test_parse(t *testing.T) { g.Assert(pipeline.Ref).Equal("refs/tags/v1.0.0") g.Assert(pipeline.Branch).Equal("") g.Assert(pipeline.ForgeURL).Equal("http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0") - g.Assert(pipeline.Message).Equal("created tag v1.0.0") + g.Assert(pipeline.PRTitleDescription).Equal("created tag v1.0.0") }) g.It("Should return a Pipeline struct from a pull_request hook", func() { @@ -139,7 +139,8 @@ func Test_parse(t *testing.T) { g.Assert(pipeline.ForgeURL).Equal("http://gitea.golang.org/gordon/hello-world/pull/1") g.Assert(pipeline.Branch).Equal("main") g.Assert(pipeline.Refspec).Equal("feature/changes:main") - g.Assert(pipeline.Message).Equal(hook.PullRequest.Title) + g.Assert(pipeline.PRTitleDescription).Equal("Update the README with new information") + g.Assert(pipeline.Message).Equal("please merge") g.Assert(pipeline.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87") g.Assert(pipeline.Author).Equal(hook.PullRequest.Poster.UserName) }) diff --git a/server/forge/gitea/parse_test.go b/server/forge/gitea/parse_test.go index a3738446487..031ae31f734 100644 --- a/server/forge/gitea/parse_test.go +++ b/server/forge/gitea/parse_test.go @@ -65,17 +65,18 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "6543", - Event: "push", - Commit: "28c3613ae62640216bea5e7dc71aa65356e4298b", - Branch: "fdsafdsa", - Ref: "refs/heads/fdsafdsa", - Message: "Delete '.woodpecker/.check.yml'\n", - Sender: "6543", - Avatar: "https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173", - Email: "6543@obermui.de", - ForgeURL: "https://codeberg.org/meisam/woodpecktester/commit/28c3613ae62640216bea5e7dc71aa65356e4298b", - ChangedFiles: []string{".woodpecker/.check.yml"}, + Author: "6543", + Event: "push", + Commit: "28c3613ae62640216bea5e7dc71aa65356e4298b", + Branch: "fdsafdsa", + Ref: "refs/heads/fdsafdsa", + PRTitleDescription: "Delete '.woodpecker/.check.yml'", + Message: "Delete '.woodpecker/.check.yml'\n", + Sender: "6543", + Avatar: "https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173", + Email: "6543@obermui.de", + ForgeURL: "https://codeberg.org/meisam/woodpecktester/commit/28c3613ae62640216bea5e7dc71aa65356e4298b", + ChangedFiles: []string{".woodpecker/.check.yml"}, }, }, { @@ -100,17 +101,18 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "gordon", - Event: "push", - Commit: "ef98532add3b2feb7a137426bba1248724367df5", - Branch: "main", - Ref: "refs/heads/main", - Message: "bump\n", - Sender: "gordon", - Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", - Email: "gordon@golang.org", - ForgeURL: "http://gitea.golang.org/gordon/hello-world/commit/ef98532add3b2feb7a137426bba1248724367df5", - ChangedFiles: []string{"CHANGELOG.md", "app/controller/application.rb"}, + Author: "gordon", + Event: "push", + Commit: "ef98532add3b2feb7a137426bba1248724367df5", + Branch: "main", + Ref: "refs/heads/main", + PRTitleDescription: "bump", + Message: "bump\n", + Sender: "gordon", + Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + Email: "gordon@golang.org", + ForgeURL: "http://gitea.golang.org/gordon/hello-world/commit/ef98532add3b2feb7a137426bba1248724367df5", + ChangedFiles: []string{"CHANGELOG.md", "app/controller/application.rb"}, }, }, { @@ -135,17 +137,18 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "test-user", - Event: "push", - Commit: "29be01c073851cf0db0c6a466e396b725a670453", - Branch: "main", - Ref: "refs/heads/main", - Message: "add some text\n", - Sender: "test-user", - Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", - Email: "test@noreply.localhost", - ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/compare/6efcf5b7c98f3e7a491675164b7a2e7acac27941...29be01c073851cf0db0c6a466e396b725a670453", - ChangedFiles: []string{"aaa", "aa"}, + Author: "test-user", + Event: "push", + Commit: "29be01c073851cf0db0c6a466e396b725a670453", + Branch: "main", + Ref: "refs/heads/main", + PRTitleDescription: "add some text", + Message: "add some text\n", + Sender: "test-user", + Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", + Email: "test@noreply.localhost", + ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/compare/6efcf5b7c98f3e7a491675164b7a2e7acac27941...29be01c073851cf0db0c6a466e396b725a670453", + ChangedFiles: []string{"aaa", "aa"}, }, }, { @@ -171,15 +174,16 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "gordon", - Event: "tag", - Commit: "ef98532add3b2feb7a137426bba1248724367df5", - Ref: "refs/tags/v1.0.0", - Message: "created tag v1.0.0", - Sender: "gordon", - Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", - Email: "gordon@golang.org", - ForgeURL: "http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0", + Author: "gordon", + Event: "tag", + Commit: "ef98532add3b2feb7a137426bba1248724367df5", + Ref: "refs/tags/v1.0.0", + PRTitleDescription: "created tag v1.0.0", + Message: "", + Sender: "gordon", + Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + Email: "gordon@golang.org", + ForgeURL: "http://gitea.golang.org/gordon/hello-world/src/tag/v1.0.0", }, }, { @@ -205,19 +209,19 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "gordon", - Event: "pull_request", - Commit: "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "feature/changes:main", - Title: "Update the README with new information", - Message: "Update the README with new information", - Sender: "gordon", - Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", - Email: "gordon@golang.org", - ForgeURL: "http://gitea.golang.org/gordon/hello-world/pull/1", - PullRequestLabels: []string{}, + Author: "gordon", + Event: "pull_request", + Commit: "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "feature/changes:main", + PRTitleDescription: "Update the README with new information", + Message: "please merge", + Sender: "gordon", + Avatar: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + Email: "gordon@golang.org", + ForgeURL: "http://gitea.golang.org/gordon/hello-world/pull/1", + PullRequestLabels: []string{}, }, }, { @@ -244,18 +248,18 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "test", - Event: "pull_request", - Commit: "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25", - Branch: "main", - Ref: "refs/pull/2/head", - Refspec: "test-patch-1:main", - Title: "New Pull", - Message: "New Pull", - Sender: "test", - Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", - Email: "test@noreply.localhost", - ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2", + Author: "test", + Event: "pull_request", + Commit: "788ed8d02d3b7fcfcf6386dbcbca696aa1d4dc25", + Branch: "main", + Ref: "refs/pull/2/head", + Refspec: "test-patch-1:main", + PRTitleDescription: "New Pull", + Message: "create an awesome pull", + Sender: "test", + Avatar: "http://127.0.0.1:3000/avatars/dd46a756faad4727fb679320751f6dea", + Email: "test@noreply.localhost", + ForgeURL: "http://127.0.0.1:3000/Test-CI/multi-line-secrets/pulls/2", PullRequestLabels: []string{ "Kind/Bug", "Kind/Security", @@ -285,19 +289,19 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "anbraten", - Event: "pull_request_closed", - Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "anbraten-patch-1:main", - Title: "Adjust file", - Message: "Adjust file", - Sender: "anbraten", - Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", - Email: "anbraten@sender.gitea.com", - ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", - PullRequestLabels: []string{}, + Author: "anbraten", + Event: "pull_request_closed", + Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "anbraten-patch-1:main", + PRTitleDescription: "Adjust file", + Message: "", + Sender: "anbraten", + Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", + Email: "anbraten@sender.gitea.com", + ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", + PullRequestLabels: []string{}, }, }, { @@ -323,19 +327,19 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "anbraten", - Event: "pull_request_closed", - Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", - Branch: "main", - Ref: "refs/pull/1/head", - Refspec: "anbraten-patch-1:main", - Title: "Adjust file", - Message: "Adjust file", - Sender: "anbraten", - Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", - Email: "anbraten@noreply.gitea.com", - ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", - PullRequestLabels: []string{}, + Author: "anbraten", + Event: "pull_request_closed", + Commit: "d555a5dd07f4d0148a58d4686ec381502ae6a2d4", + Branch: "main", + Ref: "refs/pull/1/head", + Refspec: "anbraten-patch-1:main", + PRTitleDescription: "Adjust file", + Message: "", + Sender: "anbraten", + Avatar: "https://seccdn.libravatar.org/avatar/fc9b6fe77c6b732a02925a62a81f05a0?d=identicon", + Email: "anbraten@noreply.gitea.com", + ForgeURL: "https://gitea.com/anbraten/test-repo/pulls/1", + PullRequestLabels: []string{}, }, }, { @@ -362,15 +366,16 @@ func TestGiteaParser(t *testing.T) { }, }, pipe: &model.Pipeline{ - Author: "anbraten", - Event: "release", - Branch: "main", - Ref: "refs/tags/0.0.5", - Message: "created release Version 0.0.5", - Sender: "anbraten", - Avatar: "https://git.xxx/user/avatar/anbraten/-1", - Email: "anbraten@noreply.xxx", - ForgeURL: "https://git.xxx/anbraten/demo/releases/tag/0.0.5", + Author: "anbraten", + Event: "release", + Branch: "main", + Ref: "refs/tags/0.0.5", + PRTitleDescription: "created release Version 0.0.5", + Message: "release notes", + Sender: "anbraten", + Avatar: "https://git.xxx/user/avatar/anbraten/-1", + Email: "anbraten@noreply.xxx", + ForgeURL: "https://git.xxx/anbraten/demo/releases/tag/0.0.5", }, }, } diff --git a/server/forge/github/convert_test.go b/server/forge/github/convert_test.go index 605aa04525a..27afb6d3bab 100644 --- a/server/forge/github/convert_test.go +++ b/server/forge/github/convert_test.go @@ -214,7 +214,7 @@ func Test_helper(t *testing.T) { g.Assert(pipeline.Refspec).Equal("changes:main") g.Assert(pipeline.Commit).Equal(*from.PullRequest.Head.SHA) g.Assert(pipeline.Message).Equal(*from.PullRequest.Title) - g.Assert(pipeline.Title).Equal(*from.PullRequest.Title) + g.Assert(pipeline.PRTitleDescription).Equal(*from.PullRequest.Title) g.Assert(pipeline.Author).Equal(*from.PullRequest.User.Login) g.Assert(pipeline.Avatar).Equal(*from.PullRequest.User.AvatarURL) g.Assert(pipeline.Sender).Equal(*from.Sender.Login) diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index e6fa7f068fc..1d5ef30d699 100644 --- a/server/forge/github/parse.go +++ b/server/forge/github/parse.go @@ -39,6 +39,9 @@ const ( stateOpen = "open" stateClose = "closed" + + branchRefPrefix = "refs/heads/" + tagRefPrefix = "refs/tags/" ) // parseHook parses a GitHub hook from an http.Request request and returns @@ -89,7 +92,7 @@ func parsePushHook(hook *github.PushEvent) (*model.Repo, *model.Pipeline) { Commit: hook.GetHeadCommit().GetID(), Ref: hook.GetRef(), ForgeURL: hook.GetHeadCommit().GetURL(), - Branch: strings.ReplaceAll(hook.GetRef(), "refs/heads/", ""), + Branch: strings.ReplaceAll(hook.GetRef(), branchRefPrefix, ""), Message: hook.GetHeadCommit().GetMessage(), Email: hook.GetHeadCommit().GetAuthor().GetEmail(), Avatar: hook.GetSender().GetAvatarURL(), @@ -101,15 +104,15 @@ func parsePushHook(hook *github.PushEvent) (*model.Repo, *model.Pipeline) { if len(pipeline.Author) == 0 { pipeline.Author = hook.GetHeadCommit().GetAuthor().GetLogin() } - if strings.HasPrefix(pipeline.Ref, "refs/tags/") { + if strings.HasPrefix(pipeline.Ref, tagRefPrefix) { // just kidding, this is actually a tag event. Why did this come as a push // event we'll never know! pipeline.Event = model.EventTag pipeline.ChangedFiles = nil // For tags, if the base_ref (tag's base branch) is set, we're using it // as pipeline's branch so that we can filter events base on it - if strings.HasPrefix(hook.GetBaseRef(), "refs/heads/") { - pipeline.Branch = strings.ReplaceAll(hook.GetBaseRef(), "refs/heads/", "") + if strings.HasPrefix(hook.GetBaseRef(), branchRefPrefix) { + pipeline.Branch = strings.ReplaceAll(hook.GetBaseRef(), branchRefPrefix, "") } } @@ -123,7 +126,7 @@ func parseDeployHook(hook *github.DeploymentEvent) (*model.Repo, *model.Pipeline Event: model.EventDeploy, Commit: hook.GetDeployment().GetSHA(), ForgeURL: hook.GetDeployment().GetURL(), - Message: hook.GetDeployment().GetDescription(), + Message: hook.GetDeployment().GetEnvironment() + "\n" + hook.GetDeployment().GetDescription(), Ref: hook.GetDeployment().GetRef(), Branch: hook.GetDeployment().GetRef(), Avatar: hook.GetSender().GetAvatarURL(), @@ -135,11 +138,11 @@ func parseDeployHook(hook *github.DeploymentEvent) (*model.Repo, *model.Pipeline // if the ref is a sha or short sha we need to manually construct the ref. if strings.HasPrefix(pipeline.Commit, pipeline.Ref) || pipeline.Commit == pipeline.Ref { pipeline.Branch = hook.GetRepo().GetDefaultBranch() - pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch) + pipeline.Ref = fmt.Sprintf("%s%s", branchRefPrefix, pipeline.Branch) } // if the ref is a branch we should make sure it has refs/heads prefix if !strings.HasPrefix(pipeline.Ref, "refs/") { // branch or tag - pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch) + pipeline.Ref = fmt.Sprintf("%s%s", branchRefPrefix, pipeline.Branch) } return convertRepo(hook.GetRepo()), pipeline @@ -158,16 +161,16 @@ func parsePullHook(hook *github.PullRequestEvent, merge bool) (*github.PullReque } pipeline := &model.Pipeline{ - Event: event, - Commit: hook.GetPullRequest().GetHead().GetSHA(), - ForgeURL: hook.GetPullRequest().GetHTMLURL(), - Ref: fmt.Sprintf(headRefs, hook.GetPullRequest().GetNumber()), - Branch: hook.GetPullRequest().GetBase().GetRef(), - Message: hook.GetPullRequest().GetTitle(), - Author: hook.GetPullRequest().GetUser().GetLogin(), - Avatar: hook.GetPullRequest().GetUser().GetAvatarURL(), - Title: hook.GetPullRequest().GetTitle(), - Sender: hook.GetSender().GetLogin(), + Event: event, + Commit: hook.GetPullRequest().GetHead().GetSHA(), + ForgeURL: hook.GetPullRequest().GetHTMLURL(), + Ref: fmt.Sprintf(headRefs, hook.GetPullRequest().GetNumber()), + Branch: hook.GetPullRequest().GetBase().GetRef(), + Message: hook.GetChanges().GetTitle().GetFrom() + "\n" + hook.GetChanges().GetBody().GetFrom(), + Author: hook.GetPullRequest().GetUser().GetLogin(), + Avatar: hook.GetPullRequest().GetUser().GetAvatarURL(), + PRTitleDescription: hook.GetPullRequest().GetTitle() + "\n" + hook.GetPullRequest().GetBody(), + Sender: hook.GetSender().GetLogin(), Refspec: fmt.Sprintf(refSpec, hook.GetPullRequest().GetHead().GetRef(), hook.GetPullRequest().GetBase().GetRef(), @@ -196,9 +199,9 @@ func parseReleaseHook(hook *github.ReleaseEvent) (*model.Repo, *model.Pipeline) pipeline := &model.Pipeline{ Event: model.EventRelease, ForgeURL: hook.GetRelease().GetHTMLURL(), - Ref: fmt.Sprintf("refs/tags/%s", hook.GetRelease().GetTagName()), + Ref: fmt.Sprintf("%s%s", tagRefPrefix, hook.GetRelease().GetTagName()), Branch: hook.GetRelease().GetTargetCommitish(), // cspell:disable-line - Message: fmt.Sprintf("created release %s", name), + Message: name + "\n" + hook.GetRelease().GetBody(), Author: hook.GetRelease().GetAuthor().GetLogin(), Avatar: hook.GetRelease().GetAuthor().GetAvatarURL(), Sender: hook.GetSender().GetLogin(), diff --git a/server/forge/github/parse_test.go b/server/forge/github/parse_test.go index 225bfd3c91e..8f64833df48 100644 --- a/server/forge/github/parse_test.go +++ b/server/forge/github/parse_test.go @@ -75,6 +75,8 @@ func Test_parser(t *testing.T) { g.Assert(r).IsNotNil() g.Assert(b).IsNotNil() g.Assert(b.Event).Equal(model.EventPush) + assert.EqualValues(t, "Fix multiline secrets replacer (#700)", b.PRTitleDescription) + assert.EqualValues(t, "Fix multiline secrets replacer (#700)\n\n* Fix multiline secrets replacer\r\n\r\n* Add tests", b.Message) sort.Strings(b.ChangedFiles) g.Assert(b.ChangedFiles).Equal([]string{"pipeline/shared/replace_secrets.go", "pipeline/shared/replace_secrets_test.go"}) }) diff --git a/server/forge/gitlab/convert.go b/server/forge/gitlab/convert.go index 51d16bac3f3..a15e30ace12 100644 --- a/server/forge/gitlab/convert.go +++ b/server/forge/gitlab/convert.go @@ -135,7 +135,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, * pipeline.Avatar = getUserAvatar(pipeline.Email) } - pipeline.Title = obj.Title + pipeline.PRTitleDescription = obj.Title + "\n" + obj.Description pipeline.ForgeURL = obj.URL pipeline.PullRequestLabels = convertLabels(hook.Labels) @@ -266,7 +266,7 @@ func convertReleaseHook(hook *gitlab.ReleaseEvent) (*model.Repo, *model.Pipeline Event: model.EventRelease, Commit: hook.Commit.ID, ForgeURL: hook.URL, - Message: fmt.Sprintf("created release %s", hook.Name), + Message: hook.Name + "\n" + hook.Description, Sender: hook.Commit.Author.Name, Author: hook.Commit.Author.Name, Email: hook.Commit.Author.Email, diff --git a/server/forge/gitlab/gitlab_test.go b/server/forge/gitlab/gitlab_test.go index 5e0a5afe6bd..2d203a77d70 100644 --- a/server/forge/gitlab/gitlab_test.go +++ b/server/forge/gitlab/gitlab_test.go @@ -201,7 +201,7 @@ func Test_GitLab(t *testing.T) { assert.Equal(t, "main", hookRepo.Branch) assert.Equal(t, "anbraten", hookRepo.Owner) assert.Equal(t, "woodpecker", hookRepo.Name) - assert.Equal(t, "Update client.go 🎉", pipeline.Title) + assert.Equal(t, "Update client.go 🎉", pipeline.PRTitleDescription) assert.Len(t, pipeline.ChangedFiles, 0) // see L217 assert.Equal(t, model.EventPull, pipeline.Event) } @@ -252,7 +252,7 @@ func Test_GitLab(t *testing.T) { assert.Equal(t, "main", hookRepo.Branch) assert.Equal(t, "anbraten", hookRepo.Owner) assert.Equal(t, "woodpecker-test", hookRepo.Name) - assert.Equal(t, "Add new file", pipeline.Title) + assert.Equal(t, "Add new file", pipeline.PRTitleDescription) assert.Len(t, pipeline.ChangedFiles, 0) // see L217 assert.Equal(t, model.EventPullClosed, pipeline.Event) } @@ -273,7 +273,7 @@ func Test_GitLab(t *testing.T) { assert.Equal(t, "main", hookRepo.Branch) assert.Equal(t, "anbraten", hookRepo.Owner) assert.Equal(t, "woodpecker-test", hookRepo.Name) - assert.Equal(t, "Add new file", pipeline.Title) + assert.Equal(t, "Add new file", pipeline.PRTitleDescription) assert.Len(t, pipeline.ChangedFiles, 0) // see L217 assert.Equal(t, model.EventPullClosed, pipeline.Event) } @@ -292,7 +292,8 @@ func Test_GitLab(t *testing.T) { if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) { assert.Equal(t, "refs/tags/0.0.2", pipeline.Ref) assert.Equal(t, "ci", hookRepo.Name) - assert.Equal(t, "created release Awesome version 0.0.2", pipeline.Message) + assert.Equal(t, "created release Awesome version 0.0.2", pipeline.PRTitleDescription) + assert.Equal(t, "new version desc", pipeline.Message) assert.Equal(t, model.EventRelease, pipeline.Event) } }) diff --git a/server/model/pipeline.go b/server/model/pipeline.go index a073ad15fbf..549c06ef5f6 100644 --- a/server/model/pipeline.go +++ b/server/model/pipeline.go @@ -38,7 +38,7 @@ type Pipeline struct { Branch string `json:"branch" xorm:"branch"` Ref string `json:"ref" xorm:"ref"` Refspec string `json:"refspec" xorm:"refspec"` - Title string `json:"title" xorm:"title"` + PRTitleDescription string `json:"title" xorm:"title"` Message string `json:"message" xorm:"TEXT 'message'"` Timestamp int64 `json:"timestamp" xorm:"'timestamp'"` Sender string `json:"sender" xorm:"sender"` // uses reported user for webhooks and name of cron for cron pipelines