Skip to content

Commit

Permalink
Add support for exclusions
Browse files Browse the repository at this point in the history
Paths can be negated to stop searching through the remaining patterns in
a the glob list. All changed files tested and at least one matching file
will result in a label being added.

Fixes #9
  • Loading branch information
jalaziz committed Apr 28, 2020
1 parent d2c408e commit 6d26998
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ repo:
# Add 'test' label to any change to *.spec.js files within the source dir
test:
- src/**/*.spec.js

# Add 'source' label to any change to src files within the source dir EXCEPT for the build sub-folder
source:
- !src/build/*
- src/**/*
```
#### Exclusions
Exclusions are supported by preceding the pattern with `"!"`.
Patterns are evaluated in order and the first match will result in the label being assigned. If an exclusion rule matches, all remaining patterns are skipped for the path being evaluated.
### Create Workflow
Create a workflow (eg: `.github/workflows/labeler.yml` see [Creating a Workflow file](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file)) to utilize the labeler action with content:
Expand Down
29 changes: 21 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as yaml from 'js-yaml';
import {Minimatch} from 'minimatch';
import {Minimatch, IMinimatch} from 'minimatch';

async function run() {
try {
Expand Down Expand Up @@ -116,15 +116,28 @@ function getLabelGlobMapFromObject(configObject: any): Map<string, string[]> {
return labelGlobs;
}

function printPattern(matcher: IMinimatch): string {
return (matcher.negate ? "!" : "") + matcher.pattern;
}

function checkGlobs(changedFiles: string[], globs: string[]): boolean {
for (const glob of globs) {
core.debug(` checking pattern ${glob}`);
const matcher = new Minimatch(glob);
for (const changedFile of changedFiles) {
core.debug(` - ${changedFile}`);
const matchers = globs.map(g => new Minimatch(g));
for (const changedFile of changedFiles) {
core.debug(` testing patterns against ${changedFile}`);
for (const matcher of matchers) {
core.debug(` - ${printPattern(matcher)}`);
if (matcher.match(changedFile)) {
core.debug(` ${changedFile} matches`);
return true;
// match and not an exclusion rule
if (!matcher.negate) {
core.debug(` ${printPattern(matcher)} matches`);
return true;
}
} else {
// non-match, but is an exclusion rule
if (matcher.negate) {
core.debug(` ${printPattern(matcher)} excluded`);
break;
}
}
}
}
Expand Down

0 comments on commit 6d26998

Please sign in to comment.