@@ -389,7 +389,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
389
389
},
390
390
Head : & api.PRBranchInfo {
391
391
Name : pr .HeadBranch ,
392
- Ref : fmt . Sprintf ( "%s%d/head" , git . PullPrefix , pr .Index ),
392
+ Ref : pr .GetGitRefName ( ),
393
393
RepoID : - 1 ,
394
394
},
395
395
}
@@ -422,72 +422,43 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
422
422
return nil , err
423
423
}
424
424
}
425
-
426
425
if baseBranch != nil {
427
426
apiPullRequest .Base .Sha = baseBranch .CommitID
428
427
}
429
428
430
- if pr .Flow == issues_model .PullRequestFlowAGit {
431
- apiPullRequest .Head .Sha , err = gitRepo .GetRefCommitID (pr .GetGitRefName ())
429
+ // pull request head branch, both repository and branch could not exist
430
+ if pr .HeadRepo != nil {
431
+ apiPullRequest .Head .RepoID = pr .HeadRepo .ID
432
+ exist , err := git_model .IsBranchExist (ctx , pr .HeadRepo .ID , pr .HeadBranch )
432
433
if err != nil {
433
- log .Error ("GetRefCommitID[%s ]: %v" , pr .GetGitRefName () , err )
434
+ log .Error ("IsBranchExist[%d ]: %v" , pr .HeadRepo . ID , err )
434
435
return nil , err
435
436
}
436
- apiPullRequest .Head .RepoID = pr .BaseRepoID
437
- apiPullRequest .Head .Repository = apiPullRequest .Base .Repository
438
- apiPullRequest .Head .Name = ""
439
- }
440
-
441
- var headGitRepo * git.Repository
442
- if pr .HeadRepo != nil && pr .Flow == issues_model .PullRequestFlowGithub {
443
- if pr .HeadRepoID == pr .BaseRepoID {
444
- apiPullRequest .Head .RepoID = pr .HeadRepo .ID
445
- apiPullRequest .Head .Repository = apiRepo
446
- headGitRepo = gitRepo
447
- } else {
448
- p , err := access_model .GetUserRepoPermission (ctx , pr .HeadRepo , doer )
449
- if err != nil {
450
- log .Error ("GetUserRepoPermission[%d]: %v" , pr .HeadRepoID , err )
451
- p .AccessMode = perm .AccessModeNone
452
- }
453
-
454
- apiPullRequest .Head .RepoID = pr .HeadRepo .ID
455
- apiPullRequest .Head .Repository = ToRepo (ctx , pr .HeadRepo , p )
456
-
457
- headGitRepo , err = gitrepo .OpenRepository (ctx , pr .HeadRepo )
458
- if err != nil {
459
- log .Error ("OpenRepository[%s]: %v" , pr .HeadRepo .RepoPath (), err )
460
- return nil , err
461
- }
462
- defer headGitRepo .Close ()
437
+ if exist {
438
+ apiPullRequest .Head .Ref = pr .HeadBranch
463
439
}
440
+ }
441
+ if apiPullRequest .Head .Ref == "" {
442
+ apiPullRequest .Head .Ref = pr .GetGitRefName ()
443
+ }
464
444
465
- headBranch , err := headGitRepo .GetBranch (pr .HeadBranch )
466
- if err != nil && ! git .IsErrBranchNotExist (err ) {
467
- log .Error ("GetBranch[%s]: %v" , pr .HeadBranch , err )
468
- return nil , err
445
+ if pr .HeadRepoID == pr .BaseRepoID {
446
+ apiPullRequest .Head .Repository = apiPullRequest .Base .Repository
447
+ } else {
448
+ p , err := access_model .GetUserRepoPermission (ctx , pr .HeadRepo , doer )
449
+ if err != nil {
450
+ log .Error ("GetUserRepoPermission[%d]: %v" , pr .HeadRepoID , err )
451
+ p .AccessMode = perm .AccessModeNone
469
452
}
453
+ apiPullRequest .Head .Repository = ToRepo (ctx , pr .HeadRepo , p )
454
+ }
470
455
471
- if git .IsErrBranchNotExist (err ) {
472
- headCommitID , err := headGitRepo .GetRefCommitID (apiPullRequest .Head .Ref )
473
- if err != nil && ! git .IsErrNotExist (err ) {
474
- log .Error ("GetCommit[%s]: %v" , pr .HeadBranch , err )
475
- return nil , err
476
- }
477
- if err == nil {
478
- apiPullRequest .Head .Sha = headCommitID
479
- }
480
- } else {
481
- commit , err := headBranch .GetCommit ()
482
- if err != nil && ! git .IsErrNotExist (err ) {
483
- log .Error ("GetCommit[%s]: %v" , headBranch .Name , err )
484
- return nil , err
485
- }
486
- if err == nil {
487
- apiPullRequest .Head .Ref = pr .HeadBranch
488
- apiPullRequest .Head .Sha = commit .ID .String ()
489
- }
490
- }
456
+ if pr .Flow == issues_model .PullRequestFlowAGit {
457
+ apiPullRequest .Head .Name = ""
458
+ }
459
+ apiPullRequest .Head .Sha , err = gitRepo .GetRefCommitID (pr .GetGitRefName ())
460
+ if err != nil {
461
+ log .Error ("GetRefCommitID[%s]: %v" , pr .GetGitRefName (), err )
491
462
}
492
463
493
464
if len (apiPullRequest .Head .Sha ) == 0 && len (apiPullRequest .Head .Ref ) != 0 {
0 commit comments