Убрана кобра, доработаны методы создания, закрытия пулл реквестов. Co-authored-by: Gorbunov Kirill Andreevich <kgorbunov@mtsbank.ru> Reviewed-on: #11 Co-authored-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru> Co-committed-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
This commit is contained in:
@@ -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"
|
||||
@@ -36,6 +40,24 @@ import (
|
||||
|
||||
// https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
||||
|
||||
var initMsg = `
|
||||
Pkgdashcli allows you to define a version update for a dependency and start
|
||||
merge requests in version control systems.
|
||||
|
||||
Usage:
|
||||
pkgdashcli --command {{command}} --path {{name of dep}}
|
||||
|
||||
Commands:
|
||||
checkupdate | CheckUpdate collects a list of dependencies with the latest updates.
|
||||
list | Returns a list of PR for this repository with update dependencies.
|
||||
update --path {{name of one dep or empty for update all dep}} | Creates a PR with the specified dependency update in path or creates a PR with dependency updates for all modules if path is empty.
|
||||
close --path {{name of one dep or empty for close all pr}} | Closes the PR for the specified dependency or closes all PRs with dependency updates if path is empty .
|
||||
|
||||
Flags:
|
||||
--command | The command to execute
|
||||
--path | The name of the module to create/close the PR, if empty, the command is executed for all modules.
|
||||
`
|
||||
|
||||
var (
|
||||
DefaultPullRequestTitle = `Bump {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
||||
DefaultPullRequestBody = `Bumps {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
||||
@@ -58,13 +80,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())
|
||||
@@ -94,14 +122,18 @@ func pkgdashcli() {
|
||||
|
||||
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),
|
||||
options.Codec(yamlcodec.NewCodec()),
|
||||
fileconfig.Path(".gitea/pkgdashcli.yaml"),
|
||||
fileconfig.Path(path),
|
||||
)
|
||||
err = c.Init(options.Context(ctx))
|
||||
err = c.Init()
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
|
||||
}
|
||||
@@ -111,8 +143,6 @@ func pkgdashcli() {
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository))
|
||||
|
||||
if cfg.PullRequestBody == "" {
|
||||
cfg.PullRequestBody = DefaultPullRequestBody
|
||||
}
|
||||
@@ -121,6 +151,22 @@ 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))
|
||||
}
|
||||
|
||||
if cliCfg.Path == "" && cliCfg.Command == "" {
|
||||
fmt.Print(initMsg)
|
||||
return
|
||||
}
|
||||
|
||||
path := "."
|
||||
if len(os.Args) > 1 {
|
||||
path = os.Args[1]
|
||||
@@ -173,71 +219,138 @@ 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())
|
||||
}
|
||||
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "pkgdash/main can t get info user: %s", err)
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
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.Debug(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.Debug(ctx, fmt.Sprintf("Update successful for %s", pathMod))
|
||||
}
|
||||
}
|
||||
// err = gitSource.RequestClose(ctx, "master", "modernc.org/ccgo/v4")
|
||||
|
||||
Execute(ctx, gitSource, mvs, *cliCfg, *cfg)
|
||||
|
||||
logger.Info(ctx, "Pkgdash successfully updated dependencies")
|
||||
}
|
||||
|
||||
func getRepoMgmt() string {
|
||||
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.Command {
|
||||
case "checkupdate":
|
||||
js, err := json.Marshal(mvs)
|
||||
fmt.Println(fmt.Sprintf(`Modules get update: %s, %s`, js, err))
|
||||
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))
|
||||
return
|
||||
}
|
||||
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))
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
js, err := json.Marshal(prList)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("error: %s", err))
|
||||
}
|
||||
fmt.Println(fmt.Sprintf("for %s:\n%s", cfg.Source.Repository, js))
|
||||
default:
|
||||
fmt.Print(initMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func getRepoMgmt(ctx context.Context, cfg *configcli.Config) error {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "unknown"
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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 p == "/" {
|
||||
return "unknown"
|
||||
if api, ok := repoAPI[configDir]; ok && cfg.Source.APIURL == "" && err == nil {
|
||||
cfg.Source.APIURL = api
|
||||
}
|
||||
p = filepath.Clean(filepath.Join(p, ".."))
|
||||
}
|
||||
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.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)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func giteaPullRequest(ctx context.Context, cfg *configcli.Config, branch string, mods map[string]modules.Update) error {
|
||||
|
Reference in New Issue
Block a user