Skip to content

Commit f5f869e

Browse files
authored
Merge pull request #23 from ravilushqa/revert-21-revert-18-refactor
Revert 21 revert 18 refactor
2 parents c6d76ad + 1aaca2e commit f5f869e

20 files changed

+1534
-35
lines changed

.github/workflows/golangci-lint.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
- main
9+
pull_request:
10+
permissions:
11+
contents: read
12+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
13+
# pull-requests: read
14+
jobs:
15+
golangci:
16+
name: lint
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/setup-go@v3
20+
with:
21+
go-version: 1.19
22+
- uses: actions/checkout@v3
23+
- name: golangci-lint
24+
uses: golangci/golangci-lint-action@v3
25+
with:
26+
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
27+
version: v1.52
28+
29+
# Optional: working directory, useful for monorepos
30+
# working-directory: somedir
31+
32+
# Optional: golangci-lint command line arguments.
33+
# args: --issues-exit-code=0
34+
35+
# Optional: show only new issues if it's a pull request. The default value is `false`.
36+
# only-new-issues: true
37+
38+
# Optional: if set to true then the all caching functionality will be complete disabled,
39+
# takes precedence over all other caching options.
40+
# skip-cache: true
41+
42+
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
43+
# skip-pkg-cache: true
44+
45+
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
46+
# skip-build-cache: true

.github/workflows/gpt_pullrequest_updater.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ jobs:
2525
- name: Build description and review commands
2626
run: |
2727
cd gpt-pullrequest-updater
28-
go build -o description ./cmd/description
29-
go build -o review ./cmd/review
28+
make build
3029
3130
- name: Update Pull Request Description
3231
run: |
33-
./gpt-pullrequest-updater/description
32+
./gpt-pullrequest-updater/bin/description
3433
env:
3534
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3635
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
@@ -41,7 +40,7 @@ jobs:
4140
- name: Review Pull Request
4241
if: github.event.action == 'opened'
4342
run: |
44-
./gpt-pullrequest-updater/review
43+
./gpt-pullrequest-updater/bin/review
4544
env:
4645
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4746
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
.idea

.golangci.yaml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://golangci-lint.run/usage/configuration
2+
linters:
3+
# Disable everything by default so upgrades to not include new "default
4+
# enabled" linters.
5+
disable-all: true
6+
# Specifically enable linters we want to use.
7+
enable:
8+
- depguard
9+
- errcheck
10+
- godot
11+
- gofmt
12+
- goimports
13+
- gosimple
14+
- govet
15+
- ineffassign
16+
- misspell
17+
- revive
18+
- staticcheck
19+
- typecheck
20+
- unused
21+
22+
issues:
23+
exclude-rules:
24+
# helpers in tests often (rightfully) pass a *testing.T as their first argument
25+
- path: _test\.go
26+
text: "context.Context should be the first parameter of a function"
27+
linters:
28+
- golint
29+
# Yes, they are, but it's okay in a test
30+
- path: _test\.go
31+
text: "exported func.*returns unexported type.*which can be annoying to use"
32+
linters:
33+
- golint
34+
35+
linters-settings:
36+
misspell:
37+
locale: US
38+
ignore-words:
39+
- cancelled
40+
goimports:
41+
local-prefixes: github.com/ravilushqa/gpt-pullrequest-updater

Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.PHONY: init-tools lint test test-coverage precommit help
2+
3+
build:
4+
go build -o bin/description ./cmd/description && \
5+
go build -o bin/review ./cmd/review
6+
7+
# run this once to install tools required for development.
8+
init-tools:
9+
cd tools && \
10+
go mod tidy && \
11+
go mod verify && \
12+
go generate -x -tags "tools"
13+
14+
# run golangci-lint
15+
lint: init-tools
16+
./bin/golangci-lint run --timeout=30m ./...
17+
18+
# run go test
19+
test:
20+
go test -race -count 1 ./...
21+
22+
# run go test with coverage
23+
test-coverage:
24+
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
25+
26+
# precommit command. run lint, test
27+
precommit: lint test
28+
29+
# show help
30+
help:
31+
@echo 'Usage:'
32+
@echo ' make [target]'
33+
@echo ''
34+
@echo 'Targets:'
35+
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
36+
helpMessage = match(lastLine, /^# (.*)/); \
37+
if (helpMessage) { \
38+
helpCommand = substr($$1, 0, index($$1, ":")-1); \
39+
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
40+
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
41+
} \
42+
} \
43+
{ lastLine = $$0 }' $(MAKEFILE_LIST)
44+
45+
.DEFAULT_GOAL := help

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ jobs:
101101
- name: Build description and review commands
102102
run: |
103103
cd gpt-pullrequest-updater
104-
go build -o description ./cmd/description
105-
go build -o review ./cmd/review
104+
make build
106105
107106
- name: Update Pull Request Description
108107
run: |
109-
./gpt-pullrequest-updater/description
108+
./gpt-pullrequest-updater/bin/description
110109
env:
111110
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112111
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
@@ -117,7 +116,7 @@ jobs:
117116
- name: Review Pull Request
118117
if: github.event.action == 'opened'
119118
run: |
120-
./gpt-pullrequest-updater/review
119+
./gpt-pullrequest-updater/bin/review
121120
env:
122121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
123122
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}

cmd/review/config.go

-10
This file was deleted.

