mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-21 07:56:31 +00:00
Add "path" support to gitea on push hooks (#235)
This commit is contained in:
parent
70958acc44
commit
ee3e4bb189
5 changed files with 78 additions and 17 deletions
|
@ -386,7 +386,7 @@ when:
|
||||||
|
|
||||||
Execute a step only on commit with certain files added/removed/modified:
|
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
|
```diff
|
||||||
when:
|
when:
|
||||||
|
@ -396,7 +396,7 @@ when:
|
||||||
Execute a step only on commit excluding certain files added/removed/modified:
|
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
|
```diff
|
||||||
when:
|
when:
|
||||||
|
|
|
@ -30,7 +30,10 @@ const HookPush = `
|
||||||
"name": "Gordon the Gopher",
|
"name": "Gordon the Gopher",
|
||||||
"email": "gordon@golang.org",
|
"email": "gordon@golang.org",
|
||||||
"username": "gordon"
|
"username": "gordon"
|
||||||
}
|
},
|
||||||
|
"added": ["CHANGELOG.md"],
|
||||||
|
"removed": [],
|
||||||
|
"modified": ["app/controller/application.rb"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
@ -88,20 +88,35 @@ func buildFromPush(hook *pushHook) *model.Build {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.Build{
|
return &model.Build{
|
||||||
Event: model.EventPush,
|
Event: model.EventPush,
|
||||||
Commit: hook.After,
|
Commit: hook.After,
|
||||||
Ref: hook.Ref,
|
Ref: hook.Ref,
|
||||||
Link: hook.Compare,
|
Link: hook.Compare,
|
||||||
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
|
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
|
||||||
Message: message,
|
Message: message,
|
||||||
Avatar: avatar,
|
Avatar: avatar,
|
||||||
Author: author,
|
Author: author,
|
||||||
Email: hook.Sender.Email,
|
Email: hook.Sender.Email,
|
||||||
Timestamp: time.Now().UTC().Unix(),
|
Timestamp: time.Now().UTC().Unix(),
|
||||||
Sender: sender,
|
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
|
// helper function that extracts the Build data from a Gitea tag hook
|
||||||
func buildFromTag(hook *pushHook) *model.Build {
|
func buildFromTag(hook *pushHook) *model.Build {
|
||||||
avatar := expandAvatar(
|
avatar := expandAvatar(
|
||||||
|
|
|
@ -13,3 +13,43 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package gitea
|
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"})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -43,9 +43,12 @@ type pushHook struct {
|
||||||
} `json:"repository"`
|
} `json:"repository"`
|
||||||
|
|
||||||
Commits []struct {
|
Commits []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
|
Added []string `json:"added"`
|
||||||
|
Removed []string `json:"removed"`
|
||||||
|
Modified []string `json:"modified"`
|
||||||
} `json:"commits"`
|
} `json:"commits"`
|
||||||
|
|
||||||
Sender struct {
|
Sender struct {
|
||||||
|
|
Loading…
Reference in a new issue