diff --git a/build/go.mod b/build/go.mod index 7143f07..4f94bbc 100644 --- a/build/go.mod +++ b/build/go.mod @@ -6,11 +6,6 @@ require ( github.com/carlmjohnson/requests v0.23.2 github.com/magefile/mage v1.14.0 golang.org/x/exp v0.0.0-20230307190834-24139beb5833 - golang.org/x/sync v0.1.0 ) -require ( - code.gitea.io/sdk/gitea v0.15.1 // indirect - github.com/hashicorp/go-version v1.2.1 // indirect - golang.org/x/net v0.5.0 // indirect -) +require golang.org/x/net v0.6.0 // indirect diff --git a/build/go.sum b/build/go.sum index 6a05b40..27ddc72 100644 --- a/build/go.sum +++ b/build/go.sum @@ -1,38 +1,7 @@ -code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= -code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M= -code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA= github.com/carlmjohnson/requests v0.23.2 h1:SzaY+/5v8QOvt++7HTXe1xgmIb3wc/bYf2QJmrO73sM= github.com/carlmjohnson/requests v0.23.2/go.mod h1:09VwhOaRQYCraJcByjEuvuOGO1jxUjIx6vnAEkt2ges= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= -github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s= golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= -golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= diff --git a/go.mod b/go.mod index 3a85182..5c05c45 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.12.0 github.com/stretchr/testify v1.8.2 + golang.org/x/exp v0.0.0-20230307190834-24139beb5833 ) require ( @@ -34,7 +35,7 @@ require ( github.com/subosito/gotenv v1.4.1 // indirect golang.org/x/mod v0.8.0 // indirect golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.6.0 // indirect + golang.org/x/text v0.7.0 // indirect golang.org/x/tools v0.6.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index cb733cd..9855884 100644 --- a/go.sum +++ b/go.sum @@ -222,6 +222,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s= +golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -344,8 +346,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= -golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/go.work.sum b/go.work.sum index a0e00a6..969e122 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,7 +1,11 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= diff --git a/nitters/gitea/api.go b/nitters/gitea/api.go index c0cf10c..8809764 100644 --- a/nitters/gitea/api.go +++ b/nitters/gitea/api.go @@ -8,6 +8,10 @@ type PullReviewManager interface { DeletePullReview(owner, repo string, index, id int64) (*gitea.Response, error) } +type PullRequestReader interface { + GetPullRequest(owner, repo string, index int64) (*gitea.PullRequest, *gitea.Response, error) +} + type WhoAmIer interface { GetMyUserInfo() (*gitea.User, *gitea.Response, error) } @@ -15,5 +19,6 @@ type WhoAmIer interface { //go:generate mockery --name Client --filename gitea_client.mock.go type Client interface { PullReviewManager + PullRequestReader WhoAmIer } diff --git a/nitters/gitea/pr_nitter.go b/nitters/gitea/pr_nitter.go index 754ed3d..856ddc4 100644 --- a/nitters/gitea/pr_nitter.go +++ b/nitters/gitea/pr_nitter.go @@ -45,12 +45,25 @@ type prNitter struct { } func (p prNitter) Report(report *report.Data, issues []result.Issue) error { - review, err := p.preparePullReview(report, issues) + me, _, err := p.GetMyUserInfo() if err != nil { return err } - if err := p.cleanOutdatedReviews(); err != nil { + slog.Debug("Running as", slog.String("name", me.UserName)) + + pr, _, err := p.GetPullRequest(p.cfg.Namespace, p.cfg.Repo, p.cfg.PRIndex) + if err != nil { + slog.Error("Failed to get PR details", err, slog.Int64("index", p.cfg.PRIndex)) + return err + } + + review, err := p.preparePullReview(report, issues, me, pr) + if err != nil { + return err + } + + if err := p.cleanOutdatedReviews(me); err != nil { return err } @@ -62,7 +75,12 @@ func (p prNitter) Report(report *report.Data, issues []result.Issue) error { return nil } -func (p prNitter) preparePullReview(report *report.Data, issues []result.Issue) (*gitea.CreatePullReviewOptions, error) { +func (p prNitter) preparePullReview( + report *report.Data, + issues []result.Issue, + me *gitea.User, + pr *gitea.PullRequest, +) (*gitea.CreatePullReviewOptions, error) { templateBuf := bytes.Buffer{} summaryData := map[string]any{ "Report": report, @@ -73,6 +91,8 @@ func (p prNitter) preparePullReview(report *report.Data, issues []result.Issue) return nil, err } + _, _ = templateBuf.WriteString(summaryFooter) + pullReviewOptions := &gitea.CreatePullReviewOptions{ State: p.cfg.ReviewState, Body: templateBuf.String(), @@ -80,7 +100,17 @@ func (p prNitter) preparePullReview(report *report.Data, issues []result.Issue) } if len(issues) == 0 { - pullReviewOptions.State = gitea.ReviewStateApproved + if me.ID != pr.Poster.ID { + pullReviewOptions.State = gitea.ReviewStateApproved + } else { + slog.Warn( + "Cannot approve PR if token is issued by the same user - fallback to comment state", + slog.String("nitter_user", me.UserName), + slog.String("pr_poster", pr.Poster.UserName), + ) + + pullReviewOptions.State = gitea.ReviewStateComment + } } else { templateBuf.Reset() for i := range issues { @@ -91,11 +121,6 @@ func (p prNitter) preparePullReview(report *report.Data, issues []result.Issue) return nil, err } - if _, err := templateBuf.WriteString(summaryFooter); err != nil { - slog.Error("Failed to append summary footer", err) - return nil, err - } - pullReviewOptions.Comments = append(pullReviewOptions.Comments, gitea.CreatePullReviewComment{ Path: issues[i].Pos.Filename, Body: templateBuf.String(), @@ -109,14 +134,7 @@ func (p prNitter) preparePullReview(report *report.Data, issues []result.Issue) return pullReviewOptions, nil } -func (p prNitter) cleanOutdatedReviews() error { - me, _, err := p.GetMyUserInfo() - if err != nil { - return err - } - - slog.Debug("Running as", slog.String("name", me.UserName)) - +func (p prNitter) cleanOutdatedReviews(me *gitea.User) error { reviews, _, err := p.ListPullReviews(p.cfg.Namespace, p.cfg.Repo, p.cfg.PRIndex, gitea.ListPullReviewsOptions{}) if err != nil { return err diff --git a/nitters/gitea/pr_nitter_test.go b/nitters/gitea/pr_nitter_test.go index 3414f49..469af2c 100644 --- a/nitters/gitea/pr_nitter_test.go +++ b/nitters/gitea/pr_nitter_test.go @@ -29,6 +29,11 @@ func Test_PRNitter_Report_MockCreatorError_Error(t *testing.T) { Return(&giteasdk.User{ID: 11}, nil, nil). Times(1) + client. + EXPECT(). + GetPullRequest(mock.Anything, mock.Anything, mock.Anything). + Return(&giteasdk.PullRequest{Poster: &giteasdk.User{}}, nil, nil) + client. EXPECT(). ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).