Skip to content

Commit ad8209b

Browse files
Merge pull request #3 from technote-space/release/v0.0.3
release/v0.0.3
2 parents 6bc5de9 + a0a1bbd commit ad8209b

File tree

5 files changed

+78
-50
lines changed

5 files changed

+78
-50
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
on: pull_request
1+
on: [push, pull_request]
22

33
name: Build
44

@@ -7,30 +7,38 @@ jobs:
77
name: Jest
88
runs-on: ubuntu-latest
99
steps:
10+
# neutral status 78 has removed...
11+
# https://github.community/t5/GitHub-API-Development-and/GitHub-Actions-quot-neutral-quot-exit-code-is-incorrectly/td-p/29051
12+
# - name: Filter
13+
# run: if [[ ${GITHUB_EVENT_NAME} == "push" && ${GITHUB_REF} != "refs/heads/master" ]]; then exit 78; fi
1014
- uses: actions/checkout@v1
1115
with:
1216
fetch-depth: 1
17+
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
1318
- name: Install Package dependencies
1419
run: yarn install
20+
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
1521
- name: Run tests
1622
run: yarn cover
23+
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
1724
- name: Send covarage
1825
run: yarn add coveralls && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
1926
env:
2027
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
2128
COVERALLS_SERVICE_NAME: "GitHub Action"
2229
COVERALLS_SERVICE_JOB_ID: ${{ github.sha }}
30+
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
2331
- uses: 8398a7/action-slack@v1
2432
with:
2533
type: failure
2634
env:
2735
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2836
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
29-
if: failure()
37+
if: (github.event_name == 'pull_request' || github.ref == 'refs/heads/master') && failure()
3038
- uses: 8398a7/action-slack@v1
3139
with:
3240
type: success
3341
env:
3442
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3543
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
36-
if: success()
44+
if: (github.event_name == 'pull_request' || github.ref == 'refs/heads/master') && success()

