Skip to content

Commit

Permalink
Merge pull request #601 from bcrocker15/ogr-issue-584
Browse files Browse the repository at this point in the history
Support using the merge ref instead of the head ref in a pull request

Add merge_commit_sha and mergeable properties to GithubPullRequest class
(and PullRequest superclass).
OGR issue #584.
Signed-off-by: Ben Crocker [email protected]

Reviewed-by: Tomas Tomecek <[email protected]>
Reviewed-by: None <None>
Reviewed-by: None <None>
Reviewed-by: Matej Focko <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jul 12, 2021
2 parents 9814561 + 6e5bf1a commit 3809720
Show file tree
Hide file tree
Showing 8 changed files with 2,218 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ check:
PYTHONPATH=$(CURDIR) PYTHONDONTWRITEBYTECODE=1 pytest --verbose --showlocals $(TEST_TARGET)

check-in-container:
podman run --rm -it -v $(CURDIR):/src:Z -w /src $(OGR_IMAGE) make check
podman run --rm -it -v $(CURDIR):/src:Z -w /src $(OGR_IMAGE) make -e GITHUB_TOKEN=$(GITHUB_TOKEN) GITLAB_TOKEN=$(GITLAB_TOKEN) check

shell:
podman run --rm -ti -v $(CURDIR):/src:Z -w /src $(OGR_IMAGE) bash
Expand Down
34 changes: 25 additions & 9 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,23 @@ class PRStatus(IntEnum):
all = 4


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


class MergeCommitStatus(Enum):
can_be_merged = 1
cannot_be_merged = 2
unchecked = 3
checking = 4
cannot_be_merged_recheck = 5


class PullRequest(OgrAbstractClass):
@deprecate_and_set_removal(
since="0.9.0",
Expand Down Expand Up @@ -404,6 +421,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) -> MergeCommitStatus:
raise NotImplementedError()

@property
def source_project(self) -> "GitProject":
raise NotImplementedError()
Expand Down Expand Up @@ -588,15 +613,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, MergeCommitStatus
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) -> MergeCommitStatus:
if self._raw_pr.mergeable:
return MergeCommitStatus.can_be_merged
else:
return MergeCommitStatus.cannot_be_merged

@property
def source_project(self) -> "ogr_github.GithubProject":
if self._source_project is None:
Expand Down
23 changes: 21 additions & 2 deletions ogr/services/gitlab/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
# SOFTWARE.

import datetime
from typing import List, Optional
from typing import Dict, List, Optional

from gitlab.v4.objects import MergeRequest as _GitlabMergeRequest

from ogr.abstract import PullRequest, PRComment, PRStatus
from ogr.abstract import PullRequest, PRComment, PRStatus, MergeCommitStatus
from ogr.exceptions import GitlabAPIException
from ogr.services import gitlab as ogr_gitlab
from ogr.services.base import BasePullRequest
Expand All @@ -36,6 +36,13 @@ class GitlabPullRequest(BasePullRequest):
_raw_pr: _GitlabMergeRequest
_target_project: "ogr_gitlab.GitlabProject"
_source_project: "ogr_gitlab.GitlabProject" = None
_merge_commit_status: Dict[str, MergeCommitStatus] = {
"can_be_merged": MergeCommitStatus.can_be_merged,
"cannot_be_merged": MergeCommitStatus.cannot_be_merged,
"unchecked": MergeCommitStatus.unchecked,
"checking": MergeCommitStatus.checking,
"cannot_be_merged_recheck": MergeCommitStatus.cannot_be_merged_recheck,
}

@property
def title(self) -> str:
Expand Down Expand Up @@ -103,6 +110,18 @@ 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) -> MergeCommitStatus:
status = self._raw_pr.merge_status
if status in self._merge_commit_status:
return self._merge_commit_status[status]
else:
raise GitlabAPIException(f"Invalid merge_status {status}")

@property
def source_project(self) -> "ogr_gitlab.GitlabProject":
if self._source_project is None:
Expand Down
Loading

0 comments on commit 3809720

Please sign in to comment.