-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a shortcut integration #32
Open
vanilevsky
wants to merge
13
commits into
ravilushqa:main
Choose a base branch
from
vanilevsky:feature/sc-25996/use-gpt-0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e650c55
Add ShortcutBaseURL param
vanilevsky 35e205c
Add shortcut link to description
vanilevsky 8e5ac41
Fix after linter
vanilevsky 5d34b06
Fix after lint and add test
vanilevsky d64de60
Fix lint
vanilevsky 0af79ba
Use placeholder for description
vanilevsky 4594511
Isolate method
vanilevsky 98a9345
Update README.md
vanilevsky e0d57e5
Add template block
vanilevsky 3501227
Add PR template
vanilevsky 11d2037
Fix double description
vanilevsky d02203a
Optimizing requests
vanilevsky f50fad1
Fix nil pointer
vanilevsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gpt-updater:description | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,23 @@ package description | |
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-github/v51/github" | ||
"github.com/sashabaranov/go-openai" | ||
|
||
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai" | ||
) | ||
|
||
const placeholder = "gpt-updater:description" | ||
const placeholderFinished = "<!-- gpt-updater:description -->" | ||
|
||
type Info struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The structure doesn't look flexible and, in my opinion, needs improvement. What do you think about keeping the old flat logic without an extra structure and simply validating that only JIRA or shortcut are allowed? If both are passed, then return an error. |
||
Completion string | ||
JiraInfo string | ||
ShortcutInfo string | ||
} | ||
|
||
func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) { | ||
sumDiffs := calculateSumDiffs(diff) | ||
|
||
|
@@ -24,6 +34,40 @@ func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *git | |
return completion, err | ||
} | ||
|
||
func BuildUpdatedPullRequest(existingDescription *string, info Info) *github.PullRequest { | ||
|
||
desc := "" | ||
|
||
if info.JiraInfo != "" { | ||
desc = info.JiraInfo + "\n\n" + desc | ||
} | ||
|
||
if info.ShortcutInfo != "" { | ||
desc = info.ShortcutInfo + "\n\n" + desc | ||
} | ||
|
||
if info.Completion != "" { | ||
desc += info.Completion | ||
} | ||
|
||
builtBody := fmt.Sprintf("%s\n## 🤖 gpt-updater description\n%s", placeholderFinished, desc) | ||
|
||
if existingDescription != nil && needToUpdateByPlaceholder(*existingDescription) { | ||
builtBody = strings.Replace(*existingDescription, placeholder, builtBody, 1) | ||
} | ||
|
||
return &github.PullRequest{Body: github.String(builtBody)} | ||
} | ||
|
||
func IsDescriptionFinished(existingDescription string) bool { | ||
return strings.Contains(existingDescription, placeholderFinished) | ||
} | ||
|
||
func needToUpdateByPlaceholder(existingDescription string) bool { | ||
return !strings.Contains(existingDescription, placeholderFinished) && | ||
strings.Contains(existingDescription, placeholder) | ||
} | ||
|
||
func calculateSumDiffs(diff *github.CommitsComparison) int { | ||
sumDiffs := 0 | ||
for _, file := range diff.Files { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package shortcut | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
const storyURLFormat = "%s/story/%s" | ||
|
||
func ExtractShortcutStoryID(title string) (string, error) { | ||
|
||
// This regular expression pattern matches a Shortcut story ID (e.g. sc-12345). | ||
pattern := `sc-([\d]+)` | ||
re, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return "", fmt.Errorf("error compiling regex: %w", err) | ||
} | ||
|
||
matches := re.FindStringSubmatch(title) | ||
if len(matches) < 2 { | ||
return "", fmt.Errorf("no Shortcut story ID found in the input string") | ||
} | ||
|
||
return matches[1], nil | ||
} | ||
|
||
func GenerateShortcutStoryURL(shortcutBaseURL, ticketID string) string { | ||
return fmt.Sprintf(storyURLFormat, shortcutBaseURL, ticketID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package shortcut | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExtractShortcutStoryID(t *testing.T) { | ||
testCases := []struct { | ||
title string | ||
expectedID string | ||
expectedErr bool | ||
}{ | ||
{ | ||
title: "This is a sample title with a valid story ID sc-12345", | ||
expectedID: "12345", | ||
expectedErr: false, | ||
}, | ||
{ | ||
title: "No story ID in this title", | ||
expectedID: "", | ||
expectedErr: true, | ||
}, | ||
{ | ||
title: "Invalid story ID format sc-abcde", | ||
expectedID: "", | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
id, err := ExtractShortcutStoryID(tc.title) | ||
|
||
if tc.expectedErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expectedID, id) | ||
} | ||
} | ||
} | ||
|
||
func TestGenerateShortcutStoryURL(t *testing.T) { | ||
baseURL := "https://app.shortcut.com/foo" | ||
ticketID := "12345" | ||
expectedURL := "https://app.shortcut.com/foo/story/12345" | ||
|
||
url := GenerateShortcutStoryURL(baseURL, ticketID) | ||
assert.Equal(t, expectedURL, url) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this related to shortcut, do you mind to create a new PR for that?