Skip to content

Commit

Permalink
Split migrate_gitflow_to_tbd.sh into update_ci_infra.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkoSagadin committed Oct 2, 2023
1 parent 5bd6e9c commit d0f941d
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 83 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Changed

- Split `migrate_gitflow_to_tbd.sh` script into another, `update_ci_infra.sh`
script. First one now only does the branching model migration, second on
updates the files related to the CI infrastructure.

## [0.4.0] - 2023-10-01

### Added
Expand Down
23 changes: 15 additions & 8 deletions migration_script/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Migration script
# Migration and CI update scripts

Migration script (`migrate_gitflow_to_tbd.sh`), helps with the migration of
projects are still using the older GitFlow branching model to the newer
Trunk-based development model.

CI update script (`update_ci_infra.sh`) updates CI infrastructure in a Git
repository to the latest one in the template repositories.

## Downloading the script

Just run the below set of commands to download the script to your machine and
make it executable, super user permissions will be needed:
Just run the below set of commands to download the scripts to your machine and
make them executable, super user permissions will be needed:

```bash
sudo wget -O /usr/bin/migrate_gitflow_to_tbd https://raw.githubusercontent.com/IRNAS/irnas-workflows-software/main/migration_script/migrate_gitflow_to_tbd.sh
sudo chmod +x /usr/bin/migrate_gitflow_to_tbd
sudo wget -O /usr/bin/update_ci_infra https://raw.githubusercontent.com/IRNAS/irnas-workflows-software/main/migration_script/update_ci_infra.sh
sudo chmod +x /usr/bin/update_ci_infra
```

## Running the script
Expand All @@ -20,10 +24,13 @@ sudo chmod +x /usr/bin/migrate_gitflow_to_tbd
new GitHub release and merged the release PR into dev. Also make sure that you
do not have any uncommitted changes and any unmerged PRs.

Expected usage:
Expected usages:

```bash
migrate_gitflow_to_tbd PATH_TO_GIT_REPO WORKFLOW_GROUP
# If you want to migrate to the TBD-style repo.
migrate_gitflow_to_tbd PATH_TO_GIT_REPO
# If you only want to update CI workflows related files.
update_ci_infra PATH_TO_GIT_REPO WORKFLOW_GROUP
```

Where:
Expand All @@ -34,5 +41,5 @@ Where:
project was created from a `irnas-projects-template` repo. Use _zephyr_ if
your project was created from a `irnas-zephyr-template` repo.

Read the instructions printed by the script and follow them. **The script is not
fully automatic, some manual steps are needed at the end, follow them.**
Read the instructions printed by the script and follow them. **The scripts are
not fully automatic, some manual steps are needed at the end, follow them.**
77 changes: 2 additions & 75 deletions migration_script/migrate_gitflow_to_tbd.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Usage: migrate_gitflow_to_tbd.sh PATH_TO_GIT_REPO WORKFLOW_GROUP
# Usage: migrate_gitflow_to_tbd.sh PATH_TO_GIT_REPO
#
# Description:
# Migrate a Gitflow repository to a Trunk-based development style repository.
Expand All @@ -13,39 +13,8 @@
# PATH_TO_GIT_REPO Relative or absolute path to the Git repository that you
# want to migrate.
#
# WORKFLOW_GROUP Can be either "basic" or "zephyr".
# Use "basic" if your project was created from a
# irnas-projects-template repo.
# Use "zephyr" if your project was created from a
# irnas-zephyr-template repo.

# Details:
# If WORKFLOW_GROUP is "basic", then the following will be done:
# 1. Delete master branch, locally and remotely.
# 2. Rename dev branch to main, locally and remotely.
# 3. Delete contents of the .github directory.
# 4. Clone the irnas-projects-template repository and copy its .github
# directory into the current repository.
# 5. Delete the irnas-projects-template repository.
# 6. Create a new commit with all the changes on the main and push to
# the remote.
#
# If WORKFLOW_GROUP is "zephyr", then the following will be done:
# 1.-3. Same as above.
# 4. Clone the irnas-zephyr-template repository and copy its .github
# directory into the current repository.
# 5. Copy scripts/requirements.txt, if it exists, only append it to the
# existing one.
# 6. Copy scripts/pre_changelog.md and scripts/post_changelog.md files
# 7. Copy makefile.
# 8. Delete the irnas-zephyr-template repository.
# 9. Create a new commit with all the changes on the main and push to
# the remote.
#
# Manually change of the default branch and deletion of the dev branch in
# GitHub UI will be needed in the end.

