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 Sep 14, 2019
1 parent df04112 commit f9754ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ label2: example2/*
label3:
- example3/*
- example3/**/*.yml
label4:
- !example4/foo/*
- example4/**/*
```
Where `"label1"` is the name of the label on your repository that you want to add (eg: "merge conflict", "needs-updating") and `"example1/**/*"` is the path of the changed files.

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.

Then create a workflow (eg: `.github/workflows/label.yml` see [Creating a Workflow file](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file)) utilizing the labeler action, granting access to the GITHUB_TOKEN so the action can make calls to GitHub's rest API:
```
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 f9754ad

Please sign in to comment.