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: refactor sync template workflow to work in a whitelist setting #78

Draft
wants to merge 21 commits into
base: development
Choose a base branch
from
Draft
Changes from 10 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
76 changes: 76 additions & 0 deletions .github/workflows/sync-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Sync branch to template

on:
workflow_dispatch:
inputs:
additional_files:
description: 'Comma-separated list of extra paths to sync, for example: .eslintrc,.prettierrc,.github'
required: false
schedule:
- cron: '14 0 1 * *'

jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Check if repository is ts-template
run: |
if [[ "${{ github.repository }}" == "ubiquity/ts-template" ]]; then
echo "Skipping sync: this is the template repository."
exit 0
fi

- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get GitHub App token
uses: tibdex/github-app-token@v1.7.0
id: get_installation_token
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}

- name: Sync branch to template
env:
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }}
WHITELIST_FILES: ".github/workflows/build.yml tsconfig.json"
zugdev marked this conversation as resolved.
Show resolved Hide resolved
ADDITIONAL_FILES: ${{ github.event.inputs.additional_files }}
zugdev marked this conversation as resolved.
Show resolved Hide resolved
run: |
branch_name=$(git rev-parse --abbrev-ref HEAD)
original_remote=$(git remote get-url origin)
pr_branch="sync-template/${branch_name}"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git checkout -b "$pr_branch"
git clone https://github.com/ubiquity/ts-template template-repo

# Convert ADDITIONAL_FILES input to an array
additional_files=()
if [[ -n "$ADDITIONAL_FILES" ]]; then
IFS=',' read -r -a additional_files <<< "$ADDITIONAL_FILES"
fi

# Prepare file list for the PR body and process each whitelist file
file_list=""
for file in $WHITELIST_FILES "${additional_files[@]}"; do
if [[ -e "template-repo/$file" ]]; then
cp -rf "template-repo/$file" "$file"
file_list+="\n- \`${file}\`"
else
# Remove file from destination if not in template
rm -rf "$file"
file_list+="\n- \`${file}\` (removed)"
fi
done

# Clean up
rm -rf template-repo/
git add .
git commit -m "chore: sync template"
git push "$original_remote" "$pr_branch"
gh pr create --title "Sync branch to template" --body "This pull request merges changes from the template repository, overwriting or removing the following files:${file_list}" --head "$pr_branch" --base "$branch_name"