5
5
"fmt"
6
6
"os"
7
7
"os/signal"
8
+ "strings"
8
9
"syscall"
9
10
10
11
"github.com/google/go-github/v51/github"
@@ -29,6 +30,14 @@ var opts struct {
29
30
ShortcutBaseURL string `long:"shortcut-url" env:"SHORTCUT_URL" description:"Shortcut URL. Example: https://app.shortcut.com/foo"`
30
31
}
31
32
33
+ type prDataType struct {
34
+ completion string
35
+ jiraInfo string
36
+ shortcutInfo string
37
+ }
38
+
39
+ var prData prDataType
40
+
32
41
func main () {
33
42
ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM )
34
43
defer cancel ()
@@ -59,7 +68,7 @@ func run(ctx context.Context) error {
59
68
return fmt .Errorf ("error getting commits: %w" , err )
60
69
}
61
70
62
- completion , err : = description .GenerateCompletion (ctx , openAIClient , diff , pr )
71
+ prData . completion , err = description .GenerateCompletion (ctx , openAIClient , diff , pr )
63
72
if err != nil {
64
73
return fmt .Errorf ("error generating completion: %w" , err )
65
74
}
@@ -70,15 +79,12 @@ func run(ctx context.Context) error {
70
79
if err != nil {
71
80
fmt .Printf ("Error extracting Jira ticket ID: %v \n " , err )
72
81
} else {
73
- completion = fmt .Sprintf ("### JIRA ticket: [%s](%s) \n \n %s " , id , jira .GenerateJiraTicketURL (opts .JiraURL , id ), completion )
82
+ prData . jiraInfo = fmt .Sprintf ("### JIRA ticket: [%s](%s)" , id , jira .GenerateJiraTicketURL (opts .JiraURL , id ))
74
83
}
75
84
}
76
85
77
86
if opts .ShortcutBaseURL != "" {
78
- shortcutContent := buildShortcutContent (opts .ShortcutBaseURL , pr )
79
- if shortcutContent != "" {
80
- completion = fmt .Sprintf ("%s\n \n %s" , shortcutContent , completion )
81
- }
87
+ prData .shortcutInfo = buildShortcutContent (opts .ShortcutBaseURL , pr )
82
88
}
83
89
84
90
if opts .Test {
@@ -87,7 +93,7 @@ func run(ctx context.Context) error {
87
93
88
94
// Update the pull request description
89
95
fmt .Println ("Updating pull request" )
90
- updatePr := & github. PullRequest { Body : github . String ( completion )}
96
+ updatePr := buildUpdatedDescription ( * pr . Body , prData )
91
97
if _ , err = githubClient .UpdatePullRequest (ctx , opts .Owner , opts .Repo , opts .PRNumber , updatePr ); err != nil {
92
98
return fmt .Errorf ("error updating pull request: %w" , err )
93
99
}
@@ -112,3 +118,32 @@ func buildShortcutContent(shortcutBaseURL string, pr *github.PullRequest) string
112
118
113
119
return fmt .Sprintf ("### Shortcut story: [%s](%s)" , id , shortcut .GenerateShortcutStoryURL (shortcutBaseURL , id ))
114
120
}
121
+
122
+ func buildUpdatedDescription (existingBody string , prData prDataType ) * github.PullRequest {
123
+
124
+ desc := ""
125
+
126
+ if prData .jiraInfo != "" {
127
+ desc = prData .jiraInfo + "\n \n " + desc
128
+ }
129
+
130
+ if prData .shortcutInfo != "" {
131
+ desc = prData .shortcutInfo + "\n \n " + desc
132
+ }
133
+
134
+ if prData .completion != "" {
135
+ desc += prData .completion
136
+ }
137
+
138
+ if existingBody != "" && strings .Contains (existingBody , description .Placeholder ) {
139
+ builtBody := strings .Replace (
140
+ existingBody ,
141
+ description .Placeholder ,
142
+ description .PlaceholderHidden + desc ,
143
+ 1 ,
144
+ )
145
+ return & github.PullRequest {Body : github .String (builtBody )}
146
+ }
147
+
148
+ return & github.PullRequest {Body : github .String (desc )}
149
+ }
0 commit comments