Skip to content

Commit

Permalink
feat: files (#18)
Browse files Browse the repository at this point in the history
* feat: commits to paginate

* fix tests

* feat: files

* updated dist
  • Loading branch information
fuxingloh authored Jan 14, 2021
1 parent b2e8aaa commit 60268d6
Show file tree
Hide file tree
Showing 11 changed files with 1,708 additions and 18 deletions.
11 changes: 7 additions & 4 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ labels:
branch: "^docs/.*"
commits: "^docs:.*"

### Examples
- label: "test:labeler"
matcher:
files: ".github/labeler.yml"

- label: "test:dist"
matcher:
files: "dist/**"
files: ["dist/*"]

- label: "test:security"
- label: "test:tests"
matcher:
files: [ ".github/**", "security/**" ]
files: [ "__tests__/matcher/**", "__tests__/*.ts" ]

- label: "test:body"
matcher:
Expand Down
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

Multi labeler for title, body, comments, commit messages, branch or files.

## Features

- Append based multi-labeler, using `.github/labeler.yml` as config.
- Automatically fail if `labeler.yml` is malformed, type-checked.
- Regex Matcher:
- PR/Issue title
- PR/Issue body
- PR/Issue comments
- PR commit messages
- PR branch name
- Glob Matcher:
- Files

## Usage

```yml
Expand Down Expand Up @@ -78,18 +91,23 @@ labels:
commits: "^feat: .*"
```
## Features
### PR Files: [Glob Matcher](https://github.com/isaacs/minimatch)
- Append based multi-labeler, using `.github/labeler.yml` as config.
- Automatically fail if `labeler.yml` is malformed, type-checked.
- Regex Matcher:
- PR/Issue title
- PR/Issue body
- PR/Issue comments
- PR commit messages
- PR branch name
- Glob Matcher:
- Files
Minimatch files, maximum of 3000 only.
If you use this to audit files changes, that note of the 3000 limit.
```yml
version: v1

labels:
- label: "github"
matcher:
files: ".github/**"

- label: "security"
matcher:
files: ["web/security/**", "security/**"]
```
## Why?
Expand Down
8 changes: 8 additions & 0 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('main core and context', () => {
return {}
}
}
},
listFiles: {
endpoint: {
// @ts-ignore
merge() {
return {}
}
}
}
},
// @ts-ignore
Expand Down
194 changes: 194 additions & 0 deletions __tests__/matcher/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import match from '../../src/matcher/files'
import * as github from '@actions/github'
import {Config} from '../../src/config'

async function getMatchedLabels(config: Config): Promise<string[]> {
return await match({
pulls: {
listFiles: {
endpoint: {
// @ts-ignore
merge(params) {
return {pull_number: params.pull_number}
}
}
}
},
// @ts-ignore
paginate(params): Promise<any[]> {
// @ts-ignore
return Promise.resolve(files[params.pull_number])
}
}, config)
}

const config: Config = {
version: 'v1',
labels: [
{
label: 'security',
matcher: {
files: ['security/**', 'setup/**.xml']
}
},
{
label: 'app',
matcher: {
files: 'app/**'
}
},
{
label: 'labeler',
matcher: {
files: '.github/labeler.yml'
}
}
]
}

const files = {
1: [
{
filename: '.github/labeler.yml',
},
{
filename: 'app/main.js',
},
{
filename: 'security/main.js',
},
{
filename: 'security/abc/abc.js',
},
{
filename: 'setup/abc/abc.xml',
},
{
filename: 'setup/abc/abc.js',
},
],
2: [
{
filename: '.github/labeler.yml',
},
],
3: [
{
filename: 'app/main.js',
},
{
filename: 'setup/abc/abc.js',
},
],
4: [
{
filename: 'security/main.js',
},
],
5: [
{
filename: 'security/abc/abc.js',
},
],
6: [
{
filename: 'setup/abc/abc.xml',
},
],
7: [
{
filename: 'setup/abc/abc.js',
},
]
}

describe('files', function () {
beforeEach(() => {
// Mock github context
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
return {
owner: 'owner-name',
repo: 'repo-name'
}
})

github.context.payload = {
pull_request: {
number: 1,
title: 'nothing interesting',
}
}
})

it('1 should have security/app/labeler', async function () {
github.context.payload = {
pull_request: {
number: 1
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual(['security', 'app', 'labeler'])
})

it('2 should have labeler', async function () {
github.context.payload = {
pull_request: {
number: 2
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual(['labeler'])
})

it('3 should have app', async function () {
github.context.payload = {
pull_request: {
number: 3
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual(['app'])
})

it('4 should have security', async function () {
github.context.payload = {
pull_request: {
number: 4
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual(['security'])
})

it('5 should have security', async function () {
github.context.payload = {
pull_request: {
number: 5
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual(['security'])
})

it('6 should be empty', async function () {
github.context.payload = {
pull_request: {
number: 6
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual([])
})

it('7 should be empty', async function () {
github.context.payload = {
pull_request: {
number: 7
}
}
const labels = await getMatchedLabels(config)
expect(labels).toEqual([])
})
});


Loading

0 comments on commit 60268d6

Please sign in to comment.