@@ -525,15 +525,11 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques
525
525
headRepoName = pr .Head .Repository .Name
526
526
headCloneURL = pr .Head .Repository .CloneURL
527
527
}
528
+ if err := fixPullHeadSha (g .client , pr ); err != nil {
529
+ return nil , false , fmt .Errorf ("error while resolving head git ref: %s for pull #%d. Error: %v" , pr .Head .Ref , pr .Index , err )
530
+ }
528
531
headSHA = pr .Head .Sha
529
532
headRef = pr .Head .Ref
530
- if headSHA == "" {
531
- headCommit , _ , err := g .client .GetSingleCommit (g .repoOwner , g .repoName , url .PathEscape (pr .Head .Ref ))
532
- if err != nil {
533
- return nil , false , fmt .Errorf ("error while resolving head git ref: %s for pull #%d. Error: %v" , pr .Head .Ref , pr .Index , err )
534
- }
535
- headSHA = headCommit .SHA
536
- }
537
533
}
538
534
539
535
var mergeCommitSHA string
@@ -683,3 +679,22 @@ func (g *GiteaDownloader) GetReviews(index int64) ([]*base.Review, error) {
683
679
}
684
680
return allReviews , nil
685
681
}
682
+
683
+ // fixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675
684
+ // When no head sha is available, this is because the branch got deleted in the base repo.
685
+ // pr.Head.Ref points in this case not to the head repo branch name, but the base repo ref,
686
+ // which stays available to resolve the commit sha.
687
+ func fixPullHeadSha (client * gitea_sdk.Client , pr * gitea_sdk.PullRequest ) error {
688
+ owner := pr .Base .Repository .Owner .UserName
689
+ repo := pr .Base .Repository .Name
690
+ if pr .Head != nil && pr .Head .Sha == "" {
691
+ refs , _ , err := client .GetRepoRefs (owner , repo , pr .Head .Ref )
692
+ if err != nil {
693
+ return err
694
+ } else if len (refs ) == 0 {
695
+ return fmt .Errorf ("unable to resolve PR ref '%s'" , pr .Head .Ref )
696
+ }
697
+ pr .Head .Sha = refs [0 ].Object .SHA
698
+ }
699
+ return nil
700
+ }
0 commit comments