|
6 | 6 | # Run once a week at 00:05 UTC on Friday. |
7 | 7 | - cron: 5 0 * * 5 |
8 | 8 |
|
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
9 | 12 | jobs: |
10 | 13 | fetch-latest-versions: |
| 14 | + runs-on: ubuntu-latest |
11 | 15 | permissions: |
12 | 16 | contents: write |
13 | | - runs-on: ubuntu-latest |
| 17 | + pull-requests: write |
| 18 | + env: |
| 19 | + HEAD_BRANCH: actions/tools-update-config.json |
14 | 20 |
|
15 | 21 | steps: |
16 | 22 | - uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + persist-credentials: false |
17 | 25 |
|
18 | 26 | - name: Install Node |
19 | 27 | uses: actions/setup-node@v4 |
@@ -46,13 +54,72 @@ jobs: |
46 | 54 | }, |
47 | 55 | }" > config.json |
48 | 56 |
|
49 | | - - uses: gr2m/create-or-update-pull-request-action@77596e3166f328b24613f7082ab30bf2d93079d5 # v1.9.2 |
50 | | - # Creates a PR or update the Action's existing PR, or |
51 | | - # no-op if the base branch is already up-to-date. |
| 57 | + - name: Check if there have been changes |
| 58 | + id: check-for-changes |
| 59 | + run: | |
| 60 | + if git fetch origin "$HEAD_BRANCH"; then |
| 61 | + git diff --exit-code --quiet FETCH_HEAD config.json || echo "CONTAINS_CHANGES=true" >> "$GITHUB_OUTPUT" |
| 62 | + else |
| 63 | + git diff --exit-code --quiet HEAD config.json || echo "CONTAINS_CHANGES=true" >> "$GITHUB_OUTPUT" |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Push changes |
| 67 | + if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' |
| 68 | + run: | |
| 69 | + HEAD_SHA= |
| 70 | + if git fetch origin "$HEAD_BRANCH"; then |
| 71 | + HEAD_SHA="$(git rev-parse FETCH_HEAD)" |
| 72 | + else |
| 73 | + # The branch does not exist yet, creating it. |
| 74 | + gh api \ |
| 75 | + --method POST \ |
| 76 | + -H "Accept: application/vnd.github+json" \ |
| 77 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 78 | + "/repos/${GITHUB_REPOSITORY}/git/refs" \ |
| 79 | + -f "ref=refs/heads/$HEAD_BRANCH" -f "sha=$GITHUB_SHA" |
| 80 | + fi |
| 81 | + gh api graphql \ |
| 82 | + -F repo="$GITHUB_REPOSITORY" -F "branch=$HEAD_BRANCH" \ |
| 83 | + -F parentCommitSha="${HEAD_SHA:-$GITHUB_SHA}" \ |
| 84 | + -F commit_title="$COMMIT_MESSAGE" \ |
| 85 | + -F configChange[path]="config.json" \ |
| 86 | + -F configChange[contents]="$(base64 --wrap=0 config.json)" \ |
| 87 | + -f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $configChange: FileAddition!, $commit_title: String!, $commit_body: String) { |
| 88 | + createCommitOnBranch(input: { |
| 89 | + branch: { |
| 90 | + repositoryNameWithOwner: $repo, |
| 91 | + branchName: $branch |
| 92 | + }, |
| 93 | + message: { |
| 94 | + headline: $commit_title, |
| 95 | + body: $commit_body |
| 96 | + }, |
| 97 | + expectedHeadOid: $parentCommitSha, |
| 98 | + fileChanges: { |
| 99 | + additions: [$configChange] |
| 100 | + } |
| 101 | + }) { |
| 102 | + commit { |
| 103 | + url |
| 104 | + } |
| 105 | + } |
| 106 | + }' |
| 107 | + env: |
| 108 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 109 | + COMMIT_MESSAGE: "feat: update package manager versions" |
| 110 | + |
| 111 | + - name: Create PR if it does not exist |
| 112 | + run: | |
| 113 | + gh api \ |
| 114 | + --method POST \ |
| 115 | + -H "Accept: application/vnd.github+json" \ |
| 116 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 117 | + "/repos/${GITHUB_REPOSITORY}/pulls" \ |
| 118 | + -f "title=$TITLE" -f "body=$BODY" -f "head=$HEAD_BRANCH" -f "base=$BASE_BRANCH" \ |
| 119 | + > output.json || (<output.json jq 'if (.errors | any(.message | startswith("A pull request already exists"))) then . else halt_error end') |
| 120 | + cat output.json |
52 | 121 | env: |
53 | 122 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
54 | | - with: |
55 | | - body: This is an automated update of package manager versions |
56 | | - branch: actions/tools-update-config.json |
57 | | - commit-message: "feat: update package manager versions" |
58 | | - title: "feat: update package manager versions" |
| 123 | + BASE_BRANCH: ${{ github.event.repository.default_branch }} |
| 124 | + BODY: This is an automated update of package manager versions |
| 125 | + TITLE: "feat: update package manager versions" |
0 commit comments