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

Add PR pipeline list #1641

Merged
merged 16 commits into from
Mar 19, 2023
4 changes: 2 additions & 2 deletions pipeline/rpc/proto/woodpecker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 41 additions & 24 deletions pipeline/rpc/proto/woodpecker_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ func GetRepoBranches(c *gin.Context) {
c.JSON(http.StatusOK, branches)
}

func GetRepoPullRequests(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
page := session.Pagination(c)
f := server.Config.Services.Forge

prs, err := f.PullRequests(c, user, repo, page)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}

c.JSON(http.StatusOK, prs)
}

func DeleteRepo(c *gin.Context) {
remove, _ := strconv.ParseBool(c.Query("remove"))
_store := store.FromContext(c)
Expand Down
5 changes: 5 additions & 0 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package bitbucket

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -302,6 +303,10 @@ func (c *config) BranchHead(_ context.Context, _ *model.User, _ *model.Repo, _ s
return "", forge_types.ErrNotImplemented
}

func (c *config) PullRequests(_ context.Context, _ *model.User, _ *model.Repo, _ *model.PaginationData) ([]*model.PullRequest, error) {
return nil, errors.New("Bitbucket does not support pull requests yet")
6543 marked this conversation as resolved.
Show resolved Hide resolved
}

// Hook parses the incoming Bitbucket hook and returns the Repository and
// Pipeline details. If the hook is unsupported nil values are returned.
func (c *config) Hook(_ context.Context, req *http.Request) (*model.Repo, *model.Pipeline, error) {
Expand Down
5 changes: 5 additions & 0 deletions server/forge/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -244,6 +245,10 @@ func (c *Config) BranchHead(_ context.Context, _ *model.User, _ *model.Repo, _ s
return "", forge_types.ErrNotImplemented
}

func (c *Config) PullRequests(_ context.Context, _ *model.User, _ *model.Repo, _ *model.PaginationData) ([]*model.PullRequest, error) {
return nil, errors.New("Bitbucket server does not support pull requests yet")
}

func (c *Config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
client := internal.NewClientWithToken(ctx, c.URL, c.Consumer, u.Token)
return client.DeleteHook(r.Owner, r.Name, link)
Expand Down
3 changes: 3 additions & 0 deletions server/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type Forge interface {
// BranchHead returns the sha of the head (latest commit) of the specified branch
BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error)

// PullRequests returns all pull requests for the named repository.
PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.PaginationData) ([]*model.PullRequest, error)

// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Pipeline, error)
Expand Down
28 changes: 28 additions & 0 deletions server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,34 @@ func (c *Gitea) BranchHead(ctx context.Context, u *model.User, r *model.Repo, br
return b.Commit.ID, nil
}

func (c *Gitea) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.PaginationData) ([]*model.PullRequest, error) {
token := ""
if u != nil {
token = u.Token
}
client, err := c.newClientToken(ctx, token)
if err != nil {
return nil, err
}

pullRequests, _, err := client.ListRepoPullRequests(r.Owner, r.Name, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{Page: int(p.Page), PageSize: int(p.PerPage)},
State: gitea.StateOpen,
})
if err != nil {
return nil, err
}

result := make([]*model.PullRequest, len(pullRequests))
for i := range pullRequests {
result[i] = &model.PullRequest{
Index: pullRequests[i].Index,
Title: pullRequests[i].Title,
}
}
return result, err
}

// 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) {
Expand Down
25 changes: 25 additions & 0 deletions server/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,31 @@ func (c *client) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model
return files, nil
}

func (c *client) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.PaginationData) ([]*model.PullRequest, error) {
token := ""
if u != nil {
token = u.Token
}
client := c.newClientToken(ctx, token)

pullRequests, _, err := client.PullRequests.List(ctx, r.Owner, r.Name, &github.PullRequestListOptions{
ListOptions: github.ListOptions{Page: int(p.Page), PerPage: int(p.PerPage)},
State: "open",
})
if err != nil {
return nil, err
}

result := make([]*model.PullRequest, len(pullRequests))
for i := range pullRequests {
result[i] = &model.PullRequest{
Index: int64(pullRequests[i].GetNumber()),
Title: pullRequests[i].GetTitle(),
}
}
return result, err
}

// Netrc returns a netrc file capable of authenticating GitHub requests and
// cloning GitHub repositories. The netrc will use the global machine account
// when configured.
Expand Down