Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add script to simulate the update #173

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionaries/project.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
amannn
aquasecurity
buildx
codeowners
conventionalcommits
cpus
dockerhub
Expand Down
3 changes: 1 addition & 2 deletions .config/dictionaries/workflow.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
codeowners
hapag
Hapag
nullglob
3 changes: 1 addition & 2 deletions .github/workflows/scripts/check_dictionaries.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/bin/bash
set -euo pipefail

#
# Based on the idea outlined in https://github.com/streetsidesoftware/cspell/issues/2536#issuecomment-1126077282
# but with a few modifications to fit the needs of our project.
#

set -euo pipefail

MISSPELLED_WORDS_PATH="misspelled-words.txt"

CSPELL_CONFIGURATION_FILE=$(find . -name "cspell.json" | head -n 1)
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea/
.idea/

# output of simulate.sh
simulate-*/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ marked with `USE_WORKFLOW` which are valid within this repository only.

Make sure that this block is well formatted, otherwise the update script will fail in the related repository due to prettier.

### Simulate the update

Use the `simulate.sh` script to check the changes before applying them to the repository. The script

- creates a new repository called `simulate-*` and applies the changes there
- updates the `workflow.txt` dictionary in your current branch

### Spell Checker

1. Add the words to the `.config/dictionaries/workflow.txt` file.
Expand Down
44 changes: 44 additions & 0 deletions simulate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail

DICTIONARIES_PATH=".config/dictionaries"

path=$(mktemp -d -t simulate-XXXXX -p .)
path_origin=$(mktemp -d -t simulate-origin-XXXXX -p .)

echo "Creating the remote repository in $path_origin"
(
git init --initial-branch main "$path_origin"

cd "$path_origin"
touch README.md
git add README.md
git commit -m "xxx"
)

echo "Creating a new repository in $path"
git init --initial-branch main "$path"

cd "$path"
git remote add origin "../$path_origin"
git fetch origin main
git checkout main

cp ../update_workflows.sh .

./update_workflows.sh github-only --force . --dry-run --local-workflow-path ../
# script is now located in .github/
rm update_workflows.sh

echo "Creating the word list file"

# don't use the cspell.json file to get the list of misspelled words we need to add to the dictionary
mv .config/cspell.json .config/cspell.json.temp

# renovate: datasource=github-releases depName=streetsidesoftware/cspell
cspell_version="v8.17.1"
npx cspell@${cspell_version:1} . --dot --no-progress --no-summary --unique --words-only --no-exit-code --exclude ".git/**" --exclude ".idea/**" --exclude "$DICTIONARIES_PATH/**" | sort --ignore-case --unique > ../.config/dictionaries/workflow.txt

mv .config/cspell.json.temp .config/cspell.json

echo "Dictionary workflow.txt updated in your branch. Please review and commit the changes."
25 changes: 19 additions & 6 deletions update_workflows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ release_type="auto"
force_execution="false"
repository_path=$(pwd)
dry_run="false"
local_workflow_path=""

branch_name="update-workflows-$(date +%s)"

Expand All @@ -31,7 +32,7 @@ function restart_script_if_newer_version_available() {
repository_path=$1
latest_template_path=$2

current_sha=$(sha256sum "$repository_path/.github/update_workflows.sh" | cut -d " " -f 1)
current_sha=$(sha256sum "$repository_path/.github/update_workflows.sh" | cut -d " " -f 1 || echo "missing")
new_sha=$(sha256sum "$latest_template_path/update_workflows.sh" | cut -d " " -f 1)

if [ "$current_sha" != "$new_sha" ]; then
Expand Down Expand Up @@ -114,6 +115,11 @@ function ensure_and_set_parameters_or_exit() {
shift
shift
;;
--local-workflow-path)
local_workflow_path=$2
shift
shift
;;
--release-type)
release_type=$2
shift
Expand Down Expand Up @@ -189,13 +195,17 @@ ensure_repo_preconditions_or_exit

cd "$repository_path" || exit 8

echo "Fetching the latest version of the workflows"
latest_template_path=$local_workflow_path

latest_template_path=$(mktemp -d -t repository-templates-XXXXX)
gh repo clone https://github.com/Hapag-Lloyd/Workflow-Templates.git "$latest_template_path" -- -b main -q
if [ -z "$latest_template_path" ]; then
echo "Cloning the latest version of the workflows"

latest_template_path=$(mktemp -d -t repository-templates-XXXXX)
gh repo clone https://github.com/Hapag-Lloyd/Workflow-Templates.git "$latest_template_path" -- -b main -q

if [ "$force_execution" != "true" ]; then
restart_script_if_newer_version_available "$repository_path" "$latest_template_path"
else
echo "Using the local workflow path $latest_template_path and do not check for a newer version of the script"
fi

echo "Updating the workflows in $repository_path"
Expand Down Expand Up @@ -370,4 +380,7 @@ pre-commit install -c .config/.pre-commit-config.yaml

create_commit_and_pr .

rm -rf "$latest_template_path"
# do not remove the latest template path if it was provided as a parameter
if [ -z "$local_workflow_path" ]; then
rm -rf "$latest_template_path"
fi
Loading