0
0
mirror of https://github.com/thegeeklab/wp-github-comment.git synced 2024-06-02 18:39:39 +02:00

Compare commits

...

18 Commits

Author SHA1 Message Date
renovate[bot]
98836fcb37
chore(deps): update docker.io/library/alpine docker tag to v3.20 (#122)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-06-01 13:21:53 +02:00
renovate[bot]
ade88cd306 chore(deps): update dependency golangci/golangci-lint to v1.59.0 2024-05-27 05:18:01 +00:00
renovate[bot]
9d20be9b37
fix(deps): update module github.com/rs/zerolog to v1.33.0 (#123)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-24 09:26:08 +02:00
renovate[bot]
636bc9ef4a chore(deps): update dependency golangci/golangci-lint to v1.58.2 2024-05-20 10:02:31 +00:00
3dfa592e96
chore: migrate to wp-plugin-go v3 (#120) 2024-05-17 21:50:16 +02:00
renovate[bot]
a47fc5926c
chore(docker): update docker.io/library/golang:1.22 docker digest to f43c6f0 (#118)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-16 13:41:14 +02:00
renovate[bot]
bc436f2940 chore(deps): update dependency golangci/golangci-lint to v1.58.1 2024-05-13 04:37:35 +00:00
23ed67963d
ci: fix golangci-lint deprecations 2024-05-12 11:08:27 +02:00
96d603d111
test: use static errors 2024-05-12 10:59:19 +02:00
renovate[bot]
6ddf49beb4
fix(deps): update module github.com/google/go-github/v61 to v62 (#116)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-11 11:05:13 +02:00
07d0b5405b
refactor: use dedicated github package (#115) 2024-05-11 10:25:19 +02:00
renovate[bot]
b0333e8700 chore(docker): update docker.io/library/golang:1.22 docker digest to b1e05e2 2024-05-08 05:16:37 +00:00
caafe6c2cf
refactor: rework github client and comment methods (#113) 2024-05-07 21:52:18 +02:00
4138e06711
docs: hide system flags and refactor generator (#112) 2024-05-07 15:03:20 +02:00
renovate[bot]
8ce857b9f9
fix(deps): update module golang.org/x/oauth2 to v0.20.0 (#109)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-07 12:16:08 +02:00
renovate[bot]
14441d18c4 chore(deps): update dependency golangci/golangci-lint to v1.58.0 2024-05-06 03:24:05 +00:00
renovate[bot]
495b0aa272
fix(deps): update module github.com/urfave/cli/v2 to v2.27.2 (#107)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-29 10:25:43 +02:00
renovate[bot]
87d4a691d6 chore(docker): update docker.io/library/golang:1.22 docker digest to d5302d4 2024-04-25 04:37:10 +00:00
19 changed files with 825 additions and 308 deletions

View File

@ -23,7 +23,6 @@ linters:
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forcetypeassert
@ -37,12 +36,12 @@ linters:
- gocyclo
- godot
- godox
- goerr113
- err113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- mnd
- gomoddirectives
- gomodguard
- goprintffuncname

6
.mockery.yaml Normal file
View File

@ -0,0 +1,6 @@
---
all: True
dir: "{{.PackageName}}/mocks"
outpkg: "mocks"
packages:
github.com/thegeeklab/wp-github-comment/github:

View File

@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:450e3822c7a135e1463cd83e51c8e2eb03b86a02113c89424e6f0f8344bb4168 as build
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:f43c6f049f04cbbaeb28f0aad3eea15274a7d0a7899a617d0037aec48d7ab010 as build
ARG TARGETOS
ARG TARGETARCH
@ -8,7 +8,7 @@ WORKDIR /src
RUN make build
FROM docker.io/library/alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b
FROM docker.io/library/alpine:3.20@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>"
LABEL org.opencontainers.image.authors="Robert Kaussow <mail@thegeeklab.de>"

View File

@ -1,7 +1,7 @@
# renovate: datasource=github-releases depName=mvdan/gofumpt
GOFUMPT_PACKAGE_VERSION := v0.6.0
# renovate: datasource=github-releases depName=golangci/golangci-lint
GOLANGCI_LINT_PACKAGE_VERSION := v1.57.2
GOLANGCI_LINT_PACKAGE_VERSION := v1.59.0
EXECUTABLE := wp-github-comment
@ -18,8 +18,8 @@ GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@$(GOFUMPT_PACKAGE_VERSION)
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_PACKAGE_VERSION)
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GOTESTSUM_PACKAGE ?= gotest.tools/gotestsum@latest
MOCKERY_PACKAGE ?= github.com/vektra/mockery/v2@latest
GENERATE ?=
XGO_VERSION := go-1.22.x
XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64
@ -65,11 +65,8 @@ lint: golangci-lint
.PHONY: generate
generate:
$(GO) generate $(GENERATE)
.PHONY: generate-docs
generate-docs:
$(GO) generate ./cmd/$(EXECUTABLE)/flags.go
$(GO) generate $(PACKAGES)
$(GO) run $(MOCKERY_PACKAGE)
.PHONY: test
test:

View File

@ -1,58 +0,0 @@
//go:build generate
// +build generate
package main
import (
"bytes"
"embed"
"fmt"
"os"
"text/template"
"github.com/thegeeklab/wp-github-comment/plugin"
"github.com/thegeeklab/wp-plugin-go/docs"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
wp_template "github.com/thegeeklab/wp-plugin-go/template"
"github.com/urfave/cli/v2"
)
//go:embed templates/docs-data.yaml.tmpl
var yamlTemplate embed.FS
func main() {
settings := &plugin.Settings{}
app := &cli.App{
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
}
out, err := toYAML(app)
if err != nil {
panic(err)
}
fi, err := os.Create("../../docs/data/data-raw.yaml")
if err != nil {
panic(err)
}
defer fi.Close()
if _, err := fi.WriteString(out); err != nil {
panic(err)
}
}
func toYAML(app *cli.App) (string, error) {
var w bytes.Buffer
yamlTmpl, err := template.New("docs").Funcs(wp_template.LoadFuncMap()).ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl")
if err != nil {
fmt.Println(yamlTmpl)
return "", err
}
if err := yamlTmpl.ExecuteTemplate(&w, "docs-data.yaml.tmpl", docs.GetTemplateData(app)); err != nil {
return "", err
}
return w.String(), nil
}

View File

@ -1,61 +0,0 @@
package main
import (
"github.com/thegeeklab/wp-github-comment/plugin"
"github.com/urfave/cli/v2"
)
// settingsFlags has the cli.Flags for the plugin.Settings.
//
//go:generate go run docs.go flags.go
func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "api-key",
EnvVars: []string{"PLUGIN_API_KEY", "GITHUB_COMMENT_API_KEY"},
Usage: "personal access token to access the GitHub API",
Destination: &settings.APIKey,
Category: category,
Required: true,
},
&cli.StringFlag{
Name: "base-url",
EnvVars: []string{"PLUGIN_BASE_URL", "GITHUB_COMMENT_BASE_URL"},
Usage: "API URL",
Value: "https://api.github.com/",
Destination: &settings.BaseURL,
Category: category,
},
&cli.StringFlag{
Name: "key",
EnvVars: []string{"PLUGIN_KEY", "GITHUB_COMMENT_KEY"},
Usage: "unique identifier to assign to a comment",
Destination: &settings.Key,
Category: category,
},
&cli.StringFlag{
Name: "message",
EnvVars: []string{"PLUGIN_MESSAGE", "GITHUB_COMMENT_MESSAGE"},
Usage: "path to file or string that contains the comment text",
Destination: &settings.Message,
Category: category,
Required: true,
},
&cli.BoolFlag{
Name: "update",
EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"},
Usage: "enable update of an existing comment that matches the key",
Value: false,
Destination: &settings.Update,
Category: category,
},
&cli.BoolFlag{
Name: "skip-missing",
EnvVars: []string{"PLUGIN_SKIP_MISSING", "GITHUB_COMMENT_SKIP_MISSING"},
Usage: "skip comment creation if the given message file does not exist",
Value: false,
Destination: &settings.SkipMissing,
Category: category,
},
}
}

View File

@ -1,11 +1,7 @@
package main
import (
"fmt"
"github.com/thegeeklab/wp-github-comment/plugin"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
)
//nolint:gochecknoglobals
@ -15,14 +11,5 @@ var (
)
func main() {
settings := &plugin.Settings{}
options := wp.Options{
Name: "wp-github-comment",
Description: "Add comments to GitHub Issues and Pull Requests",
Version: BuildVersion,
VersionMetadata: fmt.Sprintf("date=%s", BuildDate),
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
}
plugin.New(options, settings).Run()
plugin.New(nil, BuildVersion, BuildDate).Run()
}

View File

@ -1,18 +0,0 @@
---
{{- if .GlobalArgs }}
properties:
{{- range $v := .GlobalArgs }}
- name: {{ $v.Name }}
{{- with $v.Description }}
description: |
{{ . | ToSentence }}
{{- end }}
{{- with $v.Type }}
type: {{ . }}
{{- end }}
{{- with $v.Default }}
defaultValue: {{ . }}
{{- end }}
required: {{ default false $v.Required }}
{{ end -}}
{{ end -}}

View File

@ -15,6 +15,13 @@ properties:
defaultValue: "https://api.github.com/"
required: false
- name: insecure_skip_verify
description: |
Skip SSL verification.
type: bool
defaultValue: false
required: false
- name: key
description: |
Unique identifier to assign to a comment.
@ -23,6 +30,13 @@ properties:
type: string
required: false
- name: log_level
description: |
Plugin log level.
type: string
defaultValue: "info"
required: false
- name: message
description: |
Path to file or string that contains the comment text.

41
github/api.go Normal file
View File

@ -0,0 +1,41 @@
package github
import (
"context"
"github.com/google/go-github/v62/github"
)
// APIClient is an interface that wraps the GitHub API client.
//
//nolint:lll
type IssueService interface {
CreateComment(ctx context.Context, owner, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
EditComment(ctx context.Context, owner, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
ListComments(ctx context.Context, owner, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error)
}
type IssueServiceImpl struct {
client *github.Client
}
// CreateComment wraps the CreateComment method of the github.IssuesService.
//
//nolint:lll
func (s *IssueServiceImpl) CreateComment(ctx context.Context, owner, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
return s.client.Issues.CreateComment(ctx, owner, repo, number, comment)
}
// EditComment wraps the EditComment method of the github.IssuesService.
//
//nolint:lll
func (s *IssueServiceImpl) EditComment(ctx context.Context, owner, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
return s.client.Issues.EditComment(ctx, owner, repo, commentID, comment)
}
// ListComments wraps the ListComments method of the github.IssuesService.
//
//nolint:lll
func (s *IssueServiceImpl) ListComments(ctx context.Context, owner, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error) {
return s.client.Issues.ListComments(ctx, owner, repo, number, opts)
}

117
github/github.go Normal file
View File

@ -0,0 +1,117 @@
package github
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/google/go-github/v62/github"
"golang.org/x/oauth2"
)
var ErrCommentNotFound = errors.New("comment not found")
type Client struct {
client *github.Client
Issue *Issue
}
type Issue struct {
client IssueService
Opt IssueOptions
}
type IssueOptions struct {
Number int
Message string
Key string
Repo string
Owner string
Update bool
}
// NewGitHubClient creates a new GitHubClient instance that wraps the provided GitHub API client.
// The GitHubClient provides a higher-level interface for interacting with the GitHub API,
// including methods for managing GitHub issues.
func NewClient(ctx context.Context, url *url.URL, token string, client *http.Client) *Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(
context.WithValue(ctx, oauth2.HTTPClient, client),
ts,
)
c := github.NewClient(tc)
c.BaseURL = url
return &Client{
client: c,
Issue: &Issue{
client: &IssueServiceImpl{client: c},
Opt: IssueOptions{},
},
}
}
// AddComment adds a new comment or updates an existing comment on a GitHub issue.
// If the Update field is true, it will append a unique identifier to the comment
// body and attempt to find and update the existing comment with that identifier.
// Otherwise, it will create a new comment on the issue.
func (i *Issue) AddComment(ctx context.Context) (*github.IssueComment, error) {
issueComment := &github.IssueComment{
Body: &i.Opt.Message,
}
if i.Opt.Update {
// Append plugin comment ID to comment message so we can search for it later
*issueComment.Body = fmt.Sprintf("%s\n<!-- id: %s -->\n", i.Opt.Message, i.Opt.Key)
comment, err := i.FindComment(ctx)
if err != nil && !errors.Is(err, ErrCommentNotFound) {
return nil, err
}
if comment != nil {
comment, _, err = i.client.EditComment(ctx, i.Opt.Owner, i.Opt.Repo, *comment.ID, issueComment)
return comment, err
}
}
comment, _, err := i.client.CreateComment(ctx, i.Opt.Owner, i.Opt.Repo, i.Opt.Number, issueComment)
return comment, err
}
// FindComment returns the GitHub issue comment that contains the specified key, or nil if no such comment exists.
// It retrieves all comments on the issue and searches for one that contains the specified key in the comment body.
func (i *Issue) FindComment(ctx context.Context) (*github.IssueComment, error) {
var allComments []*github.IssueComment
opts := &github.IssueListCommentsOptions{}
for {
comments, resp, err := i.client.ListComments(ctx, i.Opt.Owner, i.Opt.Repo, i.Opt.Number, opts)
if err != nil {
return nil, err
}
allComments = append(allComments, comments...)
if resp == nil || resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, comment := range allComments {
if strings.Contains(*comment.Body, fmt.Sprintf("<!-- id: %s -->", i.Opt.Key)) {
return comment, nil
}
}
return nil, fmt.Errorf("%w: failed to find comment with key %s", ErrCommentNotFound, i.Opt.Key)
}

212
github/github_test.go Normal file
View File

@ -0,0 +1,212 @@
package github
import (
"context"
"errors"
"fmt"
"testing"
"github.com/google/go-github/v62/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/thegeeklab/wp-github-comment/github/mocks"
)
var ErrInternalServerError = errors.New("internal server error")
func TestGithubIssue_FindComment(t *testing.T) {
tests := []struct {
name string
issueOpt IssueOptions
comments []*github.IssueComment
want *github.IssueComment
wantErr error
}{
{
name: "no comments",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
},
wantErr: ErrCommentNotFound,
},
{
name: "comment found",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
},
comments: []*github.IssueComment{
{Body: github.String("<!-- id: test-key -->\ntest comment\n")},
},
want: &github.IssueComment{
Body: github.String("<!-- id: test-key -->\ntest comment\n"),
},
},
{
name: "comment not found",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
},
comments: []*github.IssueComment{
{Body: github.String("other comment")},
},
wantErr: ErrCommentNotFound,
},
{
name: "multiple comments",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
},
comments: []*github.IssueComment{
{Body: github.String("other comment")},
{Body: github.String("<!-- id: test-key -->\ntest comment\n")},
{Body: github.String("another comment")},
},
want: &github.IssueComment{Body: github.String("<!-- id: test-key -->\ntest comment\n")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := mocks.NewMockIssueService(t)
issue := &Issue{
client: mockClient,
Opt: tt.issueOpt,
}
mockClient.
On("ListComments", mock.Anything, tt.issueOpt.Owner, tt.issueOpt.Repo, mock.Anything, mock.Anything).
Return(tt.comments, nil, nil)
got, err := issue.FindComment(context.Background())
if tt.wantErr != nil {
assert.Error(t, err)
assert.Equal(t, tt.want, got)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestGithubIssue_AddComment(t *testing.T) {
tests := []struct {
name string
issueOpt IssueOptions
comments []*github.IssueComment
want *github.IssueComment
wantErr error
}{
{
name: "create new comment",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
Message: "test message",
Update: false,
},
want: &github.IssueComment{
Body: github.String("<!-- id: test-key -->\ntest message\n"),
},
},
{
name: "update existing comment",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
Message: "test message",
Update: true,
},
comments: []*github.IssueComment{
{ID: github.Int64(123), Body: github.String("<!-- id: test-key -->\ntest message\n")},
},
want: &github.IssueComment{
Body: github.String("<!-- id: test-key -->\ntest message\n"),
},
},
{
name: "update non-existing comment",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
Message: "test message",
Update: true,
},
want: &github.IssueComment{
Body: github.String("<!-- id: test-key -->\ntest message\n"),
},
},
{
name: "create new comment with error",
issueOpt: IssueOptions{
Key: "test-key",
Owner: "test-owner",
Repo: "test-repo",
Message: "test message",
Update: false,
},
wantErr: ErrInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := mocks.NewMockIssueService(t)
issue := &Issue{
client: mockClient,
Opt: tt.issueOpt,
}
if tt.issueOpt.Update {
mockClient.
On("ListComments", mock.Anything, tt.issueOpt.Owner, tt.issueOpt.Repo, mock.Anything, mock.Anything).
Return(tt.comments, nil, nil)
}
if tt.issueOpt.Update && tt.comments != nil {
mockClient.
On("EditComment", mock.Anything, tt.issueOpt.Owner, tt.issueOpt.Repo, mock.Anything, mock.Anything).
Return(&github.IssueComment{
Body: github.String(fmt.Sprintf("<!-- id: %s -->\n%s\n", tt.issueOpt.Key, tt.issueOpt.Message)),
}, nil, nil)
}
if tt.comments == nil {
var comment *github.IssueComment
if tt.wantErr == nil {
comment = &github.IssueComment{
Body: github.String(fmt.Sprintf("<!-- id: %s -->\n%s\n", tt.issueOpt.Key, tt.issueOpt.Message)),
}
}
mockClient.
On("CreateComment", mock.Anything, tt.issueOpt.Owner, tt.issueOpt.Repo, mock.Anything, mock.Anything).
Return(comment, nil, tt.wantErr)
}
got, err := issue.AddComment(context.Background())
if tt.wantErr != nil {
assert.Error(t, err)
assert.Equal(t, tt.want, got)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}

View File

@ -0,0 +1,250 @@
// Code generated by mockery v2.43.0. DO NOT EDIT.
package mocks
import (
context "context"
github "github.com/google/go-github/v62/github"
mock "github.com/stretchr/testify/mock"
)
// MockIssueService is an autogenerated mock type for the IssueService type
type MockIssueService struct {
mock.Mock
}
type MockIssueService_Expecter struct {
mock *mock.Mock
}
func (_m *MockIssueService) EXPECT() *MockIssueService_Expecter {
return &MockIssueService_Expecter{mock: &_m.Mock}
}
// CreateComment provides a mock function with given fields: ctx, owner, repo, number, comment
func (_m *MockIssueService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
ret := _m.Called(ctx, owner, repo, number, comment)
if len(ret) == 0 {
panic("no return value specified for CreateComment")
}
var r0 *github.IssueComment
var r1 *github.Response
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, string, string, int, *github.IssueComment) (*github.IssueComment, *github.Response, error)); ok {
return rf(ctx, owner, repo, number, comment)
}
if rf, ok := ret.Get(0).(func(context.Context, string, string, int, *github.IssueComment) *github.IssueComment); ok {
r0 = rf(ctx, owner, repo, number, comment)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*github.IssueComment)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, string, int, *github.IssueComment) *github.Response); ok {
r1 = rf(ctx, owner, repo, number, comment)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*github.Response)
}
}
if rf, ok := ret.Get(2).(func(context.Context, string, string, int, *github.IssueComment) error); ok {
r2 = rf(ctx, owner, repo, number, comment)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// MockIssueService_CreateComment_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreateComment'
type MockIssueService_CreateComment_Call struct {
*mock.Call
}
// CreateComment is a helper method to define mock.On call
// - ctx context.Context
// - owner string
// - repo string
// - number int
// - comment *github.IssueComment
func (_e *MockIssueService_Expecter) CreateComment(ctx interface{}, owner interface{}, repo interface{}, number interface{}, comment interface{}) *MockIssueService_CreateComment_Call {
return &MockIssueService_CreateComment_Call{Call: _e.mock.On("CreateComment", ctx, owner, repo, number, comment)}
}
func (_c *MockIssueService_CreateComment_Call) Run(run func(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment)) *MockIssueService_CreateComment_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(int), args[4].(*github.IssueComment))
})
return _c
}
func (_c *MockIssueService_CreateComment_Call) Return(_a0 *github.IssueComment, _a1 *github.Response, _a2 error) *MockIssueService_CreateComment_Call {
_c.Call.Return(_a0, _a1, _a2)
return _c
}
func (_c *MockIssueService_CreateComment_Call) RunAndReturn(run func(context.Context, string, string, int, *github.IssueComment) (*github.IssueComment, *github.Response, error)) *MockIssueService_CreateComment_Call {
_c.Call.Return(run)
return _c
}
// EditComment provides a mock function with given fields: ctx, owner, repo, commentID, comment
func (_m *MockIssueService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
ret := _m.Called(ctx, owner, repo, commentID, comment)
if len(ret) == 0 {
panic("no return value specified for EditComment")
}
var r0 *github.IssueComment
var r1 *github.Response
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, string, string, int64, *github.IssueComment) (*github.IssueComment, *github.Response, error)); ok {
return rf(ctx, owner, repo, commentID, comment)
}
if rf, ok := ret.Get(0).(func(context.Context, string, string, int64, *github.IssueComment) *github.IssueComment); ok {
r0 = rf(ctx, owner, repo, commentID, comment)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*github.IssueComment)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, string, int64, *github.IssueComment) *github.Response); ok {
r1 = rf(ctx, owner, repo, commentID, comment)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*github.Response)
}
}
if rf, ok := ret.Get(2).(func(context.Context, string, string, int64, *github.IssueComment) error); ok {
r2 = rf(ctx, owner, repo, commentID, comment)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// MockIssueService_EditComment_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'EditComment'
type MockIssueService_EditComment_Call struct {
*mock.Call
}
// EditComment is a helper method to define mock.On call
// - ctx context.Context
// - owner string
// - repo string
// - commentID int64
// - comment *github.IssueComment
func (_e *MockIssueService_Expecter) EditComment(ctx interface{}, owner interface{}, repo interface{}, commentID interface{}, comment interface{}) *MockIssueService_EditComment_Call {
return &MockIssueService_EditComment_Call{Call: _e.mock.On("EditComment", ctx, owner, repo, commentID, comment)}
}
func (_c *MockIssueService_EditComment_Call) Run(run func(ctx context.Context, owner string, repo string, commentID int64, comment *github.IssueComment)) *MockIssueService_EditComment_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(int64), args[4].(*github.IssueComment))
})
return _c
}
func (_c *MockIssueService_EditComment_Call) Return(_a0 *github.IssueComment, _a1 *github.Response, _a2 error) *MockIssueService_EditComment_Call {
_c.Call.Return(_a0, _a1, _a2)
return _c
}
func (_c *MockIssueService_EditComment_Call) RunAndReturn(run func(context.Context, string, string, int64, *github.IssueComment) (*github.IssueComment, *github.Response, error)) *MockIssueService_EditComment_Call {
_c.Call.Return(run)
return _c
}
// ListComments provides a mock function with given fields: ctx, owner, repo, number, opts
func (_m *MockIssueService) ListComments(ctx context.Context, owner string, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error) {
ret := _m.Called(ctx, owner, repo, number, opts)
if len(ret) == 0 {
panic("no return value specified for ListComments")
}
var r0 []*github.IssueComment
var r1 *github.Response
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, string, string, int, *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error)); ok {
return rf(ctx, owner, repo, number, opts)
}
if rf, ok := ret.Get(0).(func(context.Context, string, string, int, *github.IssueListCommentsOptions) []*github.IssueComment); ok {
r0 = rf(ctx, owner, repo, number, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*github.IssueComment)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, string, int, *github.IssueListCommentsOptions) *github.Response); ok {
r1 = rf(ctx, owner, repo, number, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*github.Response)
}
}
if rf, ok := ret.Get(2).(func(context.Context, string, string, int, *github.IssueListCommentsOptions) error); ok {
r2 = rf(ctx, owner, repo, number, opts)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// MockIssueService_ListComments_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListComments'
type MockIssueService_ListComments_Call struct {
*mock.Call
}
// ListComments is a helper method to define mock.On call
// - ctx context.Context
// - owner string
// - repo string
// - number int
// - opts *github.IssueListCommentsOptions
func (_e *MockIssueService_Expecter) ListComments(ctx interface{}, owner interface{}, repo interface{}, number interface{}, opts interface{}) *MockIssueService_ListComments_Call {
return &MockIssueService_ListComments_Call{Call: _e.mock.On("ListComments", ctx, owner, repo, number, opts)}
}
func (_c *MockIssueService_ListComments_Call) Run(run func(ctx context.Context, owner string, repo string, number int, opts *github.IssueListCommentsOptions)) *MockIssueService_ListComments_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(int), args[4].(*github.IssueListCommentsOptions))
})
return _c
}
func (_c *MockIssueService_ListComments_Call) Return(_a0 []*github.IssueComment, _a1 *github.Response, _a2 error) *MockIssueService_ListComments_Call {
_c.Call.Return(_a0, _a1, _a2)
return _c
}
func (_c *MockIssueService_ListComments_Call) RunAndReturn(run func(context.Context, string, string, int, *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error)) *MockIssueService_ListComments_Call {
_c.Call.Return(run)
return _c
}
// NewMockIssueService creates a new instance of MockIssueService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockIssueService(t interface {
mock.TestingT
Cleanup(func())
}) *MockIssueService {
mock := &MockIssueService{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

25
go.mod
View File

@ -3,18 +3,20 @@ module github.com/thegeeklab/wp-github-comment
go 1.22
require (
github.com/google/go-github/v61 v61.0.0
github.com/rs/zerolog v1.32.0
github.com/thegeeklab/wp-plugin-go v1.7.1
github.com/urfave/cli/v2 v2.27.1
golang.org/x/oauth2 v0.19.0
github.com/google/go-github/v62 v62.0.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2
github.com/urfave/cli/v2 v2.27.2
golang.org/x/oauth2 v0.20.0
)
require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
@ -24,11 +26,14 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

43
go.sum
View File

@ -6,8 +6,8 @@ github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -15,8 +15,8 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5pNlu1go=
github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY=
github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwMmHdhl4=
github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
@ -40,8 +40,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
@ -49,31 +49,33 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/thegeeklab/wp-plugin-go v1.7.1 h1:zfR/rfNPuyVhXJu1fsLfp4+Mz2pTf6WwW/mIqw9750I=
github.com/thegeeklab/wp-plugin-go v1.7.1/go.mod h1:Ixi5plt9tpFGTu6yc/Inm5DcDpp3xPTeohfr86gf2EU=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g=
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg=
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo=
golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -85,8 +87,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
@ -99,6 +101,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=

42
internal/doc/main.go Normal file
View File

@ -0,0 +1,42 @@
//go:build generate
// +build generate
package main
import (
"context"
"flag"
"net/http"
"os"
"time"
"github.com/thegeeklab/wp-github-comment/plugin"
plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
)
func main() {
tmpl := "https://raw.githubusercontent.com/thegeeklab/woodpecker-plugins/main/templates/docs-data.yaml.tmpl"
client := http.Client{
Timeout: 30 * time.Second,
}
p := plugin.New(nil)
out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App))
if err != nil {
panic(err)
}
outputFile := flag.String("output", "", "Output file path")
flag.Parse()
if *outputFile == "" {
panic("no output file specified")
}
err = os.WriteFile(*outputFile, []byte(out), 0o644)
if err != nil {
panic(err)
}
}

