adding check for github link

This commit is contained in:
Jeff Storey 2016-03-29 20:05:28 -04:00
parent 6ddc2abf47
commit 3e4b871991

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -25,6 +26,8 @@ const (
DefaultMergeRef = "merge" DefaultMergeRef = "merge"
) )
var githubDeployRegex = regexp.MustCompile(".+/deployments/(\\d+)")
type Github struct { type Github struct {
URL string URL string
API string API string
@ -236,10 +239,10 @@ func (g *Github) File(u *model.User, r *model.Repo, b *model.Build, f string) ([
// An example would be the GitHub pull request status. // An example would be the GitHub pull request status.
func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link string) error {
client := NewClient(g.API, u.Token, g.SkipVerify) client := NewClient(g.API, u.Token, g.SkipVerify)
if ( b.Event == "deployment") { if b.Event == "deployment" {
return deploymentStatus(client,r,b,link) return deploymentStatus(client, r, b, link)
} else { } else {
return repoStatus(client,r,b,link) return repoStatus(client, r, b, link)
} }
} }
@ -258,14 +261,19 @@ func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link strin
func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error {
// the deployment ID is only available in the the link to the build as the last element in the URL // the deployment ID is only available in the the link to the build as the last element in the URL
parts := strings.Split(b.Link,"/") parts := strings.Split(b.Link, "/")
matches := githubDeployRegex.FindStringSubmatch(b.Link)
// if the deployment was not triggered from github, don't send a deployment status
if len(matches) != 2 {
return nil
}
id, _ := strconv.Atoi(parts[len(parts)-1]) id, _ := strconv.Atoi(parts[len(parts)-1])
status := getStatus(b.Status) status := getStatus(b.Status)
desc := getDesc(b.Status) desc := getDesc(b.Status)
data := github.DeploymentStatusRequest{ data := github.DeploymentStatusRequest{
State: github.String(status), State: github.String(status),
Description: github.String(desc), Description: github.String(desc),
TargetURL: github.String(link), TargetURL: github.String(link),
} }
_, _, err := client.Repositories.CreateDeploymentStatus(r.Owner, r.Name, id, &data) _, _, err := client.Repositories.CreateDeploymentStatus(r.Owner, r.Name, id, &data)
return err return err