Skip to content

Commit

Permalink
test: removing obsolete labels
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Jan 21, 2025
1 parent 4803aea commit a19eb97
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
16 changes: 5 additions & 11 deletions openedx_webhooks/tasks/pr_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
)
from openedx_webhooks.types import GhProject, JiraId, PrDict, PrId
from openedx_webhooks.utils import (
get_pr_state,
log_check_response,
sentry_extra_context,
text_summary,
Expand Down Expand Up @@ -227,15 +228,7 @@ def desired_support_state(pr: PrDict) -> PrDesiredInfo:
else:
desired.is_ospr = True

if pr.get("hook_action") == "reopened":
state = "reopened"
elif pr["state"] == "open":
state = "open"
elif pr["merged"]:
state = "merged"
else:
state = "closed"

state = get_pr_state(pr)
# A label of jira:xyz means we want a Jira issue in the xyz Jira.
desired.jira_nicks = {name.partition(":")[-1] for name in label_names if name.startswith("jira:")}

Expand Down Expand Up @@ -479,9 +472,10 @@ def _fix_github_labels(self) -> None:
"""
desired_labels = set(self.desired.github_labels)
ad_hoc_labels = self.current.github_labels - GITHUB_CATEGORY_LABELS - GITHUB_STATUS_LABELS
if self.pr["state"] == "closed":
state = get_pr_state(self.pr)
if state == "closed":
ad_hoc_labels -= GITHUB_CLOSED_PR_OBSOLETE_LABELS
elif self.pr["state"] == "merged":
elif state == "merged":
ad_hoc_labels -= GITHUB_MERGED_PR_OBSOLETE_LABELS
desired_labels.update(ad_hoc_labels)

Expand Down
17 changes: 16 additions & 1 deletion openedx_webhooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from openedx_webhooks import logger
from openedx_webhooks.auth import get_github_session, get_jira_session
from openedx_webhooks.types import JiraDict
from openedx_webhooks.types import JiraDict, PrDict


def environ_get(name: str, default=None) -> str:
Expand Down Expand Up @@ -345,3 +345,18 @@ def jira_get(jira_nick, *args, **kwargs):
if resp.content:
return resp
return get_jira_session(jira_nick).get(*args, **kwargs)


def get_pr_state(pr: PrDict):
"""
Get gthub pull request state.
"""
if pr.get("hook_action") == "reopened":
state = "reopened"
elif pr["state"] == "open":
state = "open"
elif pr["merged"]:
state = "merged"
else:
state = "closed"
return state
1 change: 1 addition & 0 deletions tests/fake_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def _patch_issues(self, match, request, _context) -> Dict:
patch = request.json()
if "labels" in patch:
pr.set_labels(patch["labels"])
r.pull_requests[pr.number] = pr
return pr.as_json()

# Commmits
Expand Down
18 changes: 18 additions & 0 deletions tests/test_pull_request_closed.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,21 @@ def test_pr_closed_after_employee_leaves(org, is_merged, fake_github, mocker):
assert pr.status(CLA_CONTEXT) == CLA_STATUS_GOOD
assert pr.labels == set()
assert pull_request_projects(pr.as_json()) == set()


def test_pr_closed_labels(fake_github, is_merged):
# When a core committer merges a pull request, ping the champions.
# But when it's closed without merging, no ping.
# Use body=None here to test missing bodies also.
pr = fake_github.make_pull_request(
user="newuser",
owner="openedx",
repo="edx-platform",
body=None,
)
pr.set_labels({"open-source-contribution", "waiting on author", "needs test run", "custom label 1"})

# pr.set_labels({})
pr.close(merge=is_merged)
pull_request_changed(pr.as_json())
assert pr.labels == {"open-source-contribution", "custom label 1"}

0 comments on commit a19eb97

Please sign in to comment.