Skip to content

Commit fcd9160

Browse files
test
1 parent 3f652e7 commit fcd9160

File tree

13 files changed

+79
-70
lines changed

13 files changed

+79
-70
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ jobs:
99
uses: technote-space/release-github-actions@test
1010
with:
1111
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12-
GITHUB_WORKSPACE: ${GITHUB_WORKSPACE}
1312
BUILD_COMMAND: 'yarn install && yarn build && yarn install --production'

__tests__/utils/misc.test.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import nock from 'nock';
22
import tmp from 'tmp';
33
import {encodeContent} from '../util';
4-
import {isTargetEvent, parseConfig, getWorkspace, getCommitMessage, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
5-
import {DEFAULT_COMMIT_MESSAGE} from '../../src/constant';
4+
import {isTargetEvent, parseConfig, getCommitMessage, getCloneDepth, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
5+
import {DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH} from '../../src/constant';
66

77
const fs = require('fs');
88
const path = require('path');
@@ -88,7 +88,7 @@ describe('parseConfig', () => {
8888
});
8989
});
9090

91-
describe('getWorkspace', () => {
91+
describe('getCommitMessage', () => {
9292
const OLD_ENV = process.env;
9393

9494
beforeEach(() => {
@@ -101,24 +101,17 @@ describe('getWorkspace', () => {
101101
process.env = OLD_ENV;
102102
});
103103

104-
it('should return workspace', () => {
105-
process.env.INPUT_GITHUB_WORKSPACE = 'test';
106-
expect(getWorkspace()).toBe('test');
104+
it('should get commit message', () => {
105+
process.env.INPUT_COMMIT_MESSAGE = 'test';
106+
expect(getCommitMessage()).toBe('test');
107107
});
108108

109-
it('should throw error', () => {
110-
const fn = jest.fn();
111-
try {
112-
expect(getWorkspace()).toBe('test');
113-
} catch (error) {
114-
fn();
115-
expect(error.message).toBe('Input required and not supplied: GITHUB_WORKSPACE');
116-
}
117-
expect(fn).toBeCalled();
109+
it('should get commit default message', () => {
110+
expect(getCommitMessage()).toBe(DEFAULT_COMMIT_MESSAGE);
118111
});
119112
});
120113

121-
describe('getCommitMessage', () => {
114+
describe('getCloneDepth', () => {
122115
const OLD_ENV = process.env;
123116

124117
beforeEach(() => {
@@ -131,13 +124,13 @@ describe('getCommitMessage', () => {
131124
process.env = OLD_ENV;
132125
});
133126

134-
it('should get commit message', () => {
135-
process.env.INPUT_COMMIT_MESSAGE = 'test';
136-
expect(getCommitMessage()).toBe('test');
127+
it('should get clone depth', () => {
128+
process.env.INPUT_CLONE_DEPTH = '3';
129+
expect(getCloneDepth()).toBe('3');
137130
});
138131

139-
it('should get commit default message', () => {
140-
expect(getCommitMessage()).toBe(DEFAULT_COMMIT_MESSAGE);
132+
it('should get default clone depth', () => {
133+
expect(getCloneDepth()).toBe(DEFAULT_CLONE_DEPTH);
141134
});
142135
});
143136

@@ -168,6 +161,26 @@ describe('getBuildCommands', () => {
168161

169162
describe('isGitCloned', () => {
170163
const OLD_ENV = process.env;
164+
const context = () => ({
165+
payload: {
166+
action: 'created',
167+
},
168+
eventName: 'release',
169+
sha: '',
170+
ref: '',
171+
workflow: dir.name,
172+
action: '',
173+
actor: '',
174+
issue: {
175+
owner: '',
176+
repo: '',
177+
number: 1,
178+
},
179+
repo: {
180+
owner: '',
181+
repo: '',
182+
},
183+
});
171184
let dir;
172185

173186
beforeEach(() => {
@@ -184,13 +197,11 @@ describe('isGitCloned', () => {
184197

185198
it('should return true', () => {
186199
fs.mkdirSync(path.resolve(dir.name, '.git'));
187-
process.env.INPUT_GITHUB_WORKSPACE = dir.name;
188-
expect(isGitCloned()).toBeTruthy();
200+
expect(isGitCloned(context())).toBeTruthy();
189201
});
190202

191203
it('should return false', () => {
192-
process.env.INPUT_GITHUB_WORKSPACE = dir.name;
193-
expect(isGitCloned()).toBeFalsy();
204+
expect(isGitCloned(context())).toBeFalsy();
194205
});
195206
});
196207

action.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ inputs:
55
GITHUB_TOKEN:
66
description: Secret GitHub API token to use for making API requests.
77
required: true
8-
GITHUB_WORKSPACE:
9-
description: The GitHub workspace directory path.
10-
required: true
118
BUILD_COMMAND:
129
description: Command to build.
1310
default: ''
1411
COMMIT_MESSAGE:
1512
description: Commit message of build commit.
1613
default: 'feat: Build for release'
14+
CLONE_DEPTH:
15+
description: Git clone depth.
16+
default: '50'
1717
runs:
1818
using: node12
1919
main: lib/main.js

lib/constant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.DEFAULT_COMMIT_MESSAGE = 'feat: Build for release';
4+
exports.DEFAULT_CLONE_DEPTH = '50';
45
exports.TARGET_EVENT_NAME = 'release';
56
exports.TARGET_EVENT_ACTION = 'published';

lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function run() {
2525
// }
2626
// const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
2727
yield command_1.clone(github_1.context);
28-
yield command_1.runBuild();
29-
const files = yield command_1.getDiffFiles();
28+
yield command_1.runBuild(github_1.context);
29+
const files = yield command_1.getDiffFiles(github_1.context);
3030
signale_1.default.info(`Diff files count: ${files.length}`);
3131
if (!files.length)
3232
return;

lib/utils/command.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,32 @@ const signale_1 = __importDefault(require("signale"));
1515
const child_process_1 = require("child_process");
1616
const misc_1 = require("./misc");
1717
exports.clone = (context) => __awaiter(this, void 0, void 0, function* () {
18-
yield execAsync('ls -lat');
19-
if (misc_1.isGitCloned())
18+
signale_1.default.info(yield execAsync('ls -lat'));
19+
if (misc_1.isGitCloned(context))
2020
return;
21-
const workspace = misc_1.getWorkspace();
2221
const url = misc_1.getGitUrl(context);
23-
yield execAsync(`git -C ${workspace} clone --depth=1 --branch=master ${url} .`);
24-
yield execAsync(`git -C ${workspace} checkout -qf ${context.sha}`);
22+
const depth = misc_1.getCloneDepth();
23+
yield execAsync(`git -C ${context.workflow} clone --depth=${depth} --branch=master ${url} .`);
24+
signale_1.default.info(yield execAsync('ls -lat'));
25+
yield execAsync(`git -C ${context.workflow} checkout -qf ${context.sha}`);
2526
});
26-
exports.runBuild = () => __awaiter(this, void 0, void 0, function* () {
27+
exports.runBuild = (context) => __awaiter(this, void 0, void 0, function* () {
2728
const commands = misc_1.getBuildCommands();
2829
if (!commands.length)
2930
return;
30-
const workspace = misc_1.getWorkspace();
3131
const current = process.cwd();
32-
signale_1.default.info('workspace=%s', workspace);
32+
signale_1.default.info('context.workflow=%s', context.workflow);
3333
signale_1.default.info('current=%s', current);
34-
yield execAsync(`cd ${workspace}`);
34+
yield execAsync(`cd ${context.workflow}`);
3535
for (const command of commands) {
3636
yield execAsync(command);
3737
}
3838
yield execAsync(`cd ${current}`);
3939
});
40-
exports.getDiffFiles = () => __awaiter(this, void 0, void 0, function* () {
41-
const workspace = misc_1.getWorkspace();
42-
yield execAsync(`git -C ${workspace} add --all`);
43-
yield execAsync(`git -C ${workspace} status --short -uno`);
44-
return (yield execAsync(`git -C ${workspace} status --short -uno`)).split(/\r\n|\n/).filter(line => line.match(/^[MDA]\s+/)).map(line => line.replace(/^[MDA]\s+/, ''));
40+
exports.getDiffFiles = (context) => __awaiter(this, void 0, void 0, function* () {
41+
yield execAsync(`git -C ${context.workflow} add --all`);
42+
yield execAsync(`git -C ${context.workflow} status --short -uno`);
43+
return (yield execAsync(`git -C ${context.workflow} status --short -uno`)).split(/\r\n|\n/).filter(line => line.match(/^[MDA]\s+/)).map(line => line.replace(/^[MDA]\s+/, ''));
4544
});
4645
const execAsync = (command) => new Promise((resolve, reject) => {
4746
signale_1.default.info(`Run command: ${command}`);

lib/utils/github.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const existsRef = (name, octokit, context) => {
7070
};
7171
const getRef = (name) => `refs/heads/${name}`;
7272
const createBlob = (filePath, octokit, context) => __awaiter(this, void 0, void 0, function* () {
73-
const file = path_1.default.resolve(misc_1.getWorkspace(), filePath);
73+
const file = path_1.default.resolve(context.workflow, filePath);
7474
const isExists = fs_1.default.existsSync(file);
7575
const blob = yield octokit.git.createBlob({
7676
owner: context.repo.owner,

lib/utils/misc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ const core_1 = require("@actions/core");
1010
const constant_1 = require("../constant");
1111
exports.isTargetEvent = (context) => constant_1.TARGET_EVENT_NAME === context.eventName && constant_1.TARGET_EVENT_ACTION === context.payload.action;
1212
exports.parseConfig = (content) => js_yaml_1.default.safeLoad(Buffer.from(content, 'base64').toString()) || {};
13-
exports.getWorkspace = () => core_1.getInput('GITHUB_WORKSPACE', { required: true });
14-
exports.isGitCloned = () => fs_1.default.existsSync(path_1.default.resolve(exports.getWorkspace(), '.git'));
13+
exports.isGitCloned = (context) => fs_1.default.existsSync(path_1.default.resolve(context.workflow, '.git'));
1514
exports.getGitUrl = (context) => `https://github.com/${context.repo.owner}/${context.repo.repo}.git`;
1615
exports.getBuildCommands = () => {
1716
const command = core_1.getInput('BUILD_COMMAND');
@@ -20,3 +19,4 @@ exports.getBuildCommands = () => {
2019
return [command];
2120
};
2221
exports.getCommitMessage = () => core_1.getInput('COMMIT_MESSAGE') || constant_1.DEFAULT_COMMIT_MESSAGE;
22+
exports.getCloneDepth = () => core_1.getInput('CLONE_DEPTH') || constant_1.DEFAULT_CLONE_DEPTH;

src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const DEFAULT_COMMIT_MESSAGE = 'feat: Build for release';
2+
export const DEFAULT_CLONE_DEPTH = '50';
23
export const TARGET_EVENT_NAME = 'release';
34
export const TARGET_EVENT_ACTION = 'published';

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ async function run() {
1414
// }
1515
// const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
1616
await clone(context);
17-
await runBuild();
18-
const files = await getDiffFiles();
17+
await runBuild(context);
18+
const files = await getDiffFiles(context);
1919
signale.info(`Diff files count: ${files.length}`);
2020
if (!files.length) return;
2121

0 commit comments

Comments
 (0)