Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance configuration (#12, #15) #20

Merged
merged 11 commits into from Oct 27, 2019
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -22,11 +22,13 @@ jobs:
- uses: TimonVS/pr-labeler-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CONFIG_FILENAME: ".github/pr-labeler.yml" # optional, .github/pr-labeler.yml is the default value
TimonVS marked this conversation as resolved.
Show resolved Hide resolved
```

## Configuration

Configure by creating a `.github/pr-labeler.yml` file.
Configure by creating a `.github/pr-labeler.yml` file or choose another filename and path with setting `env.CONFIG_FILENAME` to a different value. When you dont
create a filename at the default path (or your custom path) the [default configuration](#default-configuration) will be used.

For example:

Expand All @@ -43,6 +45,16 @@ Then if a pull request is opened with the branch name `feature/218-add-emoji-sup

You can use `*` as a wildcard for matching multiple branch names. See https://www.npmjs.com/package/matcher for more information about wildcard options.

### Default configuration

When no custom configuration is set the following default configuration will be used:
TimonVS marked this conversation as resolved.
Show resolved Hide resolved

```yml
feature: ['feature/*', 'feat/*'],
fix: 'fix/*',
chore: 'chore/*'
```

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand Down
22 changes: 17 additions & 5 deletions src/action.js
Expand Up @@ -3,7 +3,6 @@ const github = require('@actions/github')
const matcher = require('matcher')
const getConfig = require('./utils/config')

const CONFIG_FILENAME = 'pr-labeler.yml'
const defaults = {
feature: ['feature/*', 'feat/*'],
fix: 'fix/*',
Expand All @@ -12,6 +11,13 @@ const defaults = {

async function action(context = github.context) {
try {

var customConfigFile = '.github/pr-labeler.yml'; // default path of config file
TimonVS marked this conversation as resolved.
Show resolved Hide resolved
// if env variable CONFIG_FILENAME isset use it as the path to a custom pr-labeler config yml
if(process.env.CONFIG_FILENAME !== null) {
customConfigFile = process.env.CONFIG_FILENAME;
}

const GITHUB_TOKEN = process.env.GITHUB_TOKEN
const octokit = new github.GitHub(GITHUB_TOKEN)
const repoInfo = {
Expand All @@ -25,10 +31,16 @@ async function action(context = github.context) {
)
}

const ref = context.payload.pull_request.head.ref
const config = {
...defaults,
...(await getConfig(octokit, CONFIG_FILENAME, repoInfo, ref))
const ref = context.payload.pull_request.head.ref;

/**
* load custom config when existing or
* set default config when no custom overwrite exists
*/
var config = defaults;
var customConfig = await getConfig(octokit, customConfigFile, repoInfo, ref);
if(customConfig !== null) {
config = customConfig;
TimonVS marked this conversation as resolved.
Show resolved Hide resolved
}

const labelsToAdd = Object.entries(config).reduce(
Expand Down
4 changes: 1 addition & 3 deletions src/utils/config.js
@@ -1,7 +1,5 @@
const path = require('path')
const yaml = require('js-yaml')

const CONFIG_PATH = '.github'

/**
* @returns {Promise<Object.<string, string | string[]>>}
Expand All @@ -11,7 +9,7 @@ module.exports = async function getConfig(github, fileName, { owner, repo }, ref
const response = await github.repos.getContents({
owner,
repo,
path: path.posix.join(CONFIG_PATH, fileName),
path: fileName,
ref
})

Expand Down