Use package's structs and fix missing clone (#1172)

Closes #1169

Replaces structs that were added inline in hook structs with structs of
the corresponding SDKs. This makes it more readable and error-proof.
This commit is contained in:
qwerty287 2022-09-07 19:16:40 +02:00 committed by GitHub
parent 415903f7e8
commit 6433dfea23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 161 additions and 422 deletions

View file

@ -75,7 +75,9 @@ func ParseRepo(str string) (user, repo string, err error) {
// Update updates the repository with values from the given Repo.
func (r *Repo) Update(from *Repo) {
r.RemoteID = from.RemoteID
if from.RemoteID.IsValid() {
r.RemoteID = from.RemoteID
}
r.Owner = from.Owner
r.Name = from.Name
r.FullName = from.FullName

View file

@ -86,7 +86,9 @@ func Test_helper(t *testing.T) {
change.Changesets.Values = append(change.Changesets.Values, value)
change.Repository.Project.Key = "octocat"
change.Repository.Project = internal.Project{
Key: "octocat",
}
change.Repository.Slug = "hello-world"
build := convertPushHook(&change, "http://base.com")
g.Assert(build.Branch).Equal("")

View file

@ -55,23 +55,25 @@ type Repo struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Project struct {
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []SelfRefLink `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
Name string `json:"name"`
Project Project `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
}
type Project struct {
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []SelfRefLink `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
}
type Repos struct {
@ -157,24 +159,7 @@ type PostHook struct {
Values []Value `json:"values"`
} `json:"changesets"`
RefChanges []RefChange `json:"refChanges"`
Repository struct {
Forkable bool `json:"forkable"`
ID int `json:"id"`
Name string `json:"name"`
Project struct {
ID int `json:"id"`
IsPersonal bool `json:"isPersonal"`
Key string `json:"key"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
} `json:"repository"`
Repository Repo `json:"repository"`
}
type RefChange struct {

View file

@ -16,7 +16,6 @@ package bitbucketserver
import (
"encoding/json"
"fmt"
"net/http"
"github.com/woodpecker-ci/woodpecker/server/model"
@ -31,14 +30,7 @@ func parseHook(r *http.Request, baseURL string) (*model.Repo, *model.Build, erro
return nil, nil, err
}
build := convertPushHook(hook, baseURL)
repo := &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(hook.Repository.ID)),
Name: hook.Repository.Slug,
Owner: hook.Repository.Project.Key,
FullName: fmt.Sprintf("%s/%s", hook.Repository.Project.Key, hook.Repository.Slug),
Branch: "master",
SCMKind: model.RepoGit,
}
repo := convertRepo(&hook.Repository)
return repo, build, nil
}

View file

@ -142,6 +142,7 @@ func convertRepository(repo *Repository) (*model.Repo, error) {
Name: repo.Name,
FullName: projectFullName(repo.Owner.GlobalKey, repo.Name),
Link: repo.WebURL,
Clone: repo.HTTPSURL,
SCMKind: model.RepoGit,
}, nil
}

View file

@ -43,6 +43,7 @@ func Test_hook(t *testing.T) {
Name: "test1",
FullName: "demo1/test1",
Link: "https://coding.net/u/demo1/p/test1",
Clone: "https://git.coding.net/demo1/test1.git",
SCMKind: model.RepoGit,
}
@ -96,6 +97,7 @@ func Test_hook(t *testing.T) {
Name: "test_project",
FullName: "kelvin/test_project",
Link: "https://coding.net/u/kelvin/p/test_project",
Clone: "https://git.coding.net/kelvin/test_project.git",
SCMKind: model.RepoGit,
}
actual, err := convertRepository(repository)
@ -109,6 +111,7 @@ func Test_hook(t *testing.T) {
Name: "test1",
FullName: "demo1/test1",
Link: "https://coding.net/u/demo1/p/test1",
Clone: "https://git.coding.net/demo1/test1.git",
SCMKind: model.RepoGit,
}
@ -144,6 +147,7 @@ func Test_hook(t *testing.T) {
Name: "test2",
FullName: "demo1/test2",
Link: "https://coding.net/u/demo1/p/test2",
Clone: "https://git.coding.net/demo1/test2.git",
SCMKind: model.RepoGit,
}
@ -173,6 +177,7 @@ func Test_hook(t *testing.T) {
Name: "test1",
FullName: "demo1/test1",
Link: "https://coding.net/u/demo1/p/test1",
Clone: "https://git.coding.net/demo1/test1.git",
SCMKind: model.RepoGit,
}

