|
| 1 | +name: Rebase on upstream |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +jobs: |
| 7 | + check: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + permissions: |
| 10 | + pull-requests: write |
| 11 | + steps: |
| 12 | + - name: Checkout repository |
| 13 | + uses: actions/checkout@v4 |
| 14 | + with: |
| 15 | + fetch-depth: 0 |
| 16 | + ssh-key: ${{ secrets.DEPLOY_KEY }} |
| 17 | + |
| 18 | + - name: Set up Git |
| 19 | + run: | |
| 20 | + git config user.name "github-actions[bot]" |
| 21 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 22 | +
|
| 23 | + - name: Check PR |
| 24 | + id: check_pr |
| 25 | + run: | |
| 26 | + # Check that ${{ github.ref_name }} has main as ancestor (ie. fast forward merge is possible) |
| 27 | + if ! git merge-base --is-ancestor origin/main ${{ github.ref_name }}; then |
| 28 | + echo "${{ github.ref_name }} does not have main as ancestor" |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +
|
| 32 | + gh pr list --state open --head ${{ github.ref_name }} --base main --json number,reviewDecision,state,isDraft,mergeable,statusCheckRollup > pr.json |
| 33 | + # Check that there is a single PR with main as base branch and ${{ github.ref_name }} as head branch |
| 34 | + if [[ "$(jq length pr.json)" -eq 0 ]]; then |
| 35 | + echo "No PR with main as base branch and ${{ github.ref_name }} as head branch" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + # No need to check that PR count is greater than 1, because github prevents creating multiple PRs with the same head and base branches |
| 39 | +
|
| 40 | + # Check that PR is not draft |
| 41 | + if [[ "$(jq '.[0] | .isDraft' pr.json)" == "true" ]]; then |
| 42 | + echo "PR is a draft" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + # Check that PR is mergeable |
| 47 | + if [[ "$(jq '.[0] | .mergeable' pr.json)" == "false" ]]; then |
| 48 | + echo "PR is not mergeable" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + # Check that PR is approved |
| 53 | + if [[ "$(jq -r '.[0] | .reviewDecision' pr.json)" != "APPROVED" ]]; then |
| 54 | + echo "PR is not approved" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +
|
| 58 | + # Check that PR build is successful, sdm policy check is a bit different from the others |
| 59 | + unsuccessful_check_count=$(jq '[.[0] | .statusCheckRollup | .[] | select(.workflowName != null and (.status != "COMPLETED" or .conclusion != "SUCCESS") or (.context != null and .state != "SUCCESS"))] | length' pr.json) |
| 60 | + if [[ "$unsuccessful_check_count" -gt 0 ]]; then |
| 61 | + echo "PR build is not successful" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +
|
| 65 | + echo "pr_number=$(jq '.[0] | .number' pr.json)" >> "$GITHUB_OUTPUT" |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + - name: Rewrite datadog branch |
| 70 | + run: | |
| 71 | + git checkout datadog |
| 72 | + git reset --hard ${{ github.ref_name }} |
| 73 | + git push -f |
| 74 | +
|
| 75 | + - name: Close PR and delete update branch |
| 76 | + run: | |
| 77 | + gh pr close -d ${{ steps.check_pr.outputs.pr_number }} |
| 78 | + env: |
| 79 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments