Skip to content

Commit

Permalink
Fix merge_commit_sha for GitLab
Browse files Browse the repository at this point in the history
Previous solution utilized only merge_commit, that is after merging MR.
Since v0.20.0 python-gitlab supports test merges as GitHub. Fixes
behaviour to be consistent with GitHub's.

Signed-off-by: Matej Focko <[email protected]>
  • Loading branch information
mfocko committed Aug 5, 2021
1 parent e946024 commit 8a15981
Show file tree
Hide file tree
Showing 4 changed files with 1,321 additions and 485 deletions.
17 changes: 15 additions & 2 deletions ogr/services/gitlab/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from typing import Dict, List, Optional

from gitlab.v4.objects import MergeRequest as _GitlabMergeRequest
from gitlab.exceptions import GitlabGetError

from ogr.abstract import PullRequest, PRComment, PRStatus, MergeCommitStatus
from ogr.exceptions import GitlabAPIException
Expand Down Expand Up @@ -123,8 +124,20 @@ def head_commit(self) -> str:
return self._raw_pr.sha

@property
def merge_commit_sha(self) -> str:
return self._raw_pr.merge_commit_sha
def merge_commit_sha(self) -> Optional[str]:
# when merged => return merge_commit_sha
# otherwise => return test merge if possible
if self.status == PRStatus.merged:
return self._raw_pr.merge_commit_sha

# works for test merge only with python-gitlab>=2.10.0
try:
response = self._raw_pr.merge_ref()
except GitlabGetError as ex:
if ex.response_code == 400:
return None
raise
return response.get("commit_id")

@property
def merge_commit_status(self) -> MergeCommitStatus:
Expand Down
Loading

0 comments on commit 8a15981

Please sign in to comment.