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

Set correct link for commit #3368

Merged
merged 10 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
12 changes: 10 additions & 2 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/rs/zerolog/log"

"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
Expand Down Expand Up @@ -103,13 +104,13 @@ func BlockTilQueueHasRunningItem(c *gin.Context) {
// @Param hook body object true "the webhook payload; forge is automatically detected"
func PostHook(c *gin.Context) {
_store := store.FromContext(c)
forge := server.Config.Services.Forge
_forge := server.Config.Services.Forge

//
// 1. Parse webhook
//

tmpRepo, tmpPipeline, err := forge.Hook(c, c.Request)
tmpRepo, tmpPipeline, err := _forge.Hook(c, c.Request)
if err != nil {
if errors.Is(err, &types.ErrIgnoreEvent{}) {
msg := fmt.Sprintf("forge driver: %s", err)
Expand Down Expand Up @@ -160,6 +161,13 @@ func PostHook(c *gin.Context) {
return
}

user, err := _store.GetUser(repo.UserID)
if err != nil {
handleDBError(c, err)
return
}
forge.Refresh(c, _forge, _store, user)

//
// 3. Check if the webhook is a valid and authorized one
//
Expand Down
11 changes: 5 additions & 6 deletions server/api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ func CreatePipeline(c *gin.Context) {

lastCommit, _ := server.Config.Services.Forge.BranchHead(c, user, repo, opts.Branch)

tmpBuild := createTmpPipeline(model.EventManual, lastCommit, repo, user, &opts)
tmpPipeline := createTmpPipeline(model.EventManual, lastCommit, repo, user, &opts)

pl, err := pipeline.Create(c, _store, repo, tmpBuild)
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
if err != nil {
handlePipelineErr(c, err)
} else {
c.JSON(http.StatusOK, pl)
}
}

func createTmpPipeline(event model.WebhookEvent, commitSHA string, repo *model.Repo, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
func createTmpPipeline(event model.WebhookEvent, commit *model.Commit, repo *model.Repo, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
return &model.Pipeline{
Event: event,
Commit: commitSHA,
Commit: commit.SHA,
Branch: opts.Branch,
Timestamp: time.Now().UTC().Unix(),

Expand All @@ -87,8 +87,7 @@ func createTmpPipeline(event model.WebhookEvent, commitSHA string, repo *model.R
Author: user.Login,
Email: user.Email,

// TODO: Generate proper URL to commit
ForgeURL: repo.ForgeURL,
ForgeURL: commit.ForgeURL,
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ func CreatePipeline(ctx context.Context, store store.Store, f forge.Forge, cron

return repo, &model.Pipeline{
Event: model.EventCron,
Commit: commit,
Commit: commit.SHA,
Ref: "refs/heads/" + cron.Branch,
Branch: cron.Branch,
Message: cron.Name,
Timestamp: cron.NextExec,
Sender: cron.Name,
ForgeURL: repo.ForgeURL,
ForgeURL: commit.ForgeURL,
}, nil
}
11 changes: 9 additions & 2 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,15 @@ func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo, p *
}

// BranchHead returns the sha of the head (latest commit) of the specified branch
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
return c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
commit, err := c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
if err != nil {
return nil, err
}
return &model.Commit{
SHA: commit.Hash,
ForgeURL: commit.Links.HTML.Href,
}, nil
}

// PullRequests returns the pull requests of the named repository.
Expand Down
8 changes: 4 additions & 4 deletions server/forge/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ func (c *Client) ListBranches(owner, name string, opts *ListOpts) ([]*Branch, er
return out.Values, err
}

func (c *Client) GetBranchHead(owner, name, branch string) (string, error) {
func (c *Client) GetBranchHead(owner, name, branch string) (*Commit, error) {
out := new(CommitsResp)
uri := fmt.Sprintf(pathBranchCommits, c.base, owner, name, branch)
_, err := c.do(uri, get, nil, out)
if err != nil {
return "", err
return nil, err
}
if len(out.Values) == 0 {
return "", fmt.Errorf("no commits in branch %s", branch)
return nil, fmt.Errorf("no commits in branch %s", branch)
}
return out.Values[0].Hash, nil
return out.Values[0], nil
}

func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, error) {
Expand Down
7 changes: 6 additions & 1 deletion server/forge/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,12 @@ type CommitsResp struct {
}

type Commit struct {
Hash string `json:"hash"`
Hash string `json:"hash"`
Links struct {
HTML struct {
Href string `json:"href"`
} `json:"html"`
} `json:"links"`
}

type DirResp struct {
Expand Down
2 changes: 1 addition & 1 deletion server/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Forge interface {
Branches(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]string, error)

// 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)
BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error)

// PullRequests returns all pull requests for the named repository.
PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error)
Expand Down
11 changes: 7 additions & 4 deletions server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,21 @@ func (c *Gitea) Branches(ctx context.Context, u *model.User, r *model.Repo, p *m
}

// BranchHead returns the sha of the head (latest commit) of the specified branch
func (c *Gitea) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
func (c *Gitea) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
token := common.UserToken(ctx, r, u)
client, err := c.newClientToken(ctx, token)
if err != nil {
return "", err
return nil, err
}

b, _, err := client.GetRepoBranch(r.Owner, r.Name, branch)
if err != nil {
return "", err
return nil, err
}
return b.Commit.ID, nil
return &model.Commit{
SHA: b.Commit.ID,
ForgeURL: b.Commit.URL,
}, nil
}

func (c *Gitea) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {
Expand Down
9 changes: 6 additions & 3 deletions server/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,16 @@ func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo, p *
}

// BranchHead returns the sha of the head (latest commit) of the specified branch
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
token := common.UserToken(ctx, r, u)
b, _, err := c.newClientToken(ctx, token).Repositories.GetBranch(ctx, r.Owner, r.Name, branch, 1)
if err != nil {
return "", err
return nil, err
}
return b.GetCommit().GetSHA(), nil
return &model.Commit{
SHA: b.GetCommit().GetSHA(),
ForgeURL: b.GetCommit().GetHTMLURL(),
}, nil
}

// Hook parses the post-commit hook from the Request body
Expand Down
13 changes: 8 additions & 5 deletions server/forge/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,24 +607,27 @@ func (g *GitLab) Branches(ctx context.Context, user *model.User, repo *model.Rep
}

// BranchHead returns the sha of the head (latest commit) of the specified branch
func (g *GitLab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
func (g *GitLab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
token := common.UserToken(ctx, r, u)
client, err := newClient(g.url, token, g.SkipVerify)
if err != nil {
return "", err
return nil, err
}

_repo, err := g.getProject(ctx, client, r.ForgeRemoteID, r.Owner, r.Name)
if err != nil {
return "", err
return nil, err
}

b, _, err := client.Branches.GetBranch(_repo.ID, branch, gitlab.WithContext(ctx))
if err != nil {
return "", err
return nil, err
}

return b.Commit.ID, nil
return &model.Commit{
SHA: b.Commit.ID,
ForgeURL: b.Commit.WebURL,
}, nil
}

// Hook parses the post-commit hook from the Request body
Expand Down
6 changes: 6 additions & 0 deletions server/model/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type Commit struct {
SHA string
ForgeURL string
anbraten marked this conversation as resolved.
Show resolved Hide resolved
}