Skip to content

Commit cfc8d9e

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

File tree

1 file changed

+90
-9
lines changed

1 file changed

+90
-9
lines changed

.github/workflows/sync.yml

Lines changed: 90 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,86 @@ 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+
- run: corepack yarn install --immutable
67+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
68+
- run: corepack yarn build # We need the stubs to run the tests
69+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
70+
71+
- name: Remove old Nock files to avoid conflicts
72+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
73+
run: rm tests/nocks.db
74+
75+
- run: corepack yarn test tests/Up.test.ts || true
76+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
77+
env:
78+
NOCK_ENV: record
79+
80+
- name: Push changes
81+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
82+
run: |
83+
HEAD_SHA=
84+
if git fetch origin "$HEAD_BRANCH"; then
85+
HEAD_SHA="$(git rev-parse FETCH_HEAD)"
86+
else
87+
# The branch does not exist yet, creating it.
88+
gh api \
89+
--method POST \
90+
-H "Accept: application/vnd.github+json" \
91+
-H "X-GitHub-Api-Version: 2022-11-28" \
92+
"/repos/${GITHUB_REPOSITORY}/git/refs" \
93+
-f "ref=refs/heads/$HEAD_BRANCH" -f "sha=$GITHUB_SHA"
94+
fi
95+
gh api graphql \
96+
-F repo="$GITHUB_REPOSITORY" -F "branch=$HEAD_BRANCH" \
97+
-F parentCommitSha="${HEAD_SHA:-$GITHUB_SHA}" \
98+
-F commit_title="$COMMIT_MESSAGE" \
99+
-F configChange[path]="config.json" \
100+
-F configChange[contents]="$(base64 -i config.json -o-)" \
101+
-F nockChange[path]="tests/nocks.db" \
102+
-F nockChange[contents]="$(base64 -i tests/nocks.db -o-)" \
103+
-f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $configChange: FileAddition!, $nockChange: FileAddition!, $commit_title: String!, $commit_body: String) {
104+
createCommitOnBranch(input: {
105+
branch: {
106+
repositoryNameWithOwner: $repo,
107+
branchName: $branch
108+
},
109+
message: {
110+
headline: $commit_title,
111+
body: $commit_body
112+
},
113+
expectedHeadOid: $parentCommitSha,
114+
fileChanges: {
115+
additions: [$configChange, $nockChange]
116+
}
117+
}) {
118+
commit {
119+
url
120+
}
121+
}
122+
}'
52123
env:
53124
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"
125+
COMMIT_MESSAGE: "feat: update package manager versions"
126+
127+
- name: Create PR if it does not exist
128+
run: |
129+
gh api \
130+
--method POST \
131+
-H "Accept: application/vnd.github+json" \
132+
-H "X-GitHub-Api-Version: 2022-11-28" \
133+
"/repos/${GITHUB_REPOSITORY}/pulls" \
134+
-f "title=$TITLE" -f "body=$BODY" -f "head=$HEAD_BRANCH" -f "base=$BASE_BRANCH"
135+
env:
136+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
137+
BASE_BRANCH: ${{ github.event.repository.default_branch }}
138+
BODY: This is an automated update of package manager versions
139+
TITLE: "feat: update package manager versions"

0 commit comments

Comments
 (0)