Skip to content

Commit 5fc56b2

Browse files
authored
Merge pull request #238 from jmrt47/main
feat: Add configuration option for dependabot labels
2 parents 0a550b3 + 98ee916 commit 5fc56b2

6 files changed

+235
-30
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ This action can be configured to authenticate with GitHub App Installation or Pe
8282
| `REPO_SPECIFIC_EXEMPTIONS` | False | "" | A list of repositories that should be exempt from specific package ecosystems similar to EXEMPT_ECOSYSTEMS but those apply to all repositories. ex: `org1/repo1:docker,github-actions;org1/repo2:pip` would set exempt_ecosystems for `org1/repo1` to be `['docker', 'github-actions']`, and for `org1/repo2` it would be `['pip']`, while for every other repository evaluated, it would be set by the env variable `EXEMPT_ECOSYSTEMS`. NOTE: If you want specific exemptions to be added on top of the already specified global exemptions, you need to add the global exemptions to each repo specific exemption. |
8383
| `SCHEDULE` | False | 'weekly' | Schedule interval by which to check for dependency updates via Dependabot. Allowed values are 'daily', 'weekly', or 'monthly' |
8484
| `SCHEDULE_DAY` | False | '' | Scheduled day by which to check for dependency updates via Dependabot. Allowed values are days of the week full names (i.e., 'monday') |
85+
| `LABELS` | False | "" | A comma separated list of labels that should be added to pull requests opened by dependabot. |
8586

8687
### Example workflows
8788

dependabot_file.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def make_dependabot_config(
8-
ecosystem, group_dependencies, indent, schedule, schedule_day
8+
ecosystem, group_dependencies, indent, schedule, schedule_day, labels
99
) -> str:
1010
"""
1111
Make the dependabot configuration for a specific package ecosystem
@@ -16,6 +16,7 @@ def make_dependabot_config(
1616
indent: the number of spaces to indent the dependabot configuration ex: " "
1717
schedule: the schedule to run dependabot ex: "daily"
1818
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "weekly"
19+
labels: the list of labels to be added to dependabot configuration
1920
2021
Returns:
2122
str: the dependabot configuration for the package ecosystem
@@ -31,6 +32,13 @@ def make_dependabot_config(
3132
{indent}{indent}{indent}interval: '{schedule}'{schedule_day_line}
3233
"""
3334

35+
if labels:
36+
dependabot_config += f"""{indent}{indent}labels:
37+
"""
38+
for label in labels:
39+
dependabot_config += f"""{indent}{indent}{indent}- \"{label}\"
40+
"""
41+
3442
if group_dependencies:
3543
dependabot_config += f"""{indent}{indent}groups:
3644
{indent}{indent}{indent}production-dependencies:
@@ -49,6 +57,7 @@ def build_dependabot_file(
4957
existing_config,
5058
schedule,
5159
schedule_day,
60+
labels,
5261
) -> str | None:
5362
"""
5463
Build the dependabot.yml file for a repo based on the repo contents
@@ -61,6 +70,7 @@ def build_dependabot_file(
6170
existing_config: the existing dependabot configuration file or None if it doesn't exist
6271
schedule: the schedule to run dependabot ex: "daily"
6372
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "daily"
73+
labels: the list of labels to be added to dependabot configuration
6474
6575
Returns:
6676
str: the dependabot.yml file for the repo
@@ -144,7 +154,12 @@ def build_dependabot_file(
144154
if dependabot_file and dependabot_file[-1] != "\n":
145155
dependabot_file += "\n"
146156
dependabot_file += make_dependabot_config(
147-
manager, group_dependencies, indent, schedule, schedule_day
157+
manager,
158+
group_dependencies,
159+
indent,
160+
schedule,
161+
schedule_day,
162+
labels,
148163
)
149164
break
150165
except github3.exceptions.NotFoundError:
@@ -157,7 +172,12 @@ def build_dependabot_file(
157172
if file[0].endswith(".tf"):
158173
package_managers_found["terraform"] = True
159174
dependabot_file += make_dependabot_config(
160-
"terraform", group_dependencies, indent, schedule, schedule_day
175+
"terraform",
176+
group_dependencies,
177+
indent,
178+
schedule,
179+
schedule_day,
180+
labels,
161181
)
162182
break
163183
except github3.exceptions.NotFoundError:
@@ -173,6 +193,7 @@ def build_dependabot_file(
173193
indent,
174194
schedule,
175195
schedule_day,
196+
labels,
176197
)
177198
break
178199
except github3.exceptions.NotFoundError:

env.py

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def get_env_vars(
117117
dict,
118118
str,
119119
str,
120+
list[str],
120121
]:
121122
"""
122123
Get the environment variables for use in the action.
@@ -148,6 +149,7 @@ def get_env_vars(
148149
repo_specific_exemptions (dict): A dictionary of per repository ecosystem exemptions
149150
schedule (str): The schedule to run the action on
150151
schedule_day (str): The day of the week to run the action on if schedule is daily
152+
labels (list[str]): A list of labels to be added to dependabot configuration
151153
"""
152154

153155
if not test:
@@ -324,6 +326,11 @@ def get_env_vars(
324326
"SCHEDULE_DAY environment variable not 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', or 'sunday'"
325327
)
326328

329+
labels_str = os.getenv("LABELS")
330+
labels_list = []
331+
if labels_str:
332+
labels_list = [label.lower().strip() for label in labels_str.split(",")]
333+
327334
return (
328335
organization,
329336
repositories_list,
@@ -349,4 +356,5 @@ def get_env_vars(
349356
repo_specific_exemptions,
350357
schedule,
351358
schedule_day,
359+
labels_list,
352360
)

evergreen.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def main(): # pragma: no cover
3939
repo_specific_exemptions,
4040
schedule,
4141
schedule_day,
42+
labels,
4243
) = env.get_env_vars()
4344

4445
# Auth to GitHub.com or GHE
@@ -114,6 +115,7 @@ def main(): # pragma: no cover
114115
existing_config,
115116
schedule,
116117
schedule_day,
118+
labels,
117119
)
118120

119121
if dependabot_file is None:

0 commit comments

Comments
 (0)