__tests__/utils/misc.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import nock from 'nock';
24
import tmp from 'tmp';
35
import {encodeContent} from '../util';
46
import {isTargetEvent, parseConfig, getCommitMessage, getCloneDepth, getWorkspace, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
57
import {DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH} from '../../src/constant';
68

7-
const fs = require('fs');
8-
const path = require('path');
9-
109
nock.disableNetConnect();
1110

1211
describe('isTargetEvent', () => {

src/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ import {setFailed, getInput} from '@actions/core' ;
22
import {context, GitHub} from '@actions/github' ;
33
import signale from 'signale';
44
import {clone, runBuild, getDiffFiles} from './utils/command';
5-
import {filesToBlobs, createTree, createCommit, updateRef} from './utils/github';
5+
import {push} from './utils/github';
66
import {isTargetEvent} from './utils/misc';
77

88
async function run() {
99
try {
10+
signale.info(`Event: ${context.eventName}`);
11+
signale.info(`Action: ${context.action}`);
1012
if (!isTargetEvent(context)) {
1113
signale.info('This is not target event.');
12-
signale.info(`Event: ${context.eventName} Action: ${context.action}`);
1314
return;
1415
}
16+
17+
signale.info(`Tag name: ${context.payload.release.tag_name}`);
1518
const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
1619
await clone(context);
1720
await runBuild();
1821
const files = await getDiffFiles();
1922
signale.info(`Diff files count: ${files.length}`);
2023
if (!files.length) return;
2124

22-
const blobs = await filesToBlobs(files, octokit, context);
23-
const tree = await createTree(blobs, octokit, context);
24-
const commit = await createCommit(tree, octokit, context);
25-
await updateRef(commit, context.payload.release.tag_name, octokit, context);
25+
await push(files, context.payload.release.tag_name, octokit, context);
2626
} catch (error) {
2727
setFailed(error.message);
2828
}

src/utils/command.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import signale from 'signale';
22
import {exec} from 'child_process';
33
import {Context} from '@actions/github/lib/context';
4-
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace} from './misc';
4+
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace, getCommitMessage} from './misc';
55

66
export const clone = async (context: Context) => {
77
if (isGitCloned()) return;
@@ -28,6 +28,13 @@ export const getDiffFiles = async () => {
2828
return (await execAsync(`git -C ${workspace} status --short -uno`)).split(/\r\n|\n/).filter(line => line.match(/^[MDA]\s+/)).map(line => line.replace(/^[MDA]\s+/, ''));
2929
};
3030

31+
export const commit = async () => {
32+
const workspace = getWorkspace();
33+
const message = getCommitMessage();
34+
await execAsync(`git -C ${workspace} commit -m "${message}"`);
35+
return execAsync(`git -C ${workspace} rev-parse HEAD`);
36+
};
37+
3138
const execAsync = (command: string) => new Promise<string>((resolve, reject) => {
3239
signale.info(`Run command: ${command}`);
3340

src/utils/github.ts

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import signale from 'signale';
34
import {GitHub} from '@actions/github/lib/github';
45
import {Context} from '@actions/github/lib/context';
5-
import {Response, GitCreateTreeResponse, GitCreateCommitResponse} from '@octokit/rest';
66
import {getCommitMessage, getWorkspace} from './misc';
7+
import {commit} from './command';
78

8-
export const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => await Promise.all(Object.values(files).map(file => createBlob(file, octokit, context)));
9-
10-
export const createTree = async (blobs: object, octokit: GitHub, context: Context) => {
11-
return await octokit.git.createTree({
12-
owner: context.repo.owner,
13-
repo: context.repo.repo,
14-
base_tree: (await getCommit(octokit, context)).data.tree.sha,
15-
tree: Object.values(blobs).map(blob => ({
16-
path: blob.path,
17-
type: 'blob',
18-
mode: '100644',
19-
sha: blob.sha,
20-
})),
21-
});
22-
};
23-
24-
export const createCommit = async (tree: Response<GitCreateTreeResponse>, octokit: GitHub, context: Context) => {
25-
return await octokit.git.createCommit({
26-
owner: context.repo.owner,
27-
repo: context.repo.repo,
28-
tree: tree.data.sha,
29-
parents: [context.sha],
30-
message: getCommitMessage(),
31-
});
32-
};
33-
34-
export const updateRef = async (commit: Response<GitCreateCommitResponse>, name: string, octokit: GitHub, context: Context) => {
35-
if (!await existsRef(name, octokit, context)) {
36-
await createRef(name, octokit, context);
37-
}
38-
await octokit.git.updateRef({
39-
owner: context.repo.owner,
40-
repo: context.repo.repo,
41-
ref: getRef(name),
42-
sha: commit.data.sha,
43-
});
9+
export const push = async (files: object, name: string, octokit: GitHub, context: Context) => {
10+
// const commit = await createCommit(files, octokit, context);
11+
// await updateRef(commit.data.sha, name, octokit, context);
12+
await updateRef(await commit(), name, octokit, context);
4413
};
4514

4615
const getCommit = async (octokit: GitHub, context: Context) => {
@@ -80,10 +49,55 @@ const createBlob = async (filePath: string, octokit: GitHub, context: Context) =
8049
};
8150

8251
const createRef = async (name: string, octokit: GitHub, context: Context) => {
52+
signale.info('Create Ref');
8353
await octokit.git.createRef({
8454
owner: context.repo.owner,
8555
repo: context.repo.repo,
8656
ref: getRef(name),
8757
sha: context.sha,
8858
});
8959
};
60+
61+
const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => Object.values(files).map(async file => await createBlob(file, octokit, context));
62+
63+
const createTree = async (blobs: object, octokit: GitHub, context: Context) => {
64+
signale.info('Create Tree');
65+
return await octokit.git.createTree({
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
base_tree: (await getCommit(octokit, context)).data.tree.sha,
69+
tree: Object.values(blobs).map(blob => ({
70+
path: blob.path,
71+
type: 'blob',
72+
mode: '100644',
73+
sha: blob.sha,
74+
})),
75+
});
76+
};
77+
78+
const createCommit = async (files: object, octokit: GitHub, context: Context) => {
79+
const blobs = await filesToBlobs(files, octokit, context);
80+
const tree = await createTree(blobs, octokit, context);
81+
signale.info('Create Commit');
82+
return await octokit.git.createCommit({
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
tree: tree.data.sha,
86+
parents: [context.sha],
87+
message: getCommitMessage(),
88+
});
89+
};
90+
91+
const updateRef = async (commit: string, name: string, octokit: GitHub, context: Context) => {
92+
if (!await existsRef(name, octokit, context)) {
93+
await createRef(name, octokit, context);
94+
}
95+
96+
signale.info('Update Ref');
97+
await octokit.git.updateRef({
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
ref: getRef(name),
101+
sha: commit,
102+
});
103+
};

0 commit comments

Comments
 (0)