Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,5 @@ jobs:
- name: Push generated files to repositories
run: yarn workspace scripts pushToRepository
env:
GH_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/push-to-repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ jobs:
- run: yarn workspace scripts pushToRepository ${{ inputs.name }}
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
GH_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
FORCE: true
2 changes: 1 addition & 1 deletion scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ vi.mock('../../../common.ts', async (importOriginal) => {
describe('pushGeneratedCode', () => {
it('throws without GITHUB_TOKEN environment variable', async () => {
vi.stubEnv('GITHUB_TOKEN', '');
await expect(pushGeneratedCode()).rejects.toThrow('Environment variable `GITHUB_TOKEN` does not exist.');
await expect(pushGeneratedCode()).rejects.toThrow('Environment variable `GITHUB_TOKEN` or `GH_TOKEN` must be set.');
});
});
16 changes: 13 additions & 3 deletions scripts/ci/codegen/pushToRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path, { resolve } from 'path';
import {
CLIENTS,
configureGitHubAuthor,
ensureGitHubToken,
exists,
getOctokit,
gitBranchExists,
Expand Down Expand Up @@ -113,6 +114,8 @@ async function handleGuideFiles(guide: GuidesToPush, tempGitDir: string): Promis
}

async function pushToRepository(repository: string, config: RepositoryConfiguration): Promise<void> {
const token = ensureGitHubToken();

const lastCommitMessage = await run('git log -1 --format="%s"');
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
Expand All @@ -129,10 +132,19 @@ async function pushToRepository(repository: string, config: RepositoryConfigurat
console.log(`Preparing push to ${OWNER}/${repository}`);

const tempGitDir = resolve(process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'), repository);

console.info(`cleaning ${tempGitDir}`);

await fsp.rm(tempGitDir, { force: true, recursive: true });

console.info(`cloning ${OWNER}/${repository} in ${tempGitDir}`);

await run(`gh repo clone ${OWNER}/${repository} ${tempGitDir}`);

await configureGitHubAuthor(tempGitDir);

await run(`git config --global url.https://${token}@github.com/.insteadOf https://github.com/`);

for (const task of config.tasks) {
console.log(`Handling '${task.files.type}' file(s)`);

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

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

continue;
Expand All @@ -163,8 +175,6 @@ async function pushToRepository(repository: string, config: RepositoryConfigurat
continue;
}

await configureGitHubAuthor(tempGitDir);

await run('git add .', { cwd: tempGitDir });
await gitCommit({
message: task.commitMessage,
Expand Down
9 changes: 6 additions & 3 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,14 @@ export async function runComposerInstall(): Promise<void> {
}

export function ensureGitHubToken(): string {
const githubToken = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should you check that they are both present ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one or the other is fine, gh supports any of the two

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it needed to provide both in the CI then ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the doc is a bit vague, https://cli.github.com/manual/gh_auth_login#:~:text=To%20use%20gh%20in%20GitHub%20Actions%2C%20add%20GH_TOKEN%3A%20%24%7B%7B%20github.token%20%7D%7D%20to%20env. states that it reads GH_TOKEN on the CI, however we might use GITHUB_TOKEN locally, costs nothing to have both

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay so it expects GITHUB_TOKEN locally but GH_TOKEN on the CI, this is super weird


// use process.env here to mock with vitest
if (!process.env.GITHUB_TOKEN) {
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
if (githubToken == '') {
throw new Error('Environment variable `GITHUB_TOKEN` or `GH_TOKEN` must be set.');
}
return process.env.GITHUB_TOKEN.replaceAll('"', '');

return githubToken.replaceAll('"', '');
}

export function getOctokit(): Octokit {
Expand Down