Integrate go-gh API package (#5614)

This commit is contained in:
Sam Coe 2022-06-23 04:05:31 +01:00 committed by GitHub
parent 6e3689d58b
commit 074ed50b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 542 additions and 1006 deletions

View file

@ -18,7 +18,6 @@ import (
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/build"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/cli/v2/internal/update"
@ -30,7 +29,6 @@ import (
"github.com/cli/cli/v2/pkg/text"
"github.com/cli/cli/v2/utils"
"github.com/cli/safeexec"
"github.com/mattn/go-colorable"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
)
@ -358,40 +356,19 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
if !shouldCheckForUpdate() {
return nil, nil
}
client, err := basicClient(currentVersion)
httpClient, err := api.NewHTTPClient(api.HTTPClientOptions{
AppVersion: currentVersion,
Log: os.Stderr,
})
if err != nil {
return nil, err
}
client := api.NewClientFromHTTP(httpClient)
repo := updaterEnabled
stateFilePath := filepath.Join(config.StateDir(), "state.yml")
return update.CheckForUpdate(client, stateFilePath, repo, currentVersion)
}
// BasicClient returns an API client for github.com only that borrows from but
// does not depend on user configuration
func basicClient(currentVersion string) (*api.Client, error) {
var opts []api.ClientOption
if isVerbose, debugValue := utils.IsDebugEnabled(); isVerbose {
colorize := utils.IsTerminal(os.Stderr)
logTraffic := strings.Contains(debugValue, "api")
opts = append(opts, api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize))
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", currentVersion)))
token, _ := config.AuthTokenFromEnv(ghinstance.Default())
if token == "" {
if c, err := config.ParseDefaultConfig(); err == nil {
token, _ = c.Get(ghinstance.Default(), "oauth_token")
}
}
if token != "" {
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))
}
return api.NewClient(opts...), nil
}
func isRecentRelease(publishedAt time.Time) bool {
return !publishedAt.IsZero() && time.Since(publishedAt) < time.Hour*24
}