Skip to content

Commit 80b0102

Browse files
authored
Merge pull request #4 from paopa/feat/scopes
feat: support to specify scopes
2 parents e1bc20f + bcf2937 commit 80b0102

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `pull-request-title-validator` GitHub Action helps ensuring that contributors provide informative and well-formatted titles - based on the [conventional-commits] specification. The titles of the pull request could then be used to create automated releases.
44

5-
[conventional-commits]: https://www.conventionalcommits.org/en/v1.0.0/
5+
[conventional-commits]: https://www.conventionalcommits.org/en/v1.0.0/
66

77
## Example title
88

@@ -18,6 +18,7 @@ feat(client): add component
1818
The action can be used with both the `pull_request` and `pull_request_target` trigger.
1919

2020
`default`
21+
2122
```yaml
2223
name: validate-pull-request-title
2324

@@ -41,6 +42,7 @@ jobs:
4142
```
4243
4344
`custom types`
45+
4446
```yaml
4547
name: validate-pull-request-title
4648
@@ -64,3 +66,29 @@ jobs:
6466
with:
6567
types: "fix,feat,chore"
6668
```
69+
70+
`custom scopes`
71+
72+
```yaml
73+
name: validate-pull-request-title
74+
75+
on:
76+
pull_request:
77+
types:
78+
- opened
79+
- edited
80+
- synchronize
81+
82+
permissions:
83+
pull-requests: read
84+
85+
jobs:
86+
validator:
87+
name: validate-pull-request-title
88+
runs-on: ubuntu-latest
89+
steps:
90+
- name: validate pull request title
91+
uses: kontrolplane/pull-request-title-validator@v1.2.0
92+
with:
93+
scopes: "api,lang,parser"
94+
```

main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func main() {
2525
githubEventName := os.Getenv("GITHUB_EVENT_NAME")
2626
githubEventPath := os.Getenv("GITHUB_EVENT_PATH")
2727
conventionTypes := parseTypes(os.Getenv("INPUT_TYPES"), defaultConventionTypes)
28+
scopes := parseScopes(os.Getenv("INPUT_SCOPES"))
2829

2930
if githubEventName != "pull_request" && githubEventName != "pull_request_target" {
3031
fmt.Printf("Error: the 'pull_request' trigger type should be used, received '%s'\n", githubEventName)
@@ -39,6 +40,11 @@ func main() {
3940
os.Exit(1)
4041
}
4142

43+
if err := checkAgainstScopes(titleScope, scopes); err != nil && len(scopes) > 1 {
44+
fmt.Println(err)
45+
os.Exit(1)
46+
}
47+
4248
fmt.Printf("commit title type used: %s\n", titleType)
4349
fmt.Printf("commit title scope used: %s\n", titleScope)
4450
fmt.Printf("commit title message used: %s\n\n", titleMessage)
@@ -106,6 +112,16 @@ func checkAgainstConventionTypes(titleType string, conventionTypes []string) err
106112
return fmt.Errorf("the type passed '%s' is not present in the types allowed by the convention: %s", titleType, conventionTypes)
107113
}
108114

115+
func checkAgainstScopes(titleScope string, scopes []string) error {
116+
for _, scope := range scopes {
117+
if titleScope == scope {
118+
return nil
119+
}
120+
}
121+
122+
return fmt.Errorf("the scope '%s' is not allowed. Please choose from the following scopes: %s", titleScope, scopes)
123+
}
124+
109125
func parseTypes(input string, fallback []string) []string {
110126
if input == "" {
111127
fmt.Println("no custom list of commit types was passed using fallback.")
@@ -120,3 +136,18 @@ func parseTypes(input string, fallback []string) []string {
120136
}
121137
return types
122138
}
139+
140+
func parseScopes(input string) []string {
141+
if input == "" {
142+
fmt.Println("no custom list of commit scopes was passed using fallback.")
143+
return []string{}
144+
}
145+
scopes := strings.Split(input, ",")
146+
for i := range scopes {
147+
scopes[i] = strings.TrimSpace(scopes[i])
148+
}
149+
if len(scopes) == 0 {
150+
return []string{}
151+
}
152+
return scopes
153+
}

0 commit comments

Comments
 (0)