|
| 1 | +name: renovate hooks |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'package.json' |
| 9 | + - 'package-lock.json' |
| 10 | + |
| 11 | +jobs: |
| 12 | + renovate-post-run: |
| 13 | + name: Renovate Post Upgrade Hook |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: github.repository_owner == 'jkroepke' && github.actor == 'renovate[bot]' |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + # Using a GitHub App token, because GitHub Actions doesn't run on commits from github-actions bot |
| 22 | + # Used App: |
| 23 | + # https://github.com/organizations/prometheus-community/settings/apps/helm-charts-renovate-helper. |
| 24 | + # Ref: https://github.com/prometheus-community/helm-charts/issues/5213. |
| 25 | + - uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 |
| 26 | + id: app-token |
| 27 | + with: |
| 28 | + app-id: 1248576 |
| 29 | + private-key: ${{ secrets.AUTOMATION_APP_PRIVATE_KEY }} |
| 30 | + |
| 31 | + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 32 | + with: |
| 33 | + node-version: '22' |
| 34 | + |
| 35 | + - run: | |
| 36 | + npm install |
| 37 | + npm run all |
| 38 | + - name: Commit changes |
| 39 | + env: |
| 40 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
| 41 | + GITHUB_HEAD_REF: ${{ github.head_ref }} |
| 42 | + #language=bash |
| 43 | + run: | |
| 44 | + # Define the target directory |
| 45 | + TARGET_DIR="." |
| 46 | + # Fetch deleted files in the target directory |
| 47 | + DELETED_FILES=$(git diff --diff-filter=D --name-only HEAD -- "$TARGET_DIR") |
| 48 | + # Fetch added/modified files in the target directory |
| 49 | + MODIFIED_FILES=$(git diff --diff-filter=ACM --name-only HEAD -- "$TARGET_DIR") |
| 50 | + # Create a temporary file for JSON output |
| 51 | + FILE_CHANGES_JSON_FILE=$(mktemp) |
| 52 | + # Initialize JSON structure |
| 53 | + echo '{ "deletions": [], "additions": [] }' > "$FILE_CHANGES_JSON_FILE" |
| 54 | + # Add deletions |
| 55 | + for file in $DELETED_FILES; do |
| 56 | + jq --arg path "$file" '.deletions += [{"path": $path}]' "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp" |
| 57 | + mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE" |
| 58 | + done |
| 59 | + # Prepare additions (new or modified files) |
| 60 | + for file in $MODIFIED_FILES; do |
| 61 | + jq --rawfile content "$file" --arg path "$file" \ |
| 62 | + '.additions += [{"path": $path, "contents": ($content | @base64)}]' \ |
| 63 | + "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp" |
| 64 | + mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE" |
| 65 | + done |
| 66 | + # Create a temporary file for the final JSON payload |
| 67 | + JSON_PAYLOAD_FILE=$(mktemp) |
| 68 | + # Construct the final JSON using jq and store it in a file |
| 69 | + jq -n --arg repo "$GITHUB_REPOSITORY" \ |
| 70 | + --arg branch "$GITHUB_HEAD_REF" \ |
| 71 | + --arg message "post upgrade changes from renovate" \ |
| 72 | + --arg expectedOid "$GITHUB_SHA" \ |
| 73 | + --slurpfile fileChanges "$FILE_CHANGES_JSON_FILE" \ |
| 74 | + '{ |
| 75 | + query: "mutation ($input: CreateCommitOnBranchInput!) { |
| 76 | + createCommitOnBranch(input: $input) { |
| 77 | + commit { |
| 78 | + url |
| 79 | + } |
| 80 | + } |
| 81 | + }", |
| 82 | + variables: { |
| 83 | + input: { |
| 84 | + branch: { |
| 85 | + repositoryNameWithOwner: $repo, |
| 86 | + branchName: $branch |
| 87 | + }, |
| 88 | + message: { headline: $message }, |
| 89 | + fileChanges: $fileChanges[0], |
| 90 | + expectedHeadOid: $expectedOid |
| 91 | + } |
| 92 | + } |
| 93 | + }' > "$JSON_PAYLOAD_FILE" |
| 94 | + # Call GitHub API |
| 95 | + curl https://api.github.com/graphql -f \ |
| 96 | + -sSf -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 97 | + --data "@$JSON_PAYLOAD_FILE" |
| 98 | + # Clean up temporary files |
| 99 | + rm "$FILE_CHANGES_JSON_FILE" "$JSON_PAYLOAD_FILE" |
0 commit comments