Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions sync2jira/intermediary.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def title(self):
return "[%s] %s" % (self.upstream, self._title)

@classmethod
def from_github(cls, upstream, pr, suffix, config):
def from_github(cls, upstream, pr, suffix, config, action=None):
"""Helper function to create an intermediary PR object."""
# Set our upstream source
upstream_source = "github"
Expand All @@ -215,15 +215,20 @@ def from_github(cls, upstream, pr, suffix, config):
# Match to a JIRA
match = matcher(pr.get("body"), comments)

# Figure out what state we're transitioning too
if "reopened" in suffix:
suffix = "reopened"
elif "closed" in suffix:
# Check if we're merging or closing
if pr["merged"]:
suffix = "merged"
_lifecycle = frozenset({"open", "merged", "closed", "reopened"})
Comment thread
webbnh marked this conversation as resolved.
Resolved
if action:
if action == "reopened":
suffix = "reopened"
elif action == "closed":
suffix = "merged" if pr.get("merged") else "closed"
elif action == "opened":
suffix = "open"
else:
suffix = "closed"
suffix = "open"
Comment thread
webbnh marked this conversation as resolved.
Resolved
elif suffix in _lifecycle:
pass
else:
suffix = "open"
Comment thread
webbnh marked this conversation as resolved.
Resolved
Comment thread
webbnh marked this conversation as resolved.
Resolved

# Return our PR object
return cls(
Expand Down
4 changes: 2 additions & 2 deletions sync2jira/upstream_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def handle_github_message(body, config, suffix):
token = config["sync2jira"].get("github_token")
github_client = Github(token, retry=5)
reformat_github_pr(pr, upstream, github_client)
return i.PR.from_github(upstream, pr, suffix, config)
return i.PR.from_github(upstream, pr, suffix, config, action=body.get("action"))


def github_prs(upstream, config):
Expand All @@ -62,7 +62,7 @@ def github_prs(upstream, config):
github_client = Github(config["sync2jira"]["github_token"])
for pr in u_issue.generate_github_items("pulls", upstream, config):
reformat_github_pr(pr, upstream, github_client)
yield i.PR.from_github(upstream, pr, "open", config)
yield i.PR.from_github(upstream, pr, "open", config, action=None)
Comment thread
webbnh marked this conversation as resolved.
Resolved


def reformat_github_pr(pr, upstream, github_client):
Expand Down
37 changes: 31 additions & 6 deletions tests/test_intermediary.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,15 @@ def test_mapping_github(self):

@mock.patch(PATH + "matcher")
def test_from_github_pr_reopen(self, mock_matcher):
"""
This tests the message from GitHub for a PR
"""
# Set up return values
"""PR reopen uses webhook action, not topic suffix."""
mock_matcher.return_value = "JIRA-1234"

# Call the function
response = i.PR.from_github(
upstream="github",
pr=self.mock_github_pr,
suffix="reopened",
suffix="github.pull_request",
config=self.mock_config,
action="reopened",
)

# Assert that we made the calls correctly
Expand All @@ -253,6 +250,34 @@ def test_from_github_pr_reopen(self, mock_matcher):
self.mock_github_pr["body"], self.mock_github_pr["comments"]
)

@mock.patch(PATH + "matcher")
def test_from_github_pr_flat_topic_normalizes_suffix(self, mock_matcher):
"""Flat topic: suffix from webhook action (+ merged when closed); else open."""
mock_matcher.return_value = "JIRA-1"
flat = "github.pull_request"
_omit_action = object()
Comment thread
webbnh marked this conversation as resolved.
Resolved
cases = (
("closed with merge", {"merged": True}, "closed", "merged"),
("closed without merge", {"merged": False}, "closed", "closed"),
("reopened", {}, "reopened", "reopened"),
("opened", {}, "opened", "open"),
("edited maps to open", {}, "edited", "open"),
("missing action", {}, _omit_action, "open"),
)
for name, pr_extra, action, expected in cases:
with self.subTest(name):
pr = {**self.mock_github_pr, **pr_extra}
base_kw = dict(
upstream="github",
pr=pr,
suffix=flat,
config=self.mock_config,
)
if action is not _omit_action:
base_kw["action"] = action
Comment thread
webbnh marked this conversation as resolved.
Resolved
response = i.PR.from_github(**base_kw)
self.assertEqual(response.suffix, expected)

def test_matcher(self):
"""This tests the matcher function"""
# Found in content, no comments
Expand Down
2 changes: 2 additions & 0 deletions tests/test_upstream_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def test_handle_github_message(self, mock_pr_from_github, mock_github):
},
"mock_suffix",
self.mock_config,
action=None,
Comment thread
webbnh marked this conversation as resolved.
Resolved
)
mock_github.assert_called_with("mock_token", retry=5)
self.assertEqual("Successful Call!", response)
Expand Down Expand Up @@ -200,6 +201,7 @@ def test_github_issues(
},
"open",
self.mock_config,
action=None,
)
self.mock_github_client.get_repo.assert_called_with("org/repo")
self.mock_github_repo.get_pull.assert_called_with(number="1234")
Expand Down
Loading