Skip to content

Commit f5fc5d0

Browse files
committed
[#4244] Feature/GitLab MR Comments
- Handle author and assocaited commits Signed-off-by: Harold Wanyama <[email protected]>
1 parent c515e62 commit f5fc5d0

10 files changed

Lines changed: 484 additions & 77 deletions

File tree

cla-backend-go/gitlab_api/client.go

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ import (
1919
goGitLab "github.com/xanzy/go-gitlab"
2020
)
2121

22+
type GitLabClient interface {
23+
GetMergeRequestCommits(projectID int, mergeID int, opts *goGitLab.GetMergeRequestCommitsOptions) ([]*goGitLab.Commit, error)
24+
ListUsers(opts *goGitLab.ListUsersOptions) ([]*goGitLab.User, error)
25+
SetCommitStatus(projectID int, commitSHA string, opts *goGitLab.SetCommitStatusOptions) error
26+
GetProject(gitLabProjectID int, opts *goGitLab.GetProjectOptions) (*goGitLab.Project, error)
27+
// EditProject(projectID int,)
28+
ListGroupProjects(groupID int, opts *goGitLab.ListGroupProjectsOptions) ([]*goGitLab.Project, *goGitLab.Response, error)
29+
ListGroupMembers(gid interface{}, opt *goGitLab.ListGroupMembersOptions) ([]*goGitLab.GroupMember, *goGitLab.Response, error)
30+
CurrentUser() (*goGitLab.User, *goGitLab.Response, error)
31+
ListUserProjects(user interface{}, opt *goGitLab.ListProjectsOptions) ([]*goGitLab.Project, *goGitLab.Response, error)
32+
DeleteProjectHook(projectID, webhookID int) (*goGitLab.Response, error)
33+
ListProjectHooks(projectID int, opts *goGitLab.ListProjectHooksOptions) ([]*goGitLab.ProjectHook, *goGitLab.Response, error)
34+
AddProjectHook(projectID int, opts *goGitLab.AddProjectHookOptions) (*goGitLab.ProjectHook, *goGitLab.Response, error)
35+
EditProjectHook(projectID, existingID int, opts *goGitLab.EditProjectHookOptions) (*goGitLab.ProjectHook, *goGitLab.Response, error)
36+
}
37+
38+
// Client is the gitlab client
39+
type GitLabClientWrapper struct {
40+
gitlabClient *goGitLab.Client
41+
}
42+
2243
// OauthSuccessResponse is success response from Gitlab
2344
type OauthSuccessResponse struct {
2445
AccessToken string `json:"access_token"`
@@ -29,7 +50,7 @@ type OauthSuccessResponse struct {
2950
}
3051

3152
// NewGitlabOauthClient creates a new gitlab client from the given oauth info, authInfo is encrypted
32-
func NewGitlabOauthClient(authInfo string, gitLabApp *App) (*goGitLab.Client, error) {
53+
func NewGitlabOauthClient(authInfo string, gitLabApp *App) (GitLabClient, error) {
3354
if authInfo == "" {
3455
return nil, errors.New("unable to decrypt auth info - authentication info input is nil")
3556
}
@@ -47,7 +68,14 @@ func NewGitlabOauthClient(authInfo string, gitLabApp *App) (*goGitLab.Client, er
4768
}
4869

4970
log.Infof("creating oauth client with access token : %s", oauthResp.AccessToken)
50-
return goGitLab.NewOAuthClient(oauthResp.AccessToken)
71+
client, err := goGitLab.NewOAuthClient(oauthResp.AccessToken)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
return &GitLabClientWrapper{
77+
gitlabClient: client,
78+
}, nil
5179
}
5280

5381
// NewGitlabOauthClientFromAccessToken creates a new gitlab client from the given access token
@@ -154,3 +182,70 @@ func decrypt(key, cipherText []byte) ([]byte, error) {
154182

155183
return cipherText, nil
156184
}
185+
186+
func (c *GitLabClientWrapper) ListGroupMembers(gid interface{}, opt *goGitLab.ListGroupMembersOptions) ([]*goGitLab.GroupMember, *goGitLab.Response, error) {
187+
return c.gitlabClient.Groups.ListGroupMembers(gid, opt)
188+
}
189+
190+
func (c *GitLabClientWrapper) GetProject(gitLabProjectID int, opts *goGitLab.GetProjectOptions) (*goGitLab.Project, error) {
191+
project, _, err := c.gitlabClient.Projects.GetProject(gitLabProjectID, opts)
192+
if err != nil {
193+
return nil, err
194+
}
195+
return project, err
196+
}
197+
198+
func (c *GitLabClientWrapper) CurrentUser() (*goGitLab.User, *goGitLab.Response, error) {
199+
return c.gitlabClient.Users.CurrentUser()
200+
}
201+
202+
// GetMergeRequestCommits returns the commits for the given merge request
203+
func (c *GitLabClientWrapper) GetMergeRequestCommits(projectID int, mergeID int, opts *goGitLab.GetMergeRequestCommitsOptions) ([]*goGitLab.Commit, error) {
204+
commits, _, err := c.gitlabClient.MergeRequests.GetMergeRequestCommits(projectID, mergeID, opts)
205+
if err != nil {
206+
return nil, err
207+
}
208+
return commits, nil
209+
}
210+
211+
// ListUsers returns the list of users
212+
func (c *GitLabClientWrapper) ListUsers(opts *goGitLab.ListUsersOptions) ([]*goGitLab.User, error) {
213+
users, _, err := c.gitlabClient.Users.ListUsers(opts)
214+
if err != nil {
215+
return nil, err
216+
}
217+
return users, nil
218+
}
219+
220+
// SetCommitStatus sets the commit status
221+
func (c *GitLabClientWrapper) SetCommitStatus(projectID int, commitSHA string, opts *goGitLab.SetCommitStatusOptions) error {
222+
_, _, err := c.gitlabClient.Commits.SetCommitStatus(projectID, commitSHA, opts)
223+
if err != nil {
224+
return err
225+
}
226+
return nil
227+
}
228+
229+
func (c *GitLabClientWrapper) ListGroupProjects(groupID int, opts *goGitLab.ListGroupProjectsOptions) ([]*goGitLab.Project, *goGitLab.Response, error) {
230+
return c.gitlabClient.Groups.ListGroupProjects(groupID, opts)
231+
}
232+
233+
func (c *GitLabClientWrapper) ListUserProjects(user interface{}, opt *goGitLab.ListProjectsOptions) ([]*goGitLab.Project, *goGitLab.Response, error) {
234+
return c.gitlabClient.Projects.ListUserProjects(user, opt)
235+
}
236+
237+
func (c *GitLabClientWrapper) DeleteProjectHook(projectID, webhookID int) (*goGitLab.Response, error) {
238+
return c.gitlabClient.Projects.DeleteProjectHook(projectID, webhookID)
239+
}
240+
241+
func (c *GitLabClientWrapper) ListProjectHooks(projectID int, opts *goGitLab.ListProjectHooksOptions) ([]*goGitLab.ProjectHook, *goGitLab.Response, error) {
242+
return c.gitlabClient.Projects.ListProjectHooks(projectID, opts)
243+
}
244+
245+
func (c *GitLabClientWrapper) AddProjectHook(projectID int, opts *goGitLab.AddProjectHookOptions) (*goGitLab.ProjectHook, *goGitLab.Response, error) {
246+
return c.gitlabClient.Projects.AddProjectHook(projectID, opts)
247+
}
248+
249+
func (c *GitLabClientWrapper) EditProjectHook(projectID, existingID int, opts *goGitLab.EditProjectHookOptions) (*goGitLab.ProjectHook, *goGitLab.Response, error) {
250+
return c.gitlabClient.Projects.EditProjectHook(projectID, existingID, opts)
251+
}

cla-backend-go/gitlab_api/client_groups.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func GetGroupByFullPath(ctx context.Context, client *goGitLab.Client, fullPath s
147147
}
148148

149149
// GetGroupProjectListByGroupID returns a list of GitLab projects under the specified Organization
150-
func GetGroupProjectListByGroupID(ctx context.Context, client *goGitLab.Client, groupID int) ([]*goGitLab.Project, error) {
150+
func GetGroupProjectListByGroupID(ctx context.Context, client GitLabClient, groupID int) ([]*goGitLab.Project, error) {
151151
f := logrus.Fields{
152152
"functionName": "gitlab_api.client_groups.GetGroupProjectListByGroupID",
153153
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -168,7 +168,7 @@ func GetGroupProjectListByGroupID(ctx context.Context, client *goGitLab.Client,
168168
var projectList []*goGitLab.Project
169169
for {
170170
// https://docs.gitlab.com/ee/api/groups.html#list-a-groups-projects
171-
projects, resp, listProjectsErr := client.Groups.ListGroupProjects(groupID, opts)
171+
projects, resp, listProjectsErr := client.ListGroupProjects(groupID, opts)
172172
if listProjectsErr != nil {
173173
msg := fmt.Sprintf("unable to list projects, error: %+v", listProjectsErr)
174174
log.WithFields(f).WithError(listProjectsErr).Warn(msg)
@@ -193,7 +193,7 @@ func GetGroupProjectListByGroupID(ctx context.Context, client *goGitLab.Client,
193193
}
194194

195195
// ListGroupMembers lists the members of a given groupID
196-
func ListGroupMembers(ctx context.Context, client *goGitLab.Client, groupID int) ([]*goGitLab.GroupMember, error) {
196+
func ListGroupMembers(ctx context.Context, client GitLabClient, groupID int) ([]*goGitLab.GroupMember, error) {
197197
f := logrus.Fields{
198198
"functionName": "gitlab_api.client_groups.GetGroupMembers",
199199
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -202,7 +202,7 @@ func ListGroupMembers(ctx context.Context, client *goGitLab.Client, groupID int)
202202
log.WithFields(f).Debugf("fetching gitlab members for groupID: %d", groupID)
203203

204204
opts := &goGitLab.ListGroupMembersOptions{}
205-
members, _, err := client.Groups.ListGroupMembers(groupID, opts)
205+
members, _, err := client.ListGroupMembers(groupID, opts)
206206
if err != nil {
207207
log.WithFields(f).Debugf("unable to fetch members for gitlab GroupID : %d", groupID)
208208
return nil, err
@@ -212,7 +212,7 @@ func ListGroupMembers(ctx context.Context, client *goGitLab.Client, groupID int)
212212

213213
// ListUserProjectGroups fetches the unique groups of a gitlab users groups,
214214
// note: it doesn't list the projects/groups the user is member of ..., it's very limited
215-
func ListUserProjectGroups(ctx context.Context, client *goGitLab.Client, userID int) ([]*UserGroup, error) {
215+
func ListUserProjectGroups(ctx context.Context, client GitLabClient, userID int) ([]*UserGroup, error) {
216216
f := logrus.Fields{
217217
"functionName": "gitlab_api.client_groups.ListUserProjectGroups",
218218
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -226,7 +226,7 @@ func ListUserProjectGroups(ctx context.Context, client *goGitLab.Client, userID
226226
userGroupsMap := map[string]*UserGroup{}
227227
for {
228228
log.WithFields(f).Debugf("fetching projects for user id : %d with options : %v", userID, listOptions.ListOptions)
229-
projects, resp, err := client.Projects.ListUserProjects(userID, listOptions)
229+
projects, resp, err := client.ListUserProjects(userID, listOptions)
230230
if err != nil {
231231
msg := fmt.Sprintf("listing user : %d projects failed : %v", userID, err)
232232
log.WithFields(f).Warn(msg)

cla-backend-go/gitlab_api/client_projects.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,20 @@ func getProjectListWithOptions(ctx context.Context, client *goGitLab.Client, opt
8585
}
8686

8787
// GetProjectByID returns the GitLab project for the specified ID
88-
func GetProjectByID(ctx context.Context, client *goGitLab.Client, gitLabProjectID int) (*goGitLab.Project, error) {
88+
func GetProjectByID(ctx context.Context, client GitLabClient, gitLabProjectID int) (*goGitLab.Project, error) {
8989
f := logrus.Fields{
9090
"functionName": "gitlab.client.GetProjectByID",
9191
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
9292
"gitLabProjectID": gitLabProjectID,
9393
}
9494

9595
// Query GitLab for repos - fetch the list of repositories available to the GitLab App
96-
project, resp, getProjectErr := client.Projects.GetProject(gitLabProjectID, &goGitLab.GetProjectOptions{})
96+
project, getProjectErr := client.GetProject(gitLabProjectID, &goGitLab.GetProjectOptions{})
9797
if getProjectErr != nil {
9898
msg := fmt.Sprintf("unable to get project by ID: %d, error: %+v", gitLabProjectID, getProjectErr)
9999
log.WithFields(f).WithError(getProjectErr).Warn(msg)
100100
return nil, errors.New(msg)
101101
}
102-
if resp.StatusCode < 200 || resp.StatusCode > 299 {
103-
msg := fmt.Sprintf("unable to get project by ID: %d, status code: %d", gitLabProjectID, resp.StatusCode)
104-
log.WithFields(f).WithError(getProjectErr).Warn(msg)
105-
return nil, errors.New(msg)
106-
}
107102
if project == nil {
108103
msg := fmt.Sprintf("unable to get project by ID: %d, project is empty", gitLabProjectID)
109104
log.WithFields(f).WithError(getProjectErr).Warn(msg)
@@ -116,14 +111,14 @@ func GetProjectByID(ctx context.Context, client *goGitLab.Client, gitLabProjectI
116111
// EnableMergePipelineProtection enables the pipeline protection on given project, by default it's
117112
// turned off and when a new MR is raised users can merge requests bypassing the pipelines. With this
118113
// setting gitlab disables the Merge button if any of the pipelines are failing
119-
func EnableMergePipelineProtection(ctx context.Context, gitlabClient *goGitLab.Client, projectID int) error {
114+
func EnableMergePipelineProtection(ctx context.Context, gitlabClient GitLabClient, projectID int) error {
120115
f := logrus.Fields{
121116
"functionName": "gitlab.client.EnableMergePipelineProtection",
122117
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
123118
"gitLabProjectID": projectID,
124119
}
125120

126-
project, _, err := gitlabClient.Projects.GetProject(projectID, &goGitLab.GetProjectOptions{})
121+
project, err := gitlabClient.GetProject(projectID, &goGitLab.GetProjectOptions{})
127122
if err != nil {
128123
return fmt.Errorf("fetching project failed : %v", err)
129124
}
@@ -135,7 +130,7 @@ func EnableMergePipelineProtection(ctx context.Context, gitlabClient *goGitLab.C
135130

136131
project.OnlyAllowMergeIfPipelineSucceeds = true
137132
log.WithFields(f).Debugf("Enabling Merge Pipeline protection")
138-
_, _, err = gitlabClient.Projects.EditProject(projectID, &goGitLab.EditProjectOptions{
133+
_, _, err = gitlabClient.EditProject(projectID, &goGitLab.EditProjectOptions{
139134
OnlyAllowMergeIfPipelineSucceeds: goGitLab.Bool(true),
140135
})
141136

cla-backend-go/gitlab_api/client_users.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
)
1616

1717
// GetUserByName gets a gitlab user object by the given name
18-
func GetUserByName(ctx context.Context, client *goGitLab.Client, name string) (*goGitLab.User, error) {
18+
func GetUserByName(ctx context.Context, client GitLabClient, name string) (*goGitLab.User, error) {
1919
f := logrus.Fields{
2020
"functionName": "gitlab_api.client_users.GetUserByName",
2121
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
2222
"name": name,
2323
}
2424

25-
users, resp, err := client.Users.ListUsers(&goGitLab.ListUsersOptions{
25+
users, err := client.ListUsers(&goGitLab.ListUsersOptions{
2626
ListOptions: goGitLab.ListOptions{
2727
Page: 0,
2828
PerPage: 10,
@@ -35,11 +35,6 @@ func GetUserByName(ctx context.Context, client *goGitLab.Client, name string) (*
3535
log.WithFields(f).WithError(err).Warn(msg)
3636
return nil, errors.New(msg)
3737
}
38-
if resp.StatusCode < 200 || resp.StatusCode > 299 {
39-
msg := fmt.Sprintf("unable to get user using query: %s, status code: %d", name, resp.StatusCode)
40-
log.WithFields(f).Warn(msg)
41-
return nil, errors.New(msg)
42-
}
4338

4439
if len(users) == 0 {
4540
return nil, nil

cla-backend-go/gitlab_api/mocks/mock_client.go

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)