#8 Checkout, auth. #16

Closed
kgorbunov wants to merge 104 commits from kgorbunov:dev into master
13 changed files with 221 additions and 92 deletions
Showing only changes of commit 55c0bdb49a - Show all commits

View File

@ -5,11 +5,13 @@ import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"text/template"
@ -23,9 +25,11 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/jdx/go-netrc"
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
envconfig "go.unistack.org/micro-config-env/v4"
fileconfig "go.unistack.org/micro-config-file/v4"
microflag "go.unistack.org/micro-config-flag/v4"
"go.unistack.org/micro/v4/config"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/logger/slog"
@ -58,13 +62,19 @@ var (
".github": "github",
".gitlab": "gitlab",
}
repoAPI = map[string]string{
".gitea": "git.unistack.org",
".gogs": "gogs",
".github": "github.com/unistack-org",
".gitlab": "gitlab.mtsbank.ru",
}
)
type Data struct {
Modules map[string]modules.Update
}
func pkgdashcli() {
func main() {
var err error
ctx, cancel := context.WithCancel(context.Background())
@ -120,6 +130,17 @@ func pkgdashcli() {
cfg.PullRequestTitle = DefaultPullRequestTitle
}
cliCfg := &configcli.Cli{}
c := microflag.NewConfig(config.Struct(cliCfg), microflag.FlagErrorHandling(flag.ContinueOnError))
if err = c.Init(); err != nil {
logger.Fatal(ctx, fmt.Sprintf("init cli cfg failed: %v", err))
}
if err = c.Load(ctx); err != nil {
logger.Fatal(ctx, fmt.Sprintf("load cli cfg failed: %v", err))
}
path := "."
if len(os.Args) > 1 {
path = os.Args[1]
@ -172,71 +193,134 @@ func pkgdashcli() {
modules.Updates(updateOptions)
/*var repoGit, tokenGit string // nameGit = gitea, repoGit = pkgdash, tokenGit = {xxx}, machine??
if cfg.Source != nil {
repoGit = cfg.Source.TypeGit
tokenGit = cfg.Source.Token
} else {
repoGit = getRepoMgmt()
if repoGit == "unknown" {
logger.Fatal(ctx, "pkgdash/main failed to get repo management")
if err = getRepoMgmt(ctx, cfg); err != nil { // Filling in empty config fields.
logger.Error(ctx, err.Error())
}
logger.Debugf(ctx, fmt.Sprintf("cfg: %v", cfg))
logger.Debugf(ctx, fmt.Sprintf("cfg cli: %s", cliCfg))
gitSource := source.NewSourceControl(*cfg)
Execute(ctx, gitSource, mvs, *cliCfg, *cfg)
logger.Info(ctx, "Pkgdash successfully updated dependencies")
}
func Execute(ctx context.Context, gitSource source.SourceControl, mvs map[string]modules.Update, cliCfg configcli.Cli, cfg configcli.Config) {
var mod modules.Update
var ok bool
var path string
prList := make(map[string]map[string]string)
switch cliCfg.Method {
case "checkupdate":
logger.Info(ctx, fmt.Sprintf("Modules get update: \n %s", mvs))
case "update":
if cliCfg.Path != "" { // update one dep
path = cliCfg.Path
if mod, ok = mvs[path]; !ok {
logger.Fatal(ctx, fmt.Sprintf("For %s update not exist", path))
}
logger.Debugf(ctx, fmt.Sprintf("Start update %s from %s to %s", path, mod.Module.Version, mod.Version))
for _, branch := range cfg.Branches {
if err := gitSource.RequestOpen(ctx, branch, path, mod); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to create pr: %v", err))
}
}
logger.Debugf(ctx, fmt.Sprintf("Update successful for %s", path))
}
for _, branch := range cfg.Branches { // update all dep
for path, mod = range mvs {
logger.Debugf(ctx, fmt.Sprintf("Start update %s from %s to %s", path, mod.Module.Version, mod.Version))
err := gitSource.RequestOpen(ctx, branch, path, mod)
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to create pr: %v", err))
}
logger.Debugf(ctx, fmt.Sprintf("Update successful for %s", path))
}
}
case "close":
if cliCfg.Path != "" { // close one dep
path = cliCfg.Path
logger.Debugf(ctx, fmt.Sprintf("Start close for %s", path))
for _, branch := range cfg.Branches {
if err := gitSource.RequestClose(ctx, branch, path); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to close pr: %v", err))
}
}
logger.Debugf(ctx, fmt.Sprintf("Close successful for %s", path))
}
for _, branch := range cfg.Branches {
logger.Info(ctx, fmt.Sprintf("Start getting pr for %s", branch))
rMap, err := gitSource.RequestList(ctx, branch)
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Error with getting pr list for branch: %s", branch))
}
logger.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap))
logger.Info(ctx, fmt.Sprintf("Start close pr for base branch %s", branch))
for path, _ = range rMap {
logger.Debugf(ctx, fmt.Sprintf("Start close for %s", path))
if err = gitSource.RequestClose(ctx, branch, path); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to close pr: %v", err))
}
logger.Debugf(ctx, fmt.Sprintf("Close successful for %s", path))
}
}
case "list":
for _, branch := range cfg.Branches {
rMap, err := gitSource.RequestList(ctx, branch)
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("RequestList: error %s", err))
}
prList[branch] = rMap
}
logger.Info(ctx, fmt.Sprintf("for %s:\n%s", cfg.Source.Repository, prList))
}
}
func getRepoMgmt(ctx context.Context, cfg *configcli.Config) error {
wd, err := os.Getwd()
if err != nil {
return err
}
p := filepath.Clean(wd)
for _, configDir := range configDirs {
_, err := os.Stat(filepath.Join(p, configDir))
if name, ok := repoMgmt[configDir]; ok && cfg.Source.TypeGit == "" && err == nil {
cfg.Source.TypeGit = name
}
if api, ok := repoAPI[configDir]; ok && cfg.Source.APIURL == "" && err == nil {
cfg.Source.APIURL = api
}
}
if p == "/" && cfg.Source.TypeGit == "" && cfg.Source.APIURL == "" {
return fmt.Errorf("unknown")
}
p = filepath.Clean(filepath.Join(p, ".."))
usr, err := user.Current()
if err != nil {
logger.Error(ctx, "pkgdash/main can t get info user: %s", err)
} else {
logger.Fatal(ctx, fmt.Sprintf("pkgdash/main can t get info about user: %s", err))
}
n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
if err != nil {
logger.Error(ctx, "pkgdash/main can t parse .netrc: %s", err)
}
tokenGit = n.Machine(repoGit).Get("password")
if cfg.Source.Owner == "" {
cfg.Source.Owner = n.Machine(cfg.Source.APIURL).Get("login")
}
if cfg.Source.Token == "" {
cfg.Source.Token = n.Machine(cfg.Source.APIURL).Get("password")
}
switch repoGit {
case "gitea":
for _, branch := range cfg.Branches {
err = giteaPullRequest(ctx, cfg, branch, mvs)
}
}*/
logger.Info(ctx, fmt.Sprintf("cfg: %v", cfg))
gitSource := source.NewSourceControl(*cfg)
for _, branch := range cfg.Branches {
for pathMod, mod := range mvs {
logger.Debugf(ctx, fmt.Sprintf("Start update %s from %s to %s", pathMod, mod.Module.Version, mod.Version))
err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
if err != nil {
logger.Error(ctx, fmt.Sprintf("failed to create pr: %v", err))
}
logger.Debugf(ctx, fmt.Sprintf("Update successful for %s", pathMod))
}
}
//err = gitSource.RequestClose(ctx, "master", "modernc.org/ccgo/v4")
logger.Info(ctx, "Pkgdash successfully updated dependencies")
}
func getRepoMgmt() string {
wd, err := os.Getwd()
if err != nil {
return "unknown"
}
p := filepath.Clean(wd)
for {
for _, configDir := range configDirs {
_, err := os.Stat(filepath.Join(p, configDir))
if name, ok := repoMgmt[configDir]; ok && err == nil {
return name
}
}
if p == "/" {
return "unknown"
}
p = filepath.Clean(filepath.Join(p, ".."))
}
return nil
}
func giteaPullRequest(ctx context.Context, cfg *configcli.Config, branch string, mods map[string]modules.Update) error {

View File

@ -1,7 +0,0 @@
package main
import "git.unistack.org/unistack-org/pkgdash/internal/cli"
func main() {
cli.Execute()
}

2
go.mod
View File

@ -34,8 +34,10 @@ require (
require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jdx/go-netrc v1.0.0 // indirect
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.unistack.org/micro-config-flag/v4 v4.0.4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311132316-a219d84964c2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

4
go.sum
View File

@ -687,6 +687,8 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jdx/go-netrc v1.0.0 h1:QbLMLyCZGj0NA8glAhxUpf1zDg6cxnWgMBbjq40W0gQ=
github.com/jdx/go-netrc v1.0.0/go.mod h1:Gh9eFQJnoTNIRHXl2j5bJXA1u84hQWJWgGh569zF3v8=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
@ -1092,6 +1094,8 @@ go.unistack.org/micro-config-env/v4 v4.0.1 h1:A7W+Xm4WEsN9O6isWncfwNWEUuSuCHgt+a
go.unistack.org/micro-config-env/v4 v4.0.1/go.mod h1:kJvyLrRbVRrsM1jTdl5lHy4u88PnlK2mskOZ3T57M/0=
go.unistack.org/micro-config-file/v4 v4.0.3 h1:1zUZWnzgf+iB8qbh6kOlA/iBUhDSByp2rKXwxn1yUFM=
go.unistack.org/micro-config-file/v4 v4.0.3/go.mod h1:9MbYnGXnsrzvjxAPKutxCgxZxPYOUO/XBLW08aN8zD8=
go.unistack.org/micro-config-flag/v4 v4.0.4 h1:Bh0gPZmS5oBx5VOGB9EQn4+Xz3Fen3KG+GMkERpylio=
go.unistack.org/micro-config-flag/v4 v4.0.4/go.mod h1:QPA92j5o3AwRiw/LjfVN3WQbkjGGbJMWRFpBAEt/smA=
go.unistack.org/micro-config-vault/v4 v4.0.2 h1:QhDdtVJhQYmJZqAhsLRdnDZSZSzBunWX9a5l/WNKWNY=
go.unistack.org/micro-config-vault/v4 v4.0.2/go.mod h1:0gWQVkncMwaG0wZPqC98HhS6AYOcXR0lmPG/roR6AcM=
go.unistack.org/micro-logger-zerolog/v4 v4.0.3 h1:ao6jGMo8jJG9WcOE738eqrWeabQaqpHMrLx+IniRDpI=

View File

@ -33,7 +33,8 @@ func NewCheckUpdateCommand() *cobra.Command {
Short: "CheckUpdate collects a list of dependencies with the latest updates.",
Long: `CheckUpdate collects a list of dependencies with the latest updates.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("checkupdate called")
logger.Info(ctx, "CheckUpdate called")
path := "."
if len(os.Args) > 1 {
path = os.Args[1]

View File

@ -6,10 +6,12 @@ package cli
import (
"context"
"fmt"
"os"
"path/filepath"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/source"
"git.unistack.org/unistack-org/pkgdash/internal/source/github"
"github.com/spf13/cobra"
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
envconfig "go.unistack.org/micro-config-env/v4"
@ -23,7 +25,7 @@ import (
// initCmd represents the init command
var initCmd = NewInitCommand()
var gitsource = source.SourceControl(nil)
var gitsource source.SourceControl = (*github.Github)(nil)
var cfg = configcli.NewConfig()
@ -63,16 +65,18 @@ func NewInitCommand() *cobra.Command {
Short: "Init fills the config with data from the configuration file.",
Long: `Init fills the config with data from the configuration file.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("init called")
logger.Info(ctx, "Init called")
var err error
lCfg := configcli.NewConfig()
if err = config.Load(ctx,
[]config.Config{
config.NewConfig(
config.Struct(cfg),
config.Struct(lCfg),
),
envconfig.NewConfig(
config.Struct(cfg),
config.Struct(lCfg),
),
},
config.LoadOverride(true),
@ -82,12 +86,16 @@ func NewInitCommand() *cobra.Command {
for _, configDir := range configDirs {
for _, configFile := range configFiles {
logger.Info(ctx, fmt.Sprintf("path: %s", filepath.Join(configDir, configFile)))
path := filepath.Join(configDir, configFile)
if _, err = os.Stat(path); os.IsNotExist(err) {
continue
}
c := fileconfig.NewConfig(
config.AllowFail(false),
config.Struct(cfg),
config.Struct(lCfg),
options.Codec(yamlcodec.NewCodec()),
fileconfig.Path(".gitea/pkgdashcli.yaml"),
fileconfig.Path(path),
)
err = c.Init()
if err != nil {
@ -101,7 +109,7 @@ func NewInitCommand() *cobra.Command {
}
}
logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))
logger.Info(ctx, fmt.Sprintf("Load config... \n %s", cfg))
if cfg.PullRequestBody == "" {
cfg.PullRequestBody = DefaultPullRequestBody
@ -111,7 +119,11 @@ func NewInitCommand() *cobra.Command {
cfg.PullRequestTitle = DefaultPullRequestTitle
}
gitsource = source.NewSourceControl(*cfg)
gitsource = source.NewSourceControl(*lCfg)
logger.Info(ctx, fmt.Sprintf("Git: %s", gitsource.Name()))
cfg = lCfg
return nil
},

View File

@ -35,8 +35,6 @@ func init() {
func NewListCommand() *cobra.Command {
ctx := context.Background()
logger.Info(ctx, "RequestList start")
cmd := &cobra.Command{
Use: "list",
Short: "Update allows you to start the process of creating a PR in the repository",
@ -45,6 +43,9 @@ Update allows you to start the process of creating a PR in the repository.
Use the -a flag to update all dependencies or -m flag a specific module.
`,
RunE: func(cmd *cobra.Command, args []string) error {
logger.Info(ctx, "RequestList start")
if gitsource == nil {
return errSourceNil
}
@ -54,8 +55,9 @@ Use the -a flag to update all dependencies or -m flag a specific module.
}
prList = make(map[string]map[string]string)
logger.Info(ctx, fmt.Sprintf("Load config... \n %s", cfg))
for _, branch := range cfg.Branches {
logger.Info(ctx, fmt.Sprintf("Start getting pr for %s", branch))
rMap, err := gitsource.RequestList(ctx, branch)
if err != nil {
return err
@ -65,6 +67,8 @@ Use the -a flag to update all dependencies or -m flag a specific module.
logger.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap))
}
logger.Info(ctx, "Successful getting pull requests")
return nil
},
}

View File

@ -9,11 +9,11 @@ type Config struct {
}
type Source struct {
TypeGit string `json:"type" yaml:"type"`
Token string `json:"token" yaml:"token"`
APIURL string `json:"apiurl" yaml:"apiurl"`
Repository string `json:"repository" yaml:"repository"`
Owner string `json:"owner" yaml:"owner"`
TypeGit string `json:"type" yaml:"type" env:"GIT_TYPE"`
Token string `json:"token" yaml:"token" env:"GIT_TOKEN"`
APIURL string `json:"apiurl" yaml:"apiurl" env:"GIT_API"`
Repository string `json:"repository" yaml:"repository" env:"GIT_REPO"`
Owner string `json:"owner" yaml:"owner" env:"GIT_OWNER"`
}
type UpdateOpt struct {
@ -23,8 +23,14 @@ type UpdateOpt struct {
Cached bool `json:"cached" yaml:"cached" default:"true"`
}
type Cli struct {
Method string `flag:"name=method,desc='choice method(update, close, checkupdaue, list)',default='checkupdate'"`
Path string `flag:"name=path,desc='title of mod',default=''"`
}
func NewConfig() *Config {
return &Config{
Source: &Source{},
UpdateOpt: &UpdateOpt{},
}
}

View File

@ -59,6 +59,10 @@ type giteaPull struct {
ID int64 `json:"id"`
}
func (g *Gitea) Name() string {
return "gitea"
}
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
logger.Debugf(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
@ -256,7 +260,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
bytes.NewReader(buf),
)
if err != nil {
@ -371,6 +375,9 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
rMap := make(map[string]string)
for _, pull := range gPulls {
if !strings.HasPrefix(pull.Title, "Bump ") { //добавляем только реквесты бота по обновлению модулей
continue
}
path = strings.Split(pull.Title, " ")[1] //todo Работет только для дефолтного шаблона
rMap[path] = pull.Title
}
@ -388,7 +395,7 @@ func getVersions(s string) string {
func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) {
var buf []byte
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/api/v1/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
if err != nil {
return nil, err
}
@ -404,7 +411,7 @@ func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s/repos/%s/%s/pulls?state=open&token=%s", url, owner, repo, token),
fmt.Sprintf("https://%s/api/v1//repos/%s/%s/pulls?state=open&token=%s", url, owner, repo, token),
nil)
if err != nil {
return nil, err

View File

@ -2,6 +2,7 @@ package github
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@ -17,15 +18,19 @@ func NewGithub(cfg configcli.Config) *Github {
}
}
func (g *Github) Name() string {
return "github"
}
func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@ -2,6 +2,7 @@ package gitlab
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@ -17,15 +18,19 @@ func NewGitlab(cfg configcli.Config) *Gitlab {
}
}
func (g *Gitlab) Name() string {
return "gitlab"
}
func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@ -2,6 +2,7 @@ package gogs
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@ -17,15 +18,19 @@ func NewGogs(cfg configcli.Config) *Gogs {
}
}
func (g *Gogs) Name() string {
return "gogs"
}
func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@ -12,6 +12,7 @@ import (
)
type SourceControl interface {
Name() string
RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error
RequestClose(ctx context.Context, branch string, path string) error
RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error