Skip to content

Commit 8db88fd

Browse files
authored
fix(scripts): push to repository (#4941)
1 parent 7e49813 commit 8db88fd

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,4 +705,5 @@ jobs:
705705
- name: Push generated files to repositories
706706
run: yarn workspace scripts pushToRepository
707707
env:
708+
GH_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
708709
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

.github/workflows/push-to-repository.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ jobs:
3030
- run: yarn workspace scripts pushToRepository ${{ inputs.name }}
3131
env:
3232
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
33+
GH_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
3334
FORCE: true

scripts/ci/codegen/__tests__/codegen.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ vi.mock('../../../common.ts', async (importOriginal) => {
1313
describe('pushGeneratedCode', () => {
1414
it('throws without GITHUB_TOKEN environment variable', async () => {
1515
vi.stubEnv('GITHUB_TOKEN', '');
16-
await expect(pushGeneratedCode()).rejects.toThrow('Environment variable `GITHUB_TOKEN` does not exist.');
16+
await expect(pushGeneratedCode()).rejects.toThrow('Environment variable `GITHUB_TOKEN` or `GH_TOKEN` must be set.');
1717
});
1818
});

scripts/ci/codegen/pushToRepository.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path, { resolve } from 'path';
44
import {
55
CLIENTS,
66
configureGitHubAuthor,
7+
ensureGitHubToken,
78
exists,
89
getOctokit,
910
gitBranchExists,
@@ -113,6 +114,8 @@ async function handleGuideFiles(guide: GuidesToPush, tempGitDir: string): Promis
113114
}
114115

115116
async function pushToRepository(repository: string, config: RepositoryConfiguration): Promise<void> {
117+
const token = ensureGitHubToken();
118+
116119
const lastCommitMessage = await run('git log -1 --format="%s"');
117120
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
118121
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
@@ -129,10 +132,19 @@ async function pushToRepository(repository: string, config: RepositoryConfigurat
129132
console.log(`Preparing push to ${OWNER}/${repository}`);
130133

131134
const tempGitDir = resolve(process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'), repository);
135+
136+
console.info(`cleaning ${tempGitDir}`);
137+
132138
await fsp.rm(tempGitDir, { force: true, recursive: true });
133139

140+
console.info(`cloning ${OWNER}/${repository} in ${tempGitDir}`);
141+
134142
await run(`gh repo clone ${OWNER}/${repository} ${tempGitDir}`);
135143

144+
await configureGitHubAuthor(tempGitDir);
145+
146+
await run(`git config --global url.https://${token}@github.com/.insteadOf https://github.com/`);
147+
136148
for (const task of config.tasks) {
137149
console.log(`Handling '${task.files.type}' file(s)`);
138150

@@ -146,7 +158,7 @@ async function pushToRepository(repository: string, config: RepositoryConfigurat
146158
await handleGuideFiles(task.files, tempGitDir);
147159
}
148160

149-
if (process.env.DRYRUN) {
161+
if (process.env.DRY_RUN) {
150162
console.log(`asked for a dry run, stopping before push and PR for '${repository}' on task '${task.prBranch}'`);
151163

152164
continue;
@@ -163,8 +175,6 @@ async function pushToRepository(repository: string, config: RepositoryConfigurat
163175
continue;
164176
}
165177

166-
await configureGitHubAuthor(tempGitDir);
167-
168178
await run('git add .', { cwd: tempGitDir });
169179
await gitCommit({
170180
message: task.commitMessage,

scripts/common.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ export async function runComposerInstall(): Promise<void> {
197197
}
198198

199199
export function ensureGitHubToken(): string {
200+
const githubToken = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || '';
201+
200202
// use process.env here to mock with vitest
201-
if (!process.env.GITHUB_TOKEN) {
202-
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
203+
if (githubToken == '') {
204+
throw new Error('Environment variable `GITHUB_TOKEN` or `GH_TOKEN` must be set.');
203205
}
204-
return process.env.GITHUB_TOKEN.replaceAll('"', '');
206+
207+
return githubToken.replaceAll('"', '');
205208
}
206209

207210
export function getOctokit(): Octokit {

0 commit comments

Comments
 (0)