@@ -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
102102func 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