NUM_ARGS=2
NUM_ARGS=1
# Print help text and exit if -h, or --help or insufficient number of arguments
# was given.
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ $# -lt ${NUM_ARGS} ]; then
Expand Down Expand Up @@ -120,44 +89,6 @@ git push origin -u main
# Checkout main just in case there was no dev to begin with
git checkout main

if [[ "$WORKFLOW_GROUP" == "basic" ]]; then
TEMPLATE_REPO="irnas-projects-template"
else
TEMPLATE_REPO="irnas-zephyr-template"
fi

# Get the template repo
git clone https://github.com/IRNAS/${TEMPLATE_REPO}.git

# Delete the .github directory and copy new one from the template repo.
rm -fr .github
cp -r ${TEMPLATE_REPO}/.github .

REQS_EXISTS=false
if [[ "$WORKFLOW_GROUP" == "zephyr" ]]; then
# Create scripts folder if it does not exist.
mkdir -p scripts
if [ -f scripts/requirements.txt ]; then
REQS_EXISTS=true
else
cp ${TEMPLATE_REPO}/scripts/requirements.txt scripts
fi

cp ${TEMPLATE_REPO}/scripts/pre_changelog.md scripts
cp ${TEMPLATE_REPO}/scripts/post_changelog.md scripts
cp ${TEMPLATE_REPO}/makefile .
fi

rm -fr ${TEMPLATE_REPO}

# Add new line to .gitignore
echo '!.github/workflows/build.yaml' >>.gitignore

# Commit and push the changes.
git add .
git commit -m "Migrate to trunk-based development workflow"
git push

