Skip to content

Commit

Permalink
feat: automatic label creation
Browse files Browse the repository at this point in the history
* UPDATE main template sync script to support the automatic creation
of labels and skip PR cleanup if no labels are set

* UPDATE README in order to reflect these changes and explain the
reasoning behind it
  • Loading branch information
kevin-aude committed Jan 23, 2024
1 parent 507e164 commit cf3d47b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
source_repo_path: <owner/repo>
upstream_branch: <target_branch> # defaults to main
pr_labels: <label1>,<label2>[,...] # optional, no default
pr_labels: <label1>,<label2>[,...] # defaults to template_sync
```
You will receive a pull request within your repository if there are some changes available in the template.
Expand Down Expand Up @@ -139,7 +139,7 @@ jobs:
github_token: ${{ steps.generate_token.outputs.token }}
source_repo_path: <owner/repo>
upstream_branch: <target_branch> # defaults to main
pr_labels: <label1>,<label2>[,...] # optional, no default
pr_labels: <label1>,<label2>[,...] # defaults to template_sync
```
#### 2. Using SSH
Expand Down Expand Up @@ -175,7 +175,7 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
source_repo_path: ${{ secrets.SOURCE_REPO_PATH }} # <owner/repo>, should be within secrets
upstream_branch: ${{ secrets.TARGET_BRANCH }} #<target_branch> # defaults to main
pr_labels: <label1>,<label2>[,...] # optional, no default
pr_labels: <label1>,<label2>[,...] # defaults to template_sync
source_repo_ssh_private_key: ${{ secrets.SOURCE_REPO_SSH_PRIVATE_KEY }} # contains the private ssh key of the private repository
```

Expand Down Expand Up @@ -240,7 +240,7 @@ jobs:
| source_repo_ssh_private_key | `[optional]` private ssh key for the source repository. [see](#private-template-repository) | `false` | |
| pr_branch_name_prefix | `[optional]` the prefix of branches created by this action | `false` | `chore/template_sync` |
| pr_title | `[optional]` the title of PRs opened by this action. Must be already created. | `false` | `upstream merge template repository` |
| pr_labels | `[optional]` comma separated list. [pull request labels][pr-labels]. Must be already created. | `false` | |
| pr_labels | `[optional]` comma separated list. [pull request labels][pr-labels]. | `false` | `sync_template` |
| pr_reviewers | `[optional]` comma separated list of pull request reviewers. | `false` | |
| pr_commit_msg | `[optional]` commit message in the created pull request | `false` | `chore(template): merge template changes :up:` |
| hostname | `[optional]` the hostname of the repository | `false` | `github.com` |
Expand Down Expand Up @@ -340,6 +340,14 @@ hooks:
- echo 'hi, we are within the prepr phase'
- echo 'maybe you want to change the code a bit and do another push before creating the pr'
```
## Labels creation
By default, generated PRs will be labeled with the `template_sync` label.
If that label doesn't exist in your repository, it will be created automatically unless you specify your own existing labels.
Associating a label with the generated PRs helps keeping track of them and allows for features like automatic PR cleanup.

## Pull request cleanup
Depending on your way of working, you may end up with multiple pull requests related to template syncing pointing to the same branch.
If you want to avoid this situation, you can instruct this action to clean up older PRs pointing to the same branch (search based on labels).

## Troubleshooting

Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ inputs:
default: "upstream merge template repository"
pr_labels:
description: "[optional] comma separated list of pull request labels"
default: "template_sync"
pr_reviewers:
description: "[optional] comma separated list of pull request reviewers"
pr_commit_msg:
Expand Down
73 changes: 58 additions & 15 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ echo "::endgroup::"
cmd_from_yml_file "prepull"

echo "::group::Pull template"

debug "create new branch from default branch with name ${NEW_BRANCH}"
git checkout -b "${NEW_BRANCH}"
debug "pull changes from template"
Expand Down Expand Up @@ -97,6 +98,7 @@ fi
cmd_from_yml_file "precommit"

echo "::group::commit changes"

git add .

# we are checking the ignore file if it exists or is empty
Expand Down Expand Up @@ -124,31 +126,69 @@ echo "::endgroup::"
cleanup_older_prs () {
cmd_from_yml_file "precleanup"

if [ "$IS_DRY_RUN" != "true" ]; then
echo "::group::cleanup older PRs"
gh pr list \
--base "${UPSTREAM_BRANCH}" \
--state open \
--label "${PR_LABELS}" \
--json number \
--template '{{range .}}{{printf "%v" .number}}{{"\n"}}{{end}}' | xargs -L1 gh pr close
echo "::endgroup::"
gh pr list \
--base "${UPSTREAM_BRANCH}" \
--state open \
--label "${PR_LABELS}" \
--json number \
--template '{{range .}}{{printf "%v" .number}}{{"\n"}}{{end}}' | xargs -L1 gh pr close
}
echo "::group::cleanup older PRs"

if [ "$IS_DRY_RUN" != "true" ]; then
if [ "$IS_PR_CLEANUP" != "false" ]; then
if [[ -z "${PR_LABELS}" ]]; then
cleanup_older_prs
else
warn "env var 'PR_LABELS' is empty. Skipping older prs cleanup"
fi
else
warn "dry_run option is set to off. Skipping older prs cleanup"
warn "is_pr_cleanup option is set to off. Skipping older prs cleanup"
fi
else
warn "dry_run option is set to off. Skipping older prs cleanup"
fi

echo "::endgroup::"


maybe_create_labels () {
all_labels=${PR_LABELS//,/$'\n'} # change the semicolons to white space
for label in $all_labels
do
search_result=$(gh label list \
--search "${label}" \
--limit 1 \
--json name \
--template '{{range .}}{{printf "%v" .name}}{{"\n"}}{{end}}')

if [ "${search_result}" = "${label}" ]; then
info "label '${label}' was found in the repository"
else
gh label create "${label}"
info "label '${label}' was missing and has been created"
fi
done
}

if [ "$IS_PR_CLEANUP" != "false" ]; then
cleanup_older_prs
echo "::group::check for missing labels"

if [[ -z "${PR_LABELS}" ]]; then
info "env var 'PR_LABELS' is empty. Skipping labels check"
else
warn "is_pr_cleanup option is set to off. Skipping older prs cleanup"
if [ "$IS_DRY_RUN" != "true" ]; then
maybe_create_labels
else
warn "dry_run option is set to off. Skipping labels check"
fi
fi

echo "::endgroup::"

push_and_create_pr () {
cmd_from_yml_file "prepush"

if [ "$IS_DRY_RUN" != "true" ]; then
echo "::group::push changes and create PR"
debug "push changes"
git push --set-upstream origin "${NEW_BRANCH}"

Expand All @@ -160,10 +200,13 @@ push_and_create_pr () {
--base "${UPSTREAM_BRANCH}" \
--label "${PR_LABELS}" \
--reviewer "${PR_REVIEWERS}"
echo "::endgroup::"
else
warn "dry_run option is set to off. Skipping push changes and skip create pr"
fi
}

echo "::group::push changes and create PR"

push_and_create_pr

echo "::endgroup::"

0 comments on commit cf3d47b

Please sign in to comment.