Skip to content

Commit 9adb764

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

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

.github/workflows/sync.yml

Lines changed: 88 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,84 @@ 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
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='actions/tools-update-config.json' \
97+
-F parentCommitSha="${HEAD_SHA:-$GITHUB_SHA}" \
98+
-F commit_title="$COMMIT_MESSAGE" \
99+
-F changes="$(node -p 'JSON.stringify({
100+
additions: ["config.json", "tests/nocks.db"].map(path => ({ path, contents: fs.readFileSync(path).toString('base64') })),
101+
deletions: [],
102+
})')"
103+
-f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $changes: FileChanges!, $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: $changes
115+
}) {
116+
commit {
117+
url
118+
}
119+
}
120+
}'
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+
COMMIT_MESSAGE: "feat: update package manager versions"
124+
125+
- name: Create PR if it does not exist
126+
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true'
127+
run: |
128+
gh api \
129+
--method POST \
130+
-H "Accept: application/vnd.github+json" \
131+
-H "X-GitHub-Api-Version: 2022-11-28" \
132+
"/repos/${GITHUB_REPOSITORY}/pulls" \
133+
-f "title=$TITLE" -f "body=$BODY" -f "head=$HEAD_BRANCH"
134+
env:
135+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
BODY: This is an automated update of package manager versions
137+
TITLE: "feat: update package manager versions"

0 commit comments

Comments
 (0)