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: config files inside .GitHub folder #252

Merged
merged 13 commits into from
Dec 25, 2022
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
2 changes: 1 addition & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"login": "LuisHenri",
"name": "Luís Henrique A. Schünemann",
"avatar_url": "https://avatars.githubusercontent.com/u/44511825?v=4",
"profile": "https://www.instagram.com/LuisHenri_",
"profile": "https://github.com/LuisHenri",
"contributions": [
"ideas"
]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ jobs:

Create a `.templatesyncignore` file. Just like writing a `.gitignore` file, follow the [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) in defining the files and folders that should be excluded from syncing with the template repository.

It can also be stored inside `.github` folder.

_Note: It is not possible to sync also the `.templatesyncignore` itself. Any changes from the template repository will be restored automatically._

## Debug
Expand Down
28 changes: 14 additions & 14 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ within this section you will find some

## Code

The architecture and logic within the code
The architecture and logic within the code:

```mermaid
flowchart TD
Expand All @@ -17,26 +17,26 @@ GitHubActionEnv{Read GitHubAction env}
style Start fill:#f9f,stroke:#333,stroke-width:4px
style Exit fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5

EnvCheckEntry{required environment variables exists}
EnvCheckEntry{Required environment variables exists}
SshCheckEntry{SSH private key defined}
SshConfigureEntry[Configure ssh related variables]
GitConfigureEntry[Configure git global settings]

EnvCheckSync{required environment variables exists}
EnvCheckSync{Required environment variables exists}
SshConfigureSync[Configure SSH variables]
SetVariablesSync[Set the needed variables, e.q. with reading remote repository]
CheckTemplateFileExists{"Check if the .templatesyncrc file exists in repository"}
CheckTemplateFileExists{"Check if the .templatesyncrc file exists\n(First inside .github folder, then in root)"}
WriteTemplateVersionSync["Read and write the template sync version into variable"]
CompareTemplateVersionSync{"Compare the source repository version"}
GitCheckoutSync["create git branch <branch_prefix_git_hash>"]
GitPullSync["pull from remote repository"]
CheckIgnoreFileExistsSync{"check if .templatesyncignore file exists"}
GitCheckoutSync["Create git branch <branch_prefix_git_hash>"]
GitPullSync["Pull from remote repository"]
CheckIgnoreFileExistsSync{"Check if .templatesyncignore file exists\n(First inside .github folder, then in root)"}
ResetChangesSync["Reset the changes listed within the ignore file"]
GitCommitSync["commit the changes"]
GitCommitSync["Commit the changes"]

CheckIsDryRun{"check if is_dry_run is set to true"}
CheckIsDryRun{"Check if is_dry_run is set to true"}
GitPushSync["Push the changes to GitHub"]
GitPullRequestSync["create a pull request on GitHub"]
GitPullRequestSync["Create a pull request on GitHub"]

subgraph githubactions["GitHubActions"]

Expand Down Expand Up @@ -66,15 +66,15 @@ EnvCheckSync -->|do not exist| Exit
EnvCheckSync -->|do exist| SshConfigureSync
SshConfigureSync --> SetVariablesSync

subgraph compareVersion["compare the sync version"]
subgraph compareVersion["Compare the sync version"]
SetVariablesSync --> CheckTemplateFileExists
CheckTemplateFileExists -->|exists| WriteTemplateVersionSync
CheckTemplateFileExists -->|does not exist|CompareTemplateVersionSync
CheckTemplateFileExists -->|does not exist| CompareTemplateVersionSync
WriteTemplateVersionSync --> CompareTemplateVersionSync
CompareTemplateVersionSync -->|equal versions| Exit
end

subgraph git["Git actions"]
subgraph git["Git Actions"]
CompareTemplateVersionSync -->|versions not equal| GitCheckoutSync
GitCheckoutSync --> GitPullSync
GitPullSync --> CheckIgnoreFileExistsSync
Expand All @@ -83,7 +83,7 @@ CheckIgnoreFileExistsSync -->|exists| ResetChangesSync
ResetChangesSync --> GitCommitSync
end

subgraph github["gitHub actions"]
subgraph github["GitHub Actions"]
GitCommitSync --> CheckIsDryRun
CheckIsDryRun -->|is true| Exit
CheckIsDryRun -->|is not true| GitPushSync
Expand Down
32 changes: 20 additions & 12 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ if [[ -n "${SRC_SSH_PRIVATEKEY_ABS_PATH}" ]]; then
export GIT_SSH_COMMAND="ssh -i ${SRC_SSH_PRIVATEKEY_ABS_PATH}"
fi

TEMPLATE_VERSION_FILE_NAME=".templateversionrc"
TEMPLATE_SYNC_IGNORE_FILE_NAME=".templatesyncignore"
TEMPLATE_VERSION_FILE_PATH=".templateversionrc"
TEMPLATE_SYNC_IGNORE_FILE_PATH=".templatesyncignore"
TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk '{print $1}')
NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}")
NEW_BRANCH="${PR_BRANCH_NAME_PREFIX}_${NEW_TEMPLATE_GIT_HASH}"

echo "::group::Check new changes"
echo "::debug::new Git HASH ${NEW_TEMPLATE_GIT_HASH}"
if [ -r ${TEMPLATE_VERSION_FILE_NAME} ]
then
CURRENT_TEMPLATE_GIT_HASH=$(cat ${TEMPLATE_VERSION_FILE_NAME})

# Check if the Version File exists inside .github folder or if it doesn't exist at all
if [[ -f ".github/${TEMPLATE_VERSION_FILE_PATH}" || ! -f "${TEMPLATE_VERSION_FILE_PATH}" ]]; then
echo "::debug::using version file as in .github folder"
TEMPLATE_VERSION_FILE_PATH=".github/${TEMPLATE_VERSION_FILE_PATH}"
fi
if [ -r ${TEMPLATE_VERSION_FILE_PATH} ]; then
CURRENT_TEMPLATE_GIT_HASH=$(cat ${TEMPLATE_VERSION_FILE_PATH})
echo "::debug::Current git hash ${CURRENT_TEMPLATE_GIT_HASH}"
fi

if [ "${NEW_TEMPLATE_GIT_HASH}" == "${CURRENT_TEMPLATE_GIT_HASH}" ]
then
if [ "${NEW_TEMPLATE_GIT_HASH}" == "${CURRENT_TEMPLATE_GIT_HASH}" ]; then
echo "::warn::repository is up to date"
exit 0
fi
Expand All @@ -66,19 +70,23 @@ fi

echo "::group::persist template version"
echo "write new template version file"
echo "${NEW_TEMPLATE_GIT_HASH}" > ${TEMPLATE_VERSION_FILE_NAME}
echo "::debug::wrote new template version file with content $(cat ${TEMPLATE_VERSION_FILE_NAME})"
echo "${NEW_TEMPLATE_GIT_HASH}" > ${TEMPLATE_VERSION_FILE_PATH}
echo "::debug::wrote new template version file with content $(cat ${TEMPLATE_VERSION_FILE_PATH})"
echo "::endgroup::"

echo "::group::commit and push changes"
git add .

# Check if the Ignore File exists inside .github folder or if it doesn't exist at all
if [[ -f ".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" || ! -f "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then
echo "::debug::using ignore file as in .github folder"
TEMPLATE_SYNC_IGNORE_FILE_PATH=".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}"
fi
# we are checking the ignore file if it exists or is empty
# -s is true if the file contains whitespaces
if [ -s ${TEMPLATE_SYNC_IGNORE_FILE_NAME} ]
then
if [ -s ${TEMPLATE_SYNC_IGNORE_FILE_PATH} ]; then
echo "::debug::unstage files from template sync ignore"
git reset --pathspec-from-file="${TEMPLATE_SYNC_IGNORE_FILE_NAME}"
git reset --pathspec-from-file="${TEMPLATE_SYNC_IGNORE_FILE_PATH}"

echo "::debug::clean untracked files"
git clean -df
Expand Down