From 126ac2cefd2e9a63dda2ba86479bcdad4ebb4b87 Mon Sep 17 00:00:00 2001 From: aidenvaines-bjss Date: Fri, 6 Sep 2024 11:05:05 +0100 Subject: [PATCH 1/2] CCM-6405 adding initial repo sync script and workflow --- .../scheduled-repository-template-sync.yaml | 55 +++++++++ .../config/.repository-template-sync-ignore | 16 +++ scripts/githooks/sync-template-repo.sh | 111 ++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 .github/workflows/scheduled-repository-template-sync.yaml create mode 100644 scripts/config/.repository-template-sync-ignore create mode 100755 scripts/githooks/sync-template-repo.sh diff --git a/.github/workflows/scheduled-repository-template-sync.yaml b/.github/workflows/scheduled-repository-template-sync.yaml new file mode 100644 index 0000000..5b3cd1f --- /dev/null +++ b/.github/workflows/scheduled-repository-template-sync.yaml @@ -0,0 +1,55 @@ +name: Repository Template Sync + +on: + schedule: + - cron: '0 0 1 * *' + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + checks: read + +jobs: + update-external-repo: + runs-on: ubuntu-latest + + steps: + - name: Check out the repository + uses: actions/checkout@v3 + + - name: Check out external repository + uses: actions/checkout@v3 + with: + repository: nhsdigital/nhs-notify-repository-template + path: nhs-notify-repository-template + + - name: Run syncronisation script + run: | + /scripts/githooks/sync-template-repo.sh + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v7.0.1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: Drift from template + branch: scheduledTemplateRepositorySync + delete-branch: true + title: '[Template Sync] Drift from template-repository remediation' + body: | + # Resultant drift from repository template + + ## Who should respond to this PR? + The team which owns the responsibility for the services the repository maintains + + ## How to progress this PR + The repositories guardians should review the contents of the PR and decide + how to proceed, you may wish to back-out certain changes or accept them from the upstream + `nhsdigital/nhs-notify-repository-template` repository. + + If there are changes you do not wish to see again, it is recommended you add exclusions to + `scripts/config/.repository-template-sync-ignore` + labels: | + template + automation + draft: false diff --git a/scripts/config/.repository-template-sync-ignore b/scripts/config/.repository-template-sync-ignore new file mode 100644 index 0000000..009adf1 --- /dev/null +++ b/scripts/config/.repository-template-sync-ignore @@ -0,0 +1,16 @@ +# Files and folders to ignore when syncing nhs-notify-repository-template back in to this repository +scripts/config/.repository-template-sync-ignore + +# Files and Folders in this repository to ignore +.vscode/ +CHANGELOG.md +project.code-workspace +README.md +VERSION + +# Files and Folders in the template repository to disregard +.devcontainer/ +.github/workflows/cicd-*.yaml +*/examples/ +docs/ +infrastructure/terraform/components/ diff --git a/scripts/githooks/sync-template-repo.sh b/scripts/githooks/sync-template-repo.sh new file mode 100755 index 0000000..d82d07e --- /dev/null +++ b/scripts/githooks/sync-template-repo.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +set -euo pipefail + +# Script to synchronise the nhs-notify-template-repository with this repository +# +# Usage: +# $ [options] ./check-terraform-format.sh +# +# Options: +# new_only=true # Only identify new files from the template-repository +# changes_only=true # Only identify files which have drifted from the template-repository + +# ============================================================================== + +# Command line prameters +new_only=${new_only:-false} +changes_only=${changes_only:-false} + +# Set variables +TEMPLATE_REPO_DIR="nhs-notify-repository-template" +IGNORE_FILE="scripts/config/.repository-template-sync-ignore" + +# Check if the template directory exists +if [ ! -d "${TEMPLATE_REPO_DIR}" ]; then + echo "Template directory ${TEMPLATE_REPO_DIR} not found!" + exit 1 +fi + +# Check if the .template-ignore file exists, create an empty one if not +if [ ! -f "${IGNORE_FILE}" ]; then + echo "# Files and folders to ignore when syncing ${TEMPLATE_REPO_DIR} back in to this repository" > ${IGNORE_FILE} + echo "# Files and Folders in this repository to ignore" >> ${IGNORE_FILE} + echo "# Files and Folders in the template repository to disregard" >> ${IGNORE_FILE} +fi + +# Read the .template-ignore file into an array +# readarray -t IGNORED_PATHS < "${IGNORE_FILE}" +while IFS= read -r line || [ -n "$line" ]; do + IGNORED_PATHS+=("$line") +done < "$IGNORE_FILE" + +# Check if a file is ignored. +is_ignored() { + local file=${1} + + # Ignore .git directories and files + if [[ "$file" == *.git/* ]]; then + return 0 + fi + + for ignored in "${IGNORED_PATHS[@]}"; do + if [[ "$file" =~ $ignored ]]; then + return 0 + fi + done + return 1 +} + +# Navigate to the template directory +cd "${TEMPLATE_REPO_DIR}" || exit +FILES_ADDED=() +FILES_WITH_CHANGES=() + +# Loop through all files in the template directory +while IFS= read -r -d '' file; do + relative_path="${file#./}" # Remove leading './' + + # Check if the file is ignored + if is_ignored "$relative_path"; then + # echo "Ignoring $relative_path" + continue + fi + + target_path="../$relative_path" + mkdir -p "$(dirname "$target_path")" + + # Copy the file to the root directory if it doesn't exist or is different + if [ ! -f "$target_path" ] && [ "$changes_only" == false ]; then + echo "Copying $relative_path to the repository" + FILES_ADDED+=("${relative_path}") + # cp "$file" "$target_path" + + else + # If the file exists, check if it's different + if [ "$new_only" == false ]; then + if ! diff -q "$file" "$target_path" > /dev/null 2>&1; then + # echo "Merging changes from $relative_path" + FILES_WITH_CHANGES+=("${relative_path}") + # cp "$file" "$target_path" + fi + fi + fi +done < <(find . -type f -print0) + +echo "${#FILES_ADDED[@]}" files added, "${#FILES_WITH_CHANGES[@]}" files with changes detected. + +echo ------------------------------------------ + +if [ "$changes_only" == false ]; then + echo ------------------------------------------ + echo "New files added:" + printf ' - %s\n' "${FILES_ADDED[@]}" +fi + + +if [ "$new_only" == false ]; then + echo ------------------------------------------ + echo "Changed files:" + printf ' - %s\n' "${FILES_WITH_CHANGES[@]}" +fi From 4fe648f053f0e892cba7c7ff0c9a5fedbad006e8 Mon Sep 17 00:00:00 2001 From: aidenvaines-bjss Date: Mon, 9 Sep 2024 09:50:48 +0100 Subject: [PATCH 2/2] CCM-6405 fixes --- scripts/githooks/sync-template-repo.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/githooks/sync-template-repo.sh b/scripts/githooks/sync-template-repo.sh index d82d07e..994d430 100755 --- a/scripts/githooks/sync-template-repo.sh +++ b/scripts/githooks/sync-template-repo.sh @@ -35,7 +35,6 @@ if [ ! -f "${IGNORE_FILE}" ]; then fi # Read the .template-ignore file into an array -# readarray -t IGNORED_PATHS < "${IGNORE_FILE}" while IFS= read -r line || [ -n "$line" ]; do IGNORED_PATHS+=("$line") done < "$IGNORE_FILE" @@ -68,7 +67,7 @@ while IFS= read -r -d '' file; do # Check if the file is ignored if is_ignored "$relative_path"; then - # echo "Ignoring $relative_path" + echo "Ignoring $relative_path" continue fi @@ -79,15 +78,15 @@ while IFS= read -r -d '' file; do if [ ! -f "$target_path" ] && [ "$changes_only" == false ]; then echo "Copying $relative_path to the repository" FILES_ADDED+=("${relative_path}") - # cp "$file" "$target_path" + cp "$file" "$target_path" else # If the file exists, check if it's different if [ "$new_only" == false ]; then if ! diff -q "$file" "$target_path" > /dev/null 2>&1; then - # echo "Merging changes from $relative_path" + echo "Merging changes from $relative_path" FILES_WITH_CHANGES+=("${relative_path}") - # cp "$file" "$target_path" + cp "$file" "$target_path" fi fi fi