# Get the organization and repository name of the current repository.
ORG_REPO=$(git config --get remote.origin.url | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/')

Expand All @@ -171,9 +102,5 @@ echo -e "\t1. Open https://github.com/$ORG_REPO/settings"
echo -e "\t2. Under 'Default branch' click two arrows button, select 'main' and click 'Update'"
echo -e "\t3. Open https://github.com/$ORG_REPO/branches"
echo -e "\t4. Delete the 'dev' branch"
if [[ $REQS_EXISTS ]]; then
echo -e "\t5. You have existing scripts/requirements.txt file, please append east and west to it manually, CI expects them to be there."
echo -e "\t6. Commit and push the changes to the remote."
fi
echo ""
echo "Thats it, you are done!"
150 changes: 150 additions & 0 deletions migration_script/update_ci_infra.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/env bash
# Usage: update_ci_infra.sh PATH_TO_GIT_REPO WORKFLOW_GROUP
#
# Description:
# Update CI infrastructure in a Git repository to the latest one in the
# template repositories.
#
# Note: Make sure that you run this script just after you have created a new
# GitHub release and merged the release PR into dev. Also make sure that you
# do not have any uncommitted changes and any unmerged PRs.
#
# Arguments:
#
# PATH_TO_GIT_REPO Relative or absolute path to the Git repository that you
# want to migrate.
#
# WORKFLOW_GROUP Can be either "basic" or "zephyr".
# Use "basic" if your project was created from a
# irnas-projects-template repo.
# Use "zephyr" if your project was created from a
# irnas-zephyr-template repo.

# Details:
# If WORKFLOW_GROUP is "basic", then the following will be done:
# 1. Clone the irnas-projects-template repository and copy its .github
# directory into the current repository.
# 2. Delete the irnas-projects-template repository.
# 3. Create a new commit with all the changes on the main and push to
# the remote.
#
# If WORKFLOW_GROUP is "zephyr", then the following will be done:
# 1. Clone the irnas-zephyr-template repository and copy its .github
# directory into the current repository.
# 2. Copy scripts/requirements.txt, if it exists, only append it to the
# existing one.
# 3. Copy scripts/pre_changelog.md and scripts/post_changelog.md files
# 4. Copy makefile.
# 5. Copy ci scripts.
# 5. Copy dotfiles.
# 6. Delete the irnas-zephyr-template repository.
# 7. Create a new commit with all the changes on the main and push to
# the remote.

NUM_ARGS=2
# Print help text and exit if -h, or --help or insufficient number of arguments
# was given.
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ $# -lt ${NUM_ARGS} ]; then
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' <"$0"
exit 1
fi

PATH_TO_GIT_REPO=$1
WORKFLOW_GROUP=$2

# Check that workflow group is valid.
if [ "$WORKFLOW_GROUP" != "basic" ] && [ "$WORKFLOW_GROUP" != "zephyr" ]; then
echo "Invalid workflow group: $WORKFLOW_GROUP, aborting migration"
exit 1
fi

echo "Confirm the following statements: "
echo " - You have just created a new GitHub release."
echo " - You have merged the release PR into dev branch."
echo " - You don't have any uncommited or untracked changes laying around."
echo " - You have no unmerged PRs."
echo ""
read -p "Confirm that above is true (yes/no): " -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborting migration"
exit 1
fi

cd $PATH_TO_GIT_REPO

# Check if we are in a git repository.
if git rev-parse --git-dir >/dev/null 2>&1; then
:
else
echo "This is not a git repository, aborting migration"
exit 1
fi

# Make sure that there are no uncommitted changes or untracked files.
if [[ ! -z "$(git status -s)" ]]; then
echo "This repository has untracked files, aborting migration"
exit 1
fi

# Make sure that we are really in top level directory.
cd $(git rev-parse --show-toplevel)

# Checkout main just in case there was no dev to begin with
git checkout main

if [[ "$WORKFLOW_GROUP" == "basic" ]]; then
TEMPLATE_REPO="irnas-projects-template"
else
TEMPLATE_REPO="irnas-zephyr-template"
fi

# Get the template repo
git clone https://github.com/IRNAS/${TEMPLATE_REPO}.git

# Delete the .github directory and copy new one from the template repo.
rm -fr .github
cp -r ${TEMPLATE_REPO}/.github .

REQS_EXISTS=false
if [[ "$WORKFLOW_GROUP" == "zephyr" ]]; then
# Create scripts folder if it does not exist.
mkdir -p scripts
if [ -f scripts/requirements.txt ]; then
REQS_EXISTS=true
else
cp ${TEMPLATE_REPO}/scripts/requirements.txt scripts
fi

cp ${TEMPLATE_REPO}/scripts/pre_changelog.md scripts
cp ${TEMPLATE_REPO}/scripts/post_changelog.md scripts
cp ${TEMPLATE_REPO}/scripts/codechecker-diff.sh scripts
cp ${TEMPLATE_REPO}/makefile .
cp ${TEMPLATE_REPO}/.clang-format .
cp ${TEMPLATE_REPO}/.clang-tidy .
cp ${TEMPLATE_REPO}/.clangd .
cp ${TEMPLATE_REPO}/.gitignore .
cp ${TEMPLATE_REPO}/.gitlint .
cp ${TEMPLATE_REPO}/.codechecker_config.yaml .
fi

rm -fr ${TEMPLATE_REPO}

# Commit and push the changes.
git add .
git commit -m "Update CI infrastructure to the latest one from ${TEMPLATE_REPO}"
git push

echo ""
echo ""
echo "***********************************************************************"
echo ""
echo "CI infrastructure was updated."
if [[ $REQS_EXISTS ]]; then
echo -e "\t1. You have existing scripts/requirements.txt file, please append east and west to it manually, CI expects them to be there."
fi
echo ""
echo "Review done changes, when done commit them with the below message:"
echo ""
echo -e "\tCI infrastructure was updated to the latest one from ${TEMPLATE_REPO}"
echo ""

0 comments on commit d0f941d

Please sign in to comment.