Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support changed files for Gitea PRs #1342

Merged
merged 27 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/docs/20-usage/20-pipeline-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ when:

:::info
Path conditions are applied only to **push** and **pull_request** events.
It is currently **only available** for GitHub, GitLab.
Gitea only supports **push** at the moment ([go-gitea/gitea#18228](https://github.com/go-gitea/gitea/pull/18228)).
It is currently **only available** for GitHub, GitLab and Gitea (versions newer than 1.18.0)
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
:::

Execute a step only on a pipeline with certain files being changed:
Expand Down
4 changes: 1 addition & 3 deletions docs/docs/30-administration/11-forges/10-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@
| Event: Deploy | :white_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: |
| OAuth | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: |
| [Multi pipeline](../../20-usage/25-multi-pipeline.md) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: |
| [when.path filter](../../20-usage/20-pipeline-syntax.md#path) | :white_check_mark: | :white_check_mark:¹ | :white_check_mark: | :x: | :x: | :x: | :x: |

¹) [except for pull requests](https://github.com/woodpecker-ci/woodpecker/issues/754)
| [when.path filter](../../20-usage/20-pipeline-syntax.md#path) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: |
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/woodpecker-ci/woodpecker
go 1.18

require (
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe
code.gitea.io/sdk/gitea v0.15.1-0.20221016183512-2d9ee57af1e0
codeberg.org/6543/go-yaml2json v0.3.0
github.com/antonmedv/expr v1.9.0
github.com/bmatcuk/doublestar/v4 v4.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe h1:PeLyxnUZE85QuJtBZ4P8qCQcgWG5Ked67mlNgr0WkCQ=
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe/go.mod h1:aRmrQC3CAHdJAU1LQt0C9zqzqI8tUB/5oQtNE746aYE=
code.gitea.io/sdk/gitea v0.15.1-0.20221016183512-2d9ee57af1e0 h1:AKpsCoOtVrWWBtANM9319pwCB5ihx1Sdvr1HSbAwr54=
code.gitea.io/sdk/gitea v0.15.1-0.20221016183512-2d9ee57af1e0/go.mod h1:ndkDk99BnfiUCCYEUhpNzi0lpmApXlwRFqClBlOlEBg=
codeberg.org/6543/go-yaml2json v0.3.0 h1:BlvjmY0Gous8P+rr8aBdgPYnIfUAqFepF8q7Tp0R5t8=
codeberg.org/6543/go-yaml2json v0.3.0/go.mod h1:mz61q14LWF4ZABrgMEDMmk3t9dPi6zgR1uBh2VKV2RQ=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
61 changes: 60 additions & 1 deletion server/remote/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"code.gitea.io/sdk/gitea"
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/woodpecker/server/store"
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
"golang.org/x/oauth2"

"github.com/woodpecker-ci/woodpecker/server"
Expand Down Expand Up @@ -475,7 +476,23 @@ func (c *Gitea) BranchHead(ctx context.Context, u *model.User, r *model.Repo, br
// Hook parses the incoming Gitea hook and returns the Repository and Pipeline
// details. If the hook is unsupported nil values are returned.
func (c *Gitea) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Pipeline, error) {
return parseHook(r)
repo, pipeline, err := parseHook(r)
if err != nil {
return nil, nil, err
}

if pipeline.Event == model.EventPull && len(pipeline.ChangedFiles) == 0 {
index, err := strconv.ParseInt(strings.Split(pipeline.Ref, "/")[2], 10, 64)
if err != nil {
return nil, nil, err
}
pipeline.ChangedFiles, err = c.getChangedFilesForPR(ctx, repo, index)
if err != nil {
log.Error().Err(err).Msgf("could not get changed files for PR %s#%d", repo.FullName, index)
}
}

return repo, pipeline, nil
}

// OrgMembership returns if user is member of organization and if user
Expand Down Expand Up @@ -540,3 +557,45 @@ func getStatus(status model.StatusValue) gitea.StatusState {
return gitea.StatusFailure
}
}

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.RemoteID, 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
}

if strings.Contains(client.CheckServerVersionConstraint("1.18.0").Error(), "does not satisfy version constraint") {
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
// version too low
return []string{}, nil
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}

return common.Paginate(func(page int) ([]string, error) {
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
giteaFiles, _, err := client.ListPullRequestFiles(repo.Owner, repo.Name, index,
gitea.ListPullRequestFilesOptions{ListOptions: gitea.ListOptions{Page: page}})
if err != nil {
return nil, err
}

var files []string
for _, file := range giteaFiles {
files = append(files, file.Filename)
}
return files, nil
})
}