Skip to content

Commit 2ee51b3

Browse files
authored
Merge pull request #10 from kontrolplane/task/prevent-parantheses-outside-scope-error
fix: prevent parantheses outside scope error
2 parents cc6b962 + c963652 commit 2ee51b3

3 files changed

Lines changed: 53 additions & 21 deletions

File tree

.github/workflows/update-contributors.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ jobs:
1919
owner: kontrolplane
2020
repository: pull-request-title-validator
2121

22+
- name: check-for-changes
23+
id: check
24+
run: |
25+
if git diff --quiet README.md; then
26+
echo "No changes to commit"
27+
echo "changes=false" >> $GITHUB_OUTPUT
28+
exit 0
29+
fi
30+
echo "changes=true" >> $GITHUB_OUTPUT
31+
2232
- name: open-pull-request
33+
if: steps.check.outputs.changes == 'true'
2334
env:
2435
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2536
run: |

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
runs-on: ubuntu-latest
3939
steps:
4040
- name: validate pull request title
41-
uses: kontrolplane/pull-request-title-validator@v1.3.2
41+
uses: kontrolplane/pull-request-title-validator@v1.4.1
4242
```
4343
4444
### Custom types
@@ -62,7 +62,7 @@ jobs:
6262
runs-on: ubuntu-latest
6363
steps:
6464
- name: validate pull request title
65-
uses: kontrolplane/pull-request-title-validator@v1.3.2
65+
uses: kontrolplane/pull-request-title-validator@v1.4.1
6666
with:
6767
types: "fix,feat,chore"
6868
```
@@ -90,7 +90,7 @@ jobs:
9090
runs-on: ubuntu-latest
9191
steps:
9292
- name: validate pull request title
93-
uses: kontrolplane/pull-request-title-validator@v1.3.2
93+
uses: kontrolplane/pull-request-title-validator@v1.4.1
9494
with:
9595
scopes: "api,lang,parser,package/.+"
9696
```

main.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func fetchTitle(logger *slog.Logger, githubEventPath string) string {
8888

8989
if eventData, err = os.ReadFile(githubEventPath); err != nil {
9090
logger.Error("Problem reading the event JSON file", slog.String("path", githubEventPath), slog.Any("error", err))
91-
os.Exit(1) // You might want to return an empty string or handle this error upstream instead.
91+
os.Exit(1)
9292
}
9393

9494
if err = json.Unmarshal(eventData, &event); err != nil {
@@ -100,30 +100,51 @@ func fetchTitle(logger *slog.Logger, githubEventPath string) string {
100100
}
101101

102102
func splitTitle(logger *slog.Logger, title string) (titleType string, titleScope string, titleMessage string) {
103-
if index := strings.Index(title, "("); strings.Contains(title, "(") {
104-
titleType = title[:index]
105-
} else if index := strings.Index(title, ":"); strings.Contains(title, ":") {
106-
titleType = title[:index]
107-
} else {
108-
logger.Error("No type was included in the pull request title.", slog.String("desired format", desiredFormat))
103+
// Split title into prefix (type/scope) and message parts using colon as separator
104+
prefix, message, found := strings.Cut(title, ":")
105+
if !found {
106+
logger.Error("Title must include a message after the colon",
107+
slog.String("desired format", desiredFormat),
108+
slog.String("title", title))
109109
os.Exit(1)
110110
}
111111

112-
if strings.Contains(title, "(") && strings.Contains(title, ")") {
113-
scope := regexp.MustCompile(`\(([^)]+)\)`)
114-
if matches := scope.FindStringSubmatch(title); len(matches) > 1 {
115-
titleScope = matches[1]
116-
}
117-
}
112+
// Clean up the message part
113+
titleMessage = strings.TrimSpace(message)
118114

119-
if strings.Contains(title, ":") {
120-
titleMessage = strings.SplitAfter(title, ":")[1]
121-
titleMessage = strings.TrimSpace(titleMessage)
122-
} else {
123-
logger.Error("No message was included in the pull request title.", slog.String("desired format", desiredFormat))
115+
// Extract type and scope from the prefix
116+
titleType, titleScope = extractTypeAndScope(prefix)
117+
118+
// Validate that we found a type
119+
if titleType == "" {
120+
logger.Error("Title must include a type",
121+
slog.String("desired format", desiredFormat),
122+
slog.String("title", title))
124123
os.Exit(1)
125124
}
126125

126+
return titleType, titleScope, titleMessage
127+
}
128+
129+
func extractTypeAndScope(prefix string) (titleType string, titleScope string) {
130+
prefix = strings.TrimSpace(prefix)
131+
132+
// Check if prefix contains a scope in parentheses
133+
if strings.Contains(prefix, "(") && strings.Contains(prefix, ")") {
134+
135+
// Extract scope using regex
136+
scopeRegex := regexp.MustCompile(`\(([^)]+)\)`)
137+
138+
//
139+
if matches := scopeRegex.FindStringSubmatch(prefix); len(matches) > 1 {
140+
titleScope = matches[1]
141+
titleType = strings.TrimSpace(strings.Split(prefix, "(")[0])
142+
return
143+
}
144+
}
145+
146+
// If no scope found or invalid format, use entire prefix as type
147+
titleType = prefix
127148
return
128149
}
129150

0 commit comments

Comments
 (0)