just some initial work to enable bitbucket pull requests
This commit is contained in:
parent
93c78150b6
commit
df0b442d5d
2 changed files with 71 additions and 4 deletions
|
@ -360,12 +360,17 @@ func (bb *Bitbucket) Deactivate(u *model.User, r *model.Repo, link string) error
|
||||||
// and returns the required data in a standard format.
|
// and returns the required data in a standard format.
|
||||||
func (bb *Bitbucket) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
func (bb *Bitbucket) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
||||||
|
|
||||||
// only a subset of hooks are processed by drone
|
switch r.Header.Get("X-Event-Key") {
|
||||||
if r.Header.Get("X-Event-Key") != "repo:push" {
|
case "repo:push":
|
||||||
|
return bb.pushHook(r)
|
||||||
|
case "pullrequest:created", "pullrequest:updated":
|
||||||
|
return bb.pullHook(r)
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract the hook payload
|
func (bb *Bitbucket) pushHook(r *http.Request) (*model.Repo, *model.Build, error) {
|
||||||
payload := []byte(r.FormValue("payload"))
|
payload := []byte(r.FormValue("payload"))
|
||||||
if len(payload) == 0 {
|
if len(payload) == 0 {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
@ -405,3 +410,34 @@ func (bb *Bitbucket) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
||||||
|
|
||||||
return nil, nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bb *Bitbucket) pullHook(r *http.Request) (*model.Repo, *model.Build, error) {
|
||||||
|
payload := []byte(r.FormValue("payload"))
|
||||||
|
if len(payload) == 0 {
|
||||||
|
defer r.Body.Close()
|
||||||
|
payload, _ = ioutil.ReadAll(r.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
hook := PullRequestHook{}
|
||||||
|
err := json.Unmarshal(payload, &hook)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if hook.PullRequest.State != "OPEN" {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return convertRepo(&hook.Repo), &model.Build{
|
||||||
|
Event: model.EventPull,
|
||||||
|
Commit: hook.PullRequest.Dest.Commit.Hash,
|
||||||
|
Ref: fmt.Sprintf("refs/heads/%s", hook.PullRequest.Dest.Branch.Name),
|
||||||
|
Refspec: fmt.Sprintf("https://bitbucket.org/%s.git", hook.PullRequest.Source.Repo.FullName),
|
||||||
|
Remote: cloneLink(hook.PullRequest.Dest.Repo),
|
||||||
|
Link: hook.PullRequest.Links.Html.Href,
|
||||||
|
Branch: hook.PullRequest.Dest.Branch.Name,
|
||||||
|
Message: hook.PullRequest.Desc,
|
||||||
|
Avatar: hook.Actor.Links.Avatar.Href,
|
||||||
|
Author: hook.Actor.Login,
|
||||||
|
Timestamp: hook.PullRequest.Updated.UTC().Unix(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
|
@ -53,6 +53,37 @@ func convertRepo(from *Repo) *model.Repo {
|
||||||
return &repo
|
return &repo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cloneLink is a helper function that tries to extract the
|
||||||
|
// clone url from the repository object.
|
||||||
|
func cloneLink(repo Repo) string {
|
||||||
|
var clone string
|
||||||
|
|
||||||
|
// above we manually constructed the repository clone url.
|
||||||
|
// below we will iterate through the list of clone links and
|
||||||
|
// attempt to instead use the clone url provided by bitbucket.
|
||||||
|
for _, link := range repo.Links.Clone {
|
||||||
|
if link.Name == "https" {
|
||||||
|
clone = link.Href
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if no repository name is provided, we use the Html link.
|
||||||
|
// this excludes the .git suffix, but will still clone the repo.
|
||||||
|
if len(clone) == 0 {
|
||||||
|
clone = repo.Links.Html.Href
|
||||||
|
}
|
||||||
|
|
||||||
|
// if bitbucket tries to automatically populate the user
|
||||||
|
// in the url we must strip it out.
|
||||||
|
cloneurl, err := url.Parse(clone)
|
||||||
|
if err == nil {
|
||||||
|
cloneurl.User = nil
|
||||||
|
clone = cloneurl.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
||||||
// convertRepoLite is a helper function used to convert a Bitbucket
|
// convertRepoLite is a helper function used to convert a Bitbucket
|
||||||
// repository structure to the simplified Drone repository structure.
|
// repository structure to the simplified Drone repository structure.
|
||||||
func convertRepoLite(from *Repo) *model.RepoLite {
|
func convertRepoLite(from *Repo) *model.RepoLite {
|
||||||
|
|
Loading…
Reference in a new issue