Skip to content

Commit 59f6029

Browse files
committed
chore(ci): fix sync.yml
1 parent 256cf8a commit 59f6029

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

.github/workflows/sync.yml

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ on:
66
# Run once a week at 00:05 UTC on Friday.
77
- cron: 5 0 * * 5
88

9+
permissions:
10+
contents: read
11+
912
jobs:
1013
fetch-latest-versions:
14+
runs-on: ubuntu-latest
1115
permissions:
1216
contents: write
13-
runs-on: ubuntu-latest
17+
pull-requests: write
18+
env:
19+
HEAD_BRANCH: actions/tools-update-config.json
1420

1521
steps:
1622
- uses: actions/checkout@v4
23+
with:
24+
persist-credentials: false
1725

1826
- name: Install Node
1927
uses: actions/setup-node@v4
@@ -46,13 +54,72 @@ jobs:
4654
},
4755
}" > config.json
4856
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
52121
env:
53122
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

Comments
 (0)