Skip to content

Commit

Permalink
Specify repository template branch to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
stefaniuk committed Aug 16, 2023
1 parent 44c71d1 commit c2c3aee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Locally
make docker-build
make docker-test \
REPOSITORY_TEMPLATE=github.com/nhs-england-tools/repository-template \
BRANCH_NAME=main \
REPOSITORY_TO_UPDATE=github.com/nhs-england-tools/update-from-template-app \
GIT_USER_NAME="Update from Template App" \
GIT_USER_EMAIL="[email protected]" \
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# TODO

- Specify repository template branch to be used
- Move file operations from the shell script to `cmd/update-from-template`
- Simplify and optmise for speed `cmd/update-from-template` implementation
- Check [actions-template-sync/src/sync_template.sh](https://github.com/AndreasAugustin/actions-template-sync/blob/54cc6daa8773c61a6df312b2cb9f4f82ef72d690/src/sync_template.sh#L35C27-L35C49) implementation
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ inputs:
repository_template:
description: "Repository template"
required: true
branch_name:
description: "Branch name from the template repository to use, default is 'main'"
required: false
repository_to_update:
description: "Repository to update"
required: true
Expand Down Expand Up @@ -37,6 +40,7 @@ runs:
env:
BUILD_DATETIME: ${{ inputs.build_datetime }}
REPOSITORY_TEMPLATE: ${{ inputs.repository_template }}
BRANCH_NAME: ${{ inputs.branch_name }}
REPOSITORY_TO_UPDATE: ${{ inputs.repository_to_update }}
GIT_USER_NAME: ${{ inputs.git_user_name }}
GIT_USER_EMAIL: ${{ inputs.git_user_email }}
Expand Down
28 changes: 16 additions & 12 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ build_datetime_local=$(date --date="$datetime" +"%Y-%m-%dT%H:%M:%S%z")
build_datetime=$datetime
build_timestamp=$(date --date="$datetime" -u +"%Y%m%d%H%M%S")

branch_name=${BRANCH_NAME:-"main"}
org=$(echo "$REPOSITORY_TO_UPDATE" | cut -d'/' -f2)

work_dir=/github/workspace
src_dir=$work_dir/repository-template
dest_dir=$work_dir/repository-to-update
list_of_files_to_update_json=${work_dir}/update-from-template.json
list_of_files_to_update_json=$work_dir/update-from-template.json

git_user_name=${GIT_USER_NAME:-"unknown"}
git_user_email=${GIT_USER_EMAIL:-"[email protected]"}
Expand All @@ -25,7 +28,6 @@ GITHUB_APP_SK_CONTENT_FILE=$work_dir/gh_app_sk.pem
if ! [ -f $GITHUB_APP_SK_CONTENT_FILE ]; then
echo "$GITHUB_APP_SK_CONTENT" > $GITHUB_APP_SK_CONTENT_FILE
fi
org=$(echo "${REPOSITORY_TO_UPDATE}" | cut -d'/' -f2)

# ==============================================================================

Expand Down Expand Up @@ -112,13 +114,15 @@ function fetch-repositories-content() {
)
fi
fi
cd $src_dir
git checkout $branch_name
}

function prune-legacy-updates() {

[ -z "$github_token" ] && return

cd ${dest_dir}
cd $dest_dir
# Close legacy PRs
pr_numbers=$(gh pr list --search "Update from template" --json number,title | jq '.[] | select(.title | startswith("Update from template")).number')
for pr_number in $pr_numbers; do
Expand All @@ -132,18 +136,18 @@ function prune-legacy-updates() {

function checkout-new-branch() {

cd ${dest_dir}
cd $dest_dir
git checkout -b update-from-template-${build_timestamp}
}

function produce-list-of-files-to-update() {

cd ${dest_dir}
cd $dest_dir
/update-from-template \
--source-dir ${src_dir} \
--destination-dir ${dest_dir} \
--source-dir $src_dir \
--destination-dir $dest_dir \
--app-config-file /update-from-template.yaml \
--template-config-file ${dest_dir}/scripts/config/repository-template.yaml \
--template-config-file $dest_dir/scripts/config/repository-template.yaml \
> $list_of_files_to_update_json
}

Expand All @@ -156,22 +160,22 @@ function update-files() {
)
echo "$to_update" | while IFS= read -r file; do
dir=$(dirname "$file")
mkdir -p ${dest_dir}/$dir
cp ${src_dir}/$file ${dest_dir}/$file
mkdir -p $dest_dir/$dir
cp $src_dir/$file $dest_dir/$file
done
# Delete files
to_delete=$(
cat $list_of_files_to_update_json \
| jq -r '.comparison | to_entries[] | select(.value.action == "delete") | .key'
)
echo "$to_delete" | while IFS= read -r file; do
rm -rf ${dest_dir}/$file
rm -rf $dest_dir/$file
done
}

function push-and-create-pull-request() {

cd ${dest_dir}
cd $dest_dir
# Add and commit changes
git add -A
git commit -m "Update from template ${build_datetime_local}"
Expand Down
1 change: 1 addition & 0 deletions scripts/docker/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ docker-test: # Test Docker image
source ./scripts/docker/docker.lib.sh
args=" \
-e REPOSITORY_TEMPLATE \
-e BRANCH_NAME \
-e REPOSITORY_TO_UPDATE \
-e GIT_USER_NAME \
-e GIT_USER_EMAIL \
Expand Down
1 change: 1 addition & 0 deletions tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ runs:
with:
build_datetime: "${{ inputs.build_datetime }}"
repository_template: github.com/nhs-england-tools/repository-template
branch_name: "main"
repository_to_update: github.com/nhs-england-tools/update-from-template-app
git_user_name: "Update from Template App"
git_user_email: "[email protected]"
Expand Down

0 comments on commit c2c3aee

Please sign in to comment.