Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pull request branch configuration #40

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ These are the inputs that can be provided on the workflow.
| `committer_email` | No | Email Address of the user who will commit the changes to GitHub | "github-actions[bot]@users.noreply.github.com" | "[email protected]" |
| `commit_message` | No | Commit message for the commits created by the action | "Update GitHub Action Versions" | "Custom Commit Message" |
| `pull_request_title` | No | Title of the pull requests generated by the action | "Update GitHub Action Versions" | "Custom PR Title" |
| `pull_request_branch` | No | The pull request branch name. | "gh-actions-update-<timestamp>" | "github/actions-update" |
| `ignore` | No | A comma separated string of GitHub Actions to ignore updates for | `null` | "actions/checkout@v2, actions/cache@v2" |
| `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" |
| `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" |
Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
description: 'Title of the pull requests generated by the action'
required: false
default: 'Update GitHub Action Versions'
pull_request_branch:
description: 'The pull request branch name'
required: false
default: 'gh-actions-update-<timestamp>'
ignore:
description: 'A comma separated string of GitHub Actions to ignore updates for'
required: false
Expand Down
2 changes: 2 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Configuration(NamedTuple):
git_committer_username: str = "github-actions[bot]"
git_committer_email: str = "github-actions[bot]@users.noreply.github.com"
pull_request_title: str = "Update GitHub Action Versions"
pull_request_branch: str | None = None
commit_message: str = "Update GitHub Action Versions"
ignore_actions: set[str] = set()
update_version_with: str = LATEST_RELEASE_TAG
Expand Down Expand Up @@ -79,6 +80,7 @@ def get_user_config(cls, env: Mapping[str, str | None]) -> dict[str, str | None]
"git_committer_username": env.get("INPUT_COMMITTER_USERNAME"),
"git_committer_email": env.get("INPUT_COMMITTER_EMAIL"),
"pull_request_title": env.get("INPUT_PULL_REQUEST_TITLE"),
"pull_request_branch": env.get("INPUT_PULL_REQUEST_BRANCH"),
"commit_message": env.get("INPUT_COMMIT_MESSAGE"),
"ignore_actions": env.get("INPUT_IGNORE"),
"update_version_with": env.get("INPUT_UPDATE_VERSION_WITH"),
Expand Down
20 changes: 12 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def run(self) -> None:
gha_utils.append_job_summary(pull_request_body)

if not self.user_config.skip_pull_request:
new_branch_name = f"gh-actions-update-{int(time.time())}"
new_branch_name = self.user_config.pull_request_branch
if new_branch_name is None:
new_branch_name = f"gh-actions-update-{int(time.time())}"

create_new_git_branch(self.env.base_branch, new_branch_name)
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved
git_commit_changes(
self.user_config.commit_message,
Expand All @@ -91,13 +94,14 @@ def run(self) -> None:
pull_request_body,
self.user_config.github_token,
)
add_pull_request_reviewers(
self.env.repository,
pull_request_number,
self.user_config.pull_request_user_reviewers,
self.user_config.pull_request_team_reviewers,
self.user_config.github_token,
)
if pull_request_number is not None:
add_pull_request_reviewers(
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved
self.env.repository,
pull_request_number,
self.user_config.pull_request_user_reviewers,
self.user_config.pull_request_team_reviewers,
self.user_config.github_token,
)
else:
add_git_diff_to_job_summary()
gha_utils.error(
Expand Down
2 changes: 1 addition & 1 deletion src/run_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def git_commit_changes(
run_subprocess_command(
["git", "commit", f"--author={commit_author}", "-m", commit_message]
)
run_subprocess_command(["git", "push", "-u", "origin", commit_branch_name])
run_subprocess_command(["git", "push", "-fu", "origin", commit_branch_name])


def git_has_changes() -> bool:
Expand Down
9 changes: 8 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create_pull_request(
head_branch_name: str,
body: str,
github_token: str | None = None,
) -> int:
) -> int | None:
"""Create pull request on GitHub"""
with gha_utils.group("Create Pull Request"):
url = f"https://api.github.com/repos/{repository_name}/pulls"
Expand All @@ -46,6 +46,13 @@ def create_pull_request(
)
return response_data["number"]

elif (
response.status_code == 422
and "A pull request already exists for" in response.text
):
gha_utils.warning("A pull request already exists")
return None

gha_utils.error(
f"Could not create a pull request on "
f"{repository_name}, GitHub API Response: {response.json()}"
Expand Down