|
| 1 | +# Enforce rebasing Pull Requests if Toolchain was modified on target branch |
| 2 | +# |
| 3 | +# If new commits, which modifies toolchain files was pushed to given branch, find all PRs targeting |
| 4 | +# this branch, which also change toolchain files. Then, enforce rebasing them by changing |
| 5 | +# CI/Jenkins/toolchain check to FAILURE. |
| 6 | +# This prevents race condition issue when new toolchain bundle has to be built after PR is merged. |
| 7 | +# |
| 8 | +# Toolchain files: |
| 9 | +# * scripts/requirements-fixed.txt |
| 10 | +# * scripts/tools-versions-darwin.yml |
| 11 | +# * scripts/tools-versions-win10.yml |
| 12 | +# * scripts/tools-versions-linux.yml |
| 13 | + |
| 14 | +name: Enforce rebasing Pull Requests if Toolchain was modified on target branch |
| 15 | + |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: |
| 19 | + - '**' # Triggers on pushes to any branch |
| 20 | + |
| 21 | +jobs: |
| 22 | + check-prs: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Define list of files to check |
| 27 | + id: define_files |
| 28 | + run: | |
| 29 | + echo "TOOLCHAIN_FILES=scripts/requirements-fixed.txt,scripts/tools-versions-linux.yml,scripts/tools-versions-darwin.yml,scripts/tools-versions-win10.yml" >> $GITHUB_ENV |
| 30 | +
|
| 31 | + - name: Checkout the repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + persist-credentials: false |
| 36 | + |
| 37 | + - name: Get files modified by recent commits |
| 38 | + id: get_files |
| 39 | + run: | |
| 40 | + echo "Modified files in this commit:" |
| 41 | + git diff --name-only ${{ github.event.before }} ${{ github.sha }} > modified_files.txt |
| 42 | + cat modified_files.txt |
| 43 | +
|
| 44 | + - name: Check if any watched files are modified |
| 45 | + id: check_files |
| 46 | + run: | |
| 47 | + modified_files=$(cat modified_files.txt) |
| 48 | + IFS=',' read -r -a watched_files <<< "${{ env.TOOLCHAIN_FILES }}" |
| 49 | + modified=false |
| 50 | + for file in "${watched_files[@]}"; do |
| 51 | + if echo "$modified_files" | grep -q "$file"; then |
| 52 | + echo "$file was modified." |
| 53 | + modified=true |
| 54 | + fi |
| 55 | + done |
| 56 | + echo "modified=$modified" >> $GITHUB_ENV |
| 57 | +
|
| 58 | + # App token is required to update Check Status |
| 59 | + - name: Get jenkins-ncs App token |
| 60 | + if: env.modified == 'true' |
| 61 | + uses: actions/create-github-app-token@v1 |
| 62 | + id: app-token |
| 63 | + with: |
| 64 | + app-id: ${{ vars.JENKINS_NCS_APP_ID }} |
| 65 | + private-key: ${{ secrets.JENKINS_NCS_APP_PRIVATE_KEY }} |
| 66 | + |
| 67 | + - name: Find open pull requests targeting this branch and modyfing Toolchain files |
| 68 | + if: env.modified == 'true' |
| 69 | + id: find_prs |
| 70 | + run: | |
| 71 | + PRs=$(gh pr list --base ${{ github.ref_name }} --state open --json url,headRefName,files --jq '[.[] | select(.files[]? | .path as $file | [$file] | inside([env.TOOLCHAIN_FILES]))]') |
| 72 | + echo "Found PRs: $PRs" |
| 73 | + echo "prs=$PRs" >> $GITHUB_ENV |
| 74 | + env: |
| 75 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 76 | + |
| 77 | + - name: Set CI/Jenkins/toolchain status check to failure |
| 78 | + if: env.modified == 'true' && steps.find_prs.outputs.prs != '[]' |
| 79 | + env: |
| 80 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 81 | + run: | |
| 82 | + for pr in $(echo "$prs" | jq -r '.[].url'); do |
| 83 | + pr_url=$(echo $pr | sed 's|https://github.com/||') |
| 84 | + pr_owner=$(echo $pr_url | cut -d'/' -f1) |
| 85 | + pr_repo=$(echo $pr_url | cut -d'/' -f2) |
| 86 | + pr_number=$(echo $pr_url | cut -d'/' -f4) |
| 87 | +
|
| 88 | + echo "Setting CI/Jenkins/toolchain status to failure for PR: $pr" |
| 89 | +
|
| 90 | + # Get the SHA of the last commit in the PR branch |
| 91 | + commit_sha=$(gh pr view $pr_number --json headRefOid --jq '.headRefOid') |
| 92 | +
|
| 93 | + # Get the Check Run ID by listing the check runs for the PR's head commit |
| 94 | + check_run_id=$(gh api \ |
| 95 | + -H "Accept: application/vnd.github.v3+json" \ |
| 96 | + /repos/$pr_owner/$pr_repo/commits/$commit_sha/check-runs \ |
| 97 | + --jq '.check_runs[] | select(.name == "CI/Jenkins/toolchain") | .id') |
| 98 | +
|
| 99 | + # If no check run exists, create a new one; otherwise, update the existing one |
| 100 | + if [ -z "$check_run_id" ]; then |
| 101 | + echo "Creating new check run for PR: $pr" |
| 102 | + gh api \ |
| 103 | + -H "Accept: application/vnd.github.v3+json" \ |
| 104 | + --method POST /repos/$pr_owner/$pr_repo/check-runs \ |
| 105 | + -f name="CI/Jenkins/toolchain" \ |
| 106 | + -f head_sha="$commit_sha" \ |
| 107 | + -f status="completed" \ |
| 108 | + -f conclusion="failure" \ |
| 109 | + -f output[title]="Rebase needed - Toolchain changed on '${{ github.ref_name }}' branch" \ |
| 110 | + -f output[summary]="Toolchain was modified on '${{ github.ref_name }}' and this PR has to be rebased" |
| 111 | + else |
| 112 | + echo "Updating existing check run with ID $check_run_id" |
| 113 | + gh api \ |
| 114 | + -H "Accept: application/vnd.github.v3+json" \ |
| 115 | + --method PATCH /repos/$pr_owner/$pr_repo/check-runs/$check_run_id \ |
| 116 | + -f conclusion="failure" \ |
| 117 | + -f status="completed" \ |
| 118 | + -f output[title]="Rebase needed - Toolchain changed on '${{ github.ref_name }}' branch" \ |
| 119 | + -f output[summary]="Toolchain was modified on '${{ github.ref_name }}' and this PR has to be rebased" |
| 120 | + fi |
| 121 | + done |
0 commit comments