WIP: Pass SDK init callback into Gitea API client
Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
parent
b6b9087651
commit
2cbf823b10
|
@ -16,6 +16,7 @@ import (
|
|||
sonarQubeSdk "gitea-sonarqube-bot/internal/clients/sonarqube"
|
||||
"gitea-sonarqube-bot/internal/settings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
|
@ -61,8 +62,8 @@ func serveApi(c *cli.Context) error {
|
|||
log.Println("Hi! I'm Gitea SonarQube Bot. At your service.")
|
||||
log.Println("Config file in use:", config)
|
||||
|
||||
giteaHandler := api.NewGiteaWebhookHandler(giteaSdk.New(), sonarQubeSdk.New(&settings.SonarQube))
|
||||
sqHandler := api.NewSonarQubeWebhookHandler(giteaSdk.New(), sonarQubeSdk.New(&settings.SonarQube))
|
||||
giteaHandler := api.NewGiteaWebhookHandler(giteaSdk.New(&settings.Gitea, gitea.NewClient), sonarQubeSdk.New(&settings.SonarQube))
|
||||
sqHandler := api.NewSonarQubeWebhookHandler(giteaSdk.New(&settings.Gitea, gitea.NewClient), sonarQubeSdk.New(&settings.SonarQube))
|
||||
server := api.New(giteaHandler, sqHandler)
|
||||
|
||||
srv := &http.Server{
|
||||
|
|
|
@ -2,9 +2,10 @@ package gitea
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea-sonarqube-bot/internal/settings"
|
||||
"log"
|
||||
|
||||
"gitea-sonarqube-bot/internal/settings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
|
@ -14,8 +15,14 @@ type GiteaSdkInterface interface {
|
|||
DetermineHEAD(settings.GiteaRepository, int64) (string, error)
|
||||
}
|
||||
|
||||
type ClientInterface interface {
|
||||
CreateIssueComment(owner, repo string, index int64, opt gitea.CreateIssueCommentOption) (*gitea.Comment, *gitea.Response, error)
|
||||
CreateStatus(owner, repo, sha string, opts gitea.CreateStatusOption) (*gitea.Status, *gitea.Response, error)
|
||||
GetPullRequest(owner, repo string, index int64) (*gitea.PullRequest, *gitea.Response, error)
|
||||
}
|
||||
|
||||
type GiteaSdk struct {
|
||||
client *gitea.Client
|
||||
client ClientInterface
|
||||
}
|
||||
|
||||
func (sdk *GiteaSdk) PostComment(repo settings.GiteaRepository, idx int, msg string) error {
|
||||
|
@ -53,8 +60,8 @@ func (sdk *GiteaSdk) DetermineHEAD(repo settings.GiteaRepository, idx int64) (st
|
|||
return pr.Head.Sha, nil
|
||||
}
|
||||
|
||||
func New() *GiteaSdk {
|
||||
client, err := gitea.NewClient(settings.Gitea.Url, gitea.SetToken(settings.Gitea.Token.Value))
|
||||
func New[T ClientInterface](configuration *settings.GiteaConfig, newClient func(url string, options ...gitea.ClientOption) (T, error)) *GiteaSdk {
|
||||
client, err := newClient(configuration.Url, gitea.SetToken(configuration.Token.Value))
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot initialize Gitea client: %w", err))
|
||||
}
|
||||
|
|
37
internal/clients/gitea/gitea_test.go
Normal file
37
internal/clients/gitea/gitea_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea-sonarqube-bot/internal/settings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SdkMock struct {
|
||||
}
|
||||
|
||||
func (m *SdkMock) CreateIssueComment(owner, repo string, index int64, opt gitea.CreateIssueCommentOption) (*gitea.Comment, *gitea.Response, error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
func (m *SdkMock) CreateStatus(owner, repo, sha string, opts gitea.CreateStatusOption) (*gitea.Status, *gitea.Response, error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
func (m *SdkMock) GetPullRequest(owner, repo string, index int64) (*gitea.PullRequest, *gitea.Response, error) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
config := &settings.GiteaConfig{
|
||||
Url: "http://example.com",
|
||||
Token: &settings.Token{
|
||||
Value: "test-token",
|
||||
},
|
||||
}
|
||||
|
||||
callback := func(url string, options ...gitea.ClientOption) (*SdkMock, error) {
|
||||
return &SdkMock{}, nil
|
||||
}
|
||||
assert.IsType(t, &GiteaSdk{}, New(config, callback), "")
|
||||
}
|
14
internal/clients/gitea/main_test.go
Normal file
14
internal/clients/gitea/main_test.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// SETUP: mute logs
|
||||
func TestMain(m *testing.M) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
os.Exit(m.Run())
|
||||
}
|
Loading…
Reference in a new issue