Skip to content

Commit

Permalink
Support using the merge ref instead of the head ref in a pull request
Browse files Browse the repository at this point in the history
Add merge_commit_sha and merge_commit_status properties
to GithubPullRequest class (and PullRequest superclass);
merge_commit_status returns a CommitStatus, which is
expanded with two new values, mergeable and notmergeable.

GitHub's 'mergeable' boolean maps to CommitStatus.mergeable (true)
or CommitStatus.notmergeable (false).

GitLab's 'merge_status' string values map to CommitStatus.mergeable
("can_be_merged") or CommitStatus.notmergeable (everything else).

OGR issue #584.

Signed-off-by: Ben Crocker <[email protected]>
  • Loading branch information
bcrocker15 committed Jun 19, 2021
1 parent 9a8149e commit 0005f29
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
28 changes: 19 additions & 9 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ class PRStatus(IntEnum):
all = 4


class CommitStatus(Enum):
pending = 1
success = 2
failure = 3
error = 4
canceled = 5
running = 6
mergeable = 7
notmergeable = 8


class PullRequest(OgrAbstractClass):
@deprecate_and_set_removal(
since="0.9.0",
Expand Down Expand Up @@ -404,6 +415,14 @@ def patch(self) -> bytes:
def head_commit(self) -> str:
raise NotImplementedError

@property
def merge_commit_sha(self) -> str:
raise NotImplementedError()

@property
def merge_commit_status(self) -> CommitStatus:
raise NotImplementedError()

@property
def source_project(self) -> "GitProject":
raise NotImplementedError()
Expand Down Expand Up @@ -588,15 +607,6 @@ def get_statuses(self) -> List["CommitFlag"]:
raise NotImplementedError()


class CommitStatus(Enum):
pending = 1
success = 2
failure = 3
error = 4
canceled = 5
running = 6


class CommitFlag(OgrAbstractClass):
_states: Dict[str, CommitStatus] = dict()

Expand Down
13 changes: 12 additions & 1 deletion ogr/services/github/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from github.IssueComment import IssueComment as _GithubIssueComment
from github.PullRequestComment import PullRequestComment as _GithubPullRequestComment

from ogr.abstract import PRComment, PRStatus, PullRequest
from ogr.abstract import PRComment, PRStatus, PullRequest, CommitStatus
from ogr.exceptions import GithubAPIException
from ogr.services import github as ogr_github
from ogr.services.base import BasePullRequest
Expand Down Expand Up @@ -109,6 +109,17 @@ def commits_url(self) -> str:
def head_commit(self) -> str:
return self._raw_pr.head.sha

@property
def merge_commit_sha(self) -> str:
return self._raw_pr.merge_commit_sha

@property
def merge_commit_status(self) -> CommitStatus:
if self._raw_pr.mergeable:
return CommitStatus.mergeable
else:
return CommitStatus.notmergeable

@property
def source_project(self) -> "ogr_github.GithubProject":
if self._source_project is None:
Expand Down
13 changes: 12 additions & 1 deletion ogr/services/gitlab/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from gitlab.v4.objects import MergeRequest as _GitlabMergeRequest

from ogr.abstract import PullRequest, PRComment, PRStatus
from ogr.abstract import PullRequest, PRComment, PRStatus, CommitStatus
from ogr.exceptions import GitlabAPIException
from ogr.services import gitlab as ogr_gitlab
from ogr.services.base import BasePullRequest
Expand Down Expand Up @@ -103,6 +103,17 @@ def commits_url(self) -> str:
def head_commit(self) -> str:
return self._raw_pr.sha

@property
def merge_commit_sha(self) -> str:
return self._raw_pr.merge_commit_sha

@property
def merge_commit_status(self) -> CommitStatus:
if self._raw_pr.merge_status == "can_be_merged":
return CommitStatus.mergeable
else:
return CommitStatus.notmergeable

@property
def source_project(self) -> "ogr_gitlab.GitlabProject":
if self._source_project is None:
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/github/test_pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ def test_head_commit(self):
== "7cf6d0cbeca285ecbeb19a0067cb243783b3c768"
)

def test_merge_commit_sha_240(self):
pr240 = self.hello_world_project.get_pr(240)
assert pr240.head_commit == "dabfd3862702e49b6877da7f224e6d6458eb961a"
assert pr240.merge_commit_sha == "f502aae6920d82948f2dba0b70c9260fb1e34822"
assert pr240.merge_status == "cannot_be_merged" # Because it's already merged

def test_merge_commit_sha_111(self):
pr111 = self.hello_world_project.get_pr(111)
assert pr111.head_commit == "1abb19255a7c1bec7ffcae2487f022b23175af2b"
assert pr111.merge_commit_sha == "8512ef316918edc39c4a6eee13e6cc45344d03ac"
assert pr111.merge_status == "cannot_be_merged" # Conflicts

def test_merge_commit_sha_112(self):
pr112 = self.hello_world_project.get_pr(112)
assert pr112.head_commit == "9ab13fa4b4944510022730708045f42aea106cef"
assert pr112.merge_commit_sha == "0dc8211e10e37370f49364495249f5c693a9eff7"
# Not (yet) merged; good thing! (invalid specfile):
assert pr112.merge_status == "can_be_merged"

def test_source_project_upstream_branch(self):
# Tests source project for PR from upstream to upstream.
pr = self.hello_world_project.get_pr(72)
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/gitlab/test_pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ def test_head_commit(self):
== "59b1a9bab5b5198c619270646410867788685c16"
)

def test_merge_commit_sha_1(self):
pr1 = self.project.get_pr(1)
assert pr1.merge_commit_sha == "101a42bbbe174d04b465d49caf274dc3b4defeca"
assert pr1.merge_status == "can_be_merged"

def test_merge_commit_sha_12(self):
pr12 = self.project.get_pr(12)
assert pr12.merge_commit_sha == "f6de56d97ec3ec74cd5194e79175162d9f969195"
assert pr12.merge_status == "can_be_merged"

def test_merge_commit_sha_19(self):
pr19 = self.project.get_pr(19)
assert pr19.merge_commit_sha == "b8e18207cfdad954f1b3a96db34d0706b272e6cf"
assert pr19.merge_status == "can_be_merged"

def test_source_project_upstream_branch(self):
pr = self.project.get_pr(23)
source_project = pr.source_project
Expand Down

0 comments on commit 0005f29

Please sign in to comment.