Skip to content

Commit

Permalink
fix(#507): 🐛 pr title and body internal variable substitution (#508)
Browse files Browse the repository at this point in the history
* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* fix: 🐛

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

* feat: ✨ add output

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>

---------

Signed-off-by: Andy Augustin <dev@andreas-augustin.org>
  • Loading branch information
AndreasAugustin committed Mar 18, 2024
1 parent be793c4 commit 912b4e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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_body | `[optional]` the body of PRs opened by this action. | `false` | `Merge ${SOURCE_REPO_PATH} ${TEMPLATE_GIT_HASH}` |
| pr_body | `[optional]` the body of PRs opened by this action. | `false` | `Merge ${SOURCE_REPO} ${TEMPLATE_GIT_HASH}` |
| 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:` |
Expand All @@ -265,6 +265,7 @@ jobs:
| output | description |
| ------ | ----------- |
| pr_branch | The name of the branch used for the pull request |
| template_git_hash | The template source repository git hash |

**Remarks** Please consider following edge cases

Expand Down Expand Up @@ -394,15 +395,14 @@ or you set the hooks input parameter within the action definition with a related

The following hooks are supported (please check [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for a better understanding of the lifecycles).

* `install` is executed after the container has started and after reading and setting up the environment.
* `prepull` is executed before the code is pulled from the source repository
* `precommit` is executed before the code is commited
* `prepush` is executed before the push is executed, right after the commit
* `precleanup` is executed before older PRs targeting the same branch are closed
* `prepr` is executed before the PR is done

**Remark** The underlying OS is defined by an Alpine container.
E.q. for the installation phase you need to use commands like `apk add --update --no-cache python3`
**Remark** If you need to install aditional tools just install them in an additional step upfront the action invokation.
If using the docker image the underlying OS is defined by an Alpine container.

### Example for the hooks input parameter

Expand Down Expand Up @@ -441,10 +441,6 @@ Please not the double quotes within the following `prepull echo` command

```yml
hooks:
install:
commands:
- apk add --update --no-cache python3
- python3 --version
prepull:
commands:
- echo "hi, we are within the prepull phase ${MY_VAR}"
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ outputs:
pr_branch:
description: "The name of the PR branch"
value: ${{ steps.sync.outputs.pr_branch }}
template_git_hash:
description: "The git hash of the template source repository"
value: ${{ steps.sync.outputs.template_git_hash }}
runs:
using: "composite"
# image: "src/Dockerfile"
Expand Down
42 changes: 31 additions & 11 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,30 @@ fi
IS_FORCE_PUSH_PR="${IS_FORCE_PUSH_PR:-"false"}"
GIT_REMOTE_PULL_PARAMS="${GIT_REMOTE_PULL_PARAMS:---allow-unrelated-histories --squash --strategy=recursive -X theirs}"

cmd_from_yml "install"

TEMPLATE_SYNC_IGNORE_FILE_PATH=".templatesyncignore"
TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk '{print $1}')
NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}")
NEW_BRANCH="${PR_BRANCH_NAME_PREFIX}_${NEW_TEMPLATE_GIT_HASH}"
PR_BODY="${PR_BODY:-Merge ${SOURCE_REPO_PATH} ${NEW_TEMPLATE_GIT_HASH}}"
debug "new Git HASH ${NEW_TEMPLATE_GIT_HASH}"
SHORT_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}")

export TEMPLATE_GIT_HASH=${SHORT_TEMPLATE_GIT_HASH}
export PR_BRANCH="${PR_BRANCH_NAME_PREFIX}_${TEMPLATE_GIT_HASH}"
: "${PR_BODY:="Merge ${SOURCE_REPO} ${TEMPLATE_GIT_HASH}"}"
: "${PR_TITLE:-"upstream merge template repository"}"

# for some reasons the substitution is not working as expected
# so we substitute manually
# shellcheck disable=SC2016
PR_BODY=${PR_BODY//'${TEMPLATE_GIT_HASH}'/"${TEMPLATE_GIT_HASH}"}
# shellcheck disable=SC2016
PR_BODY=${PR_BODY//'${SOURCE_REPO}'/"${SOURCE_REPO}"}

# shellcheck disable=SC2016
PR_TITLE=${PR_TITLE//'${TEMPLATE_GIT_HASH}'/"${TEMPLATE_GIT_HASH}"}
# shellcheck disable=SC2016
PR_TITLE=${PR_TITLE//'${SOURCE_REPO}'/"${SOURCE_REPO}"}

debug "TEMPLATE_GIT_HASH ${TEMPLATE_GIT_HASH}"
debug "PR_BRANCH ${PR_BRANCH}"
debug "PR_BODY ${PR_BODY}"

# Check if the Ignore File exists inside .github folder or if it doesn't exist at all
if [[ -f ".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" || ! -f "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then
Expand All @@ -68,18 +84,22 @@ fi
# set the gh action outputs if run with github action.
# Arguments:
# pr_branch
# template_git_hash
#######################################
function set_github_action_outputs() {
echo "::group::set gh action outputs"

local pr_branch=$1
local template_git_hash=$2

info "set github action outputs"

if [[ -z "${GITHUB_RUN_ID}" ]]; then
info "env var 'GITHUB_RUN_ID' is empty -> no github action workflow"
else
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
echo "pr_branch=${pr_branch}" >> "$GITHUB_OUTPUT"
echo "template_git_hash=${template_git_hash}" >> "$GITHUB_OUTPUT"
fi
echo "::endgroup::"
}
Expand Down Expand Up @@ -359,7 +379,7 @@ function prechecks() {
warn "skipping prechecks because we force push and pr"
return 0
fi
check_branch_remote_existing "${NEW_BRANCH}"
check_branch_remote_existing "${PR_BRANCH}"

check_if_commit_already_in_hist_graceful_exit "${TEMPLATE_REMOTE_GIT_HASH}"

Expand All @@ -373,8 +393,8 @@ function checkout_branch_and_pull() {

echo "::group::checkout branch and pull"

debug "create new branch from default branch with name ${NEW_BRANCH}"
git checkout -b "${NEW_BRANCH}"
debug "create new branch from default branch with name ${PR_BRANCH}"
git checkout -b "${PR_BRANCH}"
debug "pull changes from template"

pull_source_changes "${SOURCE_REPO}" "${GIT_REMOTE_PULL_PARAMS}"
Expand Down Expand Up @@ -437,7 +457,7 @@ function push_prepare_pr_create_pr() {
echo "::group::push changes and create PR"

cmd_from_yml "prepush"
push "${NEW_BRANCH}" "${IS_FORCE_PUSH_PR}"
push "${PR_BRANCH}" "${IS_FORCE_PUSH_PR}"
cmd_from_yml "prepr"
if [ "$IS_FORCE_PUSH_PR" == true ] ; then
create_or_edit_pr "${PR_TITLE}" "${PR_BODY}" "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${PR_REVIEWERS}"
Expand All @@ -458,4 +478,4 @@ commit

push_prepare_pr_create_pr

set_github_action_outputs "${NEW_BRANCH}"
set_github_action_outputs "${PR_BRANCH}" "${TEMPLATE_GIT_HASH}"
4 changes: 2 additions & 2 deletions templatesync.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# validate

hooks:
install:
prepull:
commands:
- echo "foo ${MY_VAR}"
- echo 'hi, we are within the prepush phase'
- echo 'hi, we are within the prepull phase'

prepush:
commands:
Expand Down

0 comments on commit 912b4e0

Please sign in to comment.