Skip to content

Commit

Permalink
refactor repo clone logic to support multi platform and authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Nov 22, 2024
1 parent 6af266d commit a31b6d5
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 150 deletions.
438 changes: 438 additions & 0 deletions clone.go

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,11 @@ func NewConfig(conf string) (Config, error) {
}

// TODO(CarlJi): do we need to check the format of the copy ssh key here?
log.Debugf("config %+v", c)

return c, nil
}

func (c Config) GetLinterConfig(org, repo, ln string, repoType RepoType) Linter {
func (c Config) GetLinterConfig(org, repo, ln string, repoType Platform) Linter {
linter := Linter{
Enable: boolPtr(true),
Modifier: NewBaseModifier(),
Expand Down Expand Up @@ -434,11 +433,11 @@ const (
Quiet ReportType = "quiet"
)

type RepoType string
type Platform string

const (
GitLab RepoType = "gitlab"
GitHub RepoType = "github"
GitLab Platform = "GitLab"
GitHub Platform = "GitHub"
)

func boolPtr(b bool) *bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/linters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestFilters(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
p, err := NewGithubProvider(nil, nil, tc.a, github.PullRequestEvent{})
p, err := NewGithubProvider(nil, tc.a, github.PullRequestEvent{})
if err != nil {
t.Errorf("failed to create github provider: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linters/go/golangci_lint/golangci_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestFindGoModsPaths(t *testing.T) {
defer file.Close()
}
}
p, err := linters.NewGithubProvider(nil, nil, tc.changedFiles, github.PullRequestEvent{})
p, err := linters.NewGithubProvider(nil, tc.changedFiles, github.PullRequestEvent{})
if err != nil {
t.Errorf("Error creating github provider: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linters/go/gomodcheck/gomodcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestGoModCheck(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.id, func(t *testing.T) {
p, err := linters.NewGithubProvider(nil, nil, tc.input, github.PullRequestEvent{})
p, err := linters.NewGithubProvider(nil, tc.input, github.PullRequestEvent{})
if err != nil {
t.Errorf("Error creating github provider: %v", err)
return
Expand Down
7 changes: 1 addition & 6 deletions internal/linters/providergithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/qiniu/reviewbot/internal/lintersutil"
"github.com/qiniu/reviewbot/internal/metric"
"github.com/qiniu/x/log"
gitv2 "sigs.k8s.io/prow/pkg/git/v2"
)

var (
Expand Down Expand Up @@ -225,9 +224,6 @@ var _ Provider = (*GithubProvider)(nil)
type GithubProvider struct {
// GitHubClient is the GitHub client.
GithubClient *github.Client
// GitClient is the Git client factory.
GitClient gitv2.ClientFactory

// HunkChecker is the hunk checker for the file.
HunkChecker *FileHunkChecker

Expand All @@ -237,14 +233,13 @@ type GithubProvider struct {
PullRequestEvent github.PullRequestEvent
}

func NewGithubProvider(githubClient *github.Client, gitClient gitv2.ClientFactory, pullRequestChangedFiles []*github.CommitFile, pullRequestEvent github.PullRequestEvent) (*GithubProvider, error) {
func NewGithubProvider(githubClient *github.Client, pullRequestChangedFiles []*github.CommitFile, pullRequestEvent github.PullRequestEvent) (*GithubProvider, error) {
checker, err := newGithubHunkChecker(pullRequestChangedFiles)
if err != nil {
return nil, err
}
return &GithubProvider{
GithubClient: githubClient,
GitClient: gitClient,
PullRequestChangedFiles: pullRequestChangedFiles,
PullRequestEvent: pullRequestEvent,
HunkChecker: checker,
Expand Down
6 changes: 1 addition & 5 deletions internal/linters/providergitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/qiniu/x/errors"
"github.com/qiniu/x/log"
gitlab "github.com/xanzy/go-gitlab"
gitv2 "sigs.k8s.io/prow/pkg/git/v2"
)

var (
Expand Down Expand Up @@ -96,8 +95,6 @@ var _ Provider = (*GitlabProvider)(nil)
type GitlabProvider struct {
// GitHubClient is the GitHub client.
GitLabClient *gitlab.Client
// GitClient is the Git client factory.
GitClient gitv2.ClientFactory

// HunkChecker is the hunk checker for the file.
HunkChecker *FileHunkChecker
Expand Down Expand Up @@ -177,7 +174,7 @@ func (g *GitlabProvider) GetCodeReviewInfo() CodeReview {
}
}

func NewGitlabProvider(gitlabClient *gitlab.Client, gitClient gitv2.ClientFactory, mergeRequestChangedFiles []*gitlab.MergeRequestDiff, mergeRequestEvent gitlab.MergeEvent) (*GitlabProvider, error) {
func NewGitlabProvider(gitlabClient *gitlab.Client, mergeRequestChangedFiles []*gitlab.MergeRequestDiff, mergeRequestEvent gitlab.MergeEvent) (*GitlabProvider, error) {
checker, err := newGitlabHunkChecker(mergeRequestChangedFiles)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
Expand All @@ -187,7 +184,6 @@ func NewGitlabProvider(gitlabClient *gitlab.Client, gitClient gitv2.ClientFactor
}
return &GitlabProvider{
GitLabClient: gitlabClient,
GitClient: gitClient,
MergeRequestChangedFiles: mergeRequestChangedFiles,
MergeRequestEvent: mergeRequestEvent,
HunkChecker: checker,
Expand Down
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type options struct {
gitHubAppID int64
gitHubAppInstallationID int64
gitHubAppPrivateKey string
gitHubOAuthToken string

// log storage dir for local storage
logDir string
Expand Down Expand Up @@ -116,10 +117,10 @@ func gatherOptions() options {
fs.Int64Var(&o.gitHubAppID, "github.app-id", 0, "github app id")
fs.Int64Var(&o.gitHubAppInstallationID, "github.app-installation-id", 0, "github app installation id")
fs.StringVar(&o.gitHubAppPrivateKey, "github.app-private-key", "", "github app private key")

fs.StringVar(&o.gitHubOAuthToken, "github.oauth-token", "", "github oauth token")
// gitlab related
fs.StringVar(&o.gitLabAccessToken, "gitlab.access-token", "", "personal gitlab access token")
fs.StringVar(&o.gitLabHost, "gitlab.host", "gitlab.com", "gitlab server")
fs.StringVar(&o.gitLabHost, "gitlab.host", "https://gitlab.com", "gitlab server")

err := fs.Parse(os.Args[1:])
if err != nil {
Expand Down Expand Up @@ -237,15 +238,24 @@ func main() {
webhookSecret: []byte(o.webhookSecret),
gitClientFactory: v2,
config: cfg,
gitHubAccessToken: o.gitHubAccessToken,
gitLabAccessToken: o.gitLabAccessToken,
appID: o.gitHubAppID,
appPrivateKey: o.gitHubAppPrivateKey,
debug: o.debug,
serverAddr: o.serverAddr,
repoCacheDir: o.codeCacheDir,
kubeConfig: o.kubeConfig,
gitLabHost: o.gitLabHost,
gitLabAccessToken: o.gitLabAccessToken,
gitHubAccessToken: o.gitHubAccessToken,
gitHubOAuthToken: o.gitHubOAuthToken,
}

s.gitHubAppAuth = &GitHubAppAuth{
AppID: o.gitHubAppID,
InstallationID: o.gitHubAppInstallationID,
PrivateKeyPath: o.gitHubAppPrivateKey,
}

if s.gitHubAppAuth != nil {
s.githubAppTokenCache = newGitHubAppTokenCache(s.gitHubAppAuth.AppID, s.gitHubAppAuth.PrivateKeyPath)
}

go s.initDockerRunner()
Expand Down
Loading

0 comments on commit a31b6d5

Please sign in to comment.