diff --git a/docs/docs/pipeline.md b/docs/docs/pipeline.md index b66172a3a..8ff3afefa 100644 --- a/docs/docs/pipeline.md +++ b/docs/docs/pipeline.md @@ -386,7 +386,7 @@ when: Execute a step only on commit with certain files added/removed/modified: -**NOTE: Feature is only available for Github repositories.** +**NOTE: Feature is only available for Github and Gitea repositories.** ```diff when: @@ -396,7 +396,7 @@ when: Execute a step only on commit excluding certain files added/removed/modified: -**NOTE: Feature is only available for Github repositories.** +**NOTE: Feature is only available for Github and Gitea repositories.** ```diff when: diff --git a/remote/gitea/fixtures/hooks.go b/remote/gitea/fixtures/hooks.go index 089340365..30f190532 100644 --- a/remote/gitea/fixtures/hooks.go +++ b/remote/gitea/fixtures/hooks.go @@ -30,7 +30,10 @@ const HookPush = ` "name": "Gordon the Gopher", "email": "gordon@golang.org", "username": "gordon" - } + }, + "added": ["CHANGELOG.md"], + "removed": [], + "modified": ["app/controller/application.rb"] } ], "repository": { diff --git a/remote/gitea/helper.go b/remote/gitea/helper.go index a9fe0a9f0..3932c66fc 100644 --- a/remote/gitea/helper.go +++ b/remote/gitea/helper.go @@ -88,20 +88,35 @@ func buildFromPush(hook *pushHook) *model.Build { } return &model.Build{ - Event: model.EventPush, - Commit: hook.After, - Ref: hook.Ref, - Link: hook.Compare, - Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"), - Message: message, - Avatar: avatar, - Author: author, - Email: hook.Sender.Email, - Timestamp: time.Now().UTC().Unix(), - Sender: sender, + Event: model.EventPush, + Commit: hook.After, + Ref: hook.Ref, + Link: hook.Compare, + Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"), + Message: message, + Avatar: avatar, + Author: author, + Email: hook.Sender.Email, + Timestamp: time.Now().UTC().Unix(), + Sender: sender, + ChangedFiles: getChangedFilesFromPushHook(hook), } } +func getChangedFilesFromPushHook(hook *pushHook) []string { + files := make([]string, 0) + + if len(hook.Commits) == 0 { + return files + } + + files = append(files, hook.Commits[0].Added...) + files = append(files, hook.Commits[0].Removed...) + files = append(files, hook.Commits[0].Modified...) + + return files +} + // helper function that extracts the Build data from a Gitea tag hook func buildFromTag(hook *pushHook) *model.Build { avatar := expandAvatar( diff --git a/remote/gitea/parse_test.go b/remote/gitea/parse_test.go index d38c3f6f5..44b32e189 100644 --- a/remote/gitea/parse_test.go +++ b/remote/gitea/parse_test.go @@ -13,3 +13,43 @@ // limitations under the License. package gitea + +import ( + "bytes" + "net/http" + "testing" + + "github.com/franela/goblin" + "github.com/woodpecker-ci/woodpecker/model" + "github.com/woodpecker-ci/woodpecker/remote/gitea/fixtures" +) + +func Test_parser(t *testing.T) { + g := goblin.Goblin(t) + g.Describe("Gitea parser", func() { + g.It("should ignore unsupported hook events", func() { + buf := bytes.NewBufferString(fixtures.HookPullRequest) + req, _ := http.NewRequest("POST", "/hook", buf) + req.Header = http.Header{} + req.Header.Set(hookEvent, "issues") + r, b, err := parseHook(req) + g.Assert(r == nil).IsTrue() + g.Assert(b == nil).IsTrue() + g.Assert(err == nil).IsTrue() + }) + g.Describe("given a push hook", func() { + g.It("should extract repository and build details", func() { + buf := bytes.NewBufferString(fixtures.HookPush) + req, _ := http.NewRequest("POST", "/hook", buf) + req.Header = http.Header{} + req.Header.Set(hookEvent, hookPush) + r, b, err := parseHook(req) + g.Assert(err == nil).IsTrue() + g.Assert(r != nil).IsTrue() + g.Assert(b != nil).IsTrue() + g.Assert(b.Event).Equal(model.EventPush) + g.Assert(b.ChangedFiles).Equal([]string{"CHANGELOG.md", "app/controller/application.rb"}) + }) + }) + }) +} diff --git a/remote/gitea/types.go b/remote/gitea/types.go index f78a679ed..e7c87cadb 100644 --- a/remote/gitea/types.go +++ b/remote/gitea/types.go @@ -43,9 +43,12 @@ type pushHook struct { } `json:"repository"` Commits []struct { - ID string `json:"id"` - Message string `json:"message"` - URL string `json:"url"` + 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"` Sender struct {