Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 10 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,16 @@ 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"})
if action:
if action == "reopened":
suffix = "reopened"
elif action == "closed":
suffix = "merged" if pr.get("merged") else "closed"
else:
suffix = "closed"
suffix = "open"
elif suffix not in lifecycle:
suffix = "open"

# Return our PR object
return cls(
Expand Down
2 changes: 1 addition & 1 deletion 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, body.get("action"))


def github_prs(upstream, config):
Expand Down
38 changes: 32 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,35 @@ 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"
cases = (
("closed with merge", {"merged": True}, "closed", "merged", flat),
("closed without merge", {"merged": False}, "closed", "closed", flat),
("reopened", {}, "reopened", "reopened", flat),
("opened", {}, "opened", "open", flat),
("edited maps to open", {}, "edited", "open", flat),
("missing action flat topic", {}, None, "open", flat),
("missing action preserves closed", {}, None, "closed", "closed"),
("missing action preserves merged", {}, None, "merged", "merged"),
)
for name, pr_extra, action, expected, suffix in cases:
with self.subTest(name):
pr = {**self.mock_github_pr, **pr_extra}
base_kw = dict(
upstream="github",
pr=pr,
suffix=suffix,
config=self.mock_config,
)
if action is not None:
base_kw["action"] = action
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
1 change: 1 addition & 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,
None,
)
mock_github.assert_called_with("mock_token", retry=5)
self.assertEqual("Successful Call!", response)
Expand Down
Loading