View file

@ -49,6 +49,7 @@ const HookPush = `
"owner": {
"name": "gordon",
"email": "gordon@golang.org",
"login": "gordon",
"username": "gordon"
},
"private": true
@ -229,6 +230,7 @@ const HookPushTag = `{
"owner": {
"id": 1,
"username": "gordon",
"login": "gordon",
"full_name": "Gordon the Gopher",
"email": "gordon@golang.org",
"avatar_url": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
@ -248,6 +250,7 @@ const HookPushTag = `{
"sender": {
"id": 1,
"username": "gordon",
"login": "gordon",
"full_name": "Gordon the Gopher",
"email": "gordon@golang.org",
"avatar_url": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
@ -266,6 +269,7 @@ const HookPullRequest = `{
"user": {
"id": 1,
"username": "gordon",
"login": "gordon",
"full_name": "Gordon the Gopher",
"email": "gordon@golang.org",
"avatar_url": "http://gitea.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"
@ -288,6 +292,7 @@ const HookPullRequest = `{
"owner": {
"id": 1,
"username": "gordon",
"login": "gordon",
"full_name": "Gordon the Gopher",
"email": "gordon@golang.org",
"avatar_url": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87"

View file

@ -69,17 +69,9 @@ func toTeam(from *gitea.Organization, link string) *model.Team {
// helper function that extracts the Build data from a Gitea push hook
func buildFromPush(hook *pushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.Sender.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.Sender.AvatarURL),
)
author := hook.Sender.Login
if author == "" {
author = hook.Sender.Username
}
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
message := ""
link := hook.Compare
@ -101,10 +93,10 @@ func buildFromPush(hook *pushHook) *model.Build {
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
Message: message,
Avatar: avatar,
Author: author,
Author: hook.Sender.UserName,
Email: hook.Sender.Email,
Timestamp: time.Now().UTC().Unix(),
Sender: sender,
Sender: hook.Sender.UserName,
ChangedFiles: getChangedFilesFromPushHook(hook),
}
}
@ -128,28 +120,20 @@ func getChangedFilesFromPushHook(hook *pushHook) []string {
// helper function that extracts the Build data from a Gitea tag hook
func buildFromTag(hook *pushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.Sender.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.Sender.AvatarURL),
)
author := hook.Sender.Login
if author == "" {
author = hook.Sender.Username
}
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
return &model.Build{
Event: model.EventTag,
Commit: hook.Sha,
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
Link: fmt.Sprintf("%s/src/tag/%s", hook.Repo.URL, hook.Ref),
Link: fmt.Sprintf("%s/src/tag/%s", hook.Repo.HTMLURL, hook.Ref),
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
Message: fmt.Sprintf("created tag %s", hook.Ref),
Avatar: avatar,
Author: author,
Sender: sender,
Author: hook.Sender.UserName,
Sender: hook.Sender.UserName,
Timestamp: time.Now().UTC().Unix(),
}
}
@ -157,13 +141,9 @@ func buildFromTag(hook *pushHook) *model.Build {
// helper function that extracts the Build data from a Gitea pull_request hook
func buildFromPullRequest(hook *pullRequestHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.PullRequest.User.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.PullRequest.Poster.AvatarURL),
)
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
build := &model.Build{
Event: model.EventPull,
Commit: hook.PullRequest.Head.Sha,
@ -171,9 +151,9 @@ func buildFromPullRequest(hook *pullRequestHook) *model.Build {
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
Branch: hook.PullRequest.Base.Ref,
Message: hook.PullRequest.Title,
Author: hook.PullRequest.User.Username,
Author: hook.PullRequest.Poster.UserName,
Avatar: avatar,
Sender: sender,
Sender: hook.Sender.UserName,
Title: hook.PullRequest.Title,
Refspec: fmt.Sprintf("%s:%s",
hook.PullRequest.Head.Ref,
@ -183,28 +163,6 @@ func buildFromPullRequest(hook *pullRequestHook) *model.Build {
return build
}
// helper function that extracts the Repository data from a Gitea push hook
func repoFromPush(hook *pushHook) *model.Repo {
return &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(hook.Repo.ID)),
Name: hook.Repo.Name,
Owner: hook.Repo.Owner.Username,
FullName: hook.Repo.FullName,
Link: hook.Repo.URL,
}
}
// helper function that extracts the Repository data from a Gitea pull_request hook
func repoFromPullRequest(hook *pullRequestHook) *model.Repo {
return &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(hook.Repo.ID)),
Name: hook.Repo.Name,
Owner: hook.Repo.Owner.Username,
FullName: hook.Repo.FullName,
Link: hook.Repo.URL,
}
}
// helper function that parses a push hook from a read closer.
func parsePush(r io.Reader) (*pushHook, error) {
push := new(pushHook)

View file

@ -38,19 +38,15 @@ func Test_parse(t *testing.T) {
g.Assert(hook.Before).Equal("4b2626259b5a97b6b4eab5e6cca66adb986b672b")
g.Assert(hook.Compare).Equal("http://gitea.golang.org/gordon/hello-world/compare/4b2626259b5a97b6b4eab5e6cca66adb986b672b...ef98532add3b2feb7a137426bba1248724367df5")
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.Owner.Name).Equal("gordon")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Pusher.Name).Equal("gordon")
g.Assert(hook.Pusher.Email).Equal("gordon@golang.org")
g.Assert(hook.Pusher.Username).Equal("gordon")
g.Assert(hook.Pusher.Login).Equal("gordon")
g.Assert(hook.Sender.Login).Equal("gordon")
g.Assert(hook.Sender.Username).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("http://gitea.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Pusher.UserName).Equal("gordon")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("http://gitea.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
})
g.It("Should parse tag hook payload", func() {
@ -60,13 +56,13 @@ func Test_parse(t *testing.T) {
g.Assert(hook.Ref).Equal("v1.0.0")
g.Assert(hook.Sha).Equal("ef98532add3b2feb7a137426bba1248724367df5")
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.Username).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
})
g.It("Should parse pull_request hook payload", func() {
@ -77,21 +73,21 @@ func Test_parse(t *testing.T) {
g.Assert(hook.Number).Equal(int64(1))
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gitea.golang.org/gordon/hello-world")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.Username).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarURL).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.PullRequest.Title).Equal("Update the README with new information")
g.Assert(hook.PullRequest.Body).Equal("please merge")
g.Assert(hook.PullRequest.State).Equal("open")
g.Assert(hook.PullRequest.User.Username).Equal("gordon")
g.Assert(hook.PullRequest.Base.Label).Equal("master")
g.Assert(hook.PullRequest.State).Equal(gitea.StateOpen)
g.Assert(hook.PullRequest.Poster.UserName).Equal("gordon")
g.Assert(hook.PullRequest.Base.Name).Equal("master")
g.Assert(hook.PullRequest.Base.Ref).Equal("master")
g.Assert(hook.PullRequest.Head.Label).Equal("feature/changes")
g.Assert(hook.PullRequest.Head.Name).Equal("feature/changes")
g.Assert(hook.PullRequest.Head.Ref).Equal("feature/changes")
})
@ -106,18 +102,18 @@ func Test_parse(t *testing.T) {
g.Assert(build.Branch).Equal("master")
g.Assert(build.Message).Equal(hook.Commits[0].Message)
g.Assert(build.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(build.Author).Equal(hook.Sender.Login)
g.Assert(build.Author).Equal(hook.Sender.UserName)
g.Assert(utils.EqualStringSlice(build.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
})
g.It("Should return a Repo struct from a push hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
hook, _ := parsePush(buf)
repo := repoFromPush(hook)
repo := toRepo(hook.Repo)
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.Username)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.URL)
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
})
g.It("Should return a Build struct from a tag hook", func() {
@ -144,17 +140,17 @@ func Test_parse(t *testing.T) {
g.Assert(build.Refspec).Equal("feature/changes:master")
g.Assert(build.Message).Equal(hook.PullRequest.Title)
g.Assert(build.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(build.Author).Equal(hook.PullRequest.User.Username)
g.Assert(build.Author).Equal(hook.PullRequest.Poster.UserName)
})
g.It("Should return a Repo struct from a pull_request hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
hook, _ := parsePullRequest(buf)
repo := repoFromPullRequest(hook)
repo := toRepo(hook.Repo)
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.Username)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.URL)
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
})
g.It("Should return a Perm struct from a Gitea Perm", func() {

View file

@ -69,7 +69,7 @@ func parsePushHook(payload io.Reader) (repo *model.Repo, build *model.Build, err
return nil, nil, nil
}
repo = repoFromPush(push)
repo = toRepo(push.Repo)
build = buildFromPush(push)
return repo, build, err
}
@ -86,7 +86,7 @@ func parseCreatedHook(payload io.Reader) (repo *model.Repo, build *model.Build,
return nil, nil, nil
}
repo = repoFromPush(push)
repo = toRepo(push.Repo)
build = buildFromTag(push)
return repo, build, nil
}
@ -111,7 +111,7 @@ func parsePullRequestHook(payload io.Reader) (*model.Repo, *model.Build, error)
return nil, nil, nil
}
repo = repoFromPullRequest(pr)
repo = toRepo(pr.Repo)
build = buildFromPullRequest(pr)
return repo, build, err
}

View file

@ -14,14 +14,7 @@
package gitea
type commit struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Added []string `json:"added"`
Removed []string `json:"removed"`
Modified []string `json:"modified"`
}
import "code.gitea.io/sdk/gitea"
type pushHook struct {
Sha string `json:"sha"`
@ -31,117 +24,21 @@ type pushHook struct {
Compare string `json:"compare_url"`
RefType string `json:"ref_type"`
Pusher struct {
Name string `json:"name"`
Email string `json:"email"`
Login string `json:"login"`
Username string `json:"username"`
} `json:"pusher"`
Pusher *gitea.User `json:"pusher"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"owner"`
} `json:"repository"`
Repo *gitea.Repository `json:"repository"`
Commits []commit `json:"commits"`
Commits []gitea.PayloadCommit `json:"commits"`
HeadCommit commit `json:"head_commit"`
HeadCommit gitea.PayloadCommit `json:"head_commit"`
Sender struct {
ID int64 `json:"id"`
Login string `json:"login"`
Username string `json:"username"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"sender"`
Sender *gitea.User `json:"sender"`
}
type pullRequestHook struct {
Action string `json:"action"`
Number int64 `json:"number"`
PullRequest struct {
ID int64 `json:"id"`
User struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
URL string `json:"html_url"`
Mergeable bool `json:"mergeable"`
Merged bool `json:"merged"`
MergeBase string `json:"merge_base"`
Base struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repo"`
} `json:"base"`
Head struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repo"`
} `json:"head"`
} `json:"pull_request"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repository"`
Sender struct {
ID int64 `json:"id"`
Login string `json:"login"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"sender"`
Action string `json:"action"`
Number int64 `json:"number"`
PullRequest *gitea.PullRequest `json:"pull_request"`
Repo *gitea.Repository `json:"repository"`
Sender *gitea.User `json:"sender"`
}

View file

@ -300,7 +300,7 @@ func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, b
// Hook parses the incoming Gogs hook and returns the Repository and Build
// details. If the hook is unsupported nil values are returned.
func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error) {
return parseHook(r)
return parseHook(r, c.PrivateMode)
}
// OrgMembership returns if user is member of organization and if user

