@@ -7,7 +7,7 @@ package gitlab
77import (
88 "context"
99 "fmt"
10- "github.com/sirupsen/logrus "
10+ "github.com/mitchellh/copystructure "
1111 "net/url"
1212 "strconv"
1313 "strings"
@@ -24,7 +24,14 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P
2424 path := fmt .Sprintf ("api/v4/projects/%s/merge_requests/%d" , encode (repo ), number )
2525 out := new (pr )
2626 res , err := s .client .do (ctx , "GET" , path , nil , out )
27- return convertPullRequest (out , repo ), res , err
27+ if err != nil {
28+ return nil , res , err
29+ }
30+ convRepo , convRes , err := s .convertPullRequest (ctx , out )
31+ if err != nil {
32+ return nil , convRes , err
33+ }
34+ return convRepo , res , nil
2835}
2936
3037func (s * pullService ) FindComment (ctx context.Context , repo string , index , id int ) (* scm.Comment , * scm.Response , error ) {
@@ -38,7 +45,14 @@ func (s *pullService) List(ctx context.Context, repo string, opts scm.PullReques
3845 path := fmt .Sprintf ("api/v4/projects/%s/merge_requests?%s" , encode (repo ), encodePullRequestListOptions (opts ))
3946 out := []* pr {}
4047 res , err := s .client .do (ctx , "GET" , path , nil , & out )
41- return convertPullRequestList (out , repo ), res , err
48+ if err != nil {
49+ return nil , res , err
50+ }
51+ convRepos , convRes , err := s .convertPullRequestList (ctx , out )
52+ if err != nil {
53+ return nil , convRes , err
54+ }
55+ return convRepos , res , nil
4256}
4357
4458func (s * pullService ) ListChanges (ctx context.Context , repo string , number int , opts scm.ListOptions ) ([]* scm.Change , * scm.Response , error ) {
@@ -50,7 +64,6 @@ func (s *pullService) ListChanges(ctx context.Context, repo string, number int,
5064
5165func (s * pullService ) ListComments (ctx context.Context , repo string , index int , opts scm.ListOptions ) ([]* scm.Comment , * scm.Response , error ) {
5266 path := fmt .Sprintf ("api/v4/projects/%s/merge_requests/%d/notes?%s" , encode (repo ), index , encodeListOptions (opts ))
53- logrus .Errorf ("path: %s" , path )
5467 out := []* issueComment {}
5568 res , err := s .client .do (ctx , "GET" , path , nil , & out )
5669 return convertIssueCommentList (out ), res , err
@@ -234,7 +247,14 @@ func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRe
234247
235248 out := new (pr )
236249 res , err := s .client .do (ctx , "POST" , path , in , out )
237- return convertPullRequest (out , repo ), res , err
250+ if err != nil {
251+ return nil , res , err
252+ }
253+ convRepo , convRes , err := s .convertPullRequest (ctx , out )
254+ if err != nil {
255+ return nil , convRes , err
256+ }
257+ return convRepo , res , nil
238258}
239259
240260func (s * pullService ) Update (ctx context.Context , repo string , number int , input * scm.PullRequestInput ) (* scm.PullRequest , * scm.Response , error ) {
@@ -288,7 +308,14 @@ func (s *pullService) updateMergeRequestField(ctx context.Context, repo string,
288308
289309 out := new (pr )
290310 res , err := s .client .do (ctx , "PUT" , path , input , out )
291- return convertPullRequest (out , repo ), res , err
311+ if err != nil {
312+ return nil , res , err
313+ }
314+ convRepo , convRes , err := s .convertPullRequest (ctx , out )
315+ if err != nil {
316+ return nil , convRes , err
317+ }
318+ return convRepo , res , nil
292319}
293320
294321type pr struct {
@@ -346,16 +373,19 @@ type pullRequestMergeRequest struct {
346373 MergeWhenPipelineSucceeds string `json:"merge_when_pipeline_succeeds,omitempty"`
347374}
348375
349- func convertPullRequestList (from []* pr , repo string ) []* scm.PullRequest {
376+ func ( s * pullService ) convertPullRequestList (ctx context. Context , from []* pr ) ( []* scm.PullRequest , * scm. Response , error ) {
350377 to := []* scm.PullRequest {}
351378 for _ , v := range from {
352- to = append (to , convertPullRequest (v , repo ))
379+ converted , res , err := s .convertPullRequest (ctx , v )
380+ if err != nil {
381+ return nil , res , err
382+ }
383+ to = append (to , converted )
353384 }
354- return to
385+ return to , nil , nil
355386}
356387
357- func convertPullRequest (from * pr , repo string ) * scm.PullRequest {
358- repoNS , repoName := scm .Split (repo )
388+ func (s * pullService ) convertPullRequest (ctx context.Context , from * pr ) (* scm.PullRequest , * scm.Response , error ) {
359389 // Diff refs only seem to be populated in more recent merge requests. Default
360390 // to from.Sha for compatibility / consistency, but fallback to HeadSHA if
361391 // it's not populated.
@@ -370,6 +400,24 @@ func convertPullRequest(from *pr, repo string) *scm.PullRequest {
370400 for _ , a := range from .Assignees {
371401 assignees = append (assignees , * convertUser (a ))
372402 }
403+ var res * scm.Response
404+ baseRepo , res , err := s .client .Repositories .Find (ctx , strconv .Itoa (from .TargetProjectID ))
405+ if err != nil {
406+ return nil , res , err
407+ }
408+ var headRepo * scm.Repository
409+ if from .TargetProjectID == from .SourceProjectID {
410+ repoCopy , err := copystructure .Copy (baseRepo )
411+ if err != nil {
412+ return nil , nil , err
413+ }
414+ headRepo = repoCopy .(* scm.Repository )
415+ } else {
416+ headRepo , res , err = s .client .Repositories .Find (ctx , strconv .Itoa (from .SourceProjectID ))
417+ if err != nil {
418+ return nil , res , err
419+ }
420+ }
373421 return & scm.PullRequest {
374422 Number : from .Number ,
375423 Title : from .Title ,
@@ -389,25 +437,18 @@ func convertPullRequest(from *pr, repo string) *scm.PullRequest {
389437 Author : * convertUser (& from .Author ),
390438 Assignees : assignees ,
391439 Head : scm.PullRequestBranch {
392- Ref : from .SourceBranch ,
393- Sha : headSHA ,
394- Repo : scm.Repository {
395- ID : strconv .Itoa (from .SourceProjectID ),
396- },
440+ Ref : from .SourceBranch ,
441+ Sha : headSHA ,
442+ Repo : * headRepo ,
397443 },
398444 Base : scm.PullRequestBranch {
399- Ref : from .TargetBranch ,
400- Sha : from .DiffRefs .BaseSHA ,
401- Repo : scm.Repository {
402- ID : strconv .Itoa (from .TargetProjectID ),
403- Name : repoName ,
404- Namespace : repoNS ,
405- FullName : repo ,
406- },
445+ Ref : from .TargetBranch ,
446+ Sha : from .DiffRefs .BaseSHA ,
447+ Repo : * baseRepo ,
407448 },
408449 Created : from .Created ,
409450 Updated : from .Updated ,
410- }
451+ }, nil , nil
411452}
412453
413454func convertPullRequestLabels (from []* string ) []* scm.Label {
0 commit comments