Skip to content

Commit

Permalink
Merge pull request #335 from open-craft/navin/remove-obsolete-labels
Browse files Browse the repository at this point in the history
feat: remove obsolete labels on pr close
  • Loading branch information
feanil authored Jan 21, 2025
2 parents 97350f9 + 4401cf3 commit 403d1ca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
21 changes: 21 additions & 0 deletions openedx_webhooks/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,24 @@
"blended",
"open-source-contribution",
}

GITHUB_MERGED_PR_OBSOLETE_LABELS = {
"blocked by other work",
"changes requested",
"inactive",
"needs maintainer attention",
"needs more product information",
"needs rescoping",
"needs reviewer assigned",
"needs test run",
"waiting for eng review",
"waiting on author",
}

GITHUB_CLOSED_PR_OBSOLETE_LABELS = {
"needs maintainer attention",
"needs reviewer assigned",
"needs test run",
"waiting for eng review",
"waiting on author",
}
18 changes: 9 additions & 9 deletions openedx_webhooks/tasks/pr_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
)
from openedx_webhooks.labels import (
GITHUB_CATEGORY_LABELS,
GITHUB_CLOSED_PR_OBSOLETE_LABELS,
GITHUB_MERGED_PR_OBSOLETE_LABELS,
GITHUB_STATUS_LABELS,
)
from openedx_webhooks import settings
Expand All @@ -68,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 @@ -225,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 @@ -477,6 +472,11 @@ 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
state = get_pr_state(self.pr)
if state == "closed":
ad_hoc_labels -= GITHUB_CLOSED_PR_OBSOLETE_LABELS
elif state == "merged":
ad_hoc_labels -= GITHUB_MERGED_PR_OBSOLETE_LABELS
desired_labels.update(ad_hoc_labels)

if desired_labels != self.current.github_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
17 changes: 17 additions & 0 deletions tests/test_pull_request_closed.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,20 @@ 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):
"""
Test whether obsolete labels are removed on closing merge requests
"""
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.close(merge=is_merged)
pull_request_changed(pr.as_json())
assert pr.labels == {"open-source-contribution", "custom label 1"}

0 comments on commit 403d1ca

Please sign in to comment.