@@ -14,6 +14,16 @@ import (
1414 "github.com/xanzy/go-gitlab"
1515)
1616
17+ type UserCommitSummary struct {
18+ AuthorID int
19+ AuthorUsername string
20+ CommitSha string
21+ AuthorName string
22+ AuthorEmail string
23+ Authorized bool
24+ Affiliated bool
25+ }
26+
1727// FetchMrInfo is responsible for fetching the MR info for given project
1828func FetchMrInfo (client * gitlab.Client , projectID int , mergeID int ) (* gitlab.MergeRequest , error ) {
1929 m , _ , err := client .MergeRequests .GetMergeRequest (projectID , mergeID , & gitlab.GetMergeRequestsOptions {})
@@ -24,15 +34,15 @@ func FetchMrInfo(client *gitlab.Client, projectID int, mergeID int) (*gitlab.Mer
2434 return m , nil
2535}
2636
27- func GetLatestCommit (client * gitlab. Client , projectID int , mergeID int ) (* gitlab.Commit , error ) {
37+ func GetLatestCommit (client GitLabClient , projectID int , mergeID int ) (* gitlab.Commit , error ) {
2838 f := logrus.Fields {
2939 "functionName" : "gitlab_api.GetLatestCommit" ,
3040 "projectID" : projectID ,
3141 "mergeID" : mergeID ,
3242 }
3343
3444 log .WithFields (f ).Debug ("fetching latest commit..." )
35- commits , _ , err := client . MergeRequests .GetMergeRequestCommits (projectID , mergeID , & gitlab.GetMergeRequestCommitsOptions {})
45+ commits , err := client .GetMergeRequestCommits (projectID , mergeID , & gitlab.GetMergeRequestCommitsOptions {})
3646 if err != nil {
3747 return nil , fmt .Errorf ("fetching merge request commits : %d for project : %v failed : %v" , mergeID , projectID , err )
3848 }
@@ -45,28 +55,26 @@ func GetLatestCommit(client *gitlab.Client, projectID int, mergeID int) (*gitlab
4555}
4656
4757// FetchMrParticipants is responsible to get unique mr participants
48- func FetchMrParticipants (client * gitlab. Client , projectID int , mergeID int ) ([]* gitlab. User , error ) {
58+ func FetchMrParticipants (client GitLabClient , projectID int , mergeID int ) ([]* UserCommitSummary , error ) {
4959 f := logrus.Fields {
5060 "functionName" : "gitlab_api.FetchMrParticipants" ,
5161 "projectID" : projectID ,
5262 "mergeID" : mergeID ,
5363 }
64+
65+ results := make ([]* UserCommitSummary , 0 )
66+
5467 log .WithFields (f ).Debug ("fetching mr participants..." )
55- commits , response , err := client . MergeRequests .GetMergeRequestCommits (projectID , mergeID , & gitlab.GetMergeRequestCommitsOptions {})
68+ commits , err := client .GetMergeRequestCommits (projectID , mergeID , & gitlab.GetMergeRequestCommitsOptions {})
5669 if err != nil {
5770 return nil , fmt .Errorf ("fetching gitlab participants for project : %d and merge id : %d, failed : %v" , projectID , mergeID , err )
5871 }
59- if response .StatusCode != 200 {
60- return nil , fmt .Errorf ("fetching gitlab participants for project : %d and merge id : %d, failed with status code : %d" , projectID , mergeID , response .StatusCode )
61- }
6272
6373 if len (commits ) == 0 {
6474 log .WithFields (f ).Debugf ("no commits found for project : %d and merge id : %d" , projectID , mergeID )
65- return nil , nil
75+ return results , nil
6676 }
6777
68- var results []* gitlab.User
69-
7078 for _ , commit := range commits {
7179 log .WithFields (f ).Debugf ("commit information: %v" , commit )
7280 // The author is the person who originally wrote the code. The committer, on the other hand, is assumed to be
@@ -82,14 +90,16 @@ func FetchMrParticipants(client *gitlab.Client, projectID int, mergeID int) ([]*
8290 return nil , getUserErr
8391 }
8492
93+ user .CommitSha = commit .ShortID
94+
8595 results = append (results , user )
8696 }
8797
8898 return results , nil
8999}
90100
91101// SetCommitStatus is responsible for setting the MR status for commit sha
92- func SetCommitStatus (client * gitlab. Client , projectID int , commitSha string , state gitlab.BuildStateValue , message string , targetURL string ) error {
102+ func SetCommitStatus (client GitLabClient , projectID int , commitSha string , state gitlab.BuildStateValue , message string , targetURL string ) error {
93103 f := logrus.Fields {
94104 "functionName" : "gitlab_api.SetCommitStatus" ,
95105 "projectID" : projectID ,
@@ -110,7 +120,7 @@ func SetCommitStatus(client *gitlab.Client, projectID int, commitSha string, sta
110120 options .TargetURL = gitlab .String (targetURL )
111121 }
112122
113- _ , _ , err := client . Commits .SetCommitStatus (projectID , commitSha , options )
123+ err := client .SetCommitStatus (projectID , commitSha , options )
114124 if err != nil {
115125 return fmt .Errorf ("setting commit status for the sha : %s and project id : %d failed : %v" , commitSha , projectID , err )
116126 }
@@ -161,23 +171,24 @@ func SetMrComment(client *gitlab.Client, projectID int, mergeID int, message str
161171}
162172
163173// getUser is responsible for fetching the user info for given user email
164- func getUser (client * gitlab. Client , email , name * string ) (* gitlab. User , error ) {
174+ func getUser (client GitLabClient , email , name * string ) (* UserCommitSummary , error ) {
165175 f := logrus.Fields {
166176 "functionName" : "gitlab_api.getUser" ,
167177 "email" : * email ,
168178 "name" : * name ,
169179 }
170180
171- user := & gitlab. User {
172- Email : * email ,
173- Name : * name ,
181+ user := & UserCommitSummary {
182+ AuthorEmail : * email ,
183+ AuthorName : * name ,
174184 }
175185
176- users , _ , err := client . Users .ListUsers (& gitlab.ListUsersOptions {
186+ users , err := client .ListUsers (& gitlab.ListUsersOptions {
177187 Active : utils .Bool (true ),
178188 Blocked : utils .Bool (false ),
179189 Search : email ,
180190 })
191+
181192 if err != nil {
182193 log .WithFields (f ).Warnf ("unable to find user for email : %s, error : %v" , utils .StringValue (email ), err )
183194 return nil , err
@@ -193,8 +204,8 @@ func getUser(client *gitlab.Client, email, name *string) (*gitlab.User, error) {
193204 for _ , found := range users {
194205 if strings .EqualFold (found .Name , * name ) {
195206 log .WithFields (f ).Debugf ("found matching user : %+v - updating GitLab username and ID" , found )
196- user .Username = found .Username
197- user .ID = found .ID
207+ user .AuthorID = found .ID
208+ user .AuthorUsername = found .Username
198209 break
199210 }
200211 }
0 commit comments