From 98583278f1243ee426660932e9afc17bb5fb8bba Mon Sep 17 00:00:00 2001 From: Galo Navarro Date: Wed, 18 Oct 2023 22:55:18 +0200 Subject: [PATCH] Support local config file (fix #82) 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 --- README.md | 42 +++++++++++++++++++++++++++++++++++++++--- action.yml | 3 +++ cmd/action.go | 48 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ba61c3f..85ee37a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/action.yml b/action.yml index 9278026..1b14a36 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/cmd/action.go b/cmd/action.go index cb00456..40c963d 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "os" + "strconv" "strings" "github.com/go-yaml/yaml" @@ -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)