cmd/review/main.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ import (
1111

1212
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
1313
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
14+
"github.com/ravilushqa/gpt-pullrequest-updater/review"
1415
)
1516

17+
var opts struct {
18+
GithubToken string `long:"gh-token" env:"GITHUB_TOKEN" description:"GitHub token" required:"true"`
19+
OpenAIToken string `long:"openai-token" env:"OPENAI_TOKEN" description:"OpenAI token" required:"true"`
20+
Owner string `long:"owner" env:"OWNER" description:"GitHub owner" required:"true"`
21+
Repo string `long:"repo" env:"REPO" description:"GitHub repo" required:"true"`
22+
PRNumber int `long:"pr-number" env:"PR_NUMBER" description:"Pull request number" required:"true"`
23+
Test bool `long:"test" env:"TEST" description:"Test mode"`
24+
}
25+
1626
func main() {
1727
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
1828
defer cancel()
@@ -43,16 +53,17 @@ func run(ctx context.Context) error {
4353
return fmt.Errorf("error getting commits: %w", err)
4454
}
4555

46-
comments, err := processFiles(ctx, openAIClient, diff)
56+
comments, err := review.GenerateCommentsFromDiff(ctx, openAIClient, diff)
4757
if err != nil {
4858
return err
4959
}
5060

5161
if opts.Test {
5262
fmt.Printf("Comments: %v \n", comments)
63+
return nil
5364
}
5465

55-
err = createComments(ctx, githubClient, comments)
66+
err = review.PushComments(ctx, githubClient, opts.Owner, opts.Repo, opts.PRNumber, comments)
5667
if err != nil {
5768
return fmt.Errorf("error creating comments: %w", err)
5869
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ require (
66
github.com/google/go-github/v51 v51.0.0
77
github.com/jessevdk/go-flags v1.5.0
88
github.com/sashabaranov/go-openai v1.7.0
9+
github.com/stretchr/testify v1.8.2
910
golang.org/x/oauth2 v0.6.0
1011
)
1112

1213
require (
1314
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
1415
github.com/cloudflare/circl v1.1.0 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
1517
github.com/golang/protobuf v1.5.2 // indirect
1618
github.com/google/go-querystring v1.1.0 // indirect
19+
github.com/pmezard/go-difflib v1.0.0 // indirect
20+
github.com/stretchr/objx v0.5.0 // indirect
1721
golang.org/x/crypto v0.7.0 // indirect
1822
golang.org/x/net v0.8.0 // indirect
1923
golang.org/x/sys v0.6.0 // indirect
2024
google.golang.org/appengine v1.6.7 // indirect
2125
google.golang.org/protobuf v1.28.0 // indirect
26+
gopkg.in/yaml.v3 v3.0.1 // indirect
2227
)

go.sum

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0g
33
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
44
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
55
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
69
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
710
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
811
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
@@ -16,8 +19,18 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD
1619
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
1720
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
1821
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
23+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1924
github.com/sashabaranov/go-openai v1.7.0 h1:D1dBXoZhtf/aKNu6WFf0c7Ah2NM30PZ/3Mqly6cZ7fk=
2025
github.com/sashabaranov/go-openai v1.7.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
26+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
27+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
28+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
29+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
30+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
31+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
32+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
33+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2134
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2235
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2336
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
@@ -47,3 +60,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
4760
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
4861
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
4962
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
63+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
64+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
65+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
67+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

jira/jira.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"regexp"
66
)
77

8-
const ticketUrlFormat = "%s/browse/%s"
8+
const ticketURLFormat = "%s/browse/%s"
99

1010
// ExtractJiraTicketID returns the first JIRA ticket ID found in the input string.
1111
func ExtractJiraTicketID(s string) (string, error) {
@@ -25,5 +25,5 @@ func ExtractJiraTicketID(s string) (string, error) {
2525
}
2626

2727
func GenerateJiraTicketURL(jiraURL, ticketID string) string {
28-
return fmt.Sprintf(ticketUrlFormat, jiraURL, ticketID)
28+
return fmt.Sprintf(ticketURLFormat, jiraURL, ticketID)
2929
}

openai/openai.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
_ "embed"
66
"fmt"
7+
"time"
78

89
"github.com/sashabaranov/go-openai"
910
)
1011

11-
//go:embed prompt-review.txt
12+
//go:embed prompts/review
1213
var PromptReview string
1314

1415
const (
@@ -37,8 +38,21 @@ func (o *Client) ChatCompletion(ctx context.Context, messages []openai.ChatCompl
3738
)
3839

3940
if err != nil {
40-
fmt.Printf("ChatCompletion error: %v\n", err)
41-
return "", err
41+
fmt.Println("Error completing prompt:", err)
42+
fmt.Println("Retrying after 1 minute")
43+
// retry once after 1 minute
44+
time.Sleep(time.Minute)
45+
resp, err = o.client.CreateChatCompletion(
46+
ctx,
47+
openai.ChatCompletionRequest{
48+
Model: openai.GPT3Dot5Turbo,
49+
Messages: messages,
50+
Temperature: 0.1,
51+
},
52+
)
53+
if err != nil {
54+
return "", fmt.Errorf("error completing prompt: %w", err)
55+
}
4256
}
4357

4458
return resp.Choices[0].Message.Content, nil
File renamed without changes.

0 commit comments

Comments
 (0)