diff --git a/server/remote/gitea/fixtures/hooks.go b/server/remote/gitea/fixtures/hooks.go index 30f190532..ced923d0c 100644 --- a/server/remote/gitea/fixtures/hooks.go +++ b/server/remote/gitea/fixtures/hooks.go @@ -69,6 +69,155 @@ const HookPush = ` } ` +// HookPushBranch is a sample Gitea push hook where a new branch was created from an existing commit +const HookPushBranch = ` +{ + "ref": "refs/heads/fdsafdsa", + "before": "0000000000000000000000000000000000000000", + "after": "28c3613ae62640216bea5e7dc71aa65356e4298b", + "compare_url": "https://codeberg.org/meisam/woodpecktester/compare/master...28c3613ae62640216bea5e7dc71aa65356e4298b", + "commits": [], + "head_commit": { + "id": "28c3613ae62640216bea5e7dc71aa65356e4298b", + "message": "Delete '.woodpecker/.check.yml'\n", + "url": "https://codeberg.org/meisam/woodpecktester/commit/28c3613ae62640216bea5e7dc71aa65356e4298b", + "author": { + "name": "meisam", + "email": "meisam@noreply.codeberg.org", + "username": "meisam" + }, + "committer": { + "name": "meisam", + "email": "meisam@noreply.codeberg.org", + "username": "meisam" + }, + "verification": null, + "timestamp": "2022-07-12T21:09:27+02:00", + "added": [], + "removed": [ + ".woodpecker/.check.yml" + ], + "modified": [] + }, + "repository": { + "id": 50820, + "owner": { + "id": 14844, + "login": "meisam", + "full_name": "", + "email": "meisam@noreply.codeberg.org", + "avatar_url": "https://codeberg.org/avatars/96512da76a14cf44e0bb32d1640e878e", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2020-10-08T11:19:12+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "Materials engineer, physics enthusiast, large collection of the bad programming habits, always happy to fix the old ones and make new mistakes!", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "meisam" + }, + "name": "woodpecktester", + "full_name": "meisam/woodpecktester", + "description": "Just for testing the Woodpecker CI and reporting bugs", + "empty": false, + "private": false, + "fork": false, + "template": false, + "parent": null, + "mirror": false, + "size": 367, + "language": "", + "languages_url": "https://codeberg.org/api/v1/repos/meisam/woodpecktester/languages", + "html_url": "https://codeberg.org/meisam/woodpecktester", + "ssh_url": "git@codeberg.org:meisam/woodpecktester.git", + "clone_url": "https://codeberg.org/meisam/woodpecktester.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "master", + "archived": false, + "created_at": "2022-07-04T00:34:39+02:00", + "updated_at": "2022-07-24T20:31:29+02:00", + "permissions": { + "admin": true, + "push": true, + "pull": true + }, + "has_issues": true, + "internal_tracker": { + "enable_time_tracker": true, + "allow_only_contributors_to_track_time": true, + "enable_issue_dependencies": true + }, + "has_wiki": true, + "has_pull_requests": true, + "has_projects": true, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "default_merge_style": "merge", + "avatar_url": "", + "internal": false, + "mirror_interval": "", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + }, + "pusher": { + "id": 2628, + "login": "6543", + "full_name": "", + "email": "6543@obermui.de", + "avatar_url": "https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2019-10-12T05:05:49+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "visibility": "public", + "followers_count": 22, + "following_count": 16, + "starred_repos_count": 55, + "username": "6543" + }, + "sender": { + "id": 2628, + "login": "6543", + "full_name": "", + "email": "6543@obermui.de", + "avatar_url": "https://codeberg.org/avatars/09a234c768cb9bca78f6b2f82d6af173", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2019-10-12T05:05:49+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "visibility": "public", + "followers_count": 22, + "following_count": 16, + "starred_repos_count": 55, + "username": "6543" + } +} +` + // HookPushTag is a sample Gitea tag hook const HookPushTag = `{ "sha": "ef98532add3b2feb7a137426bba1248724367df5", diff --git a/server/remote/gitea/helper.go b/server/remote/gitea/helper.go index 1132bb33b..3712a3a49 100644 --- a/server/remote/gitea/helper.go +++ b/server/remote/gitea/helper.go @@ -84,6 +84,8 @@ func buildFromPush(hook *pushHook) *model.Build { link := hook.Compare if len(hook.Commits) > 0 { message = hook.Commits[0].Message + } else { + message = hook.HeadCommit.Message } if len(hook.Commits) == 1 { @@ -115,6 +117,10 @@ func getChangedFilesFromPushHook(hook *pushHook) []string { files = append(files, c.Modified...) } + files = append(files, hook.HeadCommit.Added...) + files = append(files, hook.HeadCommit.Removed...) + files = append(files, hook.HeadCommit.Modified...) + return utils.DedupStrings(files) } diff --git a/server/remote/gitea/parse_test.go b/server/remote/gitea/parse_test.go index 3193fd593..1f6b83578 100644 --- a/server/remote/gitea/parse_test.go +++ b/server/remote/gitea/parse_test.go @@ -53,5 +53,20 @@ func Test_parser(t *testing.T) { g.Assert(utils.EqualStringSlice(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue() }) }) + g.Describe("given a push hook from an branch creation", func() { + g.It("should extract repository and build details", func() { + buf := bytes.NewBufferString(fixtures.HookPushBranch) + req, _ := http.NewRequest("POST", "/hook", buf) + req.Header = http.Header{} + req.Header.Set(hookEvent, hookPush) + r, b, err := parseHook(req) + g.Assert(err).IsNil() + g.Assert(r).IsNotNil() + g.Assert(b).IsNotNil() + g.Assert(b.Event).Equal(model.EventPush) + g.Assert(b.Message).Equal("Delete '.woodpecker/.check.yml'\n") + g.Assert(b.ChangedFiles).Equal([]string{".woodpecker/.check.yml"}) + }) + }) }) } diff --git a/server/remote/gitea/types.go b/server/remote/gitea/types.go index e7c87cadb..c045ce25c 100644 --- a/server/remote/gitea/types.go +++ b/server/remote/gitea/types.go @@ -14,6 +14,15 @@ package gitea +type commit struct { + ID string `json:"id"` + Message string `json:"message"` + URL string `json:"url"` + Added []string `json:"added"` + Removed []string `json:"removed"` + Modified []string `json:"modified"` +} + type pushHook struct { Sha string `json:"sha"` Ref string `json:"ref"` @@ -42,14 +51,9 @@ type pushHook struct { } `json:"owner"` } `json:"repository"` - Commits []struct { - ID string `json:"id"` - Message string `json:"message"` - URL string `json:"url"` - Added []string `json:"added"` - Removed []string `json:"removed"` - Modified []string `json:"modified"` - } `json:"commits"` + Commits []commit `json:"commits"` + + HeadCommit commit `json:"head_commit"` Sender struct { ID int64 `json:"id"` diff --git a/web/components.d.ts b/web/components.d.ts index 594922cba..1c8f8ad67 100644 --- a/web/components.d.ts +++ b/web/components.d.ts @@ -36,7 +36,6 @@ declare module '@vue/runtime-core' { IEntypoDotsTwoVertical: typeof import('~icons/entypo/dots-two-vertical')['default'] IGgTrash: typeof import('~icons/gg/trash')['default'] IIcBaselineDarkMode: typeof import('~icons/ic/baseline-dark-mode')['default'] - IIcBaselineDownload: typeof import('~icons/ic/baseline-download')['default'] IIcBaselineDownloadForOffline: typeof import('~icons/ic/baseline-download-for-offline')['default'] IIcBaselineEdit: typeof import('~icons/ic/baseline-edit')['default'] IIcBaselineFileDownload: typeof import('~icons/ic/baseline-file-download')['default'] @@ -53,10 +52,10 @@ declare module '@vue/runtime-core' { IMdiFormatListBulleted: typeof import('~icons/mdi/format-list-bulleted')['default'] IMdiGithub: typeof import('~icons/mdi/github')['default'] IMdiLoading: typeof import('~icons/mdi/loading')['default'] - IMdiSync: typeof import('~icons/mdi/sync')['default'] IMdiSourceBranch: typeof import('~icons/mdi/source-branch')['default'] IMdisourceCommit: typeof import('~icons/mdi/source-commit')['default'] IMdiSourcePull: typeof import('~icons/mdi/source-pull')['default'] + IMdiSync: typeof import('~icons/mdi/sync')['default'] IMdiTagOutline: typeof import('~icons/mdi/tag-outline')['default'] InputField: typeof import('./src/components/form/InputField.vue')['default'] IOcticonSkip24: typeof import('~icons/octicon/skip24')['default']