From cec1597a48c5cc7e66eb48c5478a4e91454242bf Mon Sep 17 00:00:00 2001 From: Leonardo Di Donato Date: Wed, 22 Mar 2023 23:21:03 +0100 Subject: [PATCH] refactor(pkg/reporter/gh/comment): move here logic to (sticky) comment on GitHub pull requests Signed-off-by: Leonardo Di Donato --- cmd/commands_test.go | 6 ++-- cmd/scan/scan.go | 4 +-- .../comment/comment.go} | 29 +++++++------------ .../comment/comment_test.go} | 4 +-- .../testdata/issue_with_sticky_comment.json | 0 .../issue_with_sticky_comment_updated.json | 0 .../issue_without_sticky_comment.json | 0 .../issue_without_sticky_comment_updated.json | 0 pkg/reporter/reporter.go | 11 +++---- pkg/reporter/request/request.go | 6 ++-- 10 files changed, 27 insertions(+), 33 deletions(-) rename pkg/reporter/{githubprreview/githubprreview.go => gh/comment/comment.go} (76%) rename pkg/reporter/{githubprreview/githubprreview_test.go => gh/comment/comment_test.go} (98%) rename pkg/reporter/{githubprreview => gh/comment}/testdata/issue_with_sticky_comment.json (100%) rename pkg/reporter/{githubprreview => gh/comment}/testdata/issue_with_sticky_comment_updated.json (100%) rename pkg/reporter/{githubprreview => gh/comment}/testdata/issue_without_sticky_comment.json (100%) rename pkg/reporter/{githubprreview => gh/comment}/testdata/issue_without_sticky_comment_updated.json (100%) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index c0e2b320..07782b1e 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -354,9 +354,9 @@ Global Flags: { envvar: map[string]string{ "LSTN_GH_PULL_ID": "887755", - "LSTN_GH_REPO": "go-conventionalcommits", - "LSTN_ENDPOINT": "https://npm-stage.listen.dev", - "LSTN_TIMEOUT": "33331", + "LSTN_GH_REPO": "go-conventionalcommits", + "LSTN_ENDPOINT": "https://npm-stage.listen.dev", + "LSTN_TIMEOUT": "33331", }, cmdline: []string{"scan", "--debug-options", "--config", path.Join(cwd, "testdata", "c1.yaml")}, stdout: heredoc.Doc(`Using config file: _CWD_/testdata/c1.yaml diff --git a/cmd/scan/scan.go b/cmd/scan/scan.go index 0094af22..9c266f7f 100644 --- a/cmd/scan/scan.go +++ b/cmd/scan/scan.go @@ -161,7 +161,7 @@ The verdicts it returns are listed by the name of each package and its specified tc := oauth2.NewClient(ctx, ts) client := github.NewClient(tc) - rep, err := reporter.BuildReporter(r.String()) + rep, err := reporter.BuildReporter(r) if err != nil { return err } @@ -170,7 +170,7 @@ The verdicts it returns are listed by the name of each package and its specified req := request.Report{ Packages: combinedResponse, - GithubPRReviewRequest: request.GithubPRReviewReportRequest{ + GitHubPullCommentReport: request.GitHubPullCommentReport{ Owner: scanOpts.Reporter.GitHub.Owner, Repo: scanOpts.Reporter.GitHub.Repo, ID: scanOpts.Reporter.GitHub.Pull.ID, diff --git a/pkg/reporter/githubprreview/githubprreview.go b/pkg/reporter/gh/comment/comment.go similarity index 76% rename from pkg/reporter/githubprreview/githubprreview.go rename to pkg/reporter/gh/comment/comment.go index 54b84d7e..ef7d5a22 100644 --- a/pkg/reporter/githubprreview/githubprreview.go +++ b/pkg/reporter/gh/comment/comment.go @@ -13,7 +13,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -package githubprreview +package comment import ( "bytes" @@ -24,37 +24,30 @@ import ( "github.com/google/go-github/v50/github" "github.com/listendev/lstn/pkg/cmd/report" "github.com/listendev/lstn/pkg/reporter/request" - "github.com/listendev/lstn/pkg/validate" ) -const ReporterIdentifier = "github-pr-review" - -func init() { - validate.RegisterAvailableReporter(ReporterIdentifier) -} - const stickyReviewCommentAnnotation = "" -type ReviewReporter struct { +type Reporter struct { ctx context.Context ghClient *github.Client } -func New() *ReviewReporter { - return &ReviewReporter{ +func New() *Reporter { + return &Reporter{ ghClient: github.NewClient(nil), } } -func (r *ReviewReporter) WithGithubClient(client *github.Client) { +func (r *Reporter) WithGithubClient(client *github.Client) { r.ghClient = client } -func (r *ReviewReporter) WithContext(ctx context.Context) { +func (r *Reporter) WithContext(ctx context.Context) { r.ctx = ctx } -func (r *ReviewReporter) stickyComment(owner string, repo string, id int, comment io.Reader) error { +func (r *Reporter) stickyComment(owner string, repo string, id int, comment io.Reader) error { buf := bytes.Buffer{} _, err := buf.Write([]byte(stickyReviewCommentAnnotation)) if err != nil { @@ -93,7 +86,7 @@ func (r *ReviewReporter) stickyComment(owner string, repo string, id int, commen return commentFn() } -func (r *ReviewReporter) Report(req *request.Report) error { +func (r *Reporter) Report(req *request.Report) error { buf := bytes.Buffer{} fullMarkdownReport := report.NewFullMarkdwonReport() fullMarkdownReport.WithOutput(&buf) @@ -101,9 +94,9 @@ func (r *ReviewReporter) Report(req *request.Report) error { return err } - owner := req.GithubPRReviewRequest.Owner - repo := req.GithubPRReviewRequest.Repo - id := req.GithubPRReviewRequest.ID + owner := req.GitHubPullCommentReport.Owner + repo := req.GitHubPullCommentReport.Repo + id := req.GitHubPullCommentReport.ID err := r.stickyComment(owner, repo, id, &buf) if err != nil { diff --git a/pkg/reporter/githubprreview/githubprreview_test.go b/pkg/reporter/gh/comment/comment_test.go similarity index 98% rename from pkg/reporter/githubprreview/githubprreview_test.go rename to pkg/reporter/gh/comment/comment_test.go index 81bc3346..2b53ea8e 100644 --- a/pkg/reporter/githubprreview/githubprreview_test.go +++ b/pkg/reporter/gh/comment/comment_test.go @@ -13,7 +13,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -package githubprreview +package comment import ( "context" @@ -98,7 +98,7 @@ func TestReviewReporter_stickyComment(t *testing.T) { ghClient := github.NewClient(nil) - r := &ReviewReporter{ + r := &Reporter{ ctx: context.TODO(), ghClient: ghClient, } diff --git a/pkg/reporter/githubprreview/testdata/issue_with_sticky_comment.json b/pkg/reporter/gh/comment/testdata/issue_with_sticky_comment.json similarity index 100% rename from pkg/reporter/githubprreview/testdata/issue_with_sticky_comment.json rename to pkg/reporter/gh/comment/testdata/issue_with_sticky_comment.json diff --git a/pkg/reporter/githubprreview/testdata/issue_with_sticky_comment_updated.json b/pkg/reporter/gh/comment/testdata/issue_with_sticky_comment_updated.json similarity index 100% rename from pkg/reporter/githubprreview/testdata/issue_with_sticky_comment_updated.json rename to pkg/reporter/gh/comment/testdata/issue_with_sticky_comment_updated.json diff --git a/pkg/reporter/githubprreview/testdata/issue_without_sticky_comment.json b/pkg/reporter/gh/comment/testdata/issue_without_sticky_comment.json similarity index 100% rename from pkg/reporter/githubprreview/testdata/issue_without_sticky_comment.json rename to pkg/reporter/gh/comment/testdata/issue_without_sticky_comment.json diff --git a/pkg/reporter/githubprreview/testdata/issue_without_sticky_comment_updated.json b/pkg/reporter/gh/comment/testdata/issue_without_sticky_comment_updated.json similarity index 100% rename from pkg/reporter/githubprreview/testdata/issue_without_sticky_comment_updated.json rename to pkg/reporter/gh/comment/testdata/issue_without_sticky_comment_updated.json diff --git a/pkg/reporter/reporter.go b/pkg/reporter/reporter.go index de4e82a9..4dbcbd62 100644 --- a/pkg/reporter/reporter.go +++ b/pkg/reporter/reporter.go @@ -20,12 +20,13 @@ import ( "errors" "github.com/google/go-github/v50/github" - "github.com/listendev/lstn/pkg/reporter/githubprreview" + "github.com/listendev/lstn/pkg/cmd" + ghcomment "github.com/listendev/lstn/pkg/reporter/gh/comment" "github.com/listendev/lstn/pkg/reporter/request" ) var ( - ErrReporterNotFound = errors.New("reporter not found") + ErrReporterNotFound = errors.New("unsupported reporter") ) type Reporter interface { @@ -34,10 +35,10 @@ type Reporter interface { WithGithubClient(client *github.Client) } -func BuildReporter(reporterIdentifier string) (Reporter, error) { +func BuildReporter(reporterIdentifier cmd.ReportType) (Reporter, error) { switch reporterIdentifier { - case githubprreview.ReporterIdentifier: - return githubprreview.New(), nil + case cmd.GitHubPullCommentReport: + return ghcomment.New(), nil default: return nil, ErrReporterNotFound } diff --git a/pkg/reporter/request/request.go b/pkg/reporter/request/request.go index b9821fc4..f55d0675 100644 --- a/pkg/reporter/request/request.go +++ b/pkg/reporter/request/request.go @@ -19,13 +19,13 @@ import ( "github.com/listendev/lstn/pkg/listen" ) -type GithubPRReviewReportRequest struct { +type GitHubPullCommentReport struct { Owner string Repo string ID int } type Report struct { - Packages []listen.Package - GithubPRReviewRequest GithubPRReviewReportRequest + Packages []listen.Package + GitHubPullCommentReport }