Skip to content

Commit 0af79ba

Browse files
committed
Use placeholder for description
1 parent d64de60 commit 0af79ba

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

cmd/description/main.go

+42-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"strings"
89
"syscall"
910

1011
"github.com/google/go-github/v51/github"
@@ -29,6 +30,14 @@ var opts struct {
2930
ShortcutBaseURL string `long:"shortcut-url" env:"SHORTCUT_URL" description:"Shortcut URL. Example: https://app.shortcut.com/foo"`
3031
}
3132

33+
type prDataType struct {
34+
completion string
35+
jiraInfo string
36+
shortcutInfo string
37+
}
38+
39+
var prData prDataType
40+
3241
func main() {
3342
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
3443
defer cancel()
@@ -59,7 +68,7 @@ func run(ctx context.Context) error {
5968
return fmt.Errorf("error getting commits: %w", err)
6069
}
6170

62-
completion, err := description.GenerateCompletion(ctx, openAIClient, diff, pr)
71+
prData.completion, err = description.GenerateCompletion(ctx, openAIClient, diff, pr)
6372
if err != nil {
6473
return fmt.Errorf("error generating completion: %w", err)
6574
}
@@ -70,15 +79,12 @@ func run(ctx context.Context) error {
7079
if err != nil {
7180
fmt.Printf("Error extracting Jira ticket ID: %v \n", err)
7281
} 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))
7483
}
7584
}
7685

7786
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)
8288
}
8389

8490
if opts.Test {
@@ -87,7 +93,7 @@ func run(ctx context.Context) error {
8793

8894
// Update the pull request description
8995
fmt.Println("Updating pull request")
90-
updatePr := &github.PullRequest{Body: github.String(completion)}
96+
updatePr := buildUpdatedDescription(*pr.Body, prData)
9197
if _, err = githubClient.UpdatePullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber, updatePr); err != nil {
9298
return fmt.Errorf("error updating pull request: %w", err)
9399
}
@@ -112,3 +118,32 @@ func buildShortcutContent(shortcutBaseURL string, pr *github.PullRequest) string
112118

113119
return fmt.Sprintf("### Shortcut story: [%s](%s)", id, shortcut.GenerateShortcutStoryURL(shortcutBaseURL, id))
114120
}
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+
}

description/description.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
1111
)
1212

13+
const Placeholder = "gpt-updater:description"
14+
const PlaceholderHidden = `<!--
15+
gpt-updater:description
16+
-->
17+
`
18+
1319
func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
1420
sumDiffs := calculateSumDiffs(diff)
1521

0 commit comments

Comments
 (0)