Skip to content

Commit

Permalink
refactor(pkg/reporter/gh/comment): move here logic to (sticky) commen…
Browse files Browse the repository at this point in the history
…t on GitHub pull requests

Signed-off-by: Leonardo Di Donato <[email protected]>
  • Loading branch information
leodido committed Mar 23, 2023
1 parent 6b19480 commit cec1597
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 33 deletions.
6 changes: 3 additions & 3 deletions cmd/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = "<!--@lstn-sticky-review-comment-->"

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 {
Expand Down Expand Up @@ -93,17 +86,17 @@ 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)
if err := fullMarkdownReport.Render(req.Packages); err != nil {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestReviewReporter_stickyComment(t *testing.T) {

ghClient := github.NewClient(nil)

r := &ReviewReporter{
r := &Reporter{
ctx: context.TODO(),
ghClient: ghClient,
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/reporter/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit cec1597

Please sign in to comment.