From f3fcab8ca5222d517e128dedcee10c0c85d32a75 Mon Sep 17 00:00:00 2001 From: Jonathan Wright Date: Sun, 28 May 2023 22:43:14 +0100 Subject: [PATCH] Add support for any label prefix matching Add support for a flag to change the label prefix match from the add case (all must match) to any (any one prefix can match any one label). --- README.md | 9 +++---- action.yaml | 9 +++++-- cmd/run.go | 11 +++++---- internal/action/check_labels_test.go | 36 ++++++++++++++++++++++++---- internal/action/main.go | 7 +++--- 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5bca20c..8397784 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ jobs: ## Inputs -| Name | Description | Required | Default | -| :--------------- | :---------------------------------------------------------------------- | :------: | :------ | -| `title-minimum` | The minimum number of characters that a title should contain | `false` | `25` | -| `label-prefixes` | A comma-separated list of label prefixes to check for on a pull request | `false` | `''` | +| Name | Description | Required | Type | Default | +| :------------------- | :---------------------------------------------------------------------- | :------: | :------- | :------ | +| `title-minimum` | The minimum number of characters that a title should contain | `false` | `int` | `25` | +| `label-prefixes` | A comma-separated list of label prefixes to check for on a pull request | `false` | `string` | `''` | +| `label-prefixes-any` | Set that any label prefix can match to pass, rather than all | `false` | `bool` | `false' | diff --git a/action.yaml b/action.yaml index 20cfc11..17ad761 100644 --- a/action.yaml +++ b/action.yaml @@ -13,6 +13,10 @@ inputs: description: A comma-separated list of label prefixes to check for on a pull request required: false default: '' + label-prefixes-any: + description: Set that any label prefix can match to pass, rather than all + required: false + default: 'false' runs: using: composite @@ -40,8 +44,9 @@ runs: GITHUB_TOKEN: ${{ github.token }} run: |- ${{ github.action_path }}/bin/pull-requester run \ - --title-minimum ${{ inputs.title-minimum }} \ - --label-prefixes ${{ inputs.label-prefixes }} + --title-minimum=${{ inputs.title-minimum }} \ + --label-prefixes=${{ inputs.label-prefixes }} + --label-prefixes-any=${{ inputs.label-prefixes-any }} branding: icon: user-check diff --git a/cmd/run.go b/cmd/run.go index 5044b19..f49d45b 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -16,8 +16,9 @@ var ( dryRun bool - titleMinimum int = 25 - labelPrefixes string + titleMinimum int = 25 + labelPrefixes string + labelPrefixesAny bool // runCmd represents the run command runCmd = &cobra.Command{ @@ -34,13 +35,15 @@ func init() { runCmd.Flags().IntVar(&number, "number", 0, "The number of the pull request to check") runCmd.Flags().IntVar(&titleMinimum, "title-minimum", titleMinimum, "The minimum number of characters a title should contain") runCmd.Flags().StringVar(&labelPrefixes, "label-prefixes", "", "A comma-separated list of label prefixes to check for on a pull request") + runCmd.Flags().BoolVar(&labelPrefixesAny, "label-prefixes-any", false, "Set that any label prefix can match to pass, rather than all") rootCmd.AddCommand(runCmd) } func RunChecks(cmd *cobra.Command, args []string) error { options := &action.Options{ - TitleMinimum: titleMinimum, - LabelPrefixes: labelPrefixes, + TitleMinimum: titleMinimum, + LabelPrefixes: labelPrefixes, + LabelPrefixesAny: labelPrefixesAny, } pr, err := github.NewPullRequest(logger, owner, repository, number) diff --git a/internal/action/check_labels_test.go b/internal/action/check_labels_test.go index e060f9f..9d0c2e9 100644 --- a/internal/action/check_labels_test.go +++ b/internal/action/check_labels_test.go @@ -44,19 +44,33 @@ var ( // Define the expected tests for TestCheckLabels() CheckLabelsTests = []*CheckLabelsTest{ { - Name: "all-types-match", + Name: "all-types-match-and", Labels: []*github.Label{labelTestOne, labelTestTwo, labelReleaseOne, labelReleaseTwo, labelPriorityOne, labelPriorityTwo}, RequiredPrefixes: []string{prefixTest, prefixRelease, prefixPriority}, AnyPrefix: false, Pass: true, }, { - Name: "simple-types-match", + Name: "all-types-match-all", + Labels: []*github.Label{labelTestOne, labelTestTwo, labelReleaseOne, labelReleaseTwo, labelPriorityOne, labelPriorityTwo}, + RequiredPrefixes: []string{prefixTest, prefixRelease, prefixPriority}, + AnyPrefix: true, + Pass: true, + }, + { + Name: "simple-types-match-and", Labels: []*github.Label{labelTestOne}, RequiredPrefixes: []string{prefixTest}, AnyPrefix: false, Pass: true, }, + { + Name: "simple-types-match-any", + Labels: []*github.Label{labelTestOne}, + RequiredPrefixes: []string{prefixTest}, + AnyPrefix: true, + Pass: true, + }, { Name: "empty-prefixes-case", Labels: []*github.Label{labelTestOne, labelTestTwo, labelReleaseOne, labelReleaseTwo, labelPriorityOne, labelPriorityTwo}, @@ -65,12 +79,19 @@ var ( Pass: true, }, { - Name: "empty-labels-case", + Name: "empty-labels-case-and", Labels: []*github.Label{}, RequiredPrefixes: []string{prefixTest, prefixRelease, prefixPriority}, AnyPrefix: false, Pass: false, }, + { + Name: "empty-labels-case-any", + Labels: []*github.Label{}, + RequiredPrefixes: []string{prefixTest, prefixRelease, prefixPriority}, + AnyPrefix: true, + Pass: false, + }, { Name: "missing-labels", Labels: []*github.Label{labelTestOne, labelTestTwo}, @@ -79,12 +100,19 @@ var ( Pass: false, }, { - Name: "partial-missing-labels", + Name: "partial-missing-labels-and", Labels: []*github.Label{labelTestOne, labelReleaseOne, labelReleaseTwo}, RequiredPrefixes: []string{prefixRelease, prefixPriority}, AnyPrefix: false, Pass: false, }, + { + Name: "partial-missing-labels-any", + Labels: []*github.Label{labelTestOne, labelReleaseOne, labelReleaseTwo}, + RequiredPrefixes: []string{prefixRelease, prefixPriority}, + AnyPrefix: true, + Pass: true, + }, { Name: "suffix-test-1", Labels: []*github.Label{labelTestOne}, diff --git a/internal/action/main.go b/internal/action/main.go index 1a27052..f52db5f 100644 --- a/internal/action/main.go +++ b/internal/action/main.go @@ -10,8 +10,9 @@ import ( ) type Options struct { - TitleMinimum int - LabelPrefixes string + TitleMinimum int + LabelPrefixes string + LabelPrefixesAny bool } func RunChecks(logger *logrus.Logger, pull *github.PullRequest, options *Options) error { @@ -33,7 +34,7 @@ func RunChecks(logger *logrus.Logger, pull *github.PullRequest, options *Options prefixes := strings.Split(options.LabelPrefixes, ",") - if err := CheckLabels(logger, pull, prefixes); err != nil { + if err := CheckLabels(logger, pull, prefixes, options.LabelPrefixesAny); err != nil { return fmt.Errorf("check on labels failed: %w", err) }