diff --git a/changelog.d/20231120_114120_nedbat_from_github_label_279.rst b/changelog.d/20231120_114120_nedbat_from_github_label_279.rst new file mode 100644 index 00000000..6fb258ec --- /dev/null +++ b/changelog.d/20231120_114120_nedbat_from_github_label_279.rst @@ -0,0 +1,6 @@ +.. A new scriv changelog fragment. + +- Now issues created in Jira will have a label of "from-GitHub" on them. Closes + `issue 279`_. + +.. _issue 279: https://github.com/openedx/openedx-webhooks/issues/279 diff --git a/docs/details.rst b/docs/details.rst index df9958c5..dcf8253e 100644 --- a/docs/details.rst +++ b/docs/details.rst @@ -128,3 +128,5 @@ bot will create a Jira issue in the Jira server with the "xyz" nickname. Each Jira server can specify a mapping of repos to other Jira details such as the Jira project for the issue, and the issue type to create. + +Jira issues created this way will have a "from-GitHub" Jira label applied. diff --git a/openedx_webhooks/tasks/pr_tracking.py b/openedx_webhooks/tasks/pr_tracking.py index d7ed237c..b7aa2c04 100644 --- a/openedx_webhooks/tasks/pr_tracking.py +++ b/openedx_webhooks/tasks/pr_tracking.py @@ -138,8 +138,6 @@ class PrDesiredInfo: # don't need to force a new status, but can leave the existing status. jira_status: Optional[str] = None - jira_labels: Set[str] = field(default_factory=set) - # The bot-controlled labels we want to on the pull request. # See labels.py:CATEGORY_LABELS github_labels: Set[str] = field(default_factory=set) @@ -411,7 +409,7 @@ def _make_jira_issue(self, jira_nick) -> None: issuetype=issuetype, summary=self.desired.jira_title, description=self.desired.jira_description, - labels=list(self.desired.jira_labels), + labels=["from-GitHub"], ) jira_id = JiraId(jira_nick, issue_data["key"]) diff --git a/tests/fake_jira.py b/tests/fake_jira.py index efd555ef..c0e99170 100644 --- a/tests/fake_jira.py +++ b/tests/fake_jira.py @@ -2,6 +2,7 @@ import dataclasses import itertools +import re from dataclasses import dataclass, field from typing import Dict, Optional, Set @@ -26,6 +27,15 @@ class Issue: summary: Optional[str] = None labels: Set[str] = field(default_factory=set) + def __post_init__(self) -> None: + # Jira labels can't have spaces in them. Check that they are only + # letters, numbers, dashes. + for label in self.labels: + if re.search(r"[^a-zA-Z0-9-]", label): + raise ValueError(f"Label {label!r} has invalid characters") + if len(label) < 3: + raise ValueError(f"Label {label!r} is too short") + def as_json(self) -> Dict: return { "key": self.key, diff --git a/tests/test_fake_jira.py b/tests/test_fake_jira.py index eec14c0a..e79f0344 100644 --- a/tests/test_fake_jira.py +++ b/tests/test_fake_jira.py @@ -1,5 +1,6 @@ """Tests of FakeJira.""" +import pytest import requests @@ -10,7 +11,7 @@ class TestIssues: Tests of the correct behavior of issuees. """ def test_get_issue(self, fake_jira): - fake_jira.make_issue(key="HELLO-123", summary="This is a bad bug!") + fake_jira.make_issue(key="HELLO-123", summary="This is a bad bug!", labels=["bad-bug"]) resp = requests.get("https://test.atlassian.net/rest/api/2/issue/HELLO-123") assert resp.status_code == 200 issue = resp.json() @@ -74,3 +75,9 @@ class TestBadRequests: def test_no_such_put(self, fake_jira): resp = requests.put("https://test.atlassian.net/rest/api/2/issue/XYZ-999") assert resp.status_code == 404 + + def test_bad_label(self, fake_jira): + with pytest.raises(ValueError, match="Label 'a bug' has invalid characters"): + fake_jira.make_issue(key="HELLO-123", summary="a bug!", labels=["a bug"]) + with pytest.raises(ValueError, match="Label 'a' is too short"): + fake_jira.make_issue(key="HELLO-123", summary="a bug!", labels="a bug") diff --git a/tests/test_pull_request_opened.py b/tests/test_pull_request_opened.py index aba5eed2..243534d9 100644 --- a/tests/test_pull_request_opened.py +++ b/tests/test_pull_request_opened.py @@ -370,6 +370,7 @@ def test_jira_labelling(fake_github, fake_jira, fake_jira2): Line2 """ ) + assert jira_issue.labels == {"from-GitHub"} # Processing the pull request again won't make another issue. result = pull_request_changed(pr.as_json())