Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use GitHub GraphQL API #815

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/invopop/jsonschema v0.4.0
github.com/ktr0731/go-fuzzyfinder v0.6.0
github.com/mholt/archiver/v3 v3.5.1
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00
github.com/shurcooL/graphql v0.0.0-20220520033453-bdb1221e171e // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.8.2
github.com/suzuki-shunsuke/flute v1.0.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ github.com/sanity-io/litter v1.2.0/go.mod h1:JF6pZUFgu2Q0sBZ+HSV35P8TVPI1TTzEwyu
github.com/scylladb/go-set v1.0.2/go.mod h1:DkpGd78rljTxKAnTDPFqXSGxvETQnJyuSOQwsHycqfs=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00 h1:fiFvD4lT0aWjuuAb64LlZ/67v87m+Kc9Qsu5cMFNK0w=
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/graphql v0.0.0-20220520033453-bdb1221e171e h1:dmM59/+RIH6bO/gjmUgaJwdyDhAvZkHgA5OJUcoUyGU=
github.com/shurcooL/graphql v0.0.0-20220520033453-bdb1221e171e/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"errors"
"fmt"
"net/http"
"path/filepath"

"github.com/aquaproj/aqua/pkg/config"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (runner *Runner) execAction(c *cli.Context) error {
if err := runner.setParam(c, param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeExecCommandController(c.Context, param, http.DefaultClient, runner.Runtime)
ctrl := controller.InitializeExecCommandController(c.Context, param, runner.Runtime)
exeName, args, err := parseExecArgs(c.Args().Slice())
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"net/http"

"github.com/aquaproj/aqua/pkg/config"
"github.com/aquaproj/aqua/pkg/controller"
Expand Down Expand Up @@ -106,6 +105,6 @@ func (runner *Runner) generateAction(c *cli.Context) error {
if err := runner.setParam(c, param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeGenerateCommandController(c.Context, param, http.DefaultClient)
ctrl := controller.InitializeGenerateCommandController(c.Context, param)
return ctrl.Generate(c.Context, runner.LogE, param, c.Args().Slice()...) //nolint:wrapcheck
}
3 changes: 1 addition & 2 deletions pkg/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"net/http"

"github.com/aquaproj/aqua/pkg/config"
"github.com/aquaproj/aqua/pkg/controller"
Expand Down Expand Up @@ -54,6 +53,6 @@ func (runner *Runner) installAction(c *cli.Context) error {
if err := runner.setParam(c, param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeInstallCommandController(c.Context, param, http.DefaultClient, runner.Runtime)
ctrl := controller.InitializeInstallCommandController(c.Context, param, runner.Runtime)
return ctrl.Install(c.Context, param, runner.LogE) //nolint:wrapcheck
}
3 changes: 1 addition & 2 deletions pkg/cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"net/http"

"github.com/aquaproj/aqua/pkg/config"
"github.com/aquaproj/aqua/pkg/controller"
Expand Down Expand Up @@ -32,6 +31,6 @@ func (runner *Runner) listAction(c *cli.Context) error {
if err := runner.setParam(c, param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeListCommandController(c.Context, param, http.DefaultClient)
ctrl := controller.InitializeListCommandController(c.Context, param)
return ctrl.List(c.Context, param, runner.LogE) //nolint:wrapcheck
}
3 changes: 1 addition & 2 deletions pkg/cli/which.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"net/http"
"os"

"github.com/aquaproj/aqua/pkg/config"
Expand Down Expand Up @@ -39,7 +38,7 @@ func (runner *Runner) whichAction(c *cli.Context) error {
if err := runner.setParam(c, param); err != nil {
return fmt.Errorf("parse the command line arguments: %w", err)
}
ctrl := controller.InitializeWhichCommandController(c.Context, param, http.DefaultClient, runner.Runtime)
ctrl := controller.InitializeWhichCommandController(c.Context, param, runner.Runtime)
exeName, _, err := parseExecArgs(c.Args().Slice())
if err != nil {
return err
Expand Down
46 changes: 19 additions & 27 deletions pkg/controller/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ type Controller struct {
stdin io.Reader
stdout io.Writer
gitHubRepositoryService githubSvc.RepositoryService
githubV4 githubSvc.GraphQL
registryInstaller registry.Installer
configFinder finder.ConfigFinder
configReader reader.ConfigReader
fuzzyFinder FuzzyFinder
fs afero.Fs
}

func New(configFinder finder.ConfigFinder, configReader reader.ConfigReader, registInstaller registry.Installer, gh githubSvc.RepositoryService, fs afero.Fs, fuzzyFinder FuzzyFinder) *Controller {
func New(configFinder finder.ConfigFinder, configReader reader.ConfigReader, registInstaller registry.Installer, gh githubSvc.RepositoryService, fs afero.Fs, fuzzyFinder FuzzyFinder, githubV4 githubSvc.GraphQL) *Controller {
return &Controller{
stdin: os.Stdin,
stdout: os.Stdout,
configFinder: configFinder,
configReader: configReader,
registryInstaller: registInstaller,
gitHubRepositoryService: gh,
githubV4: githubV4,
fs: fs,
fuzzyFinder: fuzzyFinder,
}
Expand Down Expand Up @@ -235,35 +237,26 @@ func (ctrl *Controller) listAndGetTagName(ctx context.Context, pkgInfo *config.P
func (ctrl *Controller) listAndGetTagNameFromTag(ctx context.Context, pkgInfo *config.PackageInfo, logE *logrus.Entry) string {
repoOwner := pkgInfo.RepoOwner
repoName := pkgInfo.RepoName
opt := &github.ListOptions{
PerPage: 30, //nolint:gomnd
}
versionFilter, err := constraint.CompileVersionFilter(*pkgInfo.VersionFilter)
if err != nil {
return ""
}
for {
tags, _, err := ctrl.gitHubRepositoryService.ListTags(ctx, repoOwner, repoName, opt)
if err != nil {
logerr.WithError(logE, err).WithFields(logrus.Fields{
"repo_owner": repoOwner,
"repo_name": repoName,
}).Warn("list releases")
return ""
}
for _, tag := range tags {
tagName := tag.GetName()
f, err := constraint.EvaluateVersionFilter(versionFilter, tagName)
if err != nil || !f {
continue
}
return tagName
}
if len(tags) != opt.PerPage {
return ""
tags, err := ctrl.githubV4.ListTags(ctx, repoOwner, repoName)
if err != nil {
logerr.WithError(logE, err).WithFields(logrus.Fields{
"repo_owner": repoOwner,
"repo_name": repoName,
}).Warn("list releases")
return ""
}
for _, tag := range tags {
f, err := constraint.EvaluateVersionFilter(versionFilter, tag)
if err != nil || !f {
continue
}
opt.Page++
return tag
}
return ""
}

func (ctrl *Controller) getOutputtedGitHubPkgFromTag(ctx context.Context, outputPkg *config.Package, pkgInfo *config.PackageInfo, logE *logrus.Entry) {
Expand All @@ -273,7 +266,7 @@ func (ctrl *Controller) getOutputtedGitHubPkgFromTag(ctx context.Context, output
if pkgInfo.VersionFilter != nil {
tagName = ctrl.listAndGetTagNameFromTag(ctx, pkgInfo, logE)
} else {
tags, _, err := ctrl.gitHubRepositoryService.ListTags(ctx, repoOwner, repoName, nil)
tags, err := ctrl.githubV4.ListTags(ctx, repoOwner, repoName)
if err != nil {
logerr.WithError(logE, err).WithFields(logrus.Fields{
"repo_owner": repoOwner,
Expand All @@ -284,8 +277,7 @@ func (ctrl *Controller) getOutputtedGitHubPkgFromTag(ctx context.Context, output
if len(tags) == 0 {
return
}
tag := tags[0]
tagName = tag.GetName()
tagName = tags[0]
}

if pkgName := pkgInfo.GetName(); pkgName == repoOwner+"/"+repoName || strings.HasPrefix(pkgName, repoOwner+"/"+repoName+"/") {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Test_controller_Generate(t *testing.T) { //nolint:funlen
idxs []int
fuzzyFinderErr error
releases []*github.RepositoryRelease
tags []string
}{
{
name: "normal",
Expand Down Expand Up @@ -234,11 +235,12 @@ packages:
}
configFinder := finder.NewConfigFinder(fs)
gh := githubSvc.NewMock(d.releases, nil, "")
v4Client := githubSvc.NewMockGraphQL(d.tags, nil)
downloader := download.NewRegistryDownloader(gh, download.NewHTTPDownloader(http.DefaultClient))
registryInstaller := registry.New(d.param, downloader, fs)
configReader := reader.New(fs)
fuzzyFinder := generate.NewMockFuzzyFinder(d.idxs, d.fuzzyFinderErr)
ctrl := generate.New(configFinder, configReader, registryInstaller, gh, fs, fuzzyFinder)
ctrl := generate.New(configFinder, configReader, registryInstaller, gh, fs, fuzzyFinder, v4Client)
if err := ctrl.Generate(ctx, logE, d.param, d.args...); err != nil {
if d.isErr {
return
Expand Down
24 changes: 12 additions & 12 deletions pkg/controller/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package controller

import (
"context"
"net/http"

"github.com/aquaproj/aqua/pkg/config"
finder "github.com/aquaproj/aqua/pkg/config-finder"
Expand All @@ -24,36 +23,37 @@ import (
"github.com/aquaproj/aqua/pkg/link"
"github.com/aquaproj/aqua/pkg/runtime"
"github.com/google/wire"
"github.com/shurcooL/githubv4"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/go-osenv/osenv"
)

func InitializeListCommandController(ctx context.Context, param *config.Param, httpClient *http.Client) *list.Controller {
wire.Build(list.NewController, finder.NewConfigFinder, github.New, registry.New, download.NewRegistryDownloader, reader.New, afero.NewOsFs, download.NewHTTPDownloader)
func InitializeListCommandController(ctx context.Context, param *config.Param) *list.Controller {
wire.Build(list.NewController, finder.NewConfigFinder, github.New, github.NewHTTPClient, github.NewAccessToken, registry.New, download.NewRegistryDownloader, reader.New, afero.NewOsFs, download.NewHTTPDownloader)
return &list.Controller{}
}

func InitializeInitCommandController(ctx context.Context, param *config.Param) *initcmd.Controller {
wire.Build(initcmd.New, github.New, afero.NewOsFs)
wire.Build(initcmd.New, github.New, github.NewHTTPClient, github.NewAccessToken, afero.NewOsFs)
return &initcmd.Controller{}
}

func InitializeGenerateCommandController(ctx context.Context, param *config.Param, httpClient *http.Client) *generate.Controller {
wire.Build(generate.New, finder.NewConfigFinder, github.New, registry.New, download.NewRegistryDownloader, reader.New, afero.NewOsFs, generate.NewFuzzyFinder, download.NewHTTPDownloader)
func InitializeGenerateCommandController(ctx context.Context, param *config.Param) *generate.Controller {
wire.Build(generate.New, finder.NewConfigFinder, github.New, github.NewHTTPClient, github.NewAccessToken, github.NewGraphQL, githubv4.NewClient, registry.New, download.NewRegistryDownloader, reader.New, afero.NewOsFs, generate.NewFuzzyFinder, download.NewHTTPDownloader)
return &generate.Controller{}
}

func InitializeInstallCommandController(ctx context.Context, param *config.Param, httpClient *http.Client, rt *runtime.Runtime) *install.Controller {
wire.Build(install.New, finder.NewConfigFinder, github.New, registry.New, download.NewRegistryDownloader, reader.New, installpackage.New, download.NewPackageDownloader, afero.NewOsFs, link.New, download.NewHTTPDownloader, exec.New)
func InitializeInstallCommandController(ctx context.Context, param *config.Param, rt *runtime.Runtime) *install.Controller {
wire.Build(install.New, finder.NewConfigFinder, github.New, github.NewHTTPClient, github.NewAccessToken, registry.New, download.NewRegistryDownloader, reader.New, installpackage.New, download.NewPackageDownloader, afero.NewOsFs, link.New, download.NewHTTPDownloader, exec.New)
return &install.Controller{}
}

func InitializeWhichCommandController(ctx context.Context, param *config.Param, httpClient *http.Client, rt *runtime.Runtime) which.Controller {
wire.Build(which.New, finder.NewConfigFinder, github.New, registry.New, download.NewRegistryDownloader, reader.New, osenv.New, afero.NewOsFs, download.NewHTTPDownloader, link.New)
func InitializeWhichCommandController(ctx context.Context, param *config.Param, rt *runtime.Runtime) which.Controller {
wire.Build(which.New, finder.NewConfigFinder, github.New, github.NewHTTPClient, github.NewAccessToken, registry.New, download.NewRegistryDownloader, reader.New, osenv.New, afero.NewOsFs, download.NewHTTPDownloader, link.New)
return nil
}

func InitializeExecCommandController(ctx context.Context, param *config.Param, httpClient *http.Client, rt *runtime.Runtime) *cexec.Controller {
wire.Build(cexec.New, finder.NewConfigFinder, download.NewPackageDownloader, installpackage.New, github.New, registry.New, download.NewRegistryDownloader, reader.New, which.New, exec.New, osenv.New, afero.NewOsFs, link.New, download.NewHTTPDownloader)
func InitializeExecCommandController(ctx context.Context, param *config.Param, rt *runtime.Runtime) *cexec.Controller {
wire.Build(cexec.New, finder.NewConfigFinder, download.NewPackageDownloader, installpackage.New, github.New, github.NewHTTPClient, github.NewAccessToken, registry.New, download.NewRegistryDownloader, reader.New, which.New, exec.New, osenv.New, afero.NewOsFs, link.New, download.NewHTTPDownloader)
return &cexec.Controller{}
}
50 changes: 32 additions & 18 deletions pkg/controller/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading