Skip to content

Commit

Permalink
Merge f6a6189 into ecdcf8d
Browse files Browse the repository at this point in the history
  • Loading branch information
technote-space committed Aug 31, 2019
2 parents ecdcf8d + f6a6189 commit 06ab59a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Just run [DocToc](https://github.com/thlorenz/doctoc) and commit to branch if ch
- [Installation](#installation)
- [Options](#options)
- [TARGET_PATHS](#target_paths)
- [TOC_TITLE](#toc_title)
- [COMMIT_MESSAGE](#commit_message)
- [Action event details](#action-event-details)
- [Target event](#target-event)
Expand Down Expand Up @@ -50,6 +51,9 @@ Just run [DocToc](https://github.com/thlorenz/doctoc) and commit to branch if ch
### TARGET_PATHS
Target file path. (Comma separated, [Detail](https://github.com/thlorenz/doctoc#adding-toc-to-individual-files))
default: `'README.md'`
### TOC_TITLE
TOC Title.
default: `'**Table of Contents**'`
### COMMIT_MESSAGE
Commit message.
default: `'docs: Update TOC'`
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('push', () => {
fn8();
return getApiFixture('repos.git.commits');
})
.patch('/repos/hello/world/git/refs/heads/test', body => {
.patch('/repos/hello/world/git/refs/' + encodeURIComponent('heads/test'), body => {
fn9();
expect(body).toHaveProperty('sha');
return body;
Expand Down
15 changes: 8 additions & 7 deletions __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import {getContext, getApiFixture, disableNetConnect, testEnv} from '../util';
import {getContext, testEnv} from '../util';
import {
isTargetEvent,
getBuildVersion,
Expand Down Expand Up @@ -66,15 +66,16 @@ describe('getBuildVersion', () => {
describe('getDocTocArgs', () => {
testEnv();

it('should get default DocToc args', () => {
it('should get DocToc args', () => {
process.env.GITHUB_WORKSPACE = '/tmp/workspace';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md');
process.env.INPUT_TARGET_PATHS = 'README.md,.github/CONTRIBUTING.md';
process.env.INPUT_TOC_TITLE = '**Table of Contents**';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md /tmp/workspace/.work/.github/CONTRIBUTING.md --title **Table of Contents**');
});

it('should get DocToc args', () => {
it('should get default DocToc args', () => {
process.env.GITHUB_WORKSPACE = '/tmp/workspace';
process.env.INPUT_TARGET_PATHS = 'README.md,.github/CONTRIBUTING.md';
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md /tmp/workspace/.work/.github/CONTRIBUTING.md');
expect(getDocTocArgs()).toBe('/tmp/workspace/.work/README.md --notitle');
});
});

Expand Down Expand Up @@ -116,7 +117,7 @@ describe('getRefForUpdate', () => {
it('should get ref for update', () => {
expect(getRefForUpdate(getContext({
ref: 'refs/heads/test',
}))).toBe('heads/test');
}))).toBe(encodeURIComponent('heads/test'));
});
});

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
TARGET_PATHS:
description: Target file path. (Comma separated, @see https://github.com/thlorenz/doctoc#adding-toc-to-individual-files)
default: 'README.md'
TOC_TITLE:
description: TOC Title
default: '**Table of Contents**'
COMMIT_MESSAGE:
description: Commit message.
default: 'docs: Update TOC'
Expand Down
3 changes: 1 addition & 2 deletions src/utils/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ const getCurrentBranchName = async (workDir: string): Promise<string> => {
const runDocToc = async (workDir: string): Promise<boolean> => {
const args = getDocTocArgs();
const doctoc = path.resolve(workDir, 'node_modules/.bin/doctoc');
await execAsync(`yarn --cwd ${workDir} add doctoc`);
await execAsync(`yarn --cwd ${workDir} add doctoc`, false, null, false, true);
await execAsync(`${doctoc} ${args} --github`);
await execAsync(`yarn --cwd ${workDir} remove doctoc`);
return true;
};

Expand Down
4 changes: 2 additions & 2 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const push = async (files: string[], octokit: GitHub, context: Context):
signale.info('>> Creating tree...');
const tree = await createTree(blobs, octokit, context);

signale.info('>> Creating commit...');
signale.info('>> Creating commit... [%s]', tree.data.sha);
const commit = await createCommit(tree, octokit, context);

signale.info('>> Updating ref...');
signale.info('>> Updating ref... [%s] [%s]', getRefForUpdate(context), commit.data.sha);
await updateRef(commit, octokit, context);
return true;
};
Expand Down
7 changes: 5 additions & 2 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ const getTargetPaths = (): string[] => [...new Set<string>((getInput('TARGET_PAT

export const getDocTocArgs = () => {
const workDir = getWorkDir();
return getTargetPaths().map(item => path.resolve(workDir, item)).join(' ');
const title = getTocTitle().replace('\'', '\\\'').replace('"', '\\"');
return getTargetPaths().map(item => path.resolve(workDir, item)).join(' ') + (title ? ` --title ${title}` : ' --notitle');
};

const getTocTitle = (): string => getInput('TOC_TITLE') || '';

const getWorkspace = (): string => process.env.GITHUB_WORKSPACE || '';

export const getWorkDir = () => path.resolve(getWorkspace(), '.work');
Expand All @@ -40,7 +43,7 @@ export const getGitUrl = (context: Context): string => `https://github.com/${con

export const getRef = (context: Context): string => context.ref;

export const getRefForUpdate = (context: Context): string => getRef(context).replace(/^refs\//, '');
export const getRefForUpdate = (context: Context): string => encodeURIComponent(getRef(context).replace(/^refs\//, ''));

const isTargetRef = (context: Context): boolean => /^refs\/heads\//.test(getRef(context));

Expand Down

0 comments on commit 06ab59a

Please sign in to comment.