View file

@ -34,10 +34,6 @@ func toRepo(from *gogs.Repository, privateMode bool) *model.Repo {
from.HTMLURL,
from.Owner.AvatarUrl,
)
private := from.Private
if privateMode {
private = true
}
return &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(from.ID)),
SCMKind: model.RepoGit,
@ -46,7 +42,7 @@ func toRepo(from *gogs.Repository, privateMode bool) *model.Repo {
FullName: from.FullName,
Avatar: avatar,
Link: from.HTMLURL,
IsSCMPrivate: private,
IsSCMPrivate: from.Private || privateMode,
Clone: from.CloneURL,
Branch: from.DefaultBranch,
}
@ -72,14 +68,14 @@ func toTeam(from *gogs.Organization, link string) *model.Team {
// helper function that extracts the Build data from a Gogs push hook
func buildFromPush(hook *pushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.Sender.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.Sender.AvatarUrl),
)
author := hook.Sender.Login
if author == "" {
author = hook.Sender.Username
author = hook.Sender.UserName
}
sender := hook.Sender.Username
sender := hook.Sender.UserName
if sender == "" {
sender = hook.Sender.Login
}
@ -102,14 +98,14 @@ func buildFromPush(hook *pushHook) *model.Build {
// helper function that extracts the Build data from a Gogs tag hook
func buildFromTag(hook *pushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.Sender.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.Sender.AvatarUrl),
)
author := hook.Sender.Login
if author == "" {
author = hook.Sender.Username
author = hook.Sender.UserName
}
sender := hook.Sender.Username
sender := hook.Sender.UserName
if sender == "" {
sender = hook.Sender.Login
}
@ -118,7 +114,7 @@ func buildFromTag(hook *pushHook) *model.Build {
Event: model.EventTag,
Commit: hook.After,
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
Link: fmt.Sprintf("%s/src/%s", hook.Repo.URL, hook.Ref),
Link: fmt.Sprintf("%s/src/%s", hook.Repo.HTMLURL, hook.Ref),
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
Message: fmt.Sprintf("created tag %s", hook.Ref),
Avatar: avatar,
@ -131,10 +127,10 @@ func buildFromTag(hook *pushHook) *model.Build {
// helper function that extracts the Build data from a Gogs pull_request hook
func buildFromPullRequest(hook *pullRequestHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.PullRequest.User.Avatar),
hook.Repo.HTMLURL,
fixMalformedAvatar(hook.PullRequest.User.AvatarUrl),
)
sender := hook.Sender.Username
sender := hook.Sender.UserName
if sender == "" {
sender = hook.Sender.Login
}
@ -145,7 +141,7 @@ func buildFromPullRequest(hook *pullRequestHook) *model.Build {
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
Branch: hook.PullRequest.BaseBranch,
Message: hook.PullRequest.Title,
Author: hook.PullRequest.User.Username,
Author: hook.PullRequest.User.UserName,
Avatar: avatar,
Sender: sender,
Title: hook.PullRequest.Title,
@ -157,28 +153,6 @@ func buildFromPullRequest(hook *pullRequestHook) *model.Build {
return build
}
// helper function that extracts the Repository data from a Gogs push hook
func repoFromPush(hook *pushHook) *model.Repo {
return &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(hook.Repo.ID)),
Name: hook.Repo.Name,
Owner: hook.Repo.Owner.Username,
FullName: hook.Repo.FullName,
Link: hook.Repo.URL,
}
}
// helper function that extracts the Repository data from a Gogs pull_request hook
func repoFromPullRequest(hook *pullRequestHook) *model.Repo {
return &model.Repo{
RemoteID: model.RemoteID(fmt.Sprint(hook.Repo.ID)),
Name: hook.Repo.Name,
Owner: hook.Repo.Owner.Username,
FullName: hook.Repo.FullName,
Link: hook.Repo.URL,
}
}
// helper function that parses a push hook from a read closer.
func parsePush(r io.Reader) (*pushHook, error) {
push := new(pushHook)

View file

@ -37,17 +37,15 @@ func Test_parse(t *testing.T) {
g.Assert(hook.Before).Equal("4b2626259b5a97b6b4eab5e6cca66adb986b672b")
g.Assert(hook.Compare).Equal("http://gogs.golang.org/gordon/hello-world/compare/4b2626259b5a97b6b4eab5e6cca66adb986b672b...ef98532add3b2feb7a137426bba1248724367df5")
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.Owner.Name).Equal("gordon")
g.Assert(hook.Repo.HTMLURL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Pusher.Name).Equal("gordon")
g.Assert(hook.Pusher.Email).Equal("gordon@golang.org")
g.Assert(hook.Pusher.Username).Equal("gordon")
g.Assert(hook.Pusher.UserName).Equal("gordon")
g.Assert(hook.Sender.Login).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("http://gogs.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Sender.AvatarUrl).Equal("http://gogs.golang.org///1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
})
g.It("Should parse tag hook payload", func() {
@ -56,13 +54,13 @@ func Test_parse(t *testing.T) {
g.Assert(err).IsNil()
g.Assert(hook.Ref).Equal("v1.0.0")
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.Username).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarUrl).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
})
g.It("Should parse pull_request hook payload", func() {
@ -73,18 +71,18 @@ func Test_parse(t *testing.T) {
g.Assert(hook.Number).Equal(int64(1))
g.Assert(hook.Repo.Name).Equal("hello-world")
g.Assert(hook.Repo.URL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.HTMLURL).Equal("http://gogs.golang.org/gordon/hello-world")
g.Assert(hook.Repo.FullName).Equal("gordon/hello-world")
g.Assert(hook.Repo.Owner.Email).Equal("gordon@golang.org")
g.Assert(hook.Repo.Owner.Username).Equal("gordon")
g.Assert(hook.Repo.Owner.UserName).Equal("gordon")
g.Assert(hook.Repo.Private).Equal(true)
g.Assert(hook.Sender.Username).Equal("gordon")
g.Assert(hook.Sender.Avatar).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.Sender.UserName).Equal("gordon")
g.Assert(hook.Sender.AvatarUrl).Equal("https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(hook.PullRequest.Title).Equal("Update the README with new information")
g.Assert(hook.PullRequest.Body).Equal("please merge")
g.Assert(hook.PullRequest.State).Equal("open")
g.Assert(hook.PullRequest.User.Username).Equal("gordon")
g.Assert(hook.PullRequest.User.UserName).Equal("gordon")
g.Assert(hook.PullRequest.Base.Label).Equal("master")
g.Assert(hook.PullRequest.Base.Ref).Equal("master")
g.Assert(hook.PullRequest.Head.Label).Equal("feature/changes")
@ -108,11 +106,11 @@ func Test_parse(t *testing.T) {
g.It("Should return a Repo struct from a push hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
hook, _ := parsePush(buf)
repo := repoFromPush(hook)
repo := toRepo(hook.Repo, false)
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.Username)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.URL)
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
})
g.It("Should return a Build struct from a pull_request hook", func() {
@ -126,17 +124,17 @@ func Test_parse(t *testing.T) {
g.Assert(build.Branch).Equal("master")
g.Assert(build.Message).Equal(hook.PullRequest.Title)
g.Assert(build.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
g.Assert(build.Author).Equal(hook.PullRequest.User.Username)
g.Assert(build.Author).Equal(hook.PullRequest.User.UserName)
})
g.It("Should return a Repo struct from a pull_request hook", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequest)
hook, _ := parsePullRequest(buf)
repo := repoFromPullRequest(hook)
repo := toRepo(hook.Repo, false)
g.Assert(repo.Name).Equal(hook.Repo.Name)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.Username)
g.Assert(repo.Owner).Equal(hook.Repo.Owner.UserName)
g.Assert(repo.FullName).Equal("gordon/hello-world")
g.Assert(repo.Link).Equal(hook.Repo.URL)
g.Assert(repo.Link).Equal(hook.Repo.HTMLURL)
})
g.It("Should return a Perm struct from a Gogs Perm", func() {

View file

@ -38,21 +38,21 @@ const (
// parseHook parses a Bitbucket hook from an http.Request request and returns
// Repo and Build detail. If a hook type is unsupported nil values are returned.
func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
func parseHook(r *http.Request, privateMode bool) (*model.Repo, *model.Build, error) {
switch r.Header.Get(hookEvent) {
case hookPush:
return parsePushHook(r.Body)
return parsePushHook(r.Body, privateMode)
case hookCreated:
return parseCreatedHook(r.Body)
return parseCreatedHook(r.Body, privateMode)
case hookPullRequest:
return parsePullRequestHook(r.Body)
return parsePullRequestHook(r.Body, privateMode)
}
return nil, nil, nil
}
// parsePushHook parses a push hook and returns the Repo and Build details.
// If the commit type is unsupported nil values are returned.
func parsePushHook(payload io.Reader) (*model.Repo, *model.Build, error) {
func parsePushHook(payload io.Reader, privateMode bool) (*model.Repo, *model.Build, error) {
var (
repo *model.Repo
build *model.Build
@ -68,14 +68,14 @@ func parsePushHook(payload io.Reader) (*model.Repo, *model.Build, error) {
return nil, nil, nil
}
repo = repoFromPush(push)
repo = toRepo(push.Repo, privateMode)
build = buildFromPush(push)
return repo, build, err
}
// parseCreatedHook parses a push hook and returns the Repo and Build details.
// If the commit type is unsupported nil values are returned.
func parseCreatedHook(payload io.Reader) (*model.Repo, *model.Build, error) {
func parseCreatedHook(payload io.Reader, privateMode bool) (*model.Repo, *model.Build, error) {
var (
repo *model.Repo
build *model.Build
@ -90,13 +90,13 @@ func parseCreatedHook(payload io.Reader) (*model.Repo, *model.Build, error) {
return nil, nil, nil
}
repo = repoFromPush(push)
repo = toRepo(push.Repo, privateMode)
build = buildFromTag(push)
return repo, build, err
}
// parsePullRequestHook parses a pull_request hook and returns the Repo and Build details.
func parsePullRequestHook(payload io.Reader) (*model.Repo, *model.Build, error) {
func parsePullRequestHook(payload io.Reader, privateMode bool) (*model.Repo, *model.Build, error) {
var (
repo *model.Repo
build *model.Build
@ -115,7 +115,7 @@ func parsePullRequestHook(payload io.Reader) (*model.Repo, *model.Build, error)
return nil, nil, nil
}
repo = repoFromPullRequest(pr)
repo = toRepo(pr.Repo, privateMode)
build = buildFromPullRequest(pr)
return repo, build, err
}

View file

@ -14,6 +14,8 @@
package gogs
import "github.com/gogits/go-gogs-client"
type pushHook struct {
Ref string `json:"ref"`
Before string `json:"before"`
@ -21,121 +23,43 @@ type pushHook struct {
Compare string `json:"compare_url"`
RefType string `json:"ref_type"`
Pusher struct {
Name string `json:"name"`
Email string `json:"email"`
Login string `json:"login"`
Username string `json:"username"`
} `json:"pusher"`
Pusher *gogs.User `json:"pusher"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"owner"`
} `json:"repository"`
Repo *gogs.Repository `json:"repository"`
Commits []struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
} `json:"commits"`
Commits []gogs.PayloadCommit `json:"commits"`
Sender struct {
ID int64 `json:"id"`
Login string `json:"login"`
Username string `json:"username"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"sender"`
Sender *gogs.User `json:"sender"`
}
type pullRequestHook struct {
Action string `json:"action"`
Number int64 `json:"number"`
PullRequest struct {
ID int64 `json:"id"`
User struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
URL string `json:"html_url"`
Mergeable bool `json:"mergeable"`
Merged bool `json:"merged"`
MergeBase string `json:"merge_base"`
BaseBranch string `json:"base_branch"`
ID int64 `json:"id"`
User *gogs.User `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
URL string `json:"html_url"`
Mergeable bool `json:"mergeable"`
Merged bool `json:"merged"`
MergeBase string `json:"merge_base"`
BaseBranch string `json:"base_branch"`
Base struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repo"`
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo *gogs.Repository `json:"repo"`
} `json:"base"`
HeadBranch string `json:"head_branch"`
Head struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repo"`
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Repo *gogs.Repository `json:"repo"`
} `json:"head"`
} `json:"pull_request"`
Repo struct {
ID int64 `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
URL string `json:"html_url"`
Private bool `json:"private"`
Owner struct {
ID int64 `json:"id"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"owner"`
} `json:"repository"`
Sender struct {
ID int64 `json:"id"`
Login string `json:"login"`
Username string `json:"username"`
Name string `json:"full_name"`
Email string `json:"email"`
Avatar string `json:"avatar_url"`
} `json:"sender"`
Repo *gogs.Repository `json:"repository"`
Sender *gogs.User `json:"sender"`
}