Skip to content

Commit

Permalink
Merge pull request #9 from technote-space/release/v0.1.0
Browse files Browse the repository at this point in the history
Release/v0.1.0
  • Loading branch information
technote-space committed Sep 26, 2019
2 parents 091f00c + ab76c54 commit da77dda
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 73 deletions.
44 changes: 35 additions & 9 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,51 @@ GitHub Action を Context などでフィルタリングするためのヘルパ
1. インストール
`npm i @technote-space/filter-github-action`
1.
```js
```typescript
import { Context } from '@actions/github/lib/context';
import { context } from '@actions/github';
import { isTargetEvent } from '@technote-space/filter-github-action';

console.log( isTargetEvent( {
"release": [
"published",
"rerequested",
'release': [
// or
'published',
'rerequested',
],
"push": [
context => /^refs\/tags\//.test( context.ref ),
"rerequested",
'push': [
// use context
(context: Context): boolean => /^refs\/tags\//.test(context.ref),
'rerequested',
],
'pull_request': [
'*',
// or
[
// and
(context: Context): boolean => /^refs\/tags\//.test(context.ref),
'rerequested',
],
],
// wildcard
'project_card': '*',
} ) );
}, context ) );
```

* 上の例の結果

|eventName|action|ref|result|
|:---:|:---:|:---:|:---:|
|release|published|*|true|
|release|rerequested|*|true|
|release|created|*|false|
|push|*|refs/tags/v1.2.3|true|
|push|*|refs/heads/v1.2.3|false|
|push|rerequested|*|true|
|pull_request|rerequested|refs/tags/v1.2.3|true|
|pull_request|created|refs/tags/v1.2.3|false|
|pull_request|rerequested|refs/heads/v1.2.3|false|
|project_card|*|*|true|
|label|*|*|false|

## Author
[GitHub (Technote)](https://github.com/technote-space)
[Blog](https://technote.space)
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,51 @@ Helper to filter GitHub Action.
1. Install
`npm i @technote-space/filter-github-action`
1.
```js
```typescript
import { Context } from '@actions/github/lib/context';
import { context } from '@actions/github';
import { isTargetEvent } from '@technote-space/filter-github-action';

console.log( isTargetEvent( {
"release": [
"published",
"rerequested",
'release': [
// or
'published',
'rerequested',
],
"push": [
context => /^refs\/tags\//.test( context.ref ),
"rerequested",
'push': [
// use context
(context: Context): boolean => /^refs\/tags\//.test(context.ref),
'rerequested',
],
'pull_request': [
'*',
// or
[
// and
(context: Context): boolean => /^refs\/tags\//.test(context.ref),
'rerequested',
],
],
// wildcard
'project_card': '*',
} ) );
}, context ) );
```

* Results from the above example

|eventName|action|ref|result|
|:---:|:---:|:---:|:---:|
|release|published|*|true|
|release|rerequested|*|true|
|release|created|*|false|
|push|*|refs/tags/v1.2.3|true|
|push|*|refs/heads/v1.2.3|false|
|push|rerequested|*|true|
|pull_request|rerequested|refs/tags/v1.2.3|true|
|pull_request|created|refs/tags/v1.2.3|false|
|pull_request|rerequested|refs/heads/v1.2.3|false|
|project_card|*|*|true|
|label|*|*|false|

## Author
[GitHub (Technote)](https://github.com/technote-space)
[Blog](https://technote.space)
84 changes: 32 additions & 52 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,64 @@ export const targets = {
'rerequested',
],
'pull_request': [
'*',
[
(context: Context): boolean => /^refs\/tags\//.test(context.ref),
'rerequested',
],
],
'project_card': '*',
};

const context = (event: string, action: string | null, ref: string | null): Context => getContext({
eventName: event,
payload: {
action: action ? action : undefined,
},
ref: ref ? `refs/${ref}` : 'a',
});

describe('isTargetEvent', () => {
it('should return true 1', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'push',
ref: 'refs/tags/test',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('release', 'published', null))).toBeTruthy();
});

it('should return true 2', () => {
expect(isTargetEvent(targets, getContext({
payload: {
action: 'rerequested',
},
eventName: 'push',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('release', 'rerequested', null))).toBeTruthy();
});

it('should return true 3', () => {
expect(isTargetEvent(targets, getContext({
payload: {
action: 'published',
},
eventName: 'release',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('push', null, 'tags/v1.2.3'))).toBeTruthy();
});

it('should return true 4', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'pull_request',
ref: 'refs/tags/test',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('push', 'rerequested', null))).toBeTruthy();
});

it('should return true 5', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'pull_request',
payload: {
action: 'open',
},
ref: 'refs/tags/test',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('pull_request', 'rerequested', 'tags/v1.2.3'))).toBeTruthy();
});

it('should return true 6', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'project_card',
ref: 'refs/tags/test',
}))).toBeTruthy();
});

it('should return true 7', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'project_card',
payload: {
action: 'open',
},
ref: 'refs/tags/test',
}))).toBeTruthy();
expect(isTargetEvent(targets, context('project_card', null, null))).toBeTruthy();
});

it('should return false 1', () => {
expect(isTargetEvent(targets, getContext({
eventName: 'push',
ref: 'refs/heads/test',
}))).toBeFalsy();
expect(isTargetEvent(targets, context('release', 'created', null))).toBeFalsy();
});

it('should return false 2', () => {
expect(isTargetEvent(targets, getContext({
payload: {
action: 'created',
},
eventName: 'release',
}))).toBeFalsy();
expect(isTargetEvent(targets, context('push', null, 'heads/v1.2.3'))).toBeFalsy();
});

it('should return false 3', () => {
expect(isTargetEvent(targets, context('pull_request', 'created', 'tags/v1.2.3'))).toBeFalsy();
});

it('should return false 4', () => {
expect(isTargetEvent(targets, context('pull_request', 'rerequested', 'heads/v1.2.3'))).toBeFalsy();
});

it('should return false 5', () => {
expect(isTargetEvent(targets, context('label', null, null))).toBeFalsy();
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/filter-github-action",
"version": "0.0.5",
"version": "0.1.0",
"description": "Helper to filter GitHub Action.",
"author": "Technote <technote.space@gmail.com> (https://technote.space)",
"license": "MIT",
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { Context } from '@actions/github/lib/context';
const isTargetEventName = (events: object, context: Context): boolean => context.eventName in events;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isTargetEventAction = (action: string | any[] | Function, context: Context): boolean => {
const isTargetEventAction = (action: string | any[] | Function, context: Context, some = true): boolean => {
if (Array.isArray(action)) {
return action.some(item => isTargetEventAction(item, context));
if (some) {
return action.some(item => isTargetEventAction(item, context, false));
}
return !action.some(item => !isTargetEventAction(item, context, false));
}
if (typeof action === 'function') {
return action(context);
Expand Down

0 comments on commit da77dda

Please sign in to comment.