Skip to content

Commit

Permalink
Add "path" support to gitea on push hooks (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Jul 4, 2021
1 parent 70958ac commit ee3e4bb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/docs/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion remote/gitea/fixtures/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
37 changes: 26 additions & 11 deletions remote/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,33 @@ 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
Expand Down
40 changes: 40 additions & 0 deletions remote/gitea/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})
})
})
}
9 changes: 6 additions & 3 deletions remote/gitea/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ee3e4bb

Please sign in to comment.