Skip to content

Commit

Permalink
Merge pull request #8 from technote-space/release/v0.0.5
Browse files Browse the repository at this point in the history
Release/v0.0.5
  • Loading branch information
technote-space authored Aug 25, 2019
2 parents 93d8dfe + 5b05af1 commit fef5099
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import path from 'path';
// 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 {getCommitMessage, getWorkspace} from './misc';
// import {getCommitMessage, getWorkspace} from './misc';
import {commit} from './command';

export const push = async (files: object, name: string, octokit: GitHub, context: Context) => {
Expand All @@ -12,13 +12,13 @@ export const push = async (files: object, name: string, octokit: GitHub, context
await updateRef(await commit(), name, octokit, context);
};

const getCommit = async (octokit: GitHub, context: Context) => {
return await octokit.git.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.sha,
});
};
// const getCommit = async (octokit: GitHub, context: Context) => {
// return await octokit.git.getCommit({
// owner: context.repo.owner,
// repo: context.repo.repo,
// commit_sha: context.sha,
// });
// };

const existsRef = (name: string, octokit: GitHub, context: Context) => {
return new Promise<boolean>(resolve => {
Expand All @@ -36,20 +36,20 @@ const existsRef = (name: string, octokit: GitHub, context: Context) => {

const getRef = (name: string) => `refs/heads/${name}`;

const createBlob = async (filePath: string, octokit: GitHub, context: Context) => {
const file = path.resolve(getWorkspace(), filePath);
const isExists = fs.existsSync(file);
const blob = await octokit.git.createBlob({
owner: context.repo.owner,
repo: context.repo.repo,
content: isExists ? Buffer.from(fs.readFileSync(file)).toString('base64') : '',
encoding: 'base64',
});
return ({path: filePath, sha: blob.data.sha});
};
// const createBlob = async (filePath: string, octokit: GitHub, context: Context) => {
// const file = path.resolve(getWorkspace(), filePath);
// const isExists = fs.existsSync(file);
// const blob = await octokit.git.createBlob({
// owner: context.repo.owner,
// repo: context.repo.repo,
// content: isExists ? Buffer.from(fs.readFileSync(file)).toString('base64') : '',
// encoding: 'base64',
// });
// return ({path: filePath, sha: blob.data.sha});
// };

const createRef = async (name: string, octokit: GitHub, context: Context) => {
signale.info('Create Ref');
signale.info(`Create Ref: ${name}`);
await octokit.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -58,46 +58,46 @@ const createRef = async (name: string, octokit: GitHub, context: Context) => {
});
};

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 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');
signale.info(`Update Ref: ${commit}`);
await octokit.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: getRef(name),
sha: commit,
sha: commit.trim(),
});
};

0 comments on commit fef5099

Please sign in to comment.