Skip to content

Commit f7079fe

Browse files
committed
BUG/MINOR: spell out expected tag formats in subject errors
When a commit subject fails the tag check, the error only named the internal policy sets (e.g. "HAProxy Standard Patch") without saying which tags and severity levels are valid, nor that a bare severity tag like MINOR is also accepted. Contributors had no actionable guidance short of reading the CONTRIBUTING link. Now the errors list the valid tags and severity levels from the policy config and show the expected subject prefix forms, including the bare-scope one.
1 parent c90e0f1 commit f7079fe

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

‎check.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,25 @@ func (c CommitPolicyConfig) CheckPatchTypes(tag, severity string, patchTypeName
161161
return tagScopeOK
162162
}
163163

164-
var ErrTagScope = errors.New("invalid tag and or severity")
164+
// allowedTagSummary describes what a tag alternative accepts, so error
165+
// messages can show expected tags and severity levels instead of internal
166+
// set names.
167+
func (c CommitPolicyConfig) allowedTagSummary(patchTypes []string) string {
168+
parts := []string{}
169+
for _, pType := range patchTypes {
170+
pt := c.PatchTypes[pType]
171+
if pt.Scope == "" {
172+
parts = append(parts, fmt.Sprintf("bare '<TAG>' in [%s] for %s", strings.Join(pt.Values, ", "), pType))
173+
continue
174+
}
175+
parts = append(parts, fmt.Sprintf("'<TAG>/<SEVERITY>' with TAG in [%s] and SEVERITY in [%s] for %s",
176+
strings.Join(pt.Values, ", "), strings.Join(c.PatchScopes[pt.Scope], ", "), pType))
177+
}
178+
179+
return strings.Join(parts, "; ")
180+
}
181+
182+
var ErrTagScope = errors.New("invalid or missing commit tag")
165183

166184
func (c CommitPolicyConfig) CheckSubject(rawSubject []byte, junitSuite junit.Interface) error {
167185
// check for ascii-only before anything else
@@ -191,11 +209,12 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte, junitSuite junit.Int
191209
if len(submatch) == 0 { // no match
192210
if !tagOK {
193211
hint := suggestionHint(c.suggestFromSubject(rawSubject, tagAlternative.PatchTypes))
194-
junitSuite.AddMessageFailed("", "invalid or missing tag/severity in commit message"+hint, fmt.Sprintf("subject: %s", rawSubject))
212+
summary := c.allowedTagSummary(tagAlternative.PatchTypes)
213+
junitSuite.AddMessageFailed("", "missing tag prefix in commit message, subject must start with '<TAG>/<SEVERITY>: ' (e.g. 'BUG/MINOR: ') or a bare tag like 'MINOR: ', expected "+summary+hint, fmt.Sprintf("subject: %s", rawSubject))
195214
slog.Error("unable to find tag match in subject", "subject", string(rawSubject))
196215

197-
return fmt.Errorf("invalid tag or no tag found%s, searched through [%s]: %w",
198-
hint, strings.Join(tagAlternative.PatchTypes, ", "), ErrTagScope)
216+
return fmt.Errorf("missing tag prefix in subject: subject must start with '<TAG>/<SEVERITY>: ' (e.g. 'BUG/MINOR: ') or a bare tag like 'MINOR: ', expected %s%s: %w",
217+
summary, hint, ErrTagScope)
199218
}
200219
continue
201220
}
@@ -218,11 +237,12 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte, junitSuite junit.Int
218237

219238
if !tagOK {
220239
hint := suggestionHint(c.suggestTag(tag, severity, tagAlternative.PatchTypes))
221-
junitSuite.AddMessageFailed("", "invalid tag/severity in commit message"+hint, fmt.Sprintf("subject: %s", rawSubject))
240+
summary := c.allowedTagSummary(tagAlternative.PatchTypes)
241+
junitSuite.AddMessageFailed("", fmt.Sprintf("invalid tag/severity '%s' in commit message, expected %s%s", strings.TrimSuffix(strings.TrimSpace(string(tagPart)), ":"), summary, hint), fmt.Sprintf("subject: %s", rawSubject))
222242
slog.Error("unable to find valid tag among candidates", "candidates", candidates)
223243

224-
return fmt.Errorf("invalid tag or no tag found%s, searched through [%s]: %w",
225-
hint, strings.Join(tagAlternative.PatchTypes, ", "), ErrTagScope)
244+
return fmt.Errorf("invalid tag/severity '%s' in subject: expected %s%s: %w",
245+
strings.TrimSuffix(strings.TrimSpace(string(tagPart)), ":"), summary, hint, ErrTagScope)
226246
}
227247
}
228248

0 commit comments

Comments
 (0)