Skip to content

Commit 4594511

Browse files
committed
Isolate method
1 parent 0af79ba commit 4594511

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

cmd/description/main.go

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

1110
"github.com/google/go-github/v51/github"
@@ -30,13 +29,7 @@ var opts struct {
3029
ShortcutBaseURL string `long:"shortcut-url" env:"SHORTCUT_URL" description:"Shortcut URL. Example: https://app.shortcut.com/foo"`
3130
}
3231

33-
type prDataType struct {
34-
completion string
35-
jiraInfo string
36-
shortcutInfo string
37-
}
38-
39-
var prData prDataType
32+
var descriptionInfo description.Info
4033

4134
func main() {
4235
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
@@ -68,7 +61,7 @@ func run(ctx context.Context) error {
6861
return fmt.Errorf("error getting commits: %w", err)
6962
}
7063

71-
prData.completion, err = description.GenerateCompletion(ctx, openAIClient, diff, pr)
64+
descriptionInfo.Completion, err = description.GenerateCompletion(ctx, openAIClient, diff, pr)
7265
if err != nil {
7366
return fmt.Errorf("error generating completion: %w", err)
7467
}
@@ -79,12 +72,12 @@ func run(ctx context.Context) error {
7972
if err != nil {
8073
fmt.Printf("Error extracting Jira ticket ID: %v \n", err)
8174
} else {
82-
prData.jiraInfo = fmt.Sprintf("### JIRA ticket: [%s](%s)", id, jira.GenerateJiraTicketURL(opts.JiraURL, id))
75+
descriptionInfo.JiraInfo = fmt.Sprintf("### JIRA ticket: [%s](%s)", id, jira.GenerateJiraTicketURL(opts.JiraURL, id))
8376
}
8477
}
8578

8679
if opts.ShortcutBaseURL != "" {
87-
prData.shortcutInfo = buildShortcutContent(opts.ShortcutBaseURL, pr)
80+
descriptionInfo.ShortcutInfo = buildShortcutContent(opts.ShortcutBaseURL, pr)
8881
}
8982

9083
if opts.Test {
@@ -93,8 +86,8 @@ func run(ctx context.Context) error {
9386

9487
// Update the pull request description
9588
fmt.Println("Updating pull request")
96-
updatePr := buildUpdatedDescription(*pr.Body, prData)
97-
if _, err = githubClient.UpdatePullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber, updatePr); err != nil {
89+
updatedPr := description.BuildUpdatedPullRequest(*pr.Body, descriptionInfo)
90+
if _, err = githubClient.UpdatePullRequest(ctx, opts.Owner, opts.Repo, opts.PRNumber, updatedPr); err != nil {
9891
return fmt.Errorf("error updating pull request: %w", err)
9992
}
10093

@@ -118,32 +111,3 @@ func buildShortcutContent(shortcutBaseURL string, pr *github.PullRequest) string
118111

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

+37-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package description
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/google/go-github/v51/github"
89
"github.com/sashabaranov/go-openai"
910

1011
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
1112
)
1213

13-
const Placeholder = "gpt-updater:description"
14-
const PlaceholderHidden = `<!--
15-
gpt-updater:description
16-
-->
17-
`
14+
const placeholder = "gpt-updater:description"
15+
16+
type Info struct {
17+
Completion string
18+
JiraInfo string
19+
ShortcutInfo string
20+
}
1821

1922
func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
2023
sumDiffs := calculateSumDiffs(diff)
@@ -30,6 +33,35 @@ func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *git
3033
return completion, err
3134
}
3235

36+
func BuildUpdatedPullRequest(existingDescription string, info Info) *github.PullRequest {
37+
38+
desc := ""
39+
40+
if info.JiraInfo != "" {
41+
desc = info.JiraInfo + "\n\n" + desc
42+
}
43+
44+
if info.ShortcutInfo != "" {
45+
desc = info.ShortcutInfo + "\n\n" + desc
46+
}
47+
48+
if info.Completion != "" {
49+
desc += info.Completion
50+
}
51+
52+
if existingDescription != "" && strings.Contains(existingDescription, placeholder) {
53+
builtBody := strings.Replace(
54+
existingDescription,
55+
placeholder,
56+
fmt.Sprintf("## 🤖 gpt-updater description\n<!--\n%s\n-->\n%s", placeholder, desc),
57+
1,
58+
)
59+
return &github.PullRequest{Body: github.String(builtBody)}
60+
}
61+
62+
return &github.PullRequest{Body: github.String(desc)}
63+
}
64+
3365
func calculateSumDiffs(diff *github.CommitsComparison) int {
3466
sumDiffs := 0
3567
for _, file := range diff.Files {

0 commit comments

Comments
 (0)