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 all 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 @@
"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 @@
// @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

Check warning on line 107 in server/api/hook.go

View check run for this annotation

Codecov / codecov/patch

server/api/hook.go#L107

Added line #L107 was not covered by tests

//
// 1. Parse webhook
//

tmpRepo, tmpPipeline, err := forge.Hook(c, c.Request)
tmpRepo, tmpPipeline, err := _forge.Hook(c, c.Request)

Check warning on line 113 in server/api/hook.go

View check run for this annotation

Codecov / codecov/patch

server/api/hook.go#L113

Added line #L113 was not covered by tests
if err != nil {
if errors.Is(err, &types.ErrIgnoreEvent{}) {
msg := fmt.Sprintf("forge driver: %s", err)
Expand Down Expand Up @@ -160,6 +161,13 @@
return
}

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

Check warning on line 170 in server/api/hook.go

View check run for this annotation

Codecov / codecov/patch

server/api/hook.go#L164-L170

Added lines #L164 - L170 were not covered by tests
//
// 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 @@

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

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

Check warning on line 64 in server/api/pipeline.go

View check run for this annotation

Codecov / codecov/patch

server/api/pipeline.go#L64

Added line #L64 was not covered by tests

pl, err := pipeline.Create(c, _store, repo, tmpBuild)
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)

Check warning on line 66 in server/api/pipeline.go

View check run for this annotation

Codecov / codecov/patch

server/api/pipeline.go#L66

Added line #L66 was not covered by tests
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, user *model.User, opts *model.PipelineOptions) *model.Pipeline {

Check warning on line 74 in server/api/pipeline.go

View check run for this annotation

Codecov / codecov/patch

server/api/pipeline.go#L74

Added line #L74 was not covered by tests
return &model.Pipeline{
Event: event,
Commit: commitSHA,
Commit: commit.SHA,

Check warning on line 77 in server/api/pipeline.go

View check run for this annotation

Codecov / codecov/patch

server/api/pipeline.go#L77

Added line #L77 was not covered by tests
Branch: opts.Branch,
Timestamp: time.Now().UTC().Unix(),

Expand All @@ -87,8 +87,7 @@
Author: user.Login,
Email: user.Email,

// TODO: Generate proper URL to commit
ForgeURL: repo.ForgeURL,
ForgeURL: commit.ForgeURL,

Check warning on line 90 in server/api/pipeline.go

View check run for this annotation

Codecov / codecov/patch

server/api/pipeline.go#L90

Added line #L90 was not covered by tests
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
}
18 changes: 11 additions & 7 deletions server/cron/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,23 @@ func TestCreateBuild(t *testing.T) {
// mock things
store.On("GetRepo", mock.Anything).Return(repo1, nil)
store.On("GetUser", mock.Anything).Return(creator, nil)
forge.On("BranchHead", mock.Anything, creator, repo1, "default").Return("sha1", nil)
forge.On("BranchHead", mock.Anything, creator, repo1, "default").Return(&model.Commit{
ForgeURL: "https://example.com/sha1",
SHA: "sha1",
}, nil)

_, pipeline, err := CreatePipeline(ctx, store, forge, &model.Cron{
Name: "test",
})
assert.NoError(t, err)
assert.EqualValues(t, &model.Pipeline{
Event: "cron",
Commit: "sha1",
Branch: "default",
Ref: "refs/heads/default",
Message: "test",
Sender: "test",
Branch: "default",
Commit: "sha1",
Event: "cron",
ForgeURL: "https://example.com/sha1",
Message: "test",
Ref: "refs/heads/default",
Sender: "test",
}, pipeline)
}

Expand Down
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
3 changes: 2 additions & 1 deletion server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func Test_bitbucket(t *testing.T) {
g.It("Should return the details", func() {
branchHead, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_name")
g.Assert(err).IsNil()
g.Assert(branchHead).Equal("branch_head_name")
g.Assert(branchHead.SHA).Equal("branch_head_name")
g.Assert(branchHead.ForgeURL).Equal("https://bitbucket.org/commitlink")
})
g.It("Should handle not found errors", func() {
_, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_not_found")
Expand Down
7 changes: 6 additions & 1 deletion server/forge/bitbucket/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ const branchCommitsPayload = `
{
"values": [
{
"hash": "branch_head_name"
"hash": "branch_head_name",
"links": {
"html": {
"href": "https://bitbucket.org/commitlink"
}
}
},
{
"hash": "random1"
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 @@
}

// 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) {

Check warning on line 466 in server/forge/gitea/gitea.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitea/gitea.go#L466

Added line #L466 was not covered by tests
token := common.UserToken(ctx, r, u)
client, err := c.newClientToken(ctx, token)
if err != nil {
return "", err
return nil, err

Check warning on line 470 in server/forge/gitea/gitea.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitea/gitea.go#L470

Added line #L470 was not covered by tests
}

b, _, err := client.GetRepoBranch(r.Owner, r.Name, branch)
if err != nil {
return "", err
return nil, err

Check warning on line 475 in server/forge/gitea/gitea.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitea/gitea.go#L475

Added line #L475 was not covered by tests
}
return b.Commit.ID, nil
return &model.Commit{
SHA: b.Commit.ID,
ForgeURL: b.Commit.URL,
}, nil

Check warning on line 480 in server/forge/gitea/gitea.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitea/gitea.go#L477-L480

Added lines #L477 - L480 were not covered by tests
}

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 @@
}

// 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) {

Check warning on line 568 in server/forge/github/github.go

View check run for this annotation

Codecov / codecov/patch

server/forge/github/github.go#L568

Added line #L568 was not covered by tests
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

Check warning on line 572 in server/forge/github/github.go

View check run for this annotation

Codecov / codecov/patch

server/forge/github/github.go#L572

Added line #L572 was not covered by tests
}
return b.GetCommit().GetSHA(), nil
return &model.Commit{
SHA: b.GetCommit().GetSHA(),
ForgeURL: b.GetCommit().GetHTMLURL(),
}, nil

Check warning on line 577 in server/forge/github/github.go

View check run for this annotation

Codecov / codecov/patch

server/forge/github/github.go#L574-L577

Added lines #L574 - L577 were not covered by tests
}

// 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 @@
}

// 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) {

Check warning on line 610 in server/forge/gitlab/gitlab.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitlab/gitlab.go#L610

Added line #L610 was not covered by tests
token := common.UserToken(ctx, r, u)
client, err := newClient(g.url, token, g.SkipVerify)
if err != nil {
return "", err
return nil, err

Check warning on line 614 in server/forge/gitlab/gitlab.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitlab/gitlab.go#L614

Added line #L614 was not covered by tests
}

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

Check warning on line 619 in server/forge/gitlab/gitlab.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitlab/gitlab.go#L619

Added line #L619 was not covered by tests
}

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

Check warning on line 624 in server/forge/gitlab/gitlab.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitlab/gitlab.go#L624

Added line #L624 was not covered by tests
}

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

Check warning on line 630 in server/forge/gitlab/gitlab.go

View check run for this annotation

Codecov / codecov/patch

server/forge/gitlab/gitlab.go#L627-L630

Added lines #L627 - L630 were not covered by tests
}

// Hook parses the post-commit hook from the Request body
Expand Down
14 changes: 8 additions & 6 deletions server/forge/mocks/forge.go

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

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
}
2 changes: 1 addition & 1 deletion server/store/mocks/store.go

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

Loading