Skip to content

Commit e1521ef

Browse files
authored
Set correct link for commit (#3368)
Closes #2657 Closes #906
1 parent 894ab51 commit e1521ef

File tree

16 files changed

+92
-46
lines changed

16 files changed

+92
-46
lines changed

server/api/hook.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/rs/zerolog/log"
2828

2929
"go.woodpecker-ci.org/woodpecker/v2/server"
30+
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
3031
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
3132
"go.woodpecker-ci.org/woodpecker/v2/server/model"
3233
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
@@ -103,13 +104,13 @@ func BlockTilQueueHasRunningItem(c *gin.Context) {
103104
// @Param hook body object true "the webhook payload; forge is automatically detected"
104105
func PostHook(c *gin.Context) {
105106
_store := store.FromContext(c)
106-
forge := server.Config.Services.Forge
107+
_forge := server.Config.Services.Forge
107108

108109
//
109110
// 1. Parse webhook
110111
//
111112

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

164+
user, err := _store.GetUser(repo.UserID)
165+
if err != nil {
166+
handleDBError(c, err)
167+
return
168+
}
169+
forge.Refresh(c, _forge, _store, user)
170+
163171
//
164172
// 3. Check if the webhook is a valid and authorized one
165173
//

server/api/pipeline.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ func CreatePipeline(c *gin.Context) {
6161

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

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

66-
pl, err := pipeline.Create(c, _store, repo, tmpBuild)
66+
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
6767
if err != nil {
6868
handlePipelineErr(c, err)
6969
} else {
7070
c.JSON(http.StatusOK, pl)
7171
}
7272
}
7373

74-
func createTmpPipeline(event model.WebhookEvent, commitSHA string, repo *model.Repo, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
74+
func createTmpPipeline(event model.WebhookEvent, commit *model.Commit, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
7575
return &model.Pipeline{
7676
Event: event,
77-
Commit: commitSHA,
77+
Commit: commit.SHA,
7878
Branch: opts.Branch,
7979
Timestamp: time.Now().UTC().Unix(),
8080

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

90-
// TODO: Generate proper URL to commit
91-
ForgeURL: repo.ForgeURL,
90+
ForgeURL: commit.ForgeURL,
9291
}
9392
}
9493

server/cron/cron.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ func CreatePipeline(ctx context.Context, store store.Store, f forge.Forge, cron
133133

134134
return repo, &model.Pipeline{
135135
Event: model.EventCron,
136-
Commit: commit,
136+
Commit: commit.SHA,
137137
Ref: "refs/heads/" + cron.Branch,
138138
Branch: cron.Branch,
139139
Message: cron.Name,
140140
Timestamp: cron.NextExec,
141141
Sender: cron.Name,
142-
ForgeURL: repo.ForgeURL,
142+
ForgeURL: commit.ForgeURL,
143143
}, nil
144144
}

server/cron/cron_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ func TestCreateBuild(t *testing.T) {
4747
// mock things
4848
store.On("GetRepo", mock.Anything).Return(repo1, nil)
4949
store.On("GetUser", mock.Anything).Return(creator, nil)
50-
forge.On("BranchHead", mock.Anything, creator, repo1, "default").Return("sha1", nil)
50+
forge.On("BranchHead", mock.Anything, creator, repo1, "default").Return(&model.Commit{
51+
ForgeURL: "https://example.com/sha1",
52+
SHA: "sha1",
53+
}, nil)
5154

5255
_, pipeline, err := CreatePipeline(ctx, store, forge, &model.Cron{
5356
Name: "test",
5457
})
5558
assert.NoError(t, err)
5659
assert.EqualValues(t, &model.Pipeline{
57-
Event: "cron",
58-
Commit: "sha1",
59-
Branch: "default",
60-
Ref: "refs/heads/default",
61-
Message: "test",
62-
Sender: "test",
60+
Branch: "default",
61+
Commit: "sha1",
62+
Event: "cron",
63+
ForgeURL: "https://example.com/sha1",
64+
Message: "test",
65+
Ref: "refs/heads/default",
66+
Sender: "test",
6367
}, pipeline)
6468
}
6569

server/forge/bitbucket/bitbucket.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,15 @@ func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo, p *
363363
}
364364

365365
// BranchHead returns the sha of the head (latest commit) of the specified branch
366-
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
367-
return c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
366+
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
367+
commit, err := c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
368+
if err != nil {
369+
return nil, err
370+
}
371+
return &model.Commit{
372+
SHA: commit.Hash,
373+
ForgeURL: commit.Links.HTML.Href,
374+
}, nil
368375
}
369376

370377
// PullRequests returns the pull requests of the named repository.

server/forge/bitbucket/bitbucket_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ func Test_bitbucket(t *testing.T) {
184184
g.It("Should return the details", func() {
185185
branchHead, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_name")
186186
g.Assert(err).IsNil()
187-
g.Assert(branchHead).Equal("branch_head_name")
187+
g.Assert(branchHead.SHA).Equal("branch_head_name")
188+
g.Assert(branchHead.ForgeURL).Equal("https://bitbucket.org/commitlink")
188189
})
189190
g.It("Should handle not found errors", func() {
190191
_, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_not_found")

server/forge/bitbucket/fixtures/handler.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ const branchCommitsPayload = `
268268
{
269269
"values": [
270270
{
271-
"hash": "branch_head_name"
271+
"hash": "branch_head_name",
272+
"links": {
273+
"html": {
274+
"href": "https://bitbucket.org/commitlink"
275+
}
276+
}
272277
},
273278
{
274279
"hash": "random1"

server/forge/bitbucket/internal/client.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,17 @@ func (c *Client) ListBranches(owner, name string, opts *ListOpts) ([]*Branch, er
198198
return out.Values, err
199199
}
200200

201-
func (c *Client) GetBranchHead(owner, name, branch string) (string, error) {
201+
func (c *Client) GetBranchHead(owner, name, branch string) (*Commit, error) {
202202
out := new(CommitsResp)
203203
uri := fmt.Sprintf(pathBranchCommits, c.base, owner, name, branch)
204204
_, err := c.do(uri, get, nil, out)
205205
if err != nil {
206-
return "", err
206+
return nil, err
207207
}
208208
if len(out.Values) == 0 {
209-
return "", fmt.Errorf("no commits in branch %s", branch)
209+
return nil, fmt.Errorf("no commits in branch %s", branch)
210210
}
211-
return out.Values[0].Hash, nil
211+
return out.Values[0], nil
212212
}
213213

214214
func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, error) {

server/forge/bitbucket/internal/types.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ type CommitsResp struct {
282282
}
283283

284284
type Commit struct {
285-
Hash string `json:"hash"`
285+
Hash string `json:"hash"`
286+
Links struct {
287+
HTML struct {
288+
Href string `json:"href"`
289+
} `json:"html"`
290+
} `json:"links"`
286291
}
287292

288293
type DirResp struct {

server/forge/forge.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Forge interface {
7878
Branches(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]string, error)
7979

8080
// BranchHead returns the sha of the head (latest commit) of the specified branch
81-
BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error)
81+
BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error)
8282

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

server/forge/gitea/gitea.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,21 @@ func (c *Gitea) Branches(ctx context.Context, u *model.User, r *model.Repo, p *m
463463
}
464464

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

473473
b, _, err := client.GetRepoBranch(r.Owner, r.Name, branch)
474474
if err != nil {
475-
return "", err
475+
return nil, err
476476
}
477-
return b.Commit.ID, nil
477+
return &model.Commit{
478+
SHA: b.Commit.ID,
479+
ForgeURL: b.Commit.URL,
480+
}, nil
478481
}
479482

480483
func (c *Gitea) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {

server/forge/github/github.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,16 @@ func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo, p *
565565
}
566566

567567
// BranchHead returns the sha of the head (latest commit) of the specified branch
568-
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
568+
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
569569
token := common.UserToken(ctx, r, u)
570570
b, _, err := c.newClientToken(ctx, token).Repositories.GetBranch(ctx, r.Owner, r.Name, branch, 1)
571571
if err != nil {
572-
return "", err
572+
return nil, err
573573
}
574-
return b.GetCommit().GetSHA(), nil
574+
return &model.Commit{
575+
SHA: b.GetCommit().GetSHA(),
576+
ForgeURL: b.GetCommit().GetHTMLURL(),
577+
}, nil
575578
}
576579

577580
// Hook parses the post-commit hook from the Request body

server/forge/gitlab/gitlab.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -607,24 +607,27 @@ func (g *GitLab) Branches(ctx context.Context, user *model.User, repo *model.Rep
607607
}
608608

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

617617
_repo, err := g.getProject(ctx, client, r.ForgeRemoteID, r.Owner, r.Name)
618618
if err != nil {
619-
return "", err
619+
return nil, err
620620
}
621621

622622
b, _, err := client.Branches.GetBranch(_repo.ID, branch, gitlab.WithContext(ctx))
623623
if err != nil {
624-
return "", err
624+
return nil, err
625625
}
626626

627-
return b.Commit.ID, nil
627+
return &model.Commit{
628+
SHA: b.Commit.ID,
629+
ForgeURL: b.Commit.WebURL,
630+
}, nil
628631
}
629632

630633
// Hook parses the post-commit hook from the Request body

server/forge/mocks/forge.go

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/model/commit.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package model
2+
3+
type Commit struct {
4+
SHA string
5+
ForgeURL string
6+
}

server/store/mocks/store.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)