diff --git a/.github/project-workflows/sync-with-template.yml b/.github/project-workflows/sync-with-template.yml index bbf0c4c7..e85c270f 100644 --- a/.github/project-workflows/sync-with-template.yml +++ b/.github/project-workflows/sync-with-template.yml @@ -42,6 +42,7 @@ env: .github/workflows/deploy-docs.yml .github/workflows/new-template-version.yml .github/workflows/upstream-to-pr.yml + .github/workflows/sync-with-template.yml README-project.md jobs: @@ -71,7 +72,7 @@ jobs: - name: Get latest release of template from GitHub run: | echo "TEMPLATE_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.TEMPLATE_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV - - name: Check if the template is up to date + - name: Check if the project is up to date run: | if [[ $TEMPLATE_LATEST_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then echo "Template is up to date" diff --git a/.github/workflows/sync-with-upstream.yml b/.github/workflows/sync-with-upstream.yml new file mode 100644 index 00000000..c29710b6 --- /dev/null +++ b/.github/workflows/sync-with-upstream.yml @@ -0,0 +1,112 @@ +# ๐Ÿ”— Links: +# Source file: https://github.com/rootstrap/react-native-template/blob/master/.github/workflows/sync-with-upstream.yml + +# โœ๏ธ Description: +# This workflow is used to keep the template up to date with the latest version of the upstream repository. + +# ๐Ÿšจ GITHUB SECRETS REQUIRED: +# - UPDATE_FROM_TEMPLATE_PAT: A fine-grained Personal Access Token. +# This token is used to access the template repository and create the Pull Request. +# You can generate one from here: https://github.com/settings/tokens?type=beta +# Set the Repository access to "Only select repositories" and select the template repository. +# Set the following Repo permissions: +# - Contents: Read & write (to commit and push the update branch to the template repository) +# - Metadata: Read-only (mandatory by GitHub) +# - Pull requests: Read & write (to create the Pull Request in the template repository) +# - Workflows: Read and write (to create, update and delete workflows in the template repository) +# Make sure to add it to the repo secrets with the name UPDATE_FROM_TEMPLATE_PAT: +# - Go to Repository Settings > Secrets and variables > Actions > New repository secret +# - Name: UPDATE_FROM_TEMPLATE_PAT +# - Value: The Personal Access Token you created + +# โ„น๏ธ Environment variables: +# - UPSTREAM_REPOSITORY: Repository to sync with +# - DIFF_EXCLUDED_ROUTES: List of files or directories to exclude from the diff. +# Any changes in these files or directories will be ignored +# and won't be incorporated to the Pull Request. + +name: ๐Ÿ”„ Sync with upstream + +on: + schedule: + - cron: '0 12 * * *' # Everyday at 12:00 UTC + workflow_dispatch: + +env: + UPSTREAM_REPOSITORY: obytes/react-native-template-obytes + DIFF_EXCLUDED_ROUTES: | + ios + android + +jobs: + sync: + runs-on: ubuntu-latest + permissions: + actions: write + contents: read + + steps: + - name: Check if Personal Access Token exists + env: + PAT: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} + if: env.PAT == '' + run: | + echo "UPDATE_FROM_TEMPLATE_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file." + exit 1 + - name: Checkout template repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} + - name: Get latest release of upstream from GitHub + run: | + echo "UPSTREAM_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV + - name: Check if branch already exists + run: | + git fetch origin + BRANCH_NAME=update-upstream-${{ env.UPSTREAM_LATEST_VERSION }} + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV + - name: Check if PR already exists + run: | + prs=$(gh pr list \ + --head "$BRANCH_NAME" \ + --json title \ + --jq 'length') + if ((prs > 0)); then + echo "PR_EXISTS=true" >> $GITHUB_ENV + else + echo "PR_EXISTS=false" >> $GITHUB_ENV + fi + env: + GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} + - name: Checkout latest release of upstream + if: ${{ env.BRANCH_EXISTS == 'false' }} + run: | + git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git + git fetch upstream + - name: Merge latest release of upstream + if: ${{ env.BRANCH_EXISTS == 'false' }} + run: | + UPSTREAM_LATEST_VERSION_HASH=$(git ls-remote --tags upstream | grep $UPSTREAM_LATEST_VERSION | sed -n 2p | cut -f1) + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" + git config --global user.name "github-actions[bot]" + git merge --no-commit --no-ff $UPSTREAM_LATEST_VERSION_HASH + for route in $DIFF_EXCLUDED_ROUTES; do + git reset -- $route + done + continue-on-error: true + - name: Commit and push changes to the update branch + if: ${{ env.BRANCH_EXISTS == 'false' }} + run: | + git checkout -b ${{ env.BRANCH_NAME }} + git add . + git commit -m "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}" + git push origin ${{ env.BRANCH_NAME }} + echo "BRANCH_EXISTS=true" >> $GITHUB_ENV + - name: ๐ŸŽ‰ Create PR with changes + if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }} + run: | + gh pr create --title "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}" --body "Integrating latest changes from [obytes/react-native-template-obytes@${{ env.UPSTREAM_LATEST_VERSION }}](https://github.com/obytes/react-native-template-obytes/releases/tag/${{ env.UPSTREAM_LATEST_VERSION }})" --head ${{ env.BRANCH_NAME }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/upstream-to-pr.yml b/.github/workflows/upstream-to-pr.yml deleted file mode 100644 index a6d2f299..00000000 --- a/.github/workflows/upstream-to-pr.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: โคต๏ธ Check upstream for changes - -on: - schedule: - - cron: '0 12 * * *' - workflow_dispatch: - inputs: {} - -jobs: - autoupdate: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - token: ${{ secrets.GH_TOKEN }} - - - uses: fopina/upstream-to-pr@v1 - with: - token: ${{ secrets.GH_TOKEN }} - upstream-repository: https://github.com/obytes/react-native-template-obytes - upstream-branch: master diff --git a/cli/setup-project.js b/cli/setup-project.js index 69da821f..74bc3658 100755 --- a/cli/setup-project.js +++ b/cli/setup-project.js @@ -93,15 +93,6 @@ const updateProjectConfig = async (projectName) => { const updateGitHubWorkflows = (projectName) => { // Update useful workflows projectFilesManager.replaceFilesContent([ - { - fileName: '.github/workflows/upstream-to-pr.yml', - replacements: [ - { - searchValue: UPSTREAM_REPOSITORY, - replaceValue: TEMPLATE_REPOSITORY, - }, - ], - }, { fileName: '.github/workflows/new-template-version.yml', replacements: [ @@ -146,8 +137,8 @@ const updateGitHubWorkflows = (projectName) => { }, ]); - // Remove upstream update workflow, intended to be used only in the template repository - projectFilesManager.removeFiles(['.github/workflows/upstream-to-pr.yml']); + // Remove upstream sync workflow, intended to be used only in the template repository + projectFilesManager.removeFiles(['.github/workflows/sync-with-upstream.yml']); // Enable sync with template workflow projectFilesManager.renameFiles([