diff --git a/remote/gitlab/client/project.go b/remote/gitlab/client/project.go index 49d58f109..97b661ee5 100644 --- a/remote/gitlab/client/project.go +++ b/remote/gitlab/client/project.go @@ -7,10 +7,11 @@ import ( ) const ( - searchUrl = "/projects/search/:query" - projectsUrl = "/projects" - projectUrl = "/projects/:id" - repoUrlRawFile = "/projects/:id/repository/blobs/:sha" + searchUrl = "/projects/search/:query" + projectsUrl = "/projects" + projectUrl = "/projects/:id" + repoUrlRawFile = "/projects/:id/repository/blobs/:sha" + commitStatusUrl = "/projects/:id/statuses/:sha" ) // Get a list of all projects owned by the authenticated user. @@ -90,6 +91,28 @@ func (c *Client) RepoRawFile(id, sha, filepath string) ([]byte, error) { return contents, err } +// +func (c *Client) SetStatus(id, sha, state, desc, ref, link string) error { + url, opaque := c.ResourceUrl( + commitStatusUrl, + QMap{ + ":id": id, + ":sha": sha, + }, + QMap{ + "state": state, + "ref": ref, + "target_url": link, + "description": desc, + "context": "ci/drone", + }, + ) + + println(url, opaque) + _, err := c.Do("POST", url, opaque, nil) + return err +} + // Get a list of projects by query owned by the authenticated user. func (c *Client) SearchProjectId(namespace string, name string) (id int, err error) { diff --git a/remote/gitlab/gitlab.go b/remote/gitlab/gitlab.go index e24a2532a..4b5e8a72c 100644 --- a/remote/gitlab/gitlab.go +++ b/remote/gitlab/gitlab.go @@ -229,6 +229,22 @@ func (g *Gitlab) Script(user *model.User, repo *model.Repo, build *model.Build) // also if we want get MR status in gitlab we need implement a special plugin for gitlab, // gitlab uses API to fetch build status on client side. But for now we skip this. func (g *Gitlab) Status(u *model.User, repo *model.Repo, b *model.Build, link string) error { + client := NewClient(g.URL, u.Token, g.SkipVerify) + + status := getStatus(b.Status) + desc := getDesc(b.Status) + + client.SetStatus( + ns(repo.Owner, repo.Name), + b.Commit, + status, + desc, + strings.Replace(b.Ref, "refs/heads/", "", -1), + link, + ) + + // Gitlab statuses it's a new feature, just ignore error + // if gitlab version not support this return nil } @@ -431,3 +447,57 @@ func (g *Gitlab) Scope() string { func (g *Gitlab) String() string { return "gitlab" } + +const ( + StatusPending = "pending" + StatusRunning = "running" + StatusSuccess = "success" + StatusFailure = "failed" + StatusCanceled = "canceled" +) + +const ( + DescPending = "this build is pending" + DescRunning = "this buils is running" + DescSuccess = "the build was successful" + DescFailure = "the build failed" + DescCanceled = "the build canceled" +) + +// getStatus is a helper functin that converts a Drone +// status to a GitHub status. +func getStatus(status string) string { + switch status { + case model.StatusPending: + return StatusPending + case model.StatusRunning: + return StatusRunning + case model.StatusSuccess: + return StatusSuccess + case model.StatusFailure, model.StatusError: + return StatusFailure + case model.StatusKilled: + return StatusCanceled + default: + return StatusFailure + } +} + +// getDesc is a helper function that generates a description +// message for the build based on the status. +func getDesc(status string) string { + switch status { + case model.StatusPending: + return DescPending + case model.StatusRunning: + return DescRunning + case model.StatusSuccess: + return DescSuccess + case model.StatusFailure, model.StatusError: + return DescFailure + case model.StatusKilled: + return DescCanceled + default: + return DescFailure + } +}