From 6a8b1c67cef109bb501fb70bc01eb6a28caf0efb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 5 Nov 2016 11:31:08 +0800 Subject: [PATCH] add label when check the approvals --- remote/github/github.go | 17 +++++++++++++++++ remote/mock/remote.go | 42 ++++++++++++++++++++++++++++++++++------- remote/remote.go | 15 +++++++++++++++ store/mock/store.go | 9 +++++---- web/hook.go | 26 +++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 11 deletions(-) diff --git a/remote/github/github.go b/remote/github/github.go index 5dc79e4..6fc1b4e 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -177,6 +177,23 @@ func (g *Github) GetRepos(u *model.User) ([]*model.Repo, error) { return repos, nil } +func (g *Github) RemoveIssueLabels(user *model.User, repo *model.Repo, number int, labels []string) error { + client := setupClient(g.API, user.Token) + for _, label := range labels { + _, err := client.Issues.RemoveLabelForIssue(repo.Owner, repo.Name, number, label) + if err != nil { + return err + } + } + return nil +} + +func (g *Github) AddIssueLabels(user *model.User, repo *model.Repo, number int, labels []string) error { + client := setupClient(g.API, user.Token) + _, _, err := client.Issues.AddLabelsToIssue(repo.Owner, repo.Name, number, labels) + return err +} + func (g *Github) SetHook(user *model.User, repo *model.Repo, link string) error { client := setupClient(g.API, user.Token) diff --git a/remote/mock/remote.go b/remote/mock/remote.go index 8152cb4..cd58c7f 100644 --- a/remote/mock/remote.go +++ b/remote/mock/remote.go @@ -1,17 +1,29 @@ -package mock +package mocks -import ( - "net/http" - - "github.com/lgtmco/lgtm/model" - "github.com/stretchr/testify/mock" -) +import http "net/http" +import mock "github.com/stretchr/testify/mock" +import model "github.com/lgtmco/lgtm/model" +import remote "github.com/lgtmco/lgtm/remote" // Remote is an autogenerated mock type for the Remote type type Remote struct { mock.Mock } +// AddIssueLabels provides a mock function with given fields: user, repo, number, lables +func (_m *Remote) AddIssueLabels(user *model.User, repo *model.Repo, number int, lables []string) error { + ret := _m.Called(user, repo, number, lables) + + var r0 error + if rf, ok := ret.Get(0).(func(*model.User, *model.Repo, int, []string) error); ok { + r0 = rf(user, repo, number, lables) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // DelHook provides a mock function with given fields: _a0, _a1, _a2 func (_m *Remote) DelHook(_a0 *model.User, _a1 *model.Repo, _a2 string) error { ret := _m.Called(_a0, _a1, _a2) @@ -254,6 +266,20 @@ func (_m *Remote) GetUserToken(_a0 string) (string, error) { return r0, r1 } +// RemoveIssueLabels provides a mock function with given fields: user, repo, number, labels +func (_m *Remote) RemoveIssueLabels(user *model.User, repo *model.Repo, number int, labels []string) error { + ret := _m.Called(user, repo, number, labels) + + var r0 error + if rf, ok := ret.Get(0).(func(*model.User, *model.Repo, int, []string) error); ok { + r0 = rf(user, repo, number, labels) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // SetHook provides a mock function with given fields: _a0, _a1, _a2 func (_m *Remote) SetHook(_a0 *model.User, _a1 *model.Repo, _a2 string) error { ret := _m.Called(_a0, _a1, _a2) @@ -281,3 +307,5 @@ func (_m *Remote) SetStatus(_a0 *model.User, _a1 *model.Repo, _a2 int, _a3 int, return r0 } + +var _ remote.Remote = (*Remote)(nil) diff --git a/remote/remote.go b/remote/remote.go index bbe9627..923e886 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -49,6 +49,12 @@ type Remote interface { // GetHook gets the hook from the http Request. GetHook(r *http.Request) (*model.Hook, error) + + // RemoveIssueLabels remove the labels of an issue + RemoveIssueLabels(user *model.User, repo *model.Repo, number int, labels []string) error + + // AddIssueLabels add the labels to an issue + AddIssueLabels(user *model.User, repo *model.Repo, number int, lables []string) error } // GetUser authenticates a user with the remote system. @@ -116,3 +122,12 @@ func SetStatus(c context.Context, u *model.User, r *model.Repo, num, granted, re func GetHook(c context.Context, r *http.Request) (*model.Hook, error) { return FromContext(c).GetHook(r) } + +// RemoveIssueLabels remove the labels of some issue +func RemoveIssueLabels(c context.Context, user *model.User, repo *model.Repo, number int, labels []string) error { + return FromContext(c).RemoveIssueLabels(user, repo, number, labels) +} + +func AddIssueLabels(c context.Context, user *model.User, repo *model.Repo, number int, labels []string) error { + return FromContext(c).AddIssueLabels(user, repo, number, labels) +} diff --git a/store/mock/store.go b/store/mock/store.go index 6a3d49e..7dadffc 100644 --- a/store/mock/store.go +++ b/store/mock/store.go @@ -1,9 +1,8 @@ package mocks -import ( - "github.com/lgtmco/lgtm/model" - "github.com/stretchr/testify/mock" -) +import mock "github.com/stretchr/testify/mock" +import model "github.com/lgtmco/lgtm/model" +import store "github.com/lgtmco/lgtm/store" // Store is an autogenerated mock type for the Store type type Store struct { @@ -231,3 +230,5 @@ func (_m *Store) UpdateUser(_a0 *model.User) error { return r0 } + +var _ store.Store = (*Store)(nil) diff --git a/web/hook.go b/web/hook.go index b4976ec..dca7a49 100644 --- a/web/hook.go +++ b/web/hook.go @@ -12,6 +12,10 @@ import ( "github.com/gin-gonic/gin" ) +var ( + labels = []string{"lgtm/need 2", "lgtm/need 1", "lgtm/done"} +) + func Hook(c *gin.Context) { hook, err := remote.GetHook(c, c.Request) if err != nil { @@ -78,6 +82,7 @@ func Hook(c *gin.Context) { } approvers := getApprovers(config, maintainer, hook.Issue, comments) approved := len(approvers) >= config.Approvals + err = remote.SetStatus(c, user, repo, hook.Issue.Number, len(approvers), config.Approvals) if err != nil { log.Errorf("Error setting status for %s pr %d. %s", repo.Slug, hook.Issue.Number, err) @@ -85,6 +90,27 @@ func Hook(c *gin.Context) { return } + // remove old labels + err = remote.RemoveIssueLabels(c, user, repo, hook.Issue.Number, labels) + if err != nil { + log.Errorf("Error remove old labels for %s pr %d. %s", repo.Slug, hook.Issue.Number, err) + c.String(500, "Error remove old labels. %s.", err) + return + } + + var idx = len(approvers) + if idx > 2 { + idx = 2 + } + + // add new label + err = remote.AddIssueLabels(c, user, repo, hook.Issue.Number, []string{labels[idx]}) + if err != nil { + log.Errorf("Error add new label for %s pr %d. %s", repo.Slug, hook.Issue.Number, err) + c.String(500, "Error add new label. %s.", err) + return + } + log.Debugf("processed comment for %s. received %d of %d approvals", repo.Slug, len(approvers), config.Approvals) c.IndentedJSON(200, gin.H{