Skip to content

Commit

Permalink
Merge pull request #14 from juanigalan91/development
Browse files Browse the repository at this point in the history
Release 0.2.2
  • Loading branch information
juanigalan91 authored Dec 6, 2020
2 parents 9724a58 + 06130e2 commit 786b31c
Show file tree
Hide file tree
Showing 15 changed files with 21,058 additions and 20,811 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: "Checkout repository"
uses: actions/checkout@v2
- name: "Setup node env"
uses: actions/setup-node@1c5c137 #v1.4.0
uses: actions/setup-node@c6fd00c #v2.1.2
with:
node-version: '13.7.0'
- name: "Get cache hash"
Expand All @@ -33,7 +33,7 @@ jobs:
- name: "Checkout repository"
uses: actions/checkout@722adc6 #v2
- name: "Setup node env"
uses: actions/setup-node@1c5c137 #v1.4.0
uses: actions/setup-node@c6fd00c #v2.1.2
with:
node-version: '13.7.0'
- name: "Get cache hash"
Expand Down
13 changes: 13 additions & 0 deletions CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change log

## Unreleased

## 0.2.2
- Added the possibility to ignore main package json
- Added the possibility to just warn rather than break the CI

## 0.2.1
- Added the possibility to ignore certain workspaces from the check dependencies action

## 0.2.0 (current)
Beta version with the check dependencies action created.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Awesome Github Action to analyse your yarn monorepo!

## Requirements
This Github Action assumes that you are using yarn workspaces in order to manage your monorepo. If your `package.json` has a key called `workspaces` where you confgire where your packages are, then you are good to go!
This Github Action assumes that you are using yarn workspaces in order to manage your monorepo. If your `package.json` has a key called `workspaces` where you configure where your packages are, then you are good to go!

## Functionalities
### Dependencies check
Expand Down Expand Up @@ -37,9 +37,56 @@ steps:
- name: Checkout
uses: actions/checkout@v2
- name: Check dependencies
uses: juanigalan91/[email protected].0
uses: juanigalan91/[email protected].2
with:
# Whether you want to execute the check dependencies action or not
check-dependencies: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
If there is a workspace that you want to ignore, you can set the `ignore-workspaces` flag like this:

```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Check dependencies
uses: juanigalan91/[email protected]
with:
check-dependencies: true
ignore-workspaces: 'dev-packages,third-parties' # lists of workspaces to ignore from the check, list of strings separated by a comma
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

If you want to ignore the main package json from this check, you can set the `include-main-package-json` flag like this:

```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Check dependencies
uses: juanigalan91/[email protected]
with:
check-dependencies: true
include-main-package-json: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

If you want this action to just warn by adding a comment on the PR rather than breaking the CI, you can set the `only-warn` flag like this:

```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Check dependencies
uses: juanigalan91/[email protected]
with:
check-dependencies: true
only-warn: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ inputs:
description: 'Whether the github action will check the monorepo dependencies integrity or not'
required: false
default: false
ignore-workspaces:
description: 'List of workspaces separated by comma that will be ignored during the execution of this action'
required: false
default: ''
include-main-package-json:
description: 'Whether the main package json of the repository should be considered for the integrity check or not'
required: false
default: true
only-warn:
description: 'Whether this action should only throw a warning or an error'
required: false
default: false
runs:
using: 'node12'
main: 'packages/ga-monorepolyser/dist/index.js'
32 changes: 32 additions & 0 deletions packages/dependencies/__snapshots__/dependencies.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ Object {
],
}
`;

exports[`Dependencies returns the workspace packages grouped by package name but it ignores the root package 1`] = `
Object {
"packages": Object {
"@module/admin": Object {
"dependencies": Object {
"react": "16.8.3",
},
"name": "@module/admin",
"version": "1.0.0",
},
"@module/home": Object {
"dependencies": Object {
"lodash": "0.19.0",
"react": "16.8.3",
},
"name": "@module/home",
"version": "1.0.0",
},
"@module/search": Object {
"dependencies": Object {
"react": "16.8.3",
},
"name": "@module/search",
"version": "1.0.0",
},
},
"workspaces": Array [
"packages/**",
],
}
`;
14 changes: 14 additions & 0 deletions packages/dependencies/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@ describe('Dependencies', () => {
expect(project.packages['@module/root']).toBeDefined();
expect(project.packages['@module/search']).toBeDefined();
});

it('returns the workspace packages grouped by package name but it ignores the root package', () => {
const project = dependencies.getProjectMetadata({ includeMainPackageJson: false });

expect(project).toMatchSnapshot();
expect(project.packages).toBeDefined();
expect(project.workspaces).toBeDefined();

expect(Object.keys(project.packages)).toHaveLength(3);
expect(project.packages['@module/admin']).toBeDefined();
expect(project.packages['@module/home']).toBeDefined();
expect(project.packages['@module/root']).not.toBeDefined();
expect(project.packages['@module/search']).toBeDefined();
});
});
30 changes: 21 additions & 9 deletions packages/dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { Body } from 'package-json-types';
import { ProjectMetadata } from './types';
import { getWorkingDirectory, getRootPackageJson, getWorkspaces } from './utils';

export interface GetProjectMetadataOptions {
workspacesToIgnore?: string[];
includeMainPackageJson?: boolean;
}

const GET_PROJECT_METADATA_DEFAULTS: GetProjectMetadataOptions = {
workspacesToIgnore: [],
includeMainPackageJson: true,
};

