Skip to content

Commit 30f22b2

Browse files
authored
Merge pull request #55 from saadmk11/pr-branch-config
[Experimental Feature] Pull Request Branch Input Option
2 parents e4fc5d8 + 7bac55a commit 30f22b2

6 files changed

Lines changed: 77 additions & 25 deletions

File tree

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ jobs:
7979
8080
These are the inputs that can be provided on the workflow.
8181
82-
| Name | Required | Description | Default | Example |
83-
|-------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|--------------------------------------------|
84-
| `token` | Yes | GitHub Access Token with `workflow` scope (The Token needs to be added to the actions secrets) | `null` | `${{ secrets.WORKFLOW_SECRET }}` |
85-
| `committer_username` | No | Name of the user who will commit the changes to GitHub | "github-actions[bot]" | "Test User" |
86-
| `committer_email` | No | Email Address of the user who will commit the changes to GitHub | "github-actions[bot]@users.noreply.github.com" | "test@test.com" |
87-
| `commit_message` | No | Commit message for the commits created by the action | "Update GitHub Action Versions" | "Custom Commit Message" |
88-
| `pull_request_title` | No | Title of the pull requests generated by the action | "Update GitHub Action Versions" | "Custom PR Title" |
89-
| `ignore` | No | A comma separated string of GitHub Actions to ignore updates for | `null` | "actions/checkout@v2, actions/cache@v2" |
90-
| `skip_pull_request` | No | If **"true"**, the action will only check for updates and if any update is found the job will fail and update the build summary with the diff (**Options:** "true", "false") | "false" | "true" |
91-
| `update_version_with` | No | Use The Latest Release Tag/Commit SHA or Default Branch Commit SHA to update the actions (**options:** "release-tag", "release-commit-sha", "default-branch-sha"') | "release-tag" | "release-commit-sha" |
92-
| `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" |
93-
| `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" |
94-
| `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" |
95-
| `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" |
82+
| Name | Required | Description | Default | Example |
83+
|--------------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|--------------------------------------------|
84+
| `token` | Yes | GitHub Access Token with `workflow` scope (The Token needs to be added to the actions secrets) | `null` | `${{ secrets.WORKFLOW_SECRET }}` |
85+
| `committer_username` | No | Name of the user who will commit the changes to GitHub | "github-actions[bot]" | "Test User" |
86+
| `committer_email` | No | Email Address of the user who will commit the changes to GitHub | "github-actions[bot]@users.noreply.github.com" | "test@test.com" |
87+
| `commit_message` | No | Commit message for the commits created by the action | "Update GitHub Action Versions" | "Custom Commit Message" |
88+
| `pull_request_title` | No | Title of the pull requests generated by the action | "Update GitHub Action Versions" | "Custom PR Title" |
89+
| `pull_request_branch` (Experimental) | No | The pull request branch name. (If provided, the action will force push to the branch) | "gh-actions-update-<timestamp>" | "github/actions-update" |
90+
| `ignore` | No | A comma separated string of GitHub Actions to ignore updates for | `null` | "actions/checkout@v2, actions/cache@v2" |
91+
| `skip_pull_request` | No | If **"true"**, the action will only check for updates and if any update is found the job will fail and update the build summary with the diff (**Options:** "true", "false") | "false" | "true" |
92+
| `update_version_with` | No | Use The Latest Release Tag/Commit SHA or Default Branch Commit SHA to update the actions (**options:** "release-tag", "release-commit-sha", "default-branch-sha"') | "release-tag" | "release-commit-sha" |
93+
| `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" |
94+
| `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" |
95+
| `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+
| `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" |
9697

9798
#### Workflow with all options
9899

@@ -133,6 +134,8 @@ jobs:
133134
pull_request_user_reviewers: "octocat, hubot, other_user"
134135
pull_request_team_reviewers: "justice-league, other_team"
135136
extra_workflow_locations: "path/to/directory, path/to/workflow.yaml"
137+
# [Experimental]
138+
pull_request_branch: "actions-update"
136139
```
137140
138141
### Important Note

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
description: 'Title of the pull requests generated by the action'
2222
required: false
2323
default: 'Update GitHub Action Versions'
24+
pull_request_branch:
25+
description: 'The pull request branch name (If provided, the action will force push to the branch)'
26+
required: false
27+
default: ''
2428
ignore:
2529
description: 'A comma separated string of GitHub Actions to ignore updates for'
2630
required: false

src/config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import time
34
from collections.abc import Mapping
45
from pathlib import Path
56
from typing import Any, NamedTuple
@@ -47,6 +48,7 @@ class Configuration(NamedTuple):
4748
git_committer_username: str = "github-actions[bot]"
4849
git_committer_email: str = "github-actions[bot]@users.noreply.github.com"
4950
pull_request_title: str = "Update GitHub Action Versions"
51+
pull_request_branch: str | None = None
5052
commit_message: str = "Update GitHub Action Versions"
5153
ignore_actions: set[str] = set()
5254
update_version_with: str = LATEST_RELEASE_TAG
@@ -55,6 +57,15 @@ class Configuration(NamedTuple):
5557
release_types: list[str] = ALL_RELEASE_TYPES
5658
extra_workflow_paths: set[str] = set()
5759

60+
def get_pull_request_branch_name(self) -> tuple[bool, str]:
61+
"""
62+
Get the pull request branch name.
63+
If the branch name is provided by the user set the force push flag to True
64+
"""
65+
if self.pull_request_branch is None:
66+
return (False, f"gh-actions-update-{int(time.time())}")
67+
return (True, self.pull_request_branch)
68+
5869
@property
5970
def git_commit_author(self) -> str:
6071
"""git_commit_author option"""
@@ -81,6 +92,7 @@ def get_user_config(cls, env: Mapping[str, str | None]) -> dict[str, str | None]
8192
"git_committer_username": env.get("INPUT_COMMITTER_USERNAME"),
8293
"git_committer_email": env.get("INPUT_COMMITTER_EMAIL"),
8394
"pull_request_title": env.get("INPUT_PULL_REQUEST_TITLE"),
95+
"pull_request_branch": env.get("INPUT_PULL_REQUEST_BRANCH"),
8496
"commit_message": env.get("INPUT_COMMIT_MESSAGE"),
8597
"ignore_actions": env.get("INPUT_IGNORE"),
8698
"update_version_with": env.get("INPUT_UPDATE_VERSION_WITH"),
@@ -196,3 +208,15 @@ def clean_extra_workflow_paths(value: Any) -> set[str] | None:
196208
)
197209

198210
return workflow_file_paths
211+
212+
@staticmethod
213+
def clean_pull_request_branch(value: Any) -> str | None:
214+
if value and isinstance(value, str):
215+
if value.lower() in ["main", "master"]:
216+
gha_utils.error(
217+
"Invalid input for `pull_request_branch` field, "
218+
"the action does not support `main` or `master` branches"
219+
)
220+
raise SystemExit(1)
221+
return value
222+
return None

src/main.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ def run(self) -> None:
7777
gha_utils.append_job_summary(pull_request_body)
7878

7979
if not self.user_config.skip_pull_request:
80-
new_branch_name = f"gh-actions-update-{int(time.time())}"
80+
(
81+
force_push,
82+
new_branch_name,
83+
) = self.user_config.get_pull_request_branch_name()
8184
create_new_git_branch(self.env.base_branch, new_branch_name)
8285
git_commit_changes(
8386
self.user_config.commit_message,
8487
self.user_config.git_commit_author,
8588
new_branch_name,
89+
force_push,
8690
)
8791
pull_request_number = create_pull_request(
8892
self.user_config.pull_request_title,
@@ -92,13 +96,14 @@ def run(self) -> None:
9296
pull_request_body,
9397
self.user_config.github_token,
9498
)
95-
add_pull_request_reviewers(
96-
self.env.repository,
97-
pull_request_number,
98-
self.user_config.pull_request_user_reviewers,
99-
self.user_config.pull_request_team_reviewers,
100-
self.user_config.github_token,
101-
)
99+
if pull_request_number is not None:
100+
add_pull_request_reviewers(
101+
self.env.repository,
102+
pull_request_number,
103+
self.user_config.pull_request_user_reviewers,
104+
self.user_config.pull_request_team_reviewers,
105+
self.user_config.github_token,
106+
)
102107
else:
103108
add_git_diff_to_job_summary()
104109
gha_utils.error(

src/run_git.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def create_new_git_branch(base_branch_name: str, new_branch_name: str) -> None:
3737

3838

3939
def git_commit_changes(
40-
commit_message: str, commit_author: str, commit_branch_name: str
40+
commit_message: str,
41+
commit_author: str,
42+
commit_branch_name: str,
43+
force_push: bool = False,
4144
) -> None:
4245
"""
4346
Commit the changed files.
@@ -47,7 +50,13 @@ def git_commit_changes(
4750
run_subprocess_command(
4851
["git", "commit", f"--author={commit_author}", "-m", commit_message]
4952
)
50-
run_subprocess_command(["git", "push", "-u", "origin", commit_branch_name])
53+
push_command = ["git", "push", "-u"]
54+
55+
if force_push:
56+
push_command.append("-f")
57+
58+
push_command.extend(["origin", commit_branch_name])
59+
run_subprocess_command(push_command)
5160

5261

5362
def git_has_changes() -> bool:

src/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def create_pull_request(
2424
head_branch_name: str,
2525
body: str,
2626
github_token: str | None = None,
27-
) -> int:
27+
) -> int | None:
2828
"""Create pull request on GitHub"""
2929
with gha_utils.group("Create Pull Request"):
3030
url = f"https://api.github.com/repos/{repository_name}/pulls"
@@ -46,6 +46,13 @@ def create_pull_request(
4646
)
4747
return response_data["number"]
4848

49+
elif (
50+
response.status_code == 422
51+
and "A pull request already exists for" in response.text
52+
):
53+
gha_utils.notice("A pull request already exists")
54+
return None
55+
4956
gha_utils.error(
5057
f"Could not create a pull request on "
5158
f"{repository_name}, GitHub API Response: {response.json()}"

0 commit comments

Comments
 (0)