Skip to content

Commit c7210c3

Browse files
test
1 parent 14ac683 commit c7210c3

File tree

7 files changed

+230
-1
lines changed

7 files changed

+230
-1
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
on: push
2+
name: Test
3+
jobs:
4+
release:
5+
name: Test
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Release GitHub Actions
9+
uses: technote-space/release-github-actions@test
10+
with:
11+
repo-token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/.idea
22
/node_modules
33
/coverage
4-
/lib
4+
#/lib

lib/constant.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.DEFAULT_COMMIT_MESSAGE = 'feat: Build for release';
4+
exports.TARGET_EVENT_NAME = 'release';
5+
exports.TARGET_EVENT_ACTION = 'published';

lib/main.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importDefault = (this && this.__importDefault) || function (mod) {
11+
return (mod && mod.__esModule) ? mod : { "default": mod };
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
const core_1 = require("@actions/core");
15+
const github_1 = require("@actions/github");
16+
const signale_1 = __importDefault(require("signale"));
17+
const command_1 = require("./utils/command");
18+
function run() {
19+
return __awaiter(this, void 0, void 0, function* () {
20+
try {
21+
// if (!isTargetEvent(context)) {
22+
// signale.info('This is not target event.');
23+
// signale.info(`Event: ${context.eventName} Action: ${context.action}`);
24+
// return;
25+
// }
26+
// const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
27+
yield command_1.clone(github_1.context);
28+
yield command_1.runBuild();
29+
const files = yield command_1.getDiffFiles();
30+
signale_1.default.info(`Diff files count: ${files.length}`);
31+
if (!files.length)
32+
return;
33+
// const blobs = await filesToBlobs(files, octokit, context);
34+
// const tree = await createTree(blobs, octokit, context);
35+
// const commit = await createCommit(tree, octokit, context);
36+
// await updateRef(commit, context.payload.release.tag_name, octokit, context);
37+
}
38+
catch (error) {
39+
core_1.setFailed(error.message);
40+
}
41+
});
42+
}
43+
run();

lib/utils/command.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importDefault = (this && this.__importDefault) || function (mod) {
11+
return (mod && mod.__esModule) ? mod : { "default": mod };
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
const signale_1 = __importDefault(require("signale"));
15+
const child_process_1 = require("child_process");
16+
const misc_1 = require("./misc");
17+
exports.clone = (context) => __awaiter(this, void 0, void 0, function* () {
18+
if (misc_1.isGitCloned())
19+
return;
20+
const workspace = misc_1.getWorkspace();
21+
const url = misc_1.getGitUrl(context);
22+
yield spawnAsync(`git -C ${workspace} clone --depth=1 --branch=master ${url} .`);
23+
yield spawnAsync(`git -C ${workspace} checkout -qf ${context.sha}`);
24+
});
25+
exports.runBuild = () => __awaiter(this, void 0, void 0, function* () {
26+
const commands = misc_1.getBuildCommands();
27+
if (!commands.length)
28+
return;
29+
const workspace = misc_1.getWorkspace();
30+
const current = process.cwd();
31+
signale_1.default.info('workspace=%s', workspace);
32+
signale_1.default.info('current=%s', current);
33+
yield spawnAsync(`cd ${workspace}`);
34+
for (const command of commands) {
35+
yield spawnAsync(command);
36+
}
37+
yield spawnAsync(`cd ${current}`);
38+
});
39+
exports.getDiffFiles = () => __awaiter(this, void 0, void 0, function* () {
40+
const workspace = misc_1.getWorkspace();
41+
yield spawnAsync(`git -C ${workspace} add --all`);
42+
yield spawnAsync(`git -C ${workspace} status --short -uno`);
43+
return (yield spawnAsync(`git -C ${workspace} status --short -uno`)).split(/\r\n|\n/).filter(line => line.match(/^[MDA]\s+/)).map(line => line.replace(/^[MDA]\s+/, ''));
44+
});
45+
const spawnAsync = (command) => new Promise((resolve, reject) => {
46+
signale_1.default.info(`Run command: ${command}`);
47+
const process = child_process_1.spawn(command);
48+
let output = '';
49+
process.stdout.on('data', data => {
50+
console.log(data);
51+
output += data;
52+
});
53+
process.on('close', code => {
54+
if (code !== 0)
55+
reject(new Error(`command ${command} exited with code ${code}.`));
56+
resolve(output);
57+
});
58+
});

lib/utils/github.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importDefault = (this && this.__importDefault) || function (mod) {
11+
return (mod && mod.__esModule) ? mod : { "default": mod };
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
const fs_1 = __importDefault(require("fs"));
15+
const path_1 = __importDefault(require("path"));
16+
const misc_1 = require("./misc");
17+
exports.filesToBlobs = (files, octokit, context) => __awaiter(this, void 0, void 0, function* () { return yield Promise.all(Object.values(files).map(file => createBlob(file, octokit, context))); });
18+
exports.createTree = (blobs, octokit, context) => __awaiter(this, void 0, void 0, function* () {
19+
return yield octokit.git.createTree({
20+
owner: context.repo.owner,
21+
repo: context.repo.repo,
22+
base_tree: (yield getCommit(octokit, context)).data.tree.sha,
23+
tree: Object.values(blobs).map(blob => ({
24+
path: blob.path,
25+
type: 'blob',
26+
mode: '100644',
27+
sha: blob.sha,
28+
})),
29+
});
30+
});
31+
exports.createCommit = (tree, octokit, context) => __awaiter(this, void 0, void 0, function* () {
32+
return yield octokit.git.createCommit({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
tree: tree.data.sha,
36+
parents: [context.sha],
37+
message: misc_1.getCommitMessage(),
38+
});
39+
});
40+
exports.updateRef = (commit, name, octokit, context) => __awaiter(this, void 0, void 0, function* () {
41+
if (!(yield existsRef(name, octokit, context))) {
42+
yield createRef(name, octokit, context);
43+
}
44+
yield octokit.git.updateRef({
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
ref: getRef(name),
48+
sha: commit.data.sha,
49+
});
50+
});
51+
const getCommit = (octokit, context) => __awaiter(this, void 0, void 0, function* () {
52+
return yield octokit.git.getCommit({
53+
owner: context.repo.owner,
54+
repo: context.repo.repo,
55+
commit_sha: context.sha,
56+
});
57+
});
58+
const existsRef = (name, octokit, context) => {
59+
return new Promise(resolve => {
60+
octokit.git.getRef({
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
ref: getRef(name),
64+
}).then(() => {
65+
resolve(true);
66+
}).catch(() => {
67+
resolve(false);
68+
});
69+
});
70+
};
71+
const getRef = (name) => `refs/heads/${name}`;
72+
const createBlob = (filePath, octokit, context) => __awaiter(this, void 0, void 0, function* () {
73+
const file = path_1.default.resolve(misc_1.getWorkspace(), filePath);
74+
const isExists = fs_1.default.existsSync(file);
75+
const blob = yield octokit.git.createBlob({
76+
owner: context.repo.owner,
77+
repo: context.repo.repo,
78+
content: isExists ? new Buffer(fs_1.default.readFileSync(file)).toString('base64') : '',
79+
encoding: 'base64',
80+
});
81+
return ({ path: filePath, sha: blob.data.sha });
82+
});
83+
const createRef = (name, octokit, context) => __awaiter(this, void 0, void 0, function* () {
84+
yield octokit.git.createRef({
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
ref: getRef(name),
88+
sha: context.sha,
89+
});
90+
});

lib/utils/misc.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const fs_1 = __importDefault(require("fs"));
7+
const path_1 = __importDefault(require("path"));
8+
const js_yaml_1 = __importDefault(require("js-yaml"));
9+
const core_1 = require("@actions/core");
10+
const constant_1 = require("../constant");
11+
exports.isTargetEvent = (context) => constant_1.TARGET_EVENT_NAME === context.eventName && constant_1.TARGET_EVENT_ACTION === context.payload.action;
12+
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'));
15+
exports.getGitUrl = (context) => `https://github.com/${context.repo.owner}/${context.repo.repo}.git`;
16+
exports.getBuildCommands = () => {
17+
const command = core_1.getInput('BUILD_COMMAND');
18+
if ('' === command)
19+
return [];
20+
return [command];
21+
};
22+
exports.getCommitMessage = () => core_1.getInput('COMMIT_MESSAGE') || constant_1.DEFAULT_COMMIT_MESSAGE;

0 commit comments

Comments
 (0)