Skip to content

Commit e1bc20f

Browse files
authored
Merge pull request #3 from kontrolplane/patch
feat: add option to pass list of allowed commit types
2 parents d5485af + 89d07df commit e1bc20f

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ feat(client): add component
1717

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

20+
`default`
2021
```yaml
2122
name: validate-pull-request-title
2223

@@ -36,5 +37,30 @@ jobs:
3637
runs-on: ubuntu-latest
3738
steps:
3839
- name: validate pull request title
39-
uses: kontrolplane/pull-request-title-validator@v1.1.0
40+
uses: kontrolplane/pull-request-title-validator@v1.2.0
41+
```
42+
43+
`custom types`
44+
```yaml
45+
name: validate-pull-request-title
46+
47+
on:
48+
pull_request:
49+
types:
50+
- opened
51+
- edited
52+
- synchronize
53+
54+
permissions:
55+
pull-requests: read
56+
57+
jobs:
58+
validator:
59+
name: validate-pull-request-title
60+
runs-on: ubuntu-latest
61+
steps:
62+
- name: validate pull request title
63+
uses: kontrolplane/pull-request-title-validator@v1.2.0
64+
with:
65+
types: "fix,feat,chore"
4066
```

main.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

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

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

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

105106
return fmt.Errorf("the type passed '%s' is not present in the types allowed by the convention: %s", titleType, conventionTypes)
106107
}
108+
109+
func parseTypes(input string, fallback []string) []string {
110+
if input == "" {
111+
fmt.Println("no custom list of commit types was passed using fallback.")
112+
return fallback
113+
}
114+
types := strings.Split(input, ",")
115+
for i := range types {
116+
types[i] = strings.TrimSpace(types[i])
117+
}
118+
if len(types) == 0 {
119+
return fallback
120+
}
121+
return types
122+
}

0 commit comments

Comments
 (0)