]> source.dussan.org Git - gitea.git/commitdiff
[API] get pull, return head branch sha, even if deleted (#14931)
author6543 <6543@obermui.de>
Mon, 8 Mar 2021 20:48:31 +0000 (21:48 +0100)
committerGitHub <noreply@github.com>
Mon, 8 Mar 2021 20:48:31 +0000 (21:48 +0100)
* API: return head branch sha, even if deleted

* relax if ref not resolvable

modules/convert/pull.go

index 87eabcf53a98f6080c202ee678d82932938030ee..3c24f4532f3cedb63983b8f40d8e321d9f68ba6d 100644 (file)
@@ -134,6 +134,24 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
                }
        }
 
+       if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
+               baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+               if err != nil {
+                       log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
+                       return nil
+               }
+               defer baseGitRepo.Close()
+               refs, err := baseGitRepo.GetRefsFiltered(apiPullRequest.Head.Ref)
+               if err != nil {
+                       log.Error("GetRefsFiltered[%s]: %v", apiPullRequest.Head.Ref, err)
+                       return nil
+               } else if len(refs) == 0 {
+                       log.Error("unable to resolve PR head ref")
+               } else {
+                       apiPullRequest.Head.Sha = refs[0].Object.String()
+               }
+       }
+
        if pr.Status != models.PullRequestStatusChecking {
                mergeable := !(pr.Status == models.PullRequestStatusConflict || pr.Status == models.PullRequestStatusError) && !pr.IsWorkInProgress()
                apiPullRequest.Mergeable = mergeable