Skip to content

Commit

Permalink
feat: Introduce [only] flag for including only selected projects for …
Browse files Browse the repository at this point in the history
…given commit
  • Loading branch information
TheUnderScorer committed May 27, 2022
1 parent 28edb28 commit f0c4021
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 12 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ jobs:
- name: Typecheck
run: npm run typecheck

- name: Build package
run: npm run build

- name: Setup test repo
run: npm install --prefix test-repos/app

- name: Lint
run: npm run lint

- name: Run tests
run: npm run test
run: npm run test:skip-cache
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ You can skip commits for given project using `[skip $PROJECT_NAME]` in its body.
```

During analysis this commit will be skipped for release pipeline for my-app.
You can also use `[skip all]` to skip commit for all projects.
---
Alternatively you can include only particular projects in given commit by using `[only $PROJECT_NAME]`. Ex:

You can also use `[skip all]` to skip commit for all projects..
```
feat: update something
[only my-app]
```
During analysis this commit will be included only for release pipeline for my-app.

## CI/CD

Expand Down
8 changes: 7 additions & 1 deletion packages/nx-semantic-release/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
"options": {
"jestConfig": "packages/nx-semantic-release/jest.config.ts",
"passWithNoTests": true
}
},
"dependsOn": [
{
"target": "build",
"projects": "self"
}
]
},
"build": {
"executor": "@nrwl/js:tsc",
Expand Down
60 changes: 60 additions & 0 deletions packages/nx-semantic-release/src/common/git.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { shouldSkipCommit } from './git';

describe('git', () => {
describe('shouldSkipCommit', () => {
it('should skip commit if [skip project] flag is set', () => {
const result = shouldSkipCommit(
{
body: '[skip project]',
},
'project'
);

expect(result).toEqual(true);
});

it('should skip commit if [skip all] flag is set', () => {
const result = shouldSkipCommit(
{
body: '[skip all]',
},
'project'
);

expect(result).toEqual(true);
});

it('should not skip commit if [skip project] flag is not set in the body', () => {
const result = shouldSkipCommit(
{
body: '',
},
'project'
);

expect(result).toEqual(false);
});

it('should skip commit if it has [only other-project] flag', () => {
const result = shouldSkipCommit(
{
body: '[only other-project]',
},
'project'
);

expect(result).toEqual(true);
});

it('should not skip commit if it includes [only project] flag', () => {
const result = shouldSkipCommit(
{
body: '[only project]',
},
'project'
);

expect(result).toEqual(false);
});
});
});
28 changes: 23 additions & 5 deletions packages/nx-semantic-release/src/common/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function isCommitAffectingProjects({
verbose,
graph,
projectName,
}: CommitAffectingProjectsParams) {
}: CommitAffectingProjectsParams): Promise<boolean> {
if (shouldSkipCommit(commit, projectName)) {
if (verbose) {
context.logger.log(`ℹ️ Commit ${commit.subject} is skipped`);
Expand Down Expand Up @@ -59,11 +59,29 @@ export async function isCommitAffectingProjects({
return isAffected;
}

const shouldSkipCommit = (commit: Pick<Commit, 'body'>, projectName: string) =>
commit.body.includes(`[skip ${projectName}]`) ||
commit.body.includes(`[skip all]`);
export function shouldSkipCommit(
commit: Pick<Commit, 'body'>,
projectName: string
): boolean {
const onlyMatchRegex = /\[only (.*?)]/g;

async function listAffectedFilesInCommit(commit: Pick<Commit, 'commit'>) {
const skipMatches = [`[skip ${projectName}]`, '[skip all]'];
const onlyMatches = Array.from(commit.body.matchAll(onlyMatchRegex));

const hasOnlyMatch =
onlyMatches.length &&
!onlyMatches.some((match) => match[1] === projectName);

const hasSkipMatch = skipMatches.some((skipMatch) =>
commit.body.includes(skipMatch)
);

return Boolean(hasSkipMatch || hasOnlyMatch);
}

async function listAffectedFilesInCommit(
commit: Pick<Commit, 'commit'>
): Promise<string[]> {
const files = await exec(`git show --name-status ${commit.commit.short}`);

return files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function checkCommonLib() {
'add app-a libs',
'update test.txt',
'update test.txt again',
'add test-only.txt',
],
});
}
Expand Down Expand Up @@ -96,6 +97,7 @@ async function checkAppB() {
'update test.txt',
'update test.txt again',
'add rest',
'add test-only.txt',
],
shouldNotContain: ['add app-a', 'add app-a libs'],
});
Expand Down Expand Up @@ -134,6 +136,7 @@ async function checkAppA() {
'update test.txt',
'update test.txt again',
'add description',
'add test-only.txt',
],
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/nx-semantic-release/src/tests/setup-test-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const setupCommands: Array<string | (() => Promise<void>)> = [
addDescriptionToPkgJson,
'git add apps/app-a/package.json',
'git commit -m "feat: add description\n\n[skip app-a]"',
'echo "Test123456" > test-only.txt',
'git add test-only.txt',
'git commit -m "feat: add test-only.txt\n\n[only app-b]"',
`git remote add origin ${remoteGitPath}`,
'git push origin master',
];
Expand Down
3 changes: 2 additions & 1 deletion test-repos/app/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"tsconfig.base.json": "*",
"tslint.json": "*",
".eslintrc.json": "*",
"nx.json": "*"
"nx.json": "*",
"test-only.txt": "*"
},
"tasksRunnerOptions": {
"default": {
Expand Down

0 comments on commit f0c4021

Please sign in to comment.