Skip to content

chore(ci): skip spreading if there is no change #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
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}`;
Copy link
Collaborator

@millotp millotp Apr 8, 2022

Choose a reason for hiding this comment

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

One more thing, head can be undefined also, the line can be head ? `...${head}` : '';

Copy link
Member

Choose a reason for hiding this comment

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

Does intentionally providing undefined invalidates the default HEAD value?

Copy link
Member

Choose a reason for hiding this comment

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

I can't remember 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah no you're right I got confused

Copy link
Member

Choose a reason for hiding this comment

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

Ah oki no problem


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