/**
* Retrieves the metadata for a specific list of workspaces, returning the packages in that workspaces
* with their dependencies, name, version and development dependencies.
* @param workspaces - list of workspaces to parse
*/
const getProjectMetadata = (): ProjectMetadata => {
const workspaces = getWorkspaces();
const getProjectMetadata = (options = GET_PROJECT_METADATA_DEFAULTS): ProjectMetadata => {
const { workspacesToIgnore } = options;
const workspaces = getWorkspaces({ workspacesToIgnore });
const root = getWorkingDirectory();
const rootPackageJson = getRootPackageJson();
const packages: Record<string, Body> = {};
Expand All @@ -34,12 +44,14 @@ const getProjectMetadata = (): ProjectMetadata => {
* We also want to include the root package json metadata because these will be also dependencies
* that will be installed, so we want to check them as well
*/
packages[rootPackageJson.name] = {
name: rootPackageJson.name,
version: rootPackageJson.version,
dependencies: rootPackageJson.dependencies,
devDependencies: rootPackageJson.devDependencies,
};
if (options.includeMainPackageJson) {
packages[rootPackageJson.name] = {
name: rootPackageJson.name,
version: rootPackageJson.version,
dependencies: rootPackageJson.dependencies,
devDependencies: rootPackageJson.devDependencies,
};
}

const projectMetadata: ProjectMetadata = {
packages,
Expand Down
27 changes: 27 additions & 0 deletions packages/dependencies/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,31 @@ describe('Dependencies Utils', () => {

expect(workspaces).toEqual(null);
});

it('returns an empty array since the only workspace was ignored', () => {
const workspaces = utils.filterWorkspaces({
workspaces: ['packages/**/**'],
workspacesToIgnore: ['packages'],
});

expect(workspaces).toEqual([]);
});

it('returns list of workspaces since not all of them were ignored', () => {
const workspaces = utils.filterWorkspaces({
workspaces: ['packages/**/**', 'dev-packages/**/**'],
workspacesToIgnore: ['packages'],
});

expect(workspaces).toEqual(['dev-packages/**/**']);
});

it('returns all workspaces since none of them were ignored', () => {
const workspaces = utils.filterWorkspaces({
workspaces: ['packages/**/**', 'dev-packages/**/**'],
workspacesToIgnore: [],
});

expect(workspaces).toEqual(['packages/**/**', 'dev-packages/**/**']);
});
});
22 changes: 19 additions & 3 deletions packages/dependencies/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,27 @@ const getRootPackageJson = (): Body => {
return require(path.join(root, 'package.json'));
};

const filterWorkspaces = ({ workspacesToIgnore = [], workspaces = [] }): Body['workspaces'] => {
return workspaces.filter((workspace) => {
for (let i = 0; i < workspacesToIgnore.length; i += 1) {
const workspaceToIgnore = workspacesToIgnore[i];

if (workspace.indexOf(workspaceToIgnore) === 0) {
// eslint-disable-next-line no-console
console.log(`ignoring workspace ${workspaceToIgnore} since it was specified in the action config`);
return false;
}
}

return true;
});
};

/** Returns the workspaces property located on the root of the target repo */
const getWorkspaces = (): Body['workspaces'] => {
const getWorkspaces = ({ workspacesToIgnore = [] }): Body['workspaces'] => {
const workspaces = getWorkspacesFromPackageJson(getRootPackageJson());

return workspaces;
return filterWorkspaces({ workspaces, workspacesToIgnore });
};

export { getWorkspacesFromPackageJson, getRootPackageJson, getWorkingDirectory, getWorkspaces };
export { getWorkspacesFromPackageJson, getRootPackageJson, getWorkingDirectory, getWorkspaces, filterWorkspaces };
20 changes: 15 additions & 5 deletions packages/ga-check-dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as core from '@actions/core';
import { addCommentToCurrentPR, Comment } from '@monorepolyser/ga-utils';
import { getProjectMetadata } from '@monorepolyser/dependencies';
import { getProjectMetadata, GetProjectMetadataOptions } from '@monorepolyser/dependencies';

import { getIncoherentDependencies } from './utils';

const main = async () => {
export interface MainOptions extends GetProjectMetadataOptions {
onlyWarn?: boolean;
}

const main = async (options?: MainOptions) => {
const { onlyWarn = false, ...projectMetadataOptions } = options || {};

try {
const project = getProjectMetadata();
const project = getProjectMetadata(projectMetadataOptions);
const { incoherentDependencies, deps } = getIncoherentDependencies(project);

const repeatedDeps = Object.keys(incoherentDependencies);
Expand Down Expand Up @@ -39,13 +45,17 @@ const main = async () => {

addCommentToCurrentPR(comment);

throw new Error('There are deps with different versions');
if (!onlyWarn) {
throw new Error('There are deps with different versions');
}
} else {
// eslint-disable-next-line no-console
console.log('All packages are using the same versions of their dependencies!');
}
} catch (error) {
core.setFailed(error.message);
if (!onlyWarn) {
core.setFailed(error.message);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/ga-check-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.ts",
"dependencies": {
"@actions/github": "^2.1.0",
"@actions/core": "^1.2.4",
"@actions/core": "^1.2.6",
"@monorepolyser/dependencies": "1.0.0",
"@monorepolyser/ga-utils": "1.0.0"
},
Expand Down
Loading

0 comments on commit 786b31c

Please sign in to comment.