Skip to content

Commit 962eec3

Browse files
authored
Merge pull request #56 from saadmk11/add-pr-labels
[Feature] Add Option to Add Labels to Pull Requests
2 parents 30f22b2 + d7a9b2d commit 962eec3

5 files changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ These are the inputs that can be provided on the workflow.
9393
| `release_types` | No | A comma separated string of release types to use when updating the actions. By default, all release types are used to update the actions. Only Applicable for **"release-tag", "release-commit-sha"** (**Options:** "major", "minor", "patch" **[one or many seperated by comma]**) | "all" | "minor, patch" |
9494
| `pull_request_user_reviewers` | No | A comma separated string (usernames) which denotes the users that should be added as reviewers to the pull request | `null` | "octocat, hubot, other_user" |
9595
| `pull_request_team_reviewers` | No | A comma separated string (team slugs) which denotes the teams that should be added as reviewers to the pull request | `null` | "justice-league, other_team" |
96+
| `pull_request_labels` | No | A comma separated string (label names) which denotes the labels which will be added to the pull request | `null` | "dependencies, automated" |
9697
| `extra_workflow_locations` | No | A comma separated string of file or directory paths to look for workflows. By default, only the workflow files in the `.github/workflows` directory are checked updates | `null` | "path/to/directory, path/to/workflow.yaml" |
9798

9899
#### Workflow with all options
@@ -133,6 +134,7 @@ jobs:
133134
release_types: "minor, patch"
134135
pull_request_user_reviewers: "octocat, hubot, other_user"
135136
pull_request_team_reviewers: "justice-league, other_team"
137+
pull_request_labels: "dependencies, automated"
136138
extra_workflow_locations: "path/to/directory, path/to/workflow.yaml"
137139
# [Experimental]
138140
pull_request_branch: "actions-update"

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ inputs:
5252
description: 'A comma separated string (team slugs) which denotes the teams that should be added as reviewers to the pull request'
5353
required: false
5454
default: ''
55+
pull_request_labels:
56+
description: 'A comma separated string (label names) which denotes the labels which will be added to the pull request'
57+
required: false
58+
default: ''
5559
extra_workflow_locations:
5660
description: 'A comma separated string of file or directory paths to look for workflows. By default, only the workflow files in the .github/workflows directory are checked updates'
5761
required: false

src/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Configuration(NamedTuple):
5454
update_version_with: str = LATEST_RELEASE_TAG
5555
pull_request_user_reviewers: set[str] = set()
5656
pull_request_team_reviewers: set[str] = set()
57+
pull_request_labels: set[str] = set()
5758
release_types: list[str] = ALL_RELEASE_TYPES
5859
extra_workflow_paths: set[str] = set()
5960

@@ -99,6 +100,7 @@ def get_user_config(cls, env: Mapping[str, str | None]) -> dict[str, str | None]
99100
"release_types": env.get("INPUT_RELEASE_TYPES"),
100101
"pull_request_user_reviewers": env.get("INPUT_PULL_REQUEST_USER_REVIEWERS"),
101102
"pull_request_team_reviewers": env.get("INPUT_PULL_REQUEST_TEAM_REVIEWERS"),
103+
"pull_request_labels": env.get("INPUT_PULL_REQUEST_LABELS"),
102104
"extra_workflow_paths": env.get("INPUT_EXTRA_WORKFLOW_LOCATIONS"),
103105
}
104106
return user_config
@@ -148,6 +150,12 @@ def clean_pull_request_team_reviewers(value: Any) -> set[str] | None:
148150
return {s.strip() for s in value.strip().split(",") if s}
149151
return None
150152

153+
@staticmethod
154+
def clean_pull_request_labels(value: Any) -> set[str] | None:
155+
if value and isinstance(value, str):
156+
return {s.strip() for s in value.strip().split(",") if s}
157+
return None
158+
151159
@staticmethod
152160
def clean_release_types(value: Any) -> list[str] | None:
153161
if value and isinstance(value, str):

src/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
from .utils import (
3131
add_git_diff_to_job_summary,
32+
add_pull_request_labels,
3233
add_pull_request_reviewers,
3334
create_pull_request,
3435
display_whats_new,
@@ -104,6 +105,12 @@ def run(self) -> None:
104105
self.user_config.pull_request_team_reviewers,
105106
self.user_config.github_token,
106107
)
108+
add_pull_request_labels(
109+
self.env.repository,
110+
pull_request_number,
111+
self.user_config.pull_request_labels,
112+
self.user_config.github_token,
113+
)
107114
else:
108115
add_git_diff_to_job_summary()
109116
gha_utils.error(

src/utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,43 @@ def add_pull_request_reviewers(
104104
)
105105

106106

107+
def add_pull_request_labels(
108+
repository_name: str,
109+
pull_request_number: int,
110+
labels: set[str],
111+
github_token: str | None = None,
112+
) -> None:
113+
"""Request reviewers for a pull request on GitHub"""
114+
with gha_utils.group(f"Add Labels to Pull Request #{pull_request_number}"):
115+
116+
if not labels:
117+
gha_utils.echo("No labels to add.")
118+
return
119+
120+
payload = {"labels": list(labels)}
121+
122+
url = (
123+
f"https://github.com/__api/repos/{repository_name}/issues"
124+
f"/{pull_request_number}/labels"
125+
)
126+
127+
response = requests.post(
128+
url, json=payload, headers=get_request_headers(github_token)
129+
)
130+
131+
if response.status_code == 200:
132+
gha_utils.notice(
133+
f"Added '{labels}' labels to "
134+
f"pull request #{pull_request_number} \U0001F389"
135+
)
136+
return
137+
138+
gha_utils.error(
139+
f"Could not add labels to pull request #{pull_request_number} "
140+
f"on {repository_name}, GitHub API Response: {response.json()}"
141+
)
142+
143+
107144
def add_git_diff_to_job_summary() -> None:
108145
"""Add git diff to job summary"""
109146
markdown_diff = (

0 commit comments

Comments
 (0)