From 9efaed63976eae62962f229d5ca66caf8eb942ca Mon Sep 17 00:00:00 2001 From: Arthri <41360489+Arthri@users.noreply.github.com> Date: Sun, 24 Mar 2024 03:28:10 +0800 Subject: [PATCH] Subminute Upstream Merge & Add Comment for Access Denied (#24759) * Update action * Remove unintended permission --------- Co-authored-by: Arthri <41360489+a@users.noreply.github.com> --- .github/workflows/merge_upstream_master.yml | 48 +++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/.github/workflows/merge_upstream_master.yml b/.github/workflows/merge_upstream_master.yml index bb3805be16a..94cad7e5bae 100644 --- a/.github/workflows/merge_upstream_master.yml +++ b/.github/workflows/merge_upstream_master.yml @@ -16,16 +16,22 @@ jobs: runs-on: ubuntu-latest steps: - name: PR Data + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - curl -H "Authorization: token ${{ github.token }}" ${{ github.event.issue.pull_request.url }} > pr.json - echo "PR_REPO=`jq -r '.head.repo.full_name' < pr.json`" >> $GITHUB_ENV - echo "PR_BRANCH=`jq -r '.head.ref' < pr.json`" >> $GITHUB_ENV + pr_json=$(curl -L -s --fail-with-body -H "Authorization: token ${{ github.token }}" ${{ github.event.issue.pull_request.url }}) + if [ `jq -r '.maintainer_can_modify' <<<$pr_json` == "false" ] ; then + gh pr comment ${{ github.event.issue.html_url }} --body 'GitHub Actions can not push to the repository without "Allow edits and access to secrets by maintainers" checked.' + exit 1 + fi + echo "PR_REPO=`jq -r '.head.repo.full_name' <<<$pr_json`" >> $GITHUB_ENV + echo "PR_BRANCH=`jq -r '.head.ref' <<<$pr_json`" >> $GITHUB_ENV + echo "PR_HEAD_LABEL=`jq -r '.head.label' <<<$pr_json`" >> $GITHUB_ENV - uses: actions/checkout@v4 with: repository: ${{ env.PR_REPO }} ref: ${{ env.PR_BRANCH }} - fetch-depth: 0 - uses: actions/setup-node@v4 with: @@ -39,16 +45,42 @@ jobs: cache: 'pip' - name: Perform Merge + env: + BASE_BRANCH: ${{ github.event.repository.default_branch }} + BASE_REPOSITORY: ${{ github.repository }} run: | + # Compare head branch and base branch + compare_result=$(curl -L -s --fail-with-body \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$BASE_REPOSITORY/compare/$BASE_BRANCH...$PR_HEAD_LABEL") + # Assign multiple variables with one jq execution + if IFS=$'\n' read -d '' -r behind_by ahead_by <<<$(jq '.behind_by, .ahead_by' <<<$compare_result) ; [ -z "$behind_by" ] || [ -z "$ahead_by" ] ; then + echo '- Unable to determine the distance between the head branch and the base branch.' | tee -a "$GITHUB_STEP_SUMMARY" + exit 1 + fi + if [ "$behind_by" -le 0 ] ; then + echo '- Skipping merge. Up-to-date with base branch.' | tee -a "$GITHUB_STEP_SUMMARY" + exit 0 + else + echo '- Merging base branch. Head branch is behind by '"$behind_by"' commits and ahead by '"$ahead_by"' commits.' | tee -a "$GITHUB_STEP_SUMMARY" + fi + + # Install Tools chmod +x tools/bootstrap/python bash tools/hooks/install.sh bash tgui/bin/tgui --install-git-hooks chmod +x tools/hooks/*.merge tgui/bin/tgui + + # Actual Merge git config user.name github-actions - git config user.email github-actions@github.com - git remote add upstream "https://github.com/${{ github.repository }}.git" - git fetch upstream master - git merge upstream/master && git push origin + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git remote add upstream "https://github.com/$BASE_REPOSITORY.git" + + git fetch origin "$PR_BRANCH" --depth=$((ahead_by + 1)) + git fetch upstream "$BASE_BRANCH" --depth=$((behind_by + 1)) + git merge FETCH_HEAD + git push origin - name: Notify Failure if: failure()