Skip to content

Commit

Permalink
Merge pull request #238 from jmrt47/main
Browse files Browse the repository at this point in the history
feat: Add configuration option for dependabot labels
  • Loading branch information
zkoppert authored Oct 4, 2024
2 parents 0a550b3 + 98ee916 commit 5fc56b2
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ This action can be configured to authenticate with GitHub App Installation or Pe
| `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. |
| `SCHEDULE` | False | 'weekly' | Schedule interval by which to check for dependency updates via Dependabot. Allowed values are 'daily', 'weekly', or 'monthly' |
| `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') |
| `LABELS` | False | "" | A comma separated list of labels that should be added to pull requests opened by dependabot. |

### Example workflows

Expand Down
27 changes: 24 additions & 3 deletions dependabot_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def make_dependabot_config(
ecosystem, group_dependencies, indent, schedule, schedule_day
ecosystem, group_dependencies, indent, schedule, schedule_day, labels
) -> str:
"""
Make the dependabot configuration for a specific package ecosystem
Expand All @@ -16,6 +16,7 @@ def make_dependabot_config(
indent: the number of spaces to indent the dependabot configuration ex: " "
schedule: the schedule to run dependabot ex: "daily"
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "weekly"
labels: the list of labels to be added to dependabot configuration
Returns:
str: the dependabot configuration for the package ecosystem
Expand All @@ -31,6 +32,13 @@ def make_dependabot_config(
{indent}{indent}{indent}interval: '{schedule}'{schedule_day_line}
"""

if labels:
dependabot_config += f"""{indent}{indent}labels:
"""
for label in labels:
dependabot_config += f"""{indent}{indent}{indent}- \"{label}\"
"""

if group_dependencies:
dependabot_config += f"""{indent}{indent}groups:
{indent}{indent}{indent}production-dependencies:
Expand All @@ -49,6 +57,7 @@ def build_dependabot_file(
existing_config,
schedule,
schedule_day,
labels,
) -> str | None:
"""
Build the dependabot.yml file for a repo based on the repo contents
Expand All @@ -61,6 +70,7 @@ def build_dependabot_file(
existing_config: the existing dependabot configuration file or None if it doesn't exist
schedule: the schedule to run dependabot ex: "daily"
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "daily"
labels: the list of labels to be added to dependabot configuration
Returns:
str: the dependabot.yml file for the repo
Expand Down Expand Up @@ -144,7 +154,12 @@ def build_dependabot_file(
if dependabot_file and dependabot_file[-1] != "\n":
dependabot_file += "\n"
dependabot_file += make_dependabot_config(
manager, group_dependencies, indent, schedule, schedule_day
manager,
group_dependencies,
indent,
schedule,
schedule_day,
labels,
)
break
except github3.exceptions.NotFoundError:
Expand All @@ -157,7 +172,12 @@ def build_dependabot_file(
if file[0].endswith(".tf"):
package_managers_found["terraform"] = True
dependabot_file += make_dependabot_config(
"terraform", group_dependencies, indent, schedule, schedule_day
"terraform",
group_dependencies,
indent,
schedule,
schedule_day,
labels,
)
break
except github3.exceptions.NotFoundError:
Expand All @@ -173,6 +193,7 @@ def build_dependabot_file(
indent,
schedule,
schedule_day,
labels,
)
break
except github3.exceptions.NotFoundError:
Expand Down
8 changes: 8 additions & 0 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_env_vars(
dict,
str,
str,
list[str],
]:
"""
Get the environment variables for use in the action.
Expand Down Expand Up @@ -148,6 +149,7 @@ def get_env_vars(
repo_specific_exemptions (dict): A dictionary of per repository ecosystem exemptions
schedule (str): The schedule to run the action on
schedule_day (str): The day of the week to run the action on if schedule is daily
labels (list[str]): A list of labels to be added to dependabot configuration
"""

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

labels_str = os.getenv("LABELS")
labels_list = []
if labels_str:
labels_list = [label.lower().strip() for label in labels_str.split(",")]

return (
organization,
repositories_list,
Expand All @@ -349,4 +356,5 @@ def get_env_vars(
repo_specific_exemptions,
schedule,
schedule_day,
labels_list,
)
2 changes: 2 additions & 0 deletions evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def main(): # pragma: no cover
repo_specific_exemptions,
schedule,
schedule_day,
labels,
) = env.get_env_vars()

# Auth to GitHub.com or GHE
Expand Down Expand Up @@ -114,6 +115,7 @@ def main(): # pragma: no cover
existing_config,
schedule,
schedule_day,
labels,
)

if dependabot_file is None:
Expand Down
Loading

0 comments on commit 5fc56b2

Please sign in to comment.