Skip to content

Commit

Permalink
Apply suggestions for the beta vesrion and update the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimZhukov committed Oct 31, 2023
1 parent efe4c1c commit 9181355
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 202 deletions.
169 changes: 81 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

Automatically label new pull requests based on the paths of files being changed or the branch name.

## Breaking changes in V5
1) The ability to apply labels based on the names of base and/or head branches was added ([#186](https://github.com/actions/labeler/issues/186) and [#54](https://github.com/actions/labeler/issues/54)). The match object for changed files was expanded with new combinations in order to make it more intuitive and flexible ([#423](https://github.com/actions/labeler/issues/423) and [#101](https://github.com/actions/labeler/issues/101)). As a result, the configuration file structure was significantly redesigned and is not compatible with the structure of the previous version. Please read the documentation below to find out how to adapt your configuration files for use with the new action version.

2) The bug related to the `sync-labels` input was fixed ([#112](https://github.com/actions/labeler/issues/112)). Now the input value is read correctly.

3) By default, `dot` input is set to `true`. Now, paths starting with a dot (e.g. `.github`) are matched by default.

4) Version 5 of this action updated the [runtime to Node.js 20](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-javascript-actions). All scripts are now run with Node.js 20 instead of Node.js 16 and are affected by any breaking changes between Node.js 16 and 20.

## Usage

### Create `.github/labeler.yml`
Expand All @@ -14,108 +23,106 @@ The key is the name of the label in your repository that you want to add (eg: "m

#### Match Object

The match object allows control over the matching options, you can specify the label to be applied based on the files that have changed or the name of either the base branch or the head branch. For the changed files options you provide a [path glob](https://github.com/isaacs/minimatch#minimatch), and for the branches you provide a regexp to match against the branch name.
The match object allows control over the matching options. You can specify the label to be applied based on the files that have changed or the name of either the base branch or the head branch. For the changed files options you provide a [path glob](https://github.com/isaacs/minimatch#minimatch), and for the branches you provide a regexp to match against the branch name.

The base match object is defined as:
```yml
- changed-files: ['list', 'of', 'globs']
- changed-files:
- any-glob-to-any-file: ['list', 'of', 'globs']
- any-glob-to-all-files: ['list', 'of', 'globs']
- all-globs-to-any-file: ['list', 'of', 'globs']
- all-globs-to-all-files: ['list', 'of', 'globs']
- base-branch: ['list', 'of', 'regexps']
- head-branch: ['list', 'of', 'regexps']
```
There are two top level keys of `any` and `all`, which both accept the same config options:
There are two top-level keys, `any` and `all`, which both accept the same configuration options:
```yml
- any:
- changed-files: ['list', 'of', 'globs']
- changed-files:
- any-glob-to-any-file: ['list', 'of', 'globs']
- any-glob-to-all-files: ['list', 'of', 'globs']
- all-globs-to-any-file: ['list', 'of', 'globs']
- all-globs-to-all-files: ['list', 'of', 'globs']
- base-branch: ['list', 'of', 'regexps']
- head-branch: ['list', 'of', 'regexps']
- all:
- changed-files: ['list', 'of', 'globs']
- changed-files:
- any-glob-to-any-file: ['list', 'of', 'globs']
- any-glob-to-all-files: ['list', 'of', 'globs']
- all-globs-to-any-file: ['list', 'of', 'globs']
- all-globs-to-all-files: ['list', 'of', 'globs']
- base-branch: ['list', 'of', 'regexps']
- head-branch: ['list', 'of', 'regexps']
```

From a boolean logic perspective, top-level match objects, and options within `all` are `AND`-ed together and individual match rules within the `any` object are `OR`-ed.

One or all fields can be provided for fine-grained matching.
The fields are defined as follows:
* `all`: all of the provided options must match in order for the label to be applied
* `any`: if any of the provided options match then a label will be applied
* `base-branch`: match regexps against the base branch name
* `changed-files`: match glob patterns against the changed paths
* `head-branch`: match regexps against the head branch name

If a base option is provided without a top-level key then it will default to `any`. More specifically, the following two configurations are equivalent:
- `all`: ALL of the provided options must match for the label to be applied
- `any`: if ANY of the provided options match then the label will be applied
- `base-branch`: match regexps against the base branch name
- `head-branch`: match regexps against the head branch name
- `changed-files`: match glob patterns against the changed paths
- `any-glob-to-any-file`: ANY glob must match against ANY changed file
- `any-glob-to-all-files`: ANY glob must match against ALL changed files
- `all-globs-to-any-file`: ALL globs must match against ANY changed file
- `all-globs-to-all-files`: ALL globs must match against ALL changed files

If a base option is provided without a top-level key, then it will default to `any`. More specifically, the following two configurations are equivalent:
```yml
label1:
- changed-files: example1/*
Documentation:
- changed-files:
- any-glob-to-any-file: 'docs/*'
```
and
```yml
label1:
Documentation:
- any:
- changed-files: ['example1/*']
- changed-files:
- any-glob-to-any-file: 'docs/*'
```

From a boolean logic perspective, top-level match objects, and options within `all` are `AND`-ed together and individual match rules within the `any` object are `OR`-ed. If path globs are combined with `!` negation, you can write complex matching rules.

> ⚠️ This action uses [minimatch](https://www.npmjs.com/package/minimatch) to apply glob patterns.
> For historical reasons, paths starting with dot (e.g. `.github`) are not matched by default.
> You need to set `dot: true` to change this behavior.
> See [Inputs](#inputs) table below for details.
If path globs are combined with `!` negation, you can write complex matching rules. See the examples below for more information.

#### Basic Examples

```yml
# Add 'label1' to any changes within 'example' folder or any subfolders
label1:
- changed-files: example/**/*
# Add 'label2' to any file changes within 'example2' folder
label2:
- changed-files: example2/*
# Add label3 to any change to .txt files within the entire repository. Quotation marks are required for the leading asterisk
label3:
- changed-files: '**/*.txt'
# Add 'label4' to any PR where the head branch name starts with 'example4'
label4:
- head-branch: '^example4'
# Add 'label5' to any PR where the base branch name starts with 'example5'
label5:
- base-branch: '^example5'
```
# Add 'root' label to any root file changes
# Quotation marks are required for the leading asterisk
root:
- changed-files:
- any-glob-to-any-file: '*'
#### Common Examples
# Add 'AnyChange' label to any changes within the entire repository
AnyChange:
- changed-files:
- any-glob-to-any-file: '**'
```yml
# Add 'repo' label to any root file changes
repo:
- changed-files: '*'
# Add 'Documentation' label to any changes within 'docs' folder or any subfolders
Documentation:
- changed-files:
- any-glob-to-any-file: docs/**
# Add '@domain/core' label to any change within the 'core' package
'@domain/core':
# Add 'Documentation' label to any file changes within 'docs' folder
Documentation:
- changed-files:
- package/core/*
- package/core/**/*
- any-glob-to-any-file: docs/*
# Add 'test' label to any change to *.spec.js files within the source dir
test:
- changed-files: src/**/*.spec.js
# Add 'Documentation' label to any change to .md files within the entire repository
Documentation:
- changed-files:
- any-glob-to-any-file: '**/*.md'
# Add 'source' label to any change to src files within the source dir EXCEPT for the docs sub-folder
source:
- changed-files:
- any: ['src/**/*', '!src/docs/*']
# Add 'frontend` label to any change to *.js files as long as the `main.js` hasn't changed
frontend:
- any:
- changed-files: ['src/**/*.js']
- all:
- changed-files: ['!src/main.js']
- changed-files:
- any-glob-to-any-file: 'src/**/*'
- all-globs-to-all-files: '!src/docs/*'
# Add 'feature' label to any PR where the head branch name starts with `feature` or has a `feature` section in the name
# Add 'feature' label to any PR where the head branch name starts with `feature` or has a `feature` section in the name
feature:
- head-branch: ['^feature', 'feature']

Expand All @@ -134,7 +141,7 @@ on:
- pull_request_target
jobs:
triage:
labeler:
permissions:
contents: read
pull-requests: write
Expand All @@ -152,7 +159,7 @@ Various inputs are defined in [`action.yml`](action.yml) to let you configure th
| `repo-token` | Token to use to authorize label changes. Typically the GITHUB_TOKEN secret | `github.token` |
| `configuration-path` | The path to the label configuration file. If the file doesn't exist at the specified path on the runner, action will read from the source repository via the Github API. | `.github/labeler.yml` |
| `sync-labels` | Whether or not to remove labels when matching files are reverted or no longer changed by the PR | `false` |
| `dot` | Whether or not to auto-include paths starting with dot (e.g. `.github`) | `false` |
| `dot` | Whether or not to auto-include paths starting with dot (e.g. `.github`) | `true` |
| `pr-number` | The number(s) of pull request to update, rather than detecting from the workflow context | N/A |

##### Using `configuration-path` input together with the `@actions/checkout` action
Expand All @@ -163,27 +170,13 @@ You might want to use action called [@actions/checkout](https://github.com/actio
- uses: actions/checkout@v4 # Uploads repository content to the runner
with:
repository: "owner/repositoryName" # The one of the available inputs, visit https://github.com/actions/checkout#readme to find more
- uses: actions/labeler@v4
```

##### Peculiarities of using the `dot` input

When `dot` is disabled, and you want to include _all_ files in a folder:

```yml
label1:
- path/to/folder/**/*
- path/to/folder/**/.*
```

If `dot` is enabled:
- uses: actions/labeler@v5
with:
configuration-path: 'path/to/the/uploaded/configuration/file'
```yml
label1:
- path/to/folder/**
```

##### Example workflow specifying Pull request numbers
##### Example workflow specifying pull request numbers

```yml
name: "Label Previous Pull Requests"
Expand All @@ -192,15 +185,15 @@ on:
- cron: "0 1 * * 1"
jobs:
triage:
labeler:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
# Label PRs 1, 2, and 3
- uses: actions/labeler@v4
- uses: actions/labeler@v5
with:
pr-number: |
1
Expand All @@ -221,19 +214,19 @@ Labeler provides the following outputs:

The following example performs steps based on the output of labeler:
```yml
name: "My workflow"
name: "Pull Request Labeler"
on:
- pull_request_target
jobs:
triage:
labeler:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- id: label-the-PR
uses: actions/labeler@v4
uses: actions/labeler@v5
- id: run-frontend-tests
if: contains(steps.label-the-PR.outputs.all-labels, 'frontend')
Expand Down
30 changes: 16 additions & 14 deletions __tests__/changedFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('checkAllChangedFiles', () => {

describe('when all given glob pattern configs matched', () => {
const globPatternsConfigs = [
{AnyGlobToAnyFile: ['foo.txt']},
{AnyGlobToAllFiles: ['*.txt']},
{AllGlobsToAllFiles: ['**']}
{anyGlobToAnyFile: ['foo.txt']},
{anyGlobToAllFiles: ['*.txt']},
{allGlobsToAllFiles: ['**']}
];

it('returns true', () => {
Expand All @@ -34,9 +34,9 @@ describe('checkAllChangedFiles', () => {

describe(`when some given glob pattern config did not match`, () => {
const globPatternsConfigs = [
{AnyGlobToAnyFile: ['*.md']},
{AnyGlobToAllFiles: ['*.txt']},
{AllGlobsToAllFiles: ['**']}
{anyGlobToAnyFile: ['*.md']},
{anyGlobToAllFiles: ['*.txt']},
{allGlobsToAllFiles: ['**']}
];

it('returns false', () => {
Expand All @@ -55,8 +55,8 @@ describe('checkAnyChangedFiles', () => {

describe('when any given glob pattern config matched', () => {
const globPatternsConfigs = [
{AnyGlobToAnyFile: ['*.md']},
{AnyGlobToAllFiles: ['*.txt']}
{anyGlobToAnyFile: ['*.md']},
{anyGlobToAllFiles: ['*.txt']}
];

it('returns true', () => {
Expand All @@ -71,8 +71,8 @@ describe('checkAnyChangedFiles', () => {

describe('when none of the given glob pattern configs matched', () => {
const globPatternsConfigs = [
{AnyGlobToAnyFile: ['*.md']},
{AnyGlobToAllFiles: ['!*.txt']}
{anyGlobToAnyFile: ['*.md']},
{anyGlobToAllFiles: ['!*.txt']}
];

it('returns false', () => {
Expand Down Expand Up @@ -123,23 +123,25 @@ describe('toChangedFilesMatchConfig', () => {

describe('and the glob pattern config key is provided', () => {
describe('and the value is an array of strings', () => {
const config = {'changed-files': [{AnyGlobToAnyFile: ['testing']}]};
const config = {
'changed-files': [{'any-glob-to-any-file': ['testing']}]
};

it('sets the value in the config object', () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
changedFiles: [{AnyGlobToAnyFile: ['testing']}]
changedFiles: [{anyGlobToAnyFile: ['testing']}]
});
});
});

describe('and the value is a string', () => {
const config = {'changed-files': [{AnyGlobToAnyFile: 'testing'}]};
const config = {'changed-files': [{'any-glob-to-any-file': 'testing'}]};

it(`sets the string as an array in the config object`, () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
changedFiles: [{AnyGlobToAnyFile: ['testing']}]
changedFiles: [{anyGlobToAnyFile: ['testing']}]
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/fixtures/all_options.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
label1:
- any:
- changed-files:
- AnyGlobToAnyFile: ['glob']
- any-glob-to-any-file: ['glob']
- head-branch: ['regexp']
- base-branch: ['regexp']
- all:
- changed-files:
- AllGlobsToAllFiles: ['glob']
- all-globs-to-all-files: ['glob']
- head-branch: ['regexp']
- base-branch: ['regexp']

label2:
- changed-files:
- AnyGlobToAnyFile: ['glob']
- any-glob-to-any-file: ['glob']
- head-branch: ['regexp']
- base-branch: ['regexp']
4 changes: 2 additions & 2 deletions __tests__/fixtures/any_and_all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests:
- any:
- head-branch: ['^tests/', '^test/']
- changed-files:
- AnyGlobToAnyFile: ['tests/**/*']
- any-glob-to-any-file: ['tests/**/*']
- all:
- changed-files:
- AllGlobsToAllFiles: ['!tests/requirements.txt']
- all-globs-to-all-files: ['!tests/requirements.txt']
2 changes: 1 addition & 1 deletion __tests__/fixtures/only_pdfs.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
touched-a-pdf-file:
- changed-files:
- AnyGlobToAnyFile: ['*.pdf']
- any-glob-to-any-file: ['*.pdf']
Loading

0 comments on commit 9181355

Please sign in to comment.