diff --git a/remote/github/github.go b/remote/github/github.go index fe7244bb..9ace2477 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "strconv" "strings" @@ -25,6 +26,8 @@ const ( DefaultMergeRef = "merge" ) +var githubDeployRegex = regexp.MustCompile(".+/deployments/(\\d+)") + type Github struct { URL string API string @@ -236,7 +239,14 @@ func (g *Github) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ // 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 { client := NewClient(g.API, u.Token, g.SkipVerify) + if b.Event == "deployment" { + return deploymentStatus(client, r, b, link) + } else { + return repoStatus(client, r, b, link) + } +} +func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { status := getStatus(b.Status) desc := getDesc(b.Status) data := github.RepoStatus{ @@ -249,6 +259,25 @@ func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link strin return err } +func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { + 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 + } + // the deployment ID is only available in the the link to the build as the last element in the URL + id, _ := strconv.Atoi(matches[1]) + status := getStatus(b.Status) + desc := getDesc(b.Status) + data := github.DeploymentStatusRequest{ + State: github.String(status), + Description: github.String(desc), + TargetURL: github.String(link), + } + _, _, err := client.Repositories.CreateDeploymentStatus(r.Owner, r.Name, id, &data) + return err +} + // Netrc returns a .netrc file that can be used to clone // private repositories from a remote system. func (g *Github) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) { diff --git a/static/scripts/repo.js b/static/scripts/repo.js index 9f89105c..582e8d1d 100644 --- a/static/scripts/repo.js +++ b/static/scripts/repo.js @@ -19,6 +19,13 @@ function RepoViewModel(repo) { // construct the build entry if it doesn't already exist // so that we can append to the DOM. The code may not be // pretty, but it is simple enough and it works. + var authoredOrDeployed = "authored" + var branchOrDeploy = data.branch + if ( data.event == "deployment" ) { + authoredOrDeployed = "deployed" + branchOrDeploy = data.deploy_to + } + el = $("").attr("class", "card").attr("href", "/"+repo+"/"+data.number).attr("data-build", data.number) .append( $("
").attr("class", "card-header").append( @@ -36,13 +43,13 @@ function RepoViewModel(repo) { $("

").attr("class","card-text").append( $("").text(data.author) ).append( - $("").text("authored") + $("").text(authoredOrDeployed) ).append( $("").attr("data-livestamp", data.created_at) ).append( $("").text("to") ).append( - $("").text(data.branch) + $("").text(branchOrDeploy) ) ) ).css("display", "flex").hide().fadeIn(1000); diff --git a/template/amber/build.amber b/template/amber/build.amber index 3d8cbc8d..55a117c8 100644 --- a/template/amber/build.amber +++ b/template/amber/build.amber @@ -26,10 +26,16 @@ block content a.material-icons[href=Build.Link][target="_blank"] link p em #{Build.Author} - span authored + if Build.Event != "deployment" + span authored + else + span deployed em[data-livestamp=Build.Created] span to - em #{Build.Branch} + if Build.Event != "deployment" + em #{Build.Branch} + else + em #{Build.Deploy} diff --git a/template/amber/repo.amber b/template/amber/repo.amber index d7358c69..665226ba 100644 --- a/template/amber/repo.amber +++ b/template/amber/repo.amber @@ -42,10 +42,16 @@ block content h3 #{$build.Message} p.card-text em #{$build.Author} - span authored + if $build.Event != "deployment" + span authored + else + span deployed em[data-livestamp=$build.Created] span to - em #{$build.Branch} + if $build.Event != "deployment" + em #{$build.Branch} + else + em #{$build.Deploy} block append scripts