View File

@ -1,84 +0,0 @@
package plugin
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/v61/github"
)
type commentClient struct {
Message string
Update bool
Key string
Repo string
Owner string
IssueNum int
*github.Client
}
func (cc *commentClient) issueComment(ctx context.Context) error {
var (
err error
comment *github.IssueComment
resp *github.Response
)
issueComment := &github.IssueComment{
Body: &cc.Message,
}
if cc.Update {
// Append plugin comment ID to comment message so we can search for it later
message := fmt.Sprintf("%s\n<!-- id: %s -->\n", cc.Message, cc.Key)
issueComment.Body = &message
comment, err = cc.comment(ctx)
if err == nil && comment != nil {
_, resp, err = cc.Client.Issues.EditComment(ctx, cc.Owner, cc.Repo, *comment.ID, issueComment)
}
}
if err == nil && resp == nil {
_, _, err = cc.Client.Issues.CreateComment(ctx, cc.Owner, cc.Repo, cc.IssueNum, issueComment)
}
if err != nil {
return err
}
return nil
}
func (cc *commentClient) comment(ctx context.Context) (*github.IssueComment, error) {
var allComments []*github.IssueComment
opts := &github.IssueListCommentsOptions{}
for {
comments, resp, err := cc.Client.Issues.ListComments(ctx, cc.Owner, cc.Repo, cc.IssueNum, opts)
if err != nil {
return nil, err
}
allComments = append(allComments, comments...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, comment := range allComments {
if strings.Contains(*comment.Body, fmt.Sprintf("<!-- id: %s -->", cc.Key)) {
return comment, nil
}
}
//nolint:nilnil
return nil, nil
}

View File

@ -8,10 +8,9 @@ import (
"net/url"
"strings"
"github.com/google/go-github/v61/github"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/file"
"golang.org/x/oauth2"
gh "github.com/thegeeklab/wp-github-comment/github"
plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file"
)
var ErrPluginEventNotSupported = errors.New("event not supported")
@ -22,7 +21,6 @@ func (p *Plugin) run(ctx context.Context) error {
return fmt.Errorf("validation failed: %w", err)
}
//nolint:contextcheck
if err := p.Execute(); err != nil {
return fmt.Errorf("execution failed: %w", err)
}
@ -39,7 +37,7 @@ func (p *Plugin) Validate() error {
}
if p.Settings.Message != "" {
if p.Settings.Message, p.Settings.IsFile, err = file.ReadStringOrFile(p.Settings.Message); err != nil {
if p.Settings.Message, p.Settings.IsFile, err = plugin_file.ReadStringOrFile(p.Settings.Message); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Message, err)
}
}
@ -59,7 +57,7 @@ func (p *Plugin) Validate() error {
p.Settings.Key = fmt.Sprintf("%x", hash)
}
if p.Settings.Key, _, err = file.ReadStringOrFile(p.Settings.Key); err != nil {
if p.Settings.Key, _, err = plugin_file.ReadStringOrFile(p.Settings.Key); err != nil {
return fmt.Errorf("error while reading %s: %w", p.Settings.Key, err)
}
@ -68,23 +66,14 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: p.Settings.APIKey})
tc := oauth2.NewClient(
context.WithValue(p.Network.Context, oauth2.HTTPClient, p.Network.Client),
ts,
)
client := github.NewClient(tc)
client.BaseURL = p.Settings.baseURL
commentClient := commentClient{
Client: client,
Repo: p.Metadata.Repository.Name,
Owner: p.Metadata.Repository.Owner,
Message: p.Settings.Message,
Update: p.Settings.Update,
Key: p.Settings.Key,
IssueNum: p.Metadata.Curr.PullRequest,
client := gh.NewClient(p.Network.Context, p.Settings.baseURL, p.Settings.APIKey, p.Network.Client)
client.Issue.Opt = gh.IssueOptions{
Repo: p.Metadata.Repository.Name,
Owner: p.Metadata.Repository.Owner,
Message: p.Settings.Message,
Update: p.Settings.Update,
Key: p.Settings.Key,
Number: p.Metadata.Curr.PullRequest,
}
if p.Settings.SkipMissing && !p.Settings.IsFile {
@ -94,7 +83,7 @@ func (p *Plugin) Execute() error {
return nil
}
err := commentClient.issueComment(p.Network.Context)
_, err := client.Issue.AddComment(p.Network.Context)
if err != nil {
return fmt.Errorf("failed to create or update comment: %w", err)
}

View File

@ -1,14 +1,18 @@
package plugin
import (
"fmt"
"net/url"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
"github.com/urfave/cli/v2"
)
//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml
// Plugin implements provide the plugin.
type Plugin struct {
*wp.Plugin
*plugin_base.Plugin
Settings *Settings
}
@ -26,13 +30,85 @@ type Settings struct {
baseURL *url.URL
}
func New(options wp.Options, settings *Settings) *Plugin {
p := &Plugin{}
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
p := &Plugin{
Settings: &Settings{},
}
options.Execute = p.run
options := plugin_base.Options{
Name: "wp-github-comment",
Description: "Add comments to GitHub Issues and Pull Requests",
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
Execute: p.run,
HideWoodpeckerFlags: true,
}
p.Plugin = wp.New(options)
p.Settings = settings
if len(build) > 0 {
options.Version = build[0]
}
if len(build) > 1 {
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
}
if e != nil {
options.Execute = e
}
p.Plugin = plugin_base.New(options)
return p
}
// Flags returns a slice of CLI flags for the plugin.
func Flags(settings *Settings, category string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "api-key",
EnvVars: []string{"PLUGIN_API_KEY", "GITHUB_COMMENT_API_KEY"},
Usage: "personal access token to access the GitHub API",
Destination: &settings.APIKey,
Category: category,
Required: true,
},
&cli.StringFlag{
Name: "base-url",
EnvVars: []string{"PLUGIN_BASE_URL", "GITHUB_COMMENT_BASE_URL"},
Usage: "API URL",
Value: "https://api.github.com/",
Destination: &settings.BaseURL,
Category: category,
},
&cli.StringFlag{
Name: "key",
EnvVars: []string{"PLUGIN_KEY", "GITHUB_COMMENT_KEY"},
Usage: "unique identifier to assign to a comment",
Destination: &settings.Key,
Category: category,
},
&cli.StringFlag{
Name: "message",
EnvVars: []string{"PLUGIN_MESSAGE", "GITHUB_COMMENT_MESSAGE"},
Usage: "path to file or string that contains the comment text",
Destination: &settings.Message,
Category: category,
Required: true,
},
&cli.BoolFlag{
Name: "update",
EnvVars: []string{"PLUGIN_UPDATE", "GITHUB_COMMENT_UPDATE"},
Usage: "enable update of an existing comment that matches the key",
Value: false,
Destination: &settings.Update,
Category: category,
},
&cli.BoolFlag{
Name: "skip-missing",
EnvVars: []string{"PLUGIN_SKIP_MISSING", "GITHUB_COMMENT_SKIP_MISSING"},
Usage: "skip comment creation if the given message file does not exist",
Value: false,
Destination: &settings.SkipMissing,
Category: category,
},
}
}