Skip to content

Commit

Permalink
Merge pull request #3 from kontrolplane/patch
Browse files Browse the repository at this point in the history
feat: add option to pass list of allowed commit types
  • Loading branch information
levivannoort authored Jul 9, 2024
2 parents d5485af + 89d07df commit e1bc20f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ feat(client): add component

The action can be used with both the `pull_request` and `pull_request_target` trigger.

`default`
```yaml
name: validate-pull-request-title

Expand All @@ -36,5 +37,30 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: validate pull request title
uses: kontrolplane/[email protected]
uses: kontrolplane/[email protected]
```
`custom types`
```yaml
name: validate-pull-request-title
on:
pull_request:
types:
- opened
- edited
- synchronize
permissions:
pull-requests: read
jobs:
validator:
name: validate-pull-request-title
runs-on: ubuntu-latest
steps:
- name: validate pull request title
uses: kontrolplane/[email protected]
with:
types: "fix,feat,chore"
```
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var desiredFormat string = "<type>(optional: <scope>): <message>"
var defaultConventionTypes []string = []string{"fix", "feat", "chore", "docs", "build", "ci", "refactor", "perf", "test"}

type PullRequest struct {
Title string `json:"title"`
Expand All @@ -23,7 +24,7 @@ type Event struct {
func main() {
githubEventName := os.Getenv("GITHUB_EVENT_NAME")
githubEventPath := os.Getenv("GITHUB_EVENT_PATH")
conventionTypes := []string{"fix", "feat", "chore", "docs", "build", "ci", "refactor", "perf", "test"}
conventionTypes := parseTypes(os.Getenv("INPUT_TYPES"), defaultConventionTypes)

if githubEventName != "pull_request" && githubEventName != "pull_request_target" {
fmt.Printf("Error: the 'pull_request' trigger type should be used, received '%s'\n", githubEventName)
Expand Down Expand Up @@ -87,7 +88,7 @@ func splitTitle(title string) (titleType string, titleScope string, titleMessage
titleMessage = strings.SplitAfter(title, ":")[1]
titleMessage = strings.TrimSpace(titleMessage)
} else {
fmt.Println("No message was included in the pull request title.")
fmt.Println("no message was included in the pull request title.")
fmt.Println(desiredFormat)
os.Exit(1)
}
Expand All @@ -104,3 +105,18 @@ func checkAgainstConventionTypes(titleType string, conventionTypes []string) err

return fmt.Errorf("the type passed '%s' is not present in the types allowed by the convention: %s", titleType, conventionTypes)
}

func parseTypes(input string, fallback []string) []string {
if input == "" {
fmt.Println("no custom list of commit types was passed using fallback.")
return fallback
}
types := strings.Split(input, ",")
for i := range types {
types[i] = strings.TrimSpace(types[i])
}
if len(types) == 0 {
return fallback
}
return types
}

0 comments on commit e1bc20f

Please sign in to comment.