Skip to content

Commit dd543a7

Browse files
Merge pull request #44 from NHSDigital/aiva2/CCM-6405_TemplateRepoSync
CCM-6405 Adding github workflow to sync template repo
2 parents a256ef4 + 6b79f76 commit dd543a7

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Files and folders to ignore when syncing nhs-notify-repository-template back in to this repository
2+
scripts/config/.repository-template-sync-ignore
3+
.github/workflows/
4+
nhs-notify-repository-template/
5+
6+
# Files and Folders in this repository to ignore
7+
.vscode/
8+
CHANGELOG.md
9+
project.code-workspace
10+
README.md
11+
VERSION
12+
13+
# Files and Folders in the template repository to disregard
14+
.devcontainer/
15+
.github/workflows/cicd-*.yaml
16+
*/examples/
17+
docs/
18+
infrastructure/terraform/components/
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Script to synchronise the nhs-notify-template-repository with this repository
6+
#
7+
# Usage:
8+
# $ [options] ./check-terraform-format.sh
9+
#
10+
# Options:
11+
# new_only=true # Only identify new files from the template-repository
12+
# changes_only=true # Only identify files which have drifted from the template-repository
13+
14+
# ==============================================================================
15+
16+
# Command line prameters
17+
new_only=${new_only:-false}
18+
changes_only=${changes_only:-false}
19+
20+
# Set variables
21+
TEMPLATE_REPO_DIR="nhs-notify-repository-template"
22+
IGNORE_FILE="scripts/config/.repository-template-sync-ignore"
23+
24+
# Check if the template directory exists
25+
if [ ! -d "${TEMPLATE_REPO_DIR}" ]; then
26+
echo "Template directory ${TEMPLATE_REPO_DIR} not found!"
27+
exit 1
28+
fi
29+
30+
# Check if the .template-ignore file exists, create an empty one if not
31+
if [ ! -f "${IGNORE_FILE}" ]; then
32+
echo "# Files and folders to ignore when syncing ${TEMPLATE_REPO_DIR} back in to this repository" > ${IGNORE_FILE}
33+
echo "# Files and Folders in this repository to ignore" >> ${IGNORE_FILE}
34+
echo "# Files and Folders in the template repository to disregard" >> ${IGNORE_FILE}
35+
fi
36+
37+
# Read the .template-ignore file into an array
38+
while IFS= read -r line || [ -n "$line" ]; do
39+
IGNORED_PATHS+=("$line")
40+
done < "$IGNORE_FILE"
41+
42+
# Check if a file is ignored.
43+
is_ignored() {
44+
local file=${1}
45+
46+
# Ignore .git directories and files
47+
if [[ "$file" == *.git/* ]]; then
48+
return 0
49+
fi
50+
51+
for ignored in "${IGNORED_PATHS[@]}"; do
52+
if [[ -n "$ignored" && "$file" =~ $ignored ]]; then
53+
return 0
54+
fi
55+
done
56+
return 1
57+
}
58+
59+
# Navigate to the template directory
60+
cd "${TEMPLATE_REPO_DIR}" || exit
61+
FILES_ADDED=()
62+
FILES_WITH_CHANGES=()
63+
64+
# Loop through all files in the template directory
65+
while IFS= read -r -d '' file; do
66+
relative_path="${file#./}" # Remove leading './'
67+
68+
# Check if the file is ignored
69+
if is_ignored "$relative_path"; then
70+
echo "Ignoring $relative_path"
71+
continue
72+
fi
73+
74+
target_path="../$relative_path"
75+
mkdir -p "$(dirname "$target_path")"
76+
77+
# Copy the file to the root directory if it doesn't exist or is different
78+
if [ ! -f "$target_path" ] && [ "$changes_only" == false ]; then
79+
echo "Copying $relative_path to the repository"
80+
FILES_ADDED+=("${relative_path}")
81+
cp "$file" "$target_path"
82+
83+
else
84+
# If the file exists, check if it's different
85+
if [ "$new_only" == false ]; then
86+
if ! diff -q "$file" "$target_path" > /dev/null 2>&1; then
87+
echo "Merging changes from $relative_path"
88+
FILES_WITH_CHANGES+=("${relative_path}")
89+
cp "$file" "$target_path"
90+
fi
91+
fi
92+
fi
93+
done < <(find . -type f -print0)
94+
95+
echo "${#FILES_ADDED[@]}" files added, "${#FILES_WITH_CHANGES[@]}" files with changes detected.
96+
97+
echo ------------------------------------------
98+
99+
if [ "$changes_only" == false ]; then
100+
echo ------------------------------------------
101+
echo "New files added:"
102+
printf ' - %s\n' "${FILES_ADDED[@]}"
103+
fi
104+
105+
106+
if [ "$new_only" == false ]; then
107+
echo ------------------------------------------
108+
echo "Changed files:"
109+
printf ' - %s\n' "${FILES_WITH_CHANGES[@]}"
110+
fi

0 commit comments

Comments
 (0)