From ca110b631d7e79fea66ce2747bc93932b0de98cf Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Thu, 7 Apr 2022 18:03:32 +0200 Subject: [PATCH 1/6] chore(ci): skip spreading if there is no change --- scripts/ci/codegen/spreadGeneration.ts | 21 +++++++++++++-------- scripts/common.ts | 4 ++++ scripts/release/create-release-issue.ts | 10 ++++++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index 28177e0aed7..a1766f30450 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -5,6 +5,7 @@ import { emptyDirExceptForDotGit, gitBranchExists, gitCommit, + isWorkingDirectoryClean, LANGUAGES, run, toAbsolutePath, @@ -69,14 +70,18 @@ async function spreadGeneration(): Promise { await emptyDirExceptForDotGit(tempGitDir); await copy(clientPath, tempGitDir, { preserveTimestamps: true }); - await configureGitHubAuthor(tempGitDir); - await run(`git add .`, { cwd: tempGitDir }); - await gitCommit({ - message: commitMessage, - coauthor: { name, email }, - cwd: tempGitDir, - }); - await run(`git push`, { cwd: tempGitDir }); + if (await isWorkingDirectoryClean(tempGitDir)) { + console.log(`Skipping ${lang} repository, because there is no change.`); + } else { + await configureGitHubAuthor(tempGitDir); + await run(`git add .`, { cwd: tempGitDir }); + await gitCommit({ + message: commitMessage, + coauthor: { name, email }, + cwd: tempGitDir, + }); + await run(`git push`, { cwd: tempGitDir }); + } } } diff --git a/scripts/common.ts b/scripts/common.ts index fd7cdf85107..853801f7504 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -293,3 +293,7 @@ export async function emptyDirExceptForDotGit(dir: string): Promise { } } } + +export async function isWorkingDirectoryClean(cwd?: string): Promise { + return !(await run('git status --porcelain', { cwd })); +} diff --git a/scripts/release/create-release-issue.ts b/scripts/release/create-release-issue.ts index 0d88bf458f5..c51eda31091 100755 --- a/scripts/release/create-release-issue.ts +++ b/scripts/release/create-release-issue.ts @@ -2,7 +2,13 @@ import dotenv from 'dotenv'; import semver from 'semver'; -import { LANGUAGES, ROOT_ENV_PATH, run, getPackageVersion } from '../common'; +import { + LANGUAGES, + ROOT_ENV_PATH, + run, + getPackageVersion, + isWorkingDirectoryClean, +} from '../common'; import type { Language } from '../types'; import { @@ -160,7 +166,7 @@ async function createReleaseIssue(): Promise { ); } - if (await run('git status --porcelain')) { + if (await isWorkingDirectoryClean()) { throw new Error( 'Working directory is not clean. Commit all the changes first.' ); From 3d4374d914ef2e46e3573e13b7d0942caf65f803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 12:11:23 +0200 Subject: [PATCH 2/6] reuse getNbGitDiff --- scripts/ci/codegen/spreadGeneration.ts | 9 +++++++-- scripts/ci/utils.ts | 15 ++++++++++----- scripts/common.ts | 4 ---- scripts/release/create-release-issue.ts | 15 +++++++-------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index a1766f30450..38cc98ccdc2 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -5,13 +5,13 @@ import { emptyDirExceptForDotGit, gitBranchExists, gitCommit, - isWorkingDirectoryClean, LANGUAGES, run, toAbsolutePath, } from '../../common'; import { getLanguageFolder } from '../../config'; import { cloneRepository, configureGitHubAuthor } from '../../release/common'; +import { getNbGitDiff } from '../utils'; import { GENERATED_MAIN_BRANCH, REPO_URL } from './text'; @@ -70,7 +70,12 @@ async function spreadGeneration(): Promise { await emptyDirExceptForDotGit(tempGitDir); await copy(clientPath, tempGitDir, { preserveTimestamps: true }); - if (await isWorkingDirectoryClean(tempGitDir)) { + if ( + (await getNbGitDiff({ + head: null, + cwd: tempGitDir, + })) === 0 + ) { console.log(`Skipping ${lang} repository, because there is no change.`); } else { await configureGitHubAuthor(tempGitDir); diff --git a/scripts/ci/utils.ts b/scripts/ci/utils.ts index d00c1a05768..36962c867bd 100644 --- a/scripts/ci/utils.ts +++ b/scripts/ci/utils.ts @@ -7,17 +7,22 @@ import { run } from '../common'; export async function getNbGitDiff({ branch, head = 'HEAD', - path, -}: { + path = '.', + cwd, +}: Partial<{ branch: string; - head?: string | null; + head: string | null; path: string; -}): Promise { + cwd: string; +}>): Promise { const checkHead = head === null ? '' : `...${head}`; return parseInt( ( - await run(`git diff --shortstat ${branch}${checkHead} -- ${path} | wc -l`) + await run( + `git diff --shortstat ${branch}${checkHead} -- ${path} | wc -l`, + { cwd } + ) ).trim(), 10 ); diff --git a/scripts/common.ts b/scripts/common.ts index 853801f7504..fd7cdf85107 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -293,7 +293,3 @@ export async function emptyDirExceptForDotGit(dir: string): Promise { } } } - -export async function isWorkingDirectoryClean(cwd?: string): Promise { - return !(await run('git status --porcelain', { cwd })); -} diff --git a/scripts/release/create-release-issue.ts b/scripts/release/create-release-issue.ts index c51eda31091..98cfbe52dfe 100755 --- a/scripts/release/create-release-issue.ts +++ b/scripts/release/create-release-issue.ts @@ -2,13 +2,8 @@ import dotenv from 'dotenv'; import semver from 'semver'; -import { - LANGUAGES, - ROOT_ENV_PATH, - run, - getPackageVersion, - isWorkingDirectoryClean, -} from '../common'; +import { getNbGitDiff } from '../ci/utils'; +import { LANGUAGES, ROOT_ENV_PATH, run, getPackageVersion } from '../common'; import type { Language } from '../types'; import { @@ -166,7 +161,11 @@ async function createReleaseIssue(): Promise { ); } - if (await isWorkingDirectoryClean()) { + if ( + (await getNbGitDiff({ + head: null, + })) === 0 + ) { throw new Error( 'Working directory is not clean. Commit all the changes first.' ); From cc58d0535a2145c03424118f21da9b10335ce303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 12:17:45 +0200 Subject: [PATCH 3/6] fix cond --- scripts/ci/codegen/spreadGeneration.ts | 19 ++++++++++--------- scripts/release/create-release-issue.ts | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index 3caf718118f..5157d0e5649 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -77,16 +77,17 @@ async function spreadGeneration(): Promise { })) === 0 ) { console.log(`Skipping ${lang} repository, because there is no change.`); - } else { - await configureGitHubAuthor(tempGitDir); - await run(`git add .`, { cwd: tempGitDir }); - await gitCommit({ - message: commitMessage, - coauthor: { name, email }, - cwd: tempGitDir, - }); - await run(`git push`, { cwd: tempGitDir }); + return; } + + await configureGitHubAuthor(tempGitDir); + await run(`git add .`, { cwd: tempGitDir }); + await gitCommit({ + message: commitMessage, + coauthor: { name, email }, + cwd: tempGitDir, + }); + await run(`git push`, { cwd: tempGitDir }); } } diff --git a/scripts/release/create-release-issue.ts b/scripts/release/create-release-issue.ts index 0ddef5ad6b8..1089a9757f7 100755 --- a/scripts/release/create-release-issue.ts +++ b/scripts/release/create-release-issue.ts @@ -165,7 +165,7 @@ async function createReleaseIssue(): Promise { if ( (await getNbGitDiff({ head: null, - })) === 0 + })) !== 0 ) { throw new Error( 'Working directory is not clean. Commit all the changes first.' From cb3297f9c40785c8aaec505af55a5fb5d5bfad0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 14:10:47 +0200 Subject: [PATCH 4/6] safe default, add description --- scripts/ci/utils.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/ci/utils.ts b/scripts/ci/utils.ts index 36962c867bd..4951f2aab9a 100644 --- a/scripts/ci/utils.ts +++ b/scripts/ci/utils.ts @@ -2,10 +2,15 @@ import { run } from '../common'; /** * Returns the number of diff between a `branch` and its `HEAD` for the given `path`. - * Head defaults to `HEAD`, providing `null` will check unstaged changes. + * + * @param opts - Parameters of the method. + * @param opts.branch - The branch to trigger the operation, defaults to '' (current branch). + * @param opts.head - The head to compare the operation, defaults to 'HEAD', providing 'null' will check for unstaged changes. + * @param opts.path - The path to look for changes in, defaults to '.' (current directory). + * @param opts.cwd - The path to run the command, defaults to current directory. */ export async function getNbGitDiff({ - branch, + branch = '', head = 'HEAD', path = '.', cwd, From b73db924957df150b3488708fc793a15cc18434c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 14:12:56 +0200 Subject: [PATCH 5/6] update gen commit --- scripts/ci/codegen/pushGeneratedCode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 78dde3b53ad..6bc11878199 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -59,7 +59,7 @@ export async function pushGeneratedCode(): Promise { } const commitMessage = - await run(`git show -s ${baseBranch} --format="Generated code for commit %H. + await run(`git show -s ${baseBranch} --format="chore: generated code for commit %H. Co-authored-by: %an <%ae>"`); From 79f2672543b1f105bf88132d426ec5f57ef3b724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 14:44:25 +0200 Subject: [PATCH 6/6] fix codegen check --- scripts/ci/codegen/pushGeneratedCode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 6bc11878199..b1c7f3bf558 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -20,7 +20,7 @@ export async function pushGeneratedCode(): Promise { console.log(`Checking codegen status on '${baseBranch}'.`); const nbDiff = await getNbGitDiff({ - branch: baseBranch, + branch: 'origin/generated/main', head: null, path: FOLDERS_TO_CHECK, });