Skip to content

Commit

Permalink
chore(ci): skip spreading if there is no change (#353)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 8, 2022
1 parent fbbd752 commit 66b8edf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function pushGeneratedCode(): Promise<void> {
console.log(`Checking codegen status on '${baseBranch}'.`);

const nbDiff = await getNbGitDiff({
branch: baseBranch,
branch: 'origin/generated/main',
head: null,
path: FOLDERS_TO_CHECK,
});
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function pushGeneratedCode(): Promise<void> {
}

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>"`);

Expand Down
11 changes: 11 additions & 0 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../common';
import { getLanguageFolder } from '../../config';
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

export function decideWhereToSpread(commitMessage: string): string[] {
if (commitMessage.startsWith('chore: release')) {
Expand Down Expand Up @@ -69,6 +70,16 @@ async function spreadGeneration(): Promise<void> {
await emptyDirExceptForDotGit(tempGitDir);
await copy(clientPath, tempGitDir, { preserveTimestamps: true });

if (
(await getNbGitDiff({
head: null,
cwd: tempGitDir,
})) === 0
) {
console.log(`Skipping ${lang} repository, because there is no change.`);
return;
}

await configureGitHubAuthor(tempGitDir);
await run(`git add .`, { cwd: tempGitDir });
await gitCommit({
Expand Down
24 changes: 17 additions & 7 deletions scripts/ci/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ 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,
}: {
path = '.',
cwd,
}: Partial<{
branch: string;
head?: string | null;
head: string | null;
path: string;
}): Promise<number> {
cwd: string;
}>): Promise<number> {
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
);
Expand Down
7 changes: 6 additions & 1 deletion scripts/release/create-release-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dotenv from 'dotenv';
import semver from 'semver';

import { getNbGitDiff } from '../ci/utils';
import {
LANGUAGES,
ROOT_ENV_PATH,
Expand Down Expand Up @@ -161,7 +162,11 @@ async function createReleaseIssue(): Promise<void> {
);
}

if (await run('git status --porcelain')) {
if (
(await getNbGitDiff({
head: null,
})) !== 0
) {
throw new Error(
'Working directory is not clean. Commit all the changes first.'
);
Expand Down

0 comments on commit 66b8edf

Please sign in to comment.