55 _ "embed"
66 "fmt"
77 "log/slog"
8+ "os"
89 "regexp"
910 "strconv"
1011 "strings"
@@ -34,6 +35,7 @@ type branchTemplateData struct {
3435
3536type templateData struct {
3637 TicketNumber string
38+ TicketUrlPattern string
3739 Username string
3840 CommitBody string
3941 CommitSummary string
@@ -157,8 +159,8 @@ func truncateString(str string, maxBytes int) string {
157159 return str
158160}
159161
160- func GetPullRequestText (commitHash string , featureFlag string ) PullRequestText {
161- data := getPullRequestTemplateData (commitHash , featureFlag )
162+ func GetPullRequestText (commitHash string , featureFlag string , ticketUrlPattern string ) PullRequestText {
163+ data := getPullRequestTemplateData (commitHash , featureFlag , ticketUrlPattern )
162164 title := RunTemplate ("pr-title.template" , prTitleTemplateText , data )
163165 description := RunTemplate ("pr-description.template" , prDescriptionTemplateText , data )
164166 return PullRequestText {Description : description , Title : title }
@@ -186,17 +188,20 @@ func RunTemplate(configFilename string, defaultTemplateText string, data any) st
186188 return output .String ()
187189}
188190
189- func getPullRequestTemplateData (commitHash string , featureFlag string ) templateData {
191+ func getPullRequestTemplateData (commitHash string , featureFlag string , ticketUrlPattern string ) templateData {
190192 commitSummary := util .ExecuteOrDieTrimmed (util.ExecuteOptions {}, "git" , "--no-pager" , "show" , "--no-patch" , "--format=%s" , commitHash )
191193 commitBody := util .ExecuteOrDieTrimmed (util.ExecuteOptions {}, "git" , "--no-pager" , "show" , "--no-patch" , "--format=%b" , commitHash )
192194 commentLineRegex := regexp .MustCompile ("(?m)^#.*$" )
193195 commitBody = commentLineRegex .ReplaceAllString (commitBody , "" )
194196 commitSummaryCleaned := util .ExecuteOrDieTrimmed (util.ExecuteOptions {}, "git" , "show" , "--no-patch" , "--format=%f" , commitHash )
195197 expression := regexp .MustCompile (`^(\S+-[[:digit:]]+ )?(.*)` )
196198 summaryMatches := expression .FindStringSubmatch (commitSummary )
199+ ticketNumber := strings .TrimSpace (summaryMatches [1 ])
200+ resolvedTicketUrl := strings .ReplaceAll (ticketUrlPattern , "{TicketNumber}" , ticketNumber )
197201 return templateData {
198202 Username : util .GetUsername (),
199- TicketNumber : strings .TrimSpace (summaryMatches [1 ]),
203+ TicketNumber : ticketNumber ,
204+ TicketUrlPattern : resolvedTicketUrl ,
200205 CommitBody : commitBody ,
201206 CommitSummary : commitSummary ,
202207 CommitSummaryWithoutTicket : summaryMatches [2 ],
@@ -205,6 +210,25 @@ func getPullRequestTemplateData(commitHash string, featureFlag string) templateD
205210 }
206211}
207212
213+ // TemplateUsesTicketUrlPattern returns true if the PR description or title
214+ // template references the TicketUrlPattern variable.
215+ func TemplateUsesTicketUrlPattern () bool {
216+ return templateTextContains ("pr-description.template" , prDescriptionTemplateText , "TicketUrlPattern" ) ||
217+ templateTextContains ("pr-title.template" , prTitleTemplateText , "TicketUrlPattern" )
218+ }
219+
220+ func templateTextContains (configFilename string , defaultText string , search string ) bool {
221+ configFile := util .GetConfigFile (configFilename )
222+ if configFile != "" {
223+ data , err := os .ReadFile (configFile )
224+ if err != nil {
225+ panic (fmt .Sprint ("Could not read " , configFile , ": " , err ))
226+ }
227+ return strings .Contains (string (data ), search )
228+ }
229+ return strings .Contains (defaultText , search )
230+ }
231+
208232func getBranchTemplateData (sanitizedSummary string ) branchTemplateData {
209233 // Dots are not allowed in branch names of some Github configurations.
210234 username := strings .ReplaceAll (util .GetUsername (), "." , "-" )
0 commit comments