add label when check the approvals
This commit is contained in:
Родитель
d550b7b6f9
Коммит
6a8b1c67ce
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
26
web/hook.go
26
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{
|
||||
|
|
Загрузка…
Ссылка в новой задаче