Skip to content

Commit 1991ad4

Browse files
authored
Merge pull request #3 from michmich112/fix/other-os
Fix for MacOs and Windows runner
2 parents 8b9d06f + 510b6e6 commit 1991ad4

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

.github/workflows/test.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test package
2+
on:
3+
push:
4+
workflow_dispatch:
5+
6+
jobs:
7+
unit-tests:
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, macos-latest, windows-latest]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: actions/setup-node@v1
15+
with:
16+
node-version: '12'
17+
- name: install packages
18+
run: |
19+
npm install
20+
- name: get dir path
21+
id: get-path
22+
uses: michmich112/get-actions-dir-path@main
23+
- name: Test package
24+
run: |
25+
npm run test
26+
env:
27+
DIRPATH: ${{ steps.get-path.outputs.dir-path }}
28+

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"name": "gh-action-stats",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Library to get stats for github action runs",
5+
"repository": {
6+
"type": "git",
7+
"url": "github.com/michmich112/gh-action-stats-js"
8+
},
59
"main": "index.js",
610
"scripts": {
711
"test": "jest"

tests/utils-ci.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { getActionMetadataFromDirname } = require('../utils');
2+
3+
describe('GetActionMetadataFromDirname Tests Actual Runner', () => {
4+
test('It can get the correct results from in the runner environment', () => {
5+
if (process.env.CI) {
6+
console.debug('Runner environment', process.env.RUNNER_OS);
7+
const actionMetadata = getActionMetadataFromDirname(process.env.DIRPATH);
8+
expect(actionMetadata).toEqual({
9+
creator: 'michmich112',
10+
name: 'get-actions-dir-path',
11+
version: 'main'
12+
})
13+
} else {
14+
console.debug('Not CI, skipping');
15+
}
16+
});
17+
});
18+

tests/utils.test.js

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
jest.mock('path');
2+
const path = require('path');
13
const { getActionMetadataFromDirname, getRunMetadata } = require('../utils');
24

3-
describe('GetActionMetadataFromDirname Tests', () => {
5+
describe('GetActionMetadataFromDirname Tests Ubuntu', () => {
46
test('it can get the right info from a action with a version', () => {
7+
process.env.RUNNER_OS = 'Linux';
8+
path.sep = '/';
9+
510
const dirname = `/home/runner/work/_actions/michmich112/version-bumper/v1.0.0/node_modules/gh-action-stats`;
611
const actionMetadata = getActionMetadataFromDirname(dirname);
712
expect(actionMetadata).toEqual({
@@ -42,6 +47,94 @@ describe('GetActionMetadataFromDirname Tests', () => {
4247
});
4348
});
4449

50+
describe('GetActionMetadataFromDirname Tests MacOs', () => {
51+
test('it can get the right info from a action with a version', () => {
52+
process.env.RUNNER_OS = 'macOS';
53+
path.sep = '/';
54+
const dirname = `/Users/runner/work/_actions/michmich112/version-bumper/v1.0.0/node_modules/gh-action-stats`;
55+
const actionMetadata = getActionMetadataFromDirname(dirname);
56+
expect(actionMetadata).toEqual({
57+
creator: 'michmich112',
58+
name: 'version-bumper',
59+
version: 'v1.0.0'
60+
});
61+
});
62+
63+
test('it can get the right info from an action with a commit hash', () => {
64+
const dirname = `/Users/runner/work/_actions/michmich112/version-bumper/8fc6e82c93abcdaae3fbb8239d101efc6b63b606/node_modules/gh-action-stats`
65+
const actionMetadata = getActionMetadataFromDirname(dirname);
66+
expect(actionMetadata).toEqual({
67+
creator: 'michmich112',
68+
name: 'version-bumper',
69+
version: '8fc6e82c93abcdaae3fbb8239d101efc6b63b606'
70+
});
71+
});
72+
73+
test('it can get the right info from an action with a branch', () => {
74+
const dirname = `/Users/runner/work/_actions/michmich112/version-bumper/master/node_modules/gh-action-stats`
75+
const actionMetadata = getActionMetadataFromDirname(dirname);
76+
expect(actionMetadata).toEqual({
77+
creator: 'michmich112',
78+
name: 'version-bumper',
79+
version: 'master'
80+
});
81+
});
82+
83+
test('it can get the righ info from an action with a branch with the / character', () => {
84+
const dirname = `/Users/runner/work/_actions/michmich112/version-bumper/fix/bug-fix-important/node_modules/gh-action-stats`
85+
const actionMetadata = getActionMetadataFromDirname(dirname);
86+
expect(actionMetadata).toEqual({
87+
creator: 'michmich112',
88+
name: 'version-bumper',
89+
version: 'fix/bug-fix-important'
90+
});
91+
});
92+
});
93+
94+
describe('GetActionMetadataFromDirname Tests Windows', () => {
95+
test('it can get the right info from a action with a version', () => {
96+
process.env.RUNNER_OS = 'Windows';
97+
path.sep = '\\';
98+
const dirname = `D:\\a\\_actions\\michmich112\\version-bumper\\v1.0.0\\node_modules\\gh-action-stats`;
99+
const actionMetadata = getActionMetadataFromDirname(dirname);
100+
expect(actionMetadata).toEqual({
101+
creator: 'michmich112',
102+
name: 'version-bumper',
103+
version: 'v1.0.0'
104+
});
105+
});
106+
107+
test('it can get the right info from an action with a commit hash', () => {
108+
const dirname = `D:\\a\\_actions\\michmich112\\version-bumper\\8fc6e82c93abcdaae3fbb8239d101efc6b63b606\\node_modules\\gh-action-stats`
109+
const actionMetadata = getActionMetadataFromDirname(dirname);
110+
expect(actionMetadata).toEqual({
111+
creator: 'michmich112',
112+
name: 'version-bumper',
113+
version: '8fc6e82c93abcdaae3fbb8239d101efc6b63b606'
114+
});
115+
});
116+
117+
test('it can get the right info from an action with a branch', () => {
118+
const dirname = `D:\\a\\_actions\\michmich112\\version-bumper\\master\\node_modules\\gh-action-stats`
119+
const actionMetadata = getActionMetadataFromDirname(dirname);
120+
expect(actionMetadata).toEqual({
121+
creator: 'michmich112',
122+
name: 'version-bumper',
123+
version: 'master'
124+
});
125+
});
126+
127+
test('it can get the righ info from an action with a branch with the / character', () => {
128+
const dirname = `D:\\a\\_actions\\michmich112\\version-bumper\\fix\\bug-fix-important\\node_modules\\gh-action-stats`
129+
const actionMetadata = getActionMetadataFromDirname(dirname);
130+
expect(actionMetadata).toEqual({
131+
creator: 'michmich112',
132+
name: 'version-bumper',
133+
version: 'fix/bug-fix-important'
134+
});
135+
});
136+
});
137+
45138
describe('GetRunMetadata Tests', () => {
46139
const envVars = {
47140
'GITHUB_RUN_ID': 'id',

utils.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
const path = require('path');
2+
3+
function getOsPath() {
4+
const runner = process.env.RUNNER_OS;
5+
switch (runner) {
6+
case 'Linux':
7+
return '/home/runner/work/_actions/';
8+
case 'macOS':
9+
return '/Users/runner/work/_actions/';
10+
case 'Windows':
11+
return 'D:\\a\\_actions\\';
12+
}
13+
}
14+
115
/**
216
* returns the following information about the current action
317
* creator: github user who created the action
@@ -6,9 +20,9 @@
620
* Note this would only work with macOS and Linux, i cant make sure that it works for windows just yet.
721
*/
822
function getActionMetadataFromDirname(dirname) {
9-
const metadata = dirname.replace('/home/runner/work/_actions/', '') // remove os path
10-
.replace('/node_modules/gh-action-stats', '') // remove node_modules and
11-
.split('/')
23+
const metadata = dirname.replace(getOsPath(), '') // remove os path
24+
.replace(['', 'node_modules', 'gh-action-stats'].join(path.sep), '') // remove node_modules and
25+
.split(path.sep)
1226
return {
1327
creator: metadata[0],
1428
name: metadata[1],

0 commit comments

Comments
 (0)