Skip to content

Commit f9754ad

Browse files
committed
Add support for exclusions
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
1 parent df04112 commit f9754ad

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ label2: example2/*
1515
label3:
1616
- example3/*
1717
- example3/**/*.yml
18+
19+
label4:
20+
- !example4/foo/*
21+
- example4/**/*
1822
```
1923
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.
2024

25+
Exclusions are supported by preceding the pattern with `"!"`.
26+
27+
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.
2128

2229
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:
2330
```

src/main.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core';
22
import * as github from '@actions/github';
33
import * as yaml from 'js-yaml';
4-
import {Minimatch} from 'minimatch';
4+
import {Minimatch, IMinimatch} from 'minimatch';
55

66
async function run() {
77
try {
@@ -116,15 +116,28 @@ function getLabelGlobMapFromObject(configObject: any): Map<string, string[]> {
116116
return labelGlobs;
117117
}
118118

119+
function printPattern(matcher: IMinimatch): string {
120+
return (matcher.negate ? "!" : "") + matcher.pattern;
121+
}
122+
119123
function checkGlobs(changedFiles: string[], globs: string[]): boolean {
120-
for (const glob of globs) {
121-
core.debug(` checking pattern ${glob}`);
122-
const matcher = new Minimatch(glob);
123-
for (const changedFile of changedFiles) {
124-
core.debug(` - ${changedFile}`);
124+
const matchers = globs.map(g => new Minimatch(g));
125+
for (const changedFile of changedFiles) {
126+
core.debug(` testing patterns against ${changedFile}`);
127+
for (const matcher of matchers) {
128+
core.debug(` - ${printPattern(matcher)}`);
125129
if (matcher.match(changedFile)) {
126-
core.debug(` ${changedFile} matches`);
127-
return true;
130+
// match and not an exclusion rule
131+
if (!matcher.negate) {
132+
core.debug(` ${printPattern(matcher)} matches`);
133+
return true;
134+
}
135+
} else {
136+
// non-match, but is an exclusion rule
137+
if (matcher.negate) {
138+
core.debug(` ${printPattern(matcher)} excluded`);
139+
break;
140+
}
128141
}
129142
}
130143
}

0 commit comments

Comments
 (0)