Skip to content

Commit

Permalink
Merge pull request #5 from s0/subdir
Browse files Browse the repository at this point in the history
✨ Add support for specifying FOLDER
  • Loading branch information
s0 authored May 14, 2021
2 parents 600ad10 + f47e506 commit e22bf01
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @s0
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ your workflow, and pass in the required configuration options:
it is recommended you install your NPM using your preferred package manager
before running this action*

**Note: We recommend using the
[latest release](https://github.com/s0/libyear-node-action/releases) rather than
`develop` to avoid future breaking changes with your workflow.

```yml
jobs:
deploy:
Expand All @@ -31,10 +35,29 @@ jobs:
- run: npm install

# Calculate libyear
- uses: s0/libyear-node-action@v0.0.1
- uses: s0/libyear-node-action@develop
```
### Outputs
## Configuration
All configuration options are passed in via `env`, as environment variables. For example:

```yml
jobs:
deploy:
# ...
- uses: s0/libyear-node-action@develop
env:
FOLDER: some/sub/directory
```

### Full list of variables

| Env Variable | Description | Required? |
| ------------------ | ------------------------------------------------------------------------------------------------ | ------------- |
| `FOLDER` | Which directory within your repository should `libyear` be run in. (Default: root of repository) | No |

## Outputs

This action generates outputs that can be used in later steps or outputs
*(for example to generate badges to display in your README, see below)*.
Expand All @@ -52,7 +75,7 @@ Each of them represents a
| `minor` | An integer |
| `patch` | An integer |

### Generating badges using libyear stats
### Example: Generating badges using libyear stats

Here's an example workflow that will generate a badge for you and push it to
a special branch that can be referenced in e.g. your README.
Expand Down Expand Up @@ -86,7 +109,7 @@ jobs:
# (notice how the libyear step has an id, this is referenced later)
- run: npm install
- id: libyear
uses: s0/libyear-node-action@v0.0.1
uses: s0/libyear-node-action@develop
# Generate a badge and store it in the badge/directory
- run: mkdir badges
Expand All @@ -113,9 +136,5 @@ We use this for this repository, you can see the workflow here:

## TODO

* Add functionality to compare libyear metrics between target and base of a pull
request, and leave a comment on the PR with the difference.
* Allow for libyear to be run against different directories
(e.g. to support monorepos)
* Allow for configuration of the arguments to send to libyear
* Switch to programmatic use once that becomes available in `libyear`
Please see the [issues](https://github.com/s0/libyear-node-action/issues) for
details of planned work.
43 changes: 36 additions & 7 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3975,6 +3975,25 @@ exports.bind = bind;

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -4016,12 +4035,13 @@ exports.main = exports.runAction = void 0;
var PathReporter_1 = __webpack_require__(306);
var Either_1 = __webpack_require__(311);
var fs_1 = __webpack_require__(747);
var path = __importStar(__webpack_require__(622));
var environment_1 = __webpack_require__(626);
var libyear_1 = __webpack_require__(569);
var runAction = function (_a) {
var env = _a.env, log = _a.log, cwd = _a.cwd;
return __awaiter(void 0, void 0, void 0, function () {
var json, error, report, totals, _i, metrics_1, metric, val, output;
var json, error, dir, report, totals, _i, metrics_1, metric, val, output;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, fs_1.promises.readFile(env.GITHUB_EVENT_PATH)];
Expand All @@ -4032,7 +4052,8 @@ var runAction = function (_a) {
return new Error(msg);
};
if (!(env.GITHUB_EVENT_NAME === 'push')) return [3 /*break*/, 3];
return [4 /*yield*/, libyear_1.runLibyear(cwd)];
dir = env.FOLDER ? path.join(cwd, env.FOLDER) : cwd;
return [4 /*yield*/, libyear_1.runLibyear(dir)];
case 2:
report = _b.sent();
log.log('Result: ');
Expand Down Expand Up @@ -4126,11 +4147,19 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.ENVIRONMENT = void 0;
var t = __importStar(__webpack_require__(338));
exports.ENVIRONMENT = t.type({
/** Implicit environment variable passed by GitHub */
GITHUB_EVENT_PATH: t.string,
GITHUB_EVENT_NAME: t.string,
});
exports.ENVIRONMENT = t.intersection([
t.type({
/** Implicit environment variable passed by GitHub */
GITHUB_EVENT_PATH: t.string,
GITHUB_EVENT_NAME: t.string,
}),
t.partial({
/**
* Folder within the repository to run libyear on
*/
FOLDER: t.string,
}),
]);


/***/ }),
Expand Down
18 changes: 13 additions & 5 deletions src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import * as t from 'io-ts';

export const ENVIRONMENT = t.type({
/** Implicit environment variable passed by GitHub */
GITHUB_EVENT_PATH: t.string,
GITHUB_EVENT_NAME: t.string,
});
export const ENVIRONMENT = t.intersection([
t.type({
/** Implicit environment variable passed by GitHub */
GITHUB_EVENT_PATH: t.string,
GITHUB_EVENT_NAME: t.string,
}),
t.partial({
/**
* Folder within the repository to run libyear on
*/
FOLDER: t.string,
}),
]);

export type Environment = t.TypeOf<typeof ENVIRONMENT>;
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PathReporter } from 'io-ts/PathReporter';
import { isRight } from 'fp-ts/Either';
import { promises as fs } from 'fs';
import * as path from 'path';

import { Environment, ENVIRONMENT } from './environment';
import { runLibyear, getTotals, metrics, getResultsTable } from './libyear';
Expand All @@ -23,7 +24,8 @@ export const runAction = async ({
};

if (env.GITHUB_EVENT_NAME === 'push') {
const report = await runLibyear(cwd);
const dir = env.FOLDER ? path.join(cwd, env.FOLDER) : cwd;
const report = await runLibyear(dir);

log.log('Result: ');
log.table(getResultsTable(report));
Expand Down
43 changes: 42 additions & 1 deletion test/push-workflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,48 @@ describe('push-workflow', () => {
eventJson: {},
eventName: 'push',
logToConsole: false,
copySpec: '001',
copySpec: {
spec: '001',
},
});

const spec = await loadSpec('001');
const specTotals = getTotals(spec);

// Ensure that table is printed out
expect(run.stdout).toMatch(
`TABLE: ${JSON.stringify(getResultsTable(spec))}`
);

// Ensure that variables are set
for (const metric of ['drift', 'pulse'] as const) {
expect(specTotals.get(metric)).toBeTruthy();
expect(run.stdout).toMatch(
`::set-output name=${metric}::${Number(specTotals.get(metric)).toFixed(
2
)}`
);
}
for (const metric of ['releases', 'major', 'minor', 'patch'] as const) {
expect(specTotals.get(metric)).toBeTruthy();
expect(run.stdout).toMatch(
`::set-output name=${metric}::${specTotals.get(metric)}`
);
}
});

it('sub-directory-config', async () => {
const run = await runAction({
eventJson: {},
eventName: 'push',
logToConsole: false,
copySpec: {
spec: '001',
subdir: ['some', 'deep', 'directory'],
},
extraEnv: {
FOLDER: 'some/deep/directory',
},
});

const spec = await loadSpec('001');
Expand Down
21 changes: 17 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,24 @@ export const runAction = async ({
eventJson,
eventName,
logToConsole,
extraEnv,
copySpec,
}: {
eventJson: 'missing-file' | 'missing-var' | {};
eventName?: string;
logToConsole: boolean;
/** Additional environment variables to send to the action */
extraEnv?: Partial<Environment>;
/**
* If set, copy the files from this spec directory to the package directory
*/
copySpec?: string;
copySpec?: {
spec: string;
/**
* If set, use this directory to store the package json in
*/
subdir?: string[];
};
}) => {
const runTimestamp = Date.now();

Expand All @@ -50,7 +59,9 @@ export const runAction = async ({
const packageDir = path.join(runDir, 'package');
await mkdirP(packageDir);

const env: Partial<Environment> = {};
const env: Partial<Environment> = {
...extraEnv,
};

const eventJsonPath = path.join(runDir, 'event.json');
if (eventJson === 'missing-file') {
Expand All @@ -65,8 +76,10 @@ export const runAction = async ({
}

if (copySpec) {
const srcPkg = path.join(SPEC_DIR, copySpec, 'package.json');
const dstPkg = path.join(packageDir, 'package.json');
const srcPkg = path.join(SPEC_DIR, copySpec.spec, 'package.json');
const dstDir = path.join(packageDir, ...(copySpec.subdir || []));
await mkdirP(dstDir);
const dstPkg = path.join(dstDir, 'package.json');
const json = JSON.parse((await fs.readFile(srcPkg)).toString());
json.name = `test-${runTimestamp}`;
await fs.writeFile(dstPkg, JSON.stringify(json, null, ' '));
Expand Down

0 comments on commit e22bf01

Please sign in to comment.