Skip to content

Commit

Permalink
feat: custom config name (#3)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4dcb392a346b6caba1908f8a1d4745bbd6b4d789)
  • Loading branch information
technote-space committed Aug 27, 2019
1 parent 9f10afe commit 7a1f909
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ Project name1:
- Current Labels
- `Status: test1`

## Options
### CONFIG_FILENAME
Config file name.
default: `'card-labeler.yml'`

## Author
[GitHub (Technote)](https://github.com/technote-space)
[Blog](https://technote.space)
10 changes: 5 additions & 5 deletions __tests__/utils/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import nock from 'nock';
import {GitHub} from '@actions/github' ;
import {getConfig} from '../../src/utils/config';
import {getConfigFixture} from '../util';
import {CONFIG_FILENAME} from '../../src/constant';
import {getConfigFilename} from '../../src/utils/misc';

describe('getConfig', () => {
it('should get config', async () => {
nock('https://api.github.com')
.get(`/repos/Codertocat/Hello-World/contents/.github/${CONFIG_FILENAME}`)
.get(`/repos/Codertocat/Hello-World/contents/.github/${getConfigFilename()}`)
.reply(200, getConfigFixture());

const config = await getConfig(CONFIG_FILENAME, new GitHub(''), {
const config = await getConfig(getConfigFilename(), new GitHub(''), {
repo: {
owner: 'Codertocat', repo: 'Hello-World',
},
Expand All @@ -23,12 +23,12 @@ describe('getConfig', () => {

it('should not get config', async () => {
nock('https://api.github.com')
.get(`/repos/Codertocat/Hello-World/contents/.github/${CONFIG_FILENAME}`)
.get(`/repos/Codertocat/Hello-World/contents/.github/${getConfigFilename()}`)
.reply(404);

const fn = jest.fn();
try {
await getConfig(CONFIG_FILENAME, new GitHub(''), {
await getConfig(getConfigFilename(), new GitHub(''), {
repo: {
owner: 'Codertocat', repo: 'Hello-World',
},
Expand Down
26 changes: 25 additions & 1 deletion __tests__/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import nock from 'nock';
import {GitHub} from '@actions/github' ;
import {encodeContent, getApiFixture} from '../util';
import {isTargetEvent, parseConfig, getProjectName, getColumnName} from '../../src/utils/misc';
import {isTargetEvent, parseConfig, getProjectName, getColumnName, getConfigFilename} from '../../src/utils/misc';
import {DEFAULT_CONFIG_FILENAME} from '../../src/constant';

nock.disableNetConnect();

Expand Down Expand Up @@ -135,3 +136,26 @@ describe('getColumnName', () => {
expect(fn).toBeCalled();
});
});

describe('getConfigFilename', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = {...OLD_ENV};
delete process.env.NODE_ENV;
});

afterEach(() => {
process.env = OLD_ENV;
});

it('should get config filename', () => {
process.env.INPUT_CONFIG_FILENAME = 'test';
expect(getConfigFilename()).toBe('test');
});

it('should get default config filename', () => {
expect(getConfigFilename()).toBe(DEFAULT_CONFIG_FILENAME);
});
});
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inputs:
GITHUB_TOKEN:
description: Secret GitHub API token to use for making API requests.
required: true
CONFIG_FILENAME:
description: Config file name.
default: 'card-labeler.yml'
runs:
using: node12
main: lib/main.js
2 changes: 1 addition & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const CONFIG_PATH = '.github';
export const CONFIG_FILENAME = 'card-labeler.yml';
export const DEFAULT_CONFIG_FILENAME = 'card-labeler.yml';
export const TARGET_EVENT_NAME = 'project_card';
export const TARGET_EVENT_ACTION = 'moved';
7 changes: 3 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import signale from 'signale';
import {getConfig} from './utils/config';
import {getRelatedIssue} from './utils/issue';
import {getAddLabels, getRemoveLabels} from './utils/label';
import {isTargetEvent, getProjectName, getColumnName} from './utils/misc';
import {isTargetEvent, getProjectName, getColumnName, getConfigFilename} from './utils/misc';
import {addLabels, removeLabels} from './utils/issue';
import {CONFIG_FILENAME} from './constant';

async function run() {
try {
Expand All @@ -18,10 +17,10 @@ async function run() {
}

const octokit = new GitHub(getInput('GITHUB_TOKEN', {required: true}));
const config = await getConfig(CONFIG_FILENAME, octokit, context);
const config = await getConfig(getConfigFilename(), octokit, context);
if (!Object.keys(config).length) {
signale.warn('There is no valid config file.');
signale.warn(`Please create config file: ${CONFIG_FILENAME}`);
signale.warn(`Please create config file: ${getConfigFilename()}`);
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import yaml from 'js-yaml';
import {getInput} from '@actions/core' ;
import {GitHub} from '@actions/github/lib/github';
import {Context} from '@actions/github/lib/context';
import {TARGET_EVENT_NAME, TARGET_EVENT_ACTION} from '../constant';
import {TARGET_EVENT_NAME, TARGET_EVENT_ACTION, DEFAULT_CONFIG_FILENAME} from '../constant';

export const isTargetEvent = (context: Context) => TARGET_EVENT_NAME === context.eventName && TARGET_EVENT_ACTION === context.payload.action;

Expand All @@ -16,3 +17,5 @@ export const getColumnName = async (columnId: number, octokit: GitHub) => {
const {data: {name}} = await octokit.projects.getColumn({column_id: columnId});
return name;
};

export const getConfigFilename = (): string => getInput('CONFIG_FILENAME') || DEFAULT_CONFIG_FILENAME;

0 comments on commit 7a1f909

Please sign in to comment.