Skip to content

Commit

Permalink
Add support for any label prefix matching
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jonathanio committed May 28, 2023
1 parent 4d5d10c commit f3fcab8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' |
9 changes: 7 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
36 changes: 32 additions & 4 deletions internal/action/check_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand Down
7 changes: 4 additions & 3 deletions internal/action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down

0 comments on commit f3fcab8

Please sign in to comment.