Skip to content

Commit e22bf01

Browse files
authored
Merge pull request #5 from s0/subdir
✨ Add support for specifying FOLDER
2 parents 600ad10 + f47e506 commit e22bf01

File tree

7 files changed

+141
-28
lines changed

7 files changed

+141
-28
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @s0

README.md

+29-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ your workflow, and pass in the required configuration options:
1919
it is recommended you install your NPM using your preferred package manager
2020
before running this action*
2121

22+
**Note: We recommend using the
23+
[latest release](https://github.com/s0/libyear-node-action/releases) rather than
24+
`develop` to avoid future breaking changes with your workflow.
25+
2226
```yml
2327
jobs:
2428
deploy:
@@ -31,10 +35,29 @@ jobs:
3135
- run: npm install
3236

3337
# Calculate libyear
34-
- uses: s0/libyear-node-action@v0.0.1
38+
- uses: s0/libyear-node-action@develop
3539
```
3640
37-
### Outputs
41+
## Configuration
42+
43+
All configuration options are passed in via `env`, as environment variables. For example:
44+
45+
```yml
46+
jobs:
47+
deploy:
48+
# ...
49+
- uses: s0/libyear-node-action@develop
50+
env:
51+
FOLDER: some/sub/directory
52+
```
53+
54+
### Full list of variables
55+
56+
| Env Variable | Description | Required? |
57+
| ------------------ | ------------------------------------------------------------------------------------------------ | ------------- |
58+
| `FOLDER` | Which directory within your repository should `libyear` be run in. (Default: root of repository) | No |
59+
60+
## Outputs
3861

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

55-
### Generating badges using libyear stats
78+
### Example: Generating badges using libyear stats
5679

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

114137
## TODO
115138

116-
* Add functionality to compare libyear metrics between target and base of a pull
117-
request, and leave a comment on the PR with the difference.
118-
* Allow for libyear to be run against different directories
119-
(e.g. to support monorepos)
120-
* Allow for configuration of the arguments to send to libyear
121-
* Switch to programmatic use once that becomes available in `libyear`
139+
Please see the [issues](https://github.com/s0/libyear-node-action/issues) for
140+
details of planned work.

dist/main/index.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,25 @@ exports.bind = bind;
39753975

39763976
"use strict";
39773977

3978+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3979+
if (k2 === undefined) k2 = k;
3980+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3981+
}) : (function(o, m, k, k2) {
3982+
if (k2 === undefined) k2 = k;
3983+
o[k2] = m[k];
3984+
}));
3985+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
3986+
Object.defineProperty(o, "default", { enumerable: true, value: v });
3987+
}) : function(o, v) {
3988+
o["default"] = v;
3989+
});
3990+
var __importStar = (this && this.__importStar) || function (mod) {
3991+
if (mod && mod.__esModule) return mod;
3992+
var result = {};
3993+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
3994+
__setModuleDefault(result, mod);
3995+
return result;
3996+
};
39783997
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39793998
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
39803999
return new (P || (P = Promise))(function (resolve, reject) {
@@ -4016,12 +4035,13 @@ exports.main = exports.runAction = void 0;
40164035
var PathReporter_1 = __webpack_require__(306);
40174036
var Either_1 = __webpack_require__(311);
40184037
var fs_1 = __webpack_require__(747);
4038+
var path = __importStar(__webpack_require__(622));
40194039
var environment_1 = __webpack_require__(626);
40204040
var libyear_1 = __webpack_require__(569);
40214041
var runAction = function (_a) {
40224042
var env = _a.env, log = _a.log, cwd = _a.cwd;
40234043
return __awaiter(void 0, void 0, void 0, function () {
4024-
var json, error, report, totals, _i, metrics_1, metric, val, output;
4044+
var json, error, dir, report, totals, _i, metrics_1, metric, val, output;
40254045
return __generator(this, function (_b) {
40264046
switch (_b.label) {
40274047
case 0: return [4 /*yield*/, fs_1.promises.readFile(env.GITHUB_EVENT_PATH)];
@@ -4032,7 +4052,8 @@ var runAction = function (_a) {
40324052
return new Error(msg);
40334053
};
40344054
if (!(env.GITHUB_EVENT_NAME === 'push')) return [3 /*break*/, 3];
4035-
return [4 /*yield*/, libyear_1.runLibyear(cwd)];
4055+
dir = env.FOLDER ? path.join(cwd, env.FOLDER) : cwd;
4056+
return [4 /*yield*/, libyear_1.runLibyear(dir)];
40364057
case 2:
40374058
report = _b.sent();
40384059
log.log('Result: ');
@@ -4126,11 +4147,19 @@ var __importStar = (this && this.__importStar) || function (mod) {
41264147
Object.defineProperty(exports, "__esModule", { value: true });
41274148
exports.ENVIRONMENT = void 0;
41284149
var t = __importStar(__webpack_require__(338));
4129-
exports.ENVIRONMENT = t.type({
4130-
/** Implicit environment variable passed by GitHub */
4131-
GITHUB_EVENT_PATH: t.string,
4132-
GITHUB_EVENT_NAME: t.string,
4133-
});
4150+
exports.ENVIRONMENT = t.intersection([
4151+
t.type({
4152+
/** Implicit environment variable passed by GitHub */
4153+
GITHUB_EVENT_PATH: t.string,
4154+
GITHUB_EVENT_NAME: t.string,
4155+
}),
4156+
t.partial({
4157+
/**
4158+
* Folder within the repository to run libyear on
4159+
*/
4160+
FOLDER: t.string,
4161+
}),
4162+
]);
41344163

41354164

41364165
/***/ }),

src/environment.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import * as t from 'io-ts';
22

3-
export const ENVIRONMENT = t.type({
4-
/** Implicit environment variable passed by GitHub */
5-
GITHUB_EVENT_PATH: t.string,
6-
GITHUB_EVENT_NAME: t.string,
7-
});
3+
export const ENVIRONMENT = t.intersection([
4+
t.type({
5+
/** Implicit environment variable passed by GitHub */
6+
GITHUB_EVENT_PATH: t.string,
7+
GITHUB_EVENT_NAME: t.string,
8+
}),
9+
t.partial({
10+
/**
11+
* Folder within the repository to run libyear on
12+
*/
13+
FOLDER: t.string,
14+
}),
15+
]);
816

917
export type Environment = t.TypeOf<typeof ENVIRONMENT>;

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PathReporter } from 'io-ts/PathReporter';
22
import { isRight } from 'fp-ts/Either';
33
import { promises as fs } from 'fs';
4+
import * as path from 'path';
45

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

2526
if (env.GITHUB_EVENT_NAME === 'push') {
26-
const report = await runLibyear(cwd);
27+
const dir = env.FOLDER ? path.join(cwd, env.FOLDER) : cwd;
28+
const report = await runLibyear(dir);
2729

2830
log.log('Result: ');
2931
log.table(getResultsTable(report));

test/push-workflow.spec.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,48 @@ describe('push-workflow', () => {
99
eventJson: {},
1010
eventName: 'push',
1111
logToConsole: false,
12-
copySpec: '001',
12+
copySpec: {
13+
spec: '001',
14+
},
15+
});
16+
17+
const spec = await loadSpec('001');
18+
const specTotals = getTotals(spec);
19+
20+
// Ensure that table is printed out
21+
expect(run.stdout).toMatch(
22+
`TABLE: ${JSON.stringify(getResultsTable(spec))}`
23+
);
24+
25+
// Ensure that variables are set
26+
for (const metric of ['drift', 'pulse'] as const) {
27+
expect(specTotals.get(metric)).toBeTruthy();
28+
expect(run.stdout).toMatch(
29+
`::set-output name=${metric}::${Number(specTotals.get(metric)).toFixed(
30+
2
31+
)}`
32+
);
33+
}
34+
for (const metric of ['releases', 'major', 'minor', 'patch'] as const) {
35+
expect(specTotals.get(metric)).toBeTruthy();
36+
expect(run.stdout).toMatch(
37+
`::set-output name=${metric}::${specTotals.get(metric)}`
38+
);
39+
}
40+
});
41+
42+
it('sub-directory-config', async () => {
43+
const run = await runAction({
44+
eventJson: {},
45+
eventName: 'push',
46+
logToConsole: false,
47+
copySpec: {
48+
spec: '001',
49+
subdir: ['some', 'deep', 'directory'],
50+
},
51+
extraEnv: {
52+
FOLDER: 'some/deep/directory',
53+
},
1354
});
1455

1556
const spec = await loadSpec('001');

test/util.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,24 @@ export const runAction = async ({
3232
eventJson,
3333
eventName,
3434
logToConsole,
35+
extraEnv,
3536
copySpec,
3637
}: {
3738
eventJson: 'missing-file' | 'missing-var' | {};
3839
eventName?: string;
3940
logToConsole: boolean;
41+
/** Additional environment variables to send to the action */
42+
extraEnv?: Partial<Environment>;
4043
/**
4144
* If set, copy the files from this spec directory to the package directory
4245
*/
43-
copySpec?: string;
46+
copySpec?: {
47+
spec: string;
48+
/**
49+
* If set, use this directory to store the package json in
50+
*/
51+
subdir?: string[];
52+
};
4453
}) => {
4554
const runTimestamp = Date.now();
4655

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

53-
const env: Partial<Environment> = {};
62+
const env: Partial<Environment> = {
63+
...extraEnv,
64+
};
5465

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

6778
if (copySpec) {
68-
const srcPkg = path.join(SPEC_DIR, copySpec, 'package.json');
69-
const dstPkg = path.join(packageDir, 'package.json');
79+
const srcPkg = path.join(SPEC_DIR, copySpec.spec, 'package.json');
80+
const dstDir = path.join(packageDir, ...(copySpec.subdir || []));
81+
await mkdirP(dstDir);
82+
const dstPkg = path.join(dstDir, 'package.json');
7083
const json = JSON.parse((await fs.readFile(srcPkg)).toString());
7184
json.name = `test-${runTimestamp}`;
7285
await fs.writeFile(dstPkg, JSON.stringify(json, null, ' '));

0 commit comments

Comments
 (0)