Skip to content

Commit

Permalink
Support local config file (fix #82)
Browse files Browse the repository at this point in the history
Supporting this use case:

    It would be great if it supported labeler.yml files that are auto
    generated on build time, and only fallback to fetching it through HTTP
    if it is not present locally.

Signed-off-by: Galo Navarro <[email protected]>
  • Loading branch information
srvaroa committed Oct 18, 2023
1 parent 864245c commit 9858327
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ it useful, you can do this through [GitHub Sponsors](https://github.com/sponsors

## Installing

The action is configured by adding a file `.github/labeler.yml`. The
file contains matching rules expanded in the `Configuration` section
below.
The action is configured by adding a file `.github/labeler.yml` (which
you can override). The file contains matching rules expanded in the
`Configuration` section below.

The action will strive to maintain backwards compatibility with older
configuration versions. It is nevertheless encouraged to update your
Expand Down Expand Up @@ -100,6 +100,42 @@ labels:
title: "^WIP:.*"
```

### Advanced action settings

Please refer to the (action.yaml)[action.yaml] file in the repository
for the available inputs to the action. Below is an example using all of
them:

```yaml
name: Label PRs
on:
- pull_request
- issues
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: srvaroa/labeler@master
with:
config_path: .github/labeler.yml
use_local_config: false
cache: true
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
```

Use `config_path` to provide an alternative path for the configuration
file for the action. The default is `.github/labeler.yaml`.

Use `use_local_config` to chose where to read the config file from. By
default, the action will read the file from the default branch of your
repository. If you set `use_local_config` to `true`, then the action
will read the config file from the local checkout.

## Troubleshooting

This action will avoid failing in all cases, so if you're experiencing
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inputs:
config_path:
description: 'Path for labeling rules'
default: '.github/labeler.yml'
use_local_config:
description: 'By default the action will use the configuration file set in the default branch of the repository. When set to true, the action will instead use the configuration found in the local checkout of the repository.'
default: 'false'
runs:
using: 'docker'
image: 'Dockerfile'
Expand Down
48 changes: 36 additions & 12 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"

"github.com/go-yaml/yaml"
Expand All @@ -23,19 +24,42 @@ func main() {
eventPayload := getEventPayload()
eventName := os.Getenv("GITHUB_EVENT_NAME")

// TODO: rethink this. Currently we'll take the config from the
// PR's branch, not from master. My intuition is that one wants
// to see the rules that are set in the main branch (as those are
// vetted by the repo's owners). It seems fairly common in GH
// actions to use this approach, and I will need to consider
// whatever branch is set as main in the repo settings, so leaving
// as this for now.
configRaw, err := getRepoFile(gh,
os.Getenv("GITHUB_REPOSITORY"),
os.Getenv("INPUT_CONFIG_PATH"),
os.Getenv("GITHUB_SHA"))
// Determine if the user wants to override the upstream config
// in the main branch with the local one in the checkout
useLocalConfig, err := strconv.ParseBool(os.Getenv("INPUT_USE_LOCAL_CONFIG"))
if err != nil {
return
useLocalConfig = false
}

configFile := os.Getenv("INPUT_CONFIG_PATH")

var configRaw *[]byte
if useLocalConfig {
log.Printf("Reading configuration from local file: %s", configFile)
contents, err := ioutil.ReadFile(configFile)
if err != nil {
log.Printf("Error reading configuration from local file: %s", err)
return
}
configRaw = &contents
} else {
// TODO: rethink this. Currently we'll take the config from the
// PR's branch, not from master. My intuition is that one wants
// to see the rules that are set in the main branch (as those are
// vetted by the repo's owners). It seems fairly common in GH
// actions to use this approach, and I will need to consider
// whatever branch is set as main in the repo settings, so leaving
// as this for now.
configRaw, err = getRepoFile(gh,
os.Getenv("GITHUB_REPOSITORY"),
configFile,
os.Getenv("GITHUB_SHA"))

if err != nil {
log.Printf("Error reading configuration from default branch: %s", err)
return
}

}

config, err := getLabelerConfigV1(configRaw)
Expand Down

0 comments on commit 9858327

Please sign in to comment.