2023-09-23 21:16:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-09-27 09:45:50 +03:00
|
|
|
"encoding/json"
|
2023-10-01 16:28:03 +03:00
|
|
|
"errors"
|
2024-04-02 22:54:15 +03:00
|
|
|
"flag"
|
2023-09-23 21:16:26 +03:00
|
|
|
"fmt"
|
2023-09-30 01:00:13 +03:00
|
|
|
"io"
|
2023-09-27 09:45:50 +03:00
|
|
|
"net/http"
|
2023-09-23 21:16:26 +03:00
|
|
|
"os"
|
2023-10-01 16:28:03 +03:00
|
|
|
"os/exec"
|
2024-04-02 22:54:15 +03:00
|
|
|
"os/user"
|
2023-09-23 21:16:26 +03:00
|
|
|
"path/filepath"
|
2023-10-02 19:43:48 +03:00
|
|
|
"strings"
|
2023-09-23 21:16:26 +03:00
|
|
|
"text/template"
|
2023-10-01 16:54:00 +03:00
|
|
|
"time"
|
2023-09-23 21:16:26 +03:00
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
"github.com/go-git/go-git/v5"
|
2023-10-02 01:16:16 +03:00
|
|
|
gitconfig "github.com/go-git/go-git/v5/config"
|
2023-09-30 00:45:29 +03:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2023-10-01 21:34:58 +03:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2023-09-30 01:25:09 +03:00
|
|
|
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
|
2024-04-02 22:54:15 +03:00
|
|
|
"github.com/jdx/go-netrc"
|
2024-12-07 02:35:30 +03:00
|
|
|
yamlcodec "go.unistack.org/micro-codec-yaml/v3"
|
|
|
|
envconfig "go.unistack.org/micro-config-env/v3"
|
|
|
|
fileconfig "go.unistack.org/micro-config-file/v3"
|
|
|
|
microflag "go.unistack.org/micro-config-flag/v3"
|
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/logger/slog"
|
|
|
|
"go.unistack.org/pkgdash/internal/configcli"
|
|
|
|
"go.unistack.org/pkgdash/internal/modules"
|
|
|
|
"go.unistack.org/pkgdash/internal/source"
|
2023-09-23 21:16:26 +03:00
|
|
|
"golang.org/x/mod/modfile"
|
2024-03-24 20:52:32 +03:00
|
|
|
"golang.org/x/mod/semver"
|
2023-09-23 21:16:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// https://docs.github.com/ru/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
|
|
|
2024-04-02 22:54:15 +03:00
|
|
|
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.
|
|
|
|
`
|
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
var (
|
|
|
|
DefaultPullRequestTitle = `Bump {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
2023-09-30 01:18:30 +03:00
|
|
|
DefaultPullRequestBody = `Bumps {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
2023-09-23 21:16:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFiles = []string{
|
|
|
|
"dependabot.yml",
|
2023-10-03 01:01:31 +03:00
|
|
|
"pkgdashcli.yml",
|
|
|
|
"pkgdashcli.yaml",
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
configDirs = []string{
|
|
|
|
".gitea",
|
|
|
|
".github",
|
|
|
|
".gitlab",
|
|
|
|
}
|
|
|
|
repoMgmt = map[string]string{
|
|
|
|
".gitea": "gitea",
|
|
|
|
".gogs": "gogs",
|
|
|
|
".github": "github",
|
|
|
|
".gitlab": "gitlab",
|
|
|
|
}
|
2024-04-02 22:54:15 +03:00
|
|
|
repoAPI = map[string]string{
|
|
|
|
".gitea": "git.unistack.org",
|
|
|
|
".gogs": "gogs",
|
|
|
|
".github": "github.com/unistack-org",
|
|
|
|
".gitlab": "gitlab.mtsbank.ru",
|
|
|
|
}
|
2023-09-23 21:16:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Data struct {
|
2023-09-30 00:45:29 +03:00
|
|
|
Modules map[string]modules.Update
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
2024-04-02 22:54:15 +03:00
|
|
|
func main() {
|
2023-09-23 21:16:26 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log := slog.NewLogger()
|
2024-03-24 20:52:32 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
if err = log.Init(logger.WithLevel(logger.DebugLevel)); err != nil {
|
|
|
|
log.Error(ctx, fmt.Sprintf("logger init error: %v", err))
|
2023-10-02 23:52:27 +03:00
|
|
|
}
|
|
|
|
|
2024-03-24 20:52:32 +03:00
|
|
|
cfg := configcli.NewConfig()
|
2023-09-23 21:16:26 +03:00
|
|
|
|
|
|
|
if err = config.Load(ctx,
|
|
|
|
[]config.Config{
|
|
|
|
config.NewConfig(
|
|
|
|
config.Struct(cfg),
|
|
|
|
),
|
|
|
|
envconfig.NewConfig(
|
|
|
|
config.Struct(cfg),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
config.LoadOverride(true),
|
|
|
|
); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("failed to load config: %v", err))
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, configDir := range configDirs {
|
|
|
|
for _, configFile := range configFiles {
|
2024-04-02 22:54:15 +03:00
|
|
|
path := filepath.Join(configDir, configFile)
|
|
|
|
if _, err = os.Stat(path); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
c := fileconfig.NewConfig(
|
2024-03-24 20:52:32 +03:00
|
|
|
config.AllowFail(false),
|
2023-09-23 21:16:26 +03:00
|
|
|
config.Struct(cfg),
|
2024-12-07 02:35:30 +03:00
|
|
|
config.Codec(yamlcodec.NewCodec()),
|
2024-04-02 22:54:15 +03:00
|
|
|
fileconfig.Path(path),
|
2023-09-23 21:16:26 +03:00
|
|
|
)
|
2024-04-02 22:54:15 +03:00
|
|
|
err = c.Init()
|
2024-03-24 20:52:32 +03:00
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
|
2024-03-24 20:52:32 +03:00
|
|
|
}
|
2023-09-23 21:16:26 +03:00
|
|
|
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 01:07:50 +03:00
|
|
|
if cfg.PullRequestBody == "" {
|
|
|
|
cfg.PullRequestBody = DefaultPullRequestBody
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PullRequestTitle == "" {
|
|
|
|
cfg.PullRequestTitle = DefaultPullRequestTitle
|
|
|
|
}
|
|
|
|
|
2024-04-02 22:54:15 +03:00
|
|
|
cliCfg := &configcli.Cli{}
|
|
|
|
c := microflag.NewConfig(config.Struct(cliCfg), microflag.FlagErrorHandling(flag.ContinueOnError))
|
|
|
|
|
|
|
|
if err = c.Init(); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("init cli cfg failed: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.Load(ctx); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("load cli cfg failed: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if cliCfg.Path == "" && cliCfg.Command == "" {
|
|
|
|
fmt.Print(initMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
path := "."
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
path = os.Args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := modules.FindModFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
buf, err := os.ReadFile(name)
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to read file", err)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
mfile, err := modfile.Parse(name, buf, nil)
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to parse file", err)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mvs := make(map[string]modules.Update)
|
|
|
|
|
|
|
|
updateOptions := modules.UpdateOptions{
|
2024-03-24 20:52:32 +03:00
|
|
|
Pre: cfg.UpdateOpt.Pre,
|
|
|
|
Major: cfg.UpdateOpt.Major,
|
|
|
|
UpMajor: cfg.UpdateOpt.UpMajor,
|
|
|
|
Cached: cfg.UpdateOpt.Cached,
|
2023-09-23 21:16:26 +03:00
|
|
|
OnUpdate: func(u modules.Update) {
|
2024-03-24 20:52:32 +03:00
|
|
|
var modpath string // new mod path with major
|
2023-09-23 21:16:26 +03:00
|
|
|
if u.Err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Error(ctx, fmt.Sprintf("%s: failed: %v", u.Module.Path, u.Err))
|
2023-09-23 21:16:26 +03:00
|
|
|
return
|
|
|
|
}
|
2024-03-24 20:52:32 +03:00
|
|
|
modpath = u.Module.Path
|
|
|
|
v := semver.Major(u.Version)
|
|
|
|
p := modules.ModPrefix(modpath)
|
|
|
|
if !strings.HasPrefix(u.Module.Version, v) && v != "v1" && v != "v0" {
|
|
|
|
switch strings.HasPrefix(u.Module.Path, "gopkg.in") {
|
|
|
|
case true:
|
|
|
|
modpath = p + "." + v
|
|
|
|
case false:
|
|
|
|
modpath = p + "/" + v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mvs[modpath] = u
|
2023-09-23 21:16:26 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, req := range mfile.Require {
|
|
|
|
updateOptions.Modules = append(updateOptions.Modules, req.Mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
modules.Updates(updateOptions)
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
if err = getRepoMgmt(ctx, log, cfg); err != nil { // Filling in empty config fields.
|
|
|
|
log.Error(ctx, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.Branches) == 0 {
|
|
|
|
branchName, err := getCurrentBranch(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(ctx, "failed to get current branch", err)
|
|
|
|
}
|
|
|
|
cfg.Branches = append(cfg.Branches, branchName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Source.Repository == "" {
|
|
|
|
reposiotry, err := getCurrentRepository(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(ctx, "failed to get current repository", err)
|
|
|
|
}
|
|
|
|
cfg.Source.Repository = reposiotry
|
2024-03-24 20:52:32 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
gitSource := source.NewSourceControl(*cfg, log)
|
2023-09-27 09:45:50 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
Execute(ctx, log, gitSource, mvs, *cliCfg, *cfg)
|
2024-04-02 22:54:15 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "Pkgdash successfully updated dependencies")
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func Execute(ctx context.Context, log logger.Logger, gitSource source.SourceControl, mvs map[string]modules.Update, cliCfg configcli.Cli, cfg configcli.Config) {
|
2024-04-02 22:54:15 +03:00
|
|
|
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 {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("For %s update not exist", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Start update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
2024-04-02 22:54:15 +03:00
|
|
|
for _, branch := range cfg.Branches {
|
|
|
|
if err := gitSource.RequestOpen(ctx, branch, path, mod); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("failed to create pr: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Update successful for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
return
|
2023-10-03 01:01:31 +03:00
|
|
|
}
|
2024-04-02 22:54:15 +03:00
|
|
|
for _, branch := range cfg.Branches { // update all dep
|
|
|
|
for path, mod = range mvs {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Start update %s from %s to %s", path, mod.Module.Version, mod.Version))
|
2024-04-02 22:54:15 +03:00
|
|
|
err := gitSource.RequestOpen(ctx, branch, path, mod)
|
|
|
|
if err != nil {
|
2024-04-03 08:28:16 +03:00
|
|
|
if strings.Contains(err.Error(), "already exists") {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("skip %s, branch already exists", path))
|
2024-04-03 08:28:16 +03:00
|
|
|
continue
|
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("failed to create pr: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Update successful for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "close":
|
|
|
|
if cliCfg.Path != "" { // close one dep
|
|
|
|
path = cliCfg.Path
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Start close for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
for _, branch := range cfg.Branches {
|
|
|
|
if err := gitSource.RequestClose(ctx, branch, path); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("failed to close pr: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Close successful for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, branch := range cfg.Branches {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, fmt.Sprintf("Start getting pr for %s", branch))
|
2024-04-02 22:54:15 +03:00
|
|
|
rMap, err := gitSource.RequestList(ctx, branch)
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("Error with getting pr list for branch: %s", branch))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2023-09-27 09:45:50 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap))
|
|
|
|
log.Info(ctx, fmt.Sprintf("Start close pr for base branch %s", branch))
|
2024-03-24 20:52:32 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
for path = range rMap {
|
|
|
|
log.Debug(ctx, fmt.Sprintf("Start close for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
if err = gitSource.RequestClose(ctx, branch, path); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("failed to close pr: %v", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Debug(ctx, fmt.Sprintf("Close successful for %s", path))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "list":
|
|
|
|
for _, branch := range cfg.Branches {
|
|
|
|
rMap, err := gitSource.RequestList(ctx, branch)
|
2024-03-24 20:52:32 +03:00
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("RequestList: error %s", err))
|
2024-03-24 20:52:32 +03:00
|
|
|
}
|
2024-04-02 22:54:15 +03:00
|
|
|
|
|
|
|
prList[branch] = rMap
|
2024-03-24 20:52:32 +03:00
|
|
|
}
|
2024-04-02 22:54:15 +03:00
|
|
|
js, err := json.Marshal(prList)
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Error(ctx, fmt.Sprintf("error: %s", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
fmt.Printf("for %s:\n%s\n", cfg.Source.Repository, js)
|
2024-04-02 22:54:15 +03:00
|
|
|
default:
|
|
|
|
fmt.Print(initMsg)
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func getCurrentRepository(ctx context.Context) (string, error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Clean(wd)
|
|
|
|
|
|
|
|
repo, err := git.PlainOpen(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := repo.Config()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range cfg.Remotes {
|
|
|
|
if k != "origin" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.URLs[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("failed to get remotes")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentBranch(ctx context.Context) (string, error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Clean(wd)
|
|
|
|
|
|
|
|
repo, err := git.PlainOpen(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref.Name().Short(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoMgmt(ctx context.Context, log logger.Logger, cfg *configcli.Config) error {
|
2023-09-30 00:45:29 +03:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2024-04-02 22:54:15 +03:00
|
|
|
return err
|
2023-09-30 00:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Clean(wd)
|
2024-04-02 22:54:15 +03:00
|
|
|
for _, configDir := range configDirs {
|
|
|
|
_, err := os.Stat(filepath.Join(p, configDir))
|
2024-12-07 02:35:30 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info(ctx, fmt.Sprintf("check config dir %s", configDir))
|
|
|
|
if name, ok := repoMgmt[configDir]; ok && cfg.Source.TypeGit == "" {
|
2024-04-02 22:54:15 +03:00
|
|
|
cfg.Source.TypeGit = name
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
if api, ok := repoAPI[configDir]; ok && cfg.Source.APIURL == "" {
|
2024-04-02 22:54:15 +03:00
|
|
|
cfg.Source.APIURL = api
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 22:54:15 +03:00
|
|
|
if p == "/" && cfg.Source.TypeGit == "" && cfg.Source.APIURL == "" {
|
|
|
|
return fmt.Errorf("unknown")
|
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
// p = filepath.Clean(filepath.Join(p, ".."))
|
2024-04-02 22:54:15 +03:00
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, fmt.Sprintf("pkgdash/main cant get info about user: %s", err))
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
|
|
|
|
log.Info(ctx, fmt.Sprintf("try to configure scm source %v", cfg.Source))
|
|
|
|
|
|
|
|
netrcfile := filepath.Join(usr.HomeDir, ".netrc")
|
|
|
|
log.Info(ctx, "try to parse netrc file "+netrcfile)
|
|
|
|
n, err := netrc.Parse(netrcfile)
|
2024-04-02 22:54:15 +03:00
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Error(ctx, "pkgdash/main cant parse .netrc: %s", err)
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "try to configure scm for "+cfg.Source.APIURL)
|
2024-04-02 22:54:15 +03:00
|
|
|
if cfg.Source.Owner == "" {
|
|
|
|
cfg.Source.Owner = n.Machine(cfg.Source.APIURL).Get("login")
|
|
|
|
}
|
2024-04-03 17:29:57 +03:00
|
|
|
if cfg.Source.Username == "" {
|
|
|
|
cfg.Source.Username = n.Machine(cfg.Source.APIURL).Get("login")
|
|
|
|
}
|
|
|
|
if cfg.Source.Password == "" {
|
|
|
|
cfg.Source.Password = n.Machine(cfg.Source.APIURL).Get("password")
|
2024-04-02 22:54:15 +03:00
|
|
|
}
|
|
|
|
return nil
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func giteaPullRequest(ctx context.Context, log logger.Logger, cfg *configcli.Config, branch string, mods map[string]modules.Update) error {
|
2023-09-27 09:45:50 +03:00
|
|
|
envAPIURL := os.Getenv("GITHUB_API_URL")
|
|
|
|
envREPOSITORY := os.Getenv("GITHUB_REPOSITORY")
|
|
|
|
envTOKEN := os.Getenv("GITHUB_TOKEN")
|
2023-10-02 22:26:02 +03:00
|
|
|
|
2023-09-27 09:45:50 +03:00
|
|
|
var buf []byte
|
|
|
|
var err error
|
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
tplTitle, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle)
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to parse template: %v", err)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wTitle := bytes.NewBuffer(nil)
|
|
|
|
|
2023-09-30 01:07:50 +03:00
|
|
|
tplBody, err := template.New("pull_request_body").Parse(cfg.PullRequestBody)
|
2023-09-23 21:16:26 +03:00
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to parse template: %v", err)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wBody := bytes.NewBuffer(nil)
|
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to open repo: %v", err)
|
2023-09-30 00:45:29 +03:00
|
|
|
}
|
2023-10-02 02:04:45 +03:00
|
|
|
|
2023-10-02 22:40:08 +03:00
|
|
|
if err = repo.FetchContext(ctx, &git.FetchOptions{
|
|
|
|
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
|
|
|
|
Force: true,
|
|
|
|
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to fetch repo: %v", err)
|
2023-10-02 22:40:08 +03:00
|
|
|
}
|
|
|
|
|
2023-10-03 01:01:31 +03:00
|
|
|
var headRef *plumbing.Reference
|
|
|
|
refIter, err := repo.Branches()
|
2023-09-30 00:45:29 +03:00
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to get branches: %v", err)
|
2023-10-03 01:01:31 +03:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
ref, err := refIter.Next()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ref.Name().String() == branch {
|
|
|
|
headRef = ref
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refIter.Close()
|
|
|
|
|
|
|
|
if headRef == nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to get repo branch head")
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
2023-10-02 02:04:45 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "repo head %s", headRef)
|
2023-10-02 22:49:22 +03:00
|
|
|
|
2023-10-02 02:00:19 +03:00
|
|
|
wtree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to get worktree: %v", err)
|
2023-10-02 02:00:19 +03:00
|
|
|
}
|
2023-09-23 21:16:26 +03:00
|
|
|
|
2023-10-02 19:43:48 +03:00
|
|
|
type giteaPull struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Base struct {
|
|
|
|
Ref string `json:"ref"`
|
|
|
|
} `json:"base"`
|
2023-10-02 19:53:29 +03:00
|
|
|
ID int64 `json:"id"`
|
2023-10-02 19:43:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var pulls []*giteaPull
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, envAPIURL+"/repos/"+envREPOSITORY+"/pulls?state=open&token="+envTOKEN, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
rsp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-02 20:44:00 +03:00
|
|
|
|
|
|
|
buf, _ = io.ReadAll(rsp.Body)
|
2023-10-02 19:43:48 +03:00
|
|
|
if rsp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("unknown error: %s", buf)
|
|
|
|
}
|
2023-10-02 20:44:00 +03:00
|
|
|
|
2023-10-02 19:43:48 +03:00
|
|
|
if err = json.Unmarshal(buf, &pulls); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to decode response %s err: %v", buf, err)
|
2023-10-02 19:43:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for path := range mods {
|
|
|
|
for _, pull := range pulls {
|
2023-10-02 22:26:02 +03:00
|
|
|
if strings.Contains(pull.Title, path) && pull.Base.Ref == branch {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "skip %s as pr already exists %s", path, pull.URL)
|
2023-10-02 19:43:48 +03:00
|
|
|
delete(mods, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
for path, mod := range mods {
|
|
|
|
wTitle.Reset()
|
|
|
|
wBody.Reset()
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)
|
2023-09-30 00:45:29 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "reset worktree")
|
2023-10-02 22:26:02 +03:00
|
|
|
if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to reset repo branch: %v", err)
|
2023-10-02 02:00:19 +03:00
|
|
|
}
|
|
|
|
|
2023-10-03 00:26:33 +03:00
|
|
|
if err = wtree.PullContext(ctx, &git.PullOptions{
|
2023-10-03 00:27:30 +03:00
|
|
|
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
|
2023-10-03 00:26:33 +03:00
|
|
|
Depth: 1,
|
2023-10-03 00:27:30 +03:00
|
|
|
// RemoteURL :
|
|
|
|
Force: true,
|
|
|
|
RemoteName: "origin",
|
2023-10-03 00:37:30 +03:00
|
|
|
}); err != nil && err != git.NoErrAlreadyUpToDate {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to pull repo: %v", err)
|
2023-10-03 00:26:33 +03:00
|
|
|
}
|
2023-10-03 00:01:45 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "checkout ref %s", headRef)
|
2023-10-02 02:00:19 +03:00
|
|
|
if err = wtree.Checkout(&git.CheckoutOptions{
|
2023-10-02 02:04:45 +03:00
|
|
|
Hash: headRef.Hash(),
|
2023-10-02 23:52:27 +03:00
|
|
|
Branch: plumbing.NewBranchReferenceName(fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version)),
|
2023-10-02 22:49:22 +03:00
|
|
|
Create: true,
|
2023-10-02 02:00:19 +03:00
|
|
|
Force: true,
|
|
|
|
}); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to checkout tree: %v", err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
epath, err := exec.LookPath("go")
|
|
|
|
if errors.Is(err, exec.ErrDot) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to find go command: %v", err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
var out []byte
|
|
|
|
|
|
|
|
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
|
|
|
|
if out, err = cmd.CombinedOutput(); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to run go mod edit: %s err: %v", out, err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.CommandContext(ctx, epath, "mod", "tidy")
|
|
|
|
if out, err = cmd.CombinedOutput(); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to run go mod tidy: %s err: %v", out, err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "worktree add go.mod")
|
2023-10-01 16:28:03 +03:00
|
|
|
if _, err = wtree.Add("go.mod"); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to add file: %v", err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
2023-10-02 02:06:06 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "worktree add go.sum")
|
2023-10-01 16:28:03 +03:00
|
|
|
if _, err = wtree.Add("go.sum"); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to add file: %v", err)
|
2023-10-01 16:28:03 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "worktree commit")
|
2023-10-02 22:53:49 +03:00
|
|
|
_, err = wtree.Commit(wTitle.String(), &git.CommitOptions{
|
2023-10-02 22:49:22 +03:00
|
|
|
Parents: []plumbing.Hash{headRef.Hash()},
|
2023-10-01 16:54:00 +03:00
|
|
|
Author: &object.Signature{
|
2023-10-02 22:26:02 +03:00
|
|
|
Name: "gitea-actions",
|
|
|
|
Email: "info@unistack.org",
|
|
|
|
When: time.Now(),
|
2023-10-01 16:54:00 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to commit: %v", err)
|
2023-10-01 16:54:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-02 23:52:43 +03:00
|
|
|
// newref := plumbing.NewHashReference(plumbing.ReferenceName(fmt.Sprintf("refs/heads/pkgdash/go_modules/%s-%s", path, mod.Version)), headRef.Hash())
|
2023-10-02 23:52:27 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
if err = repo.Storer.SetReference(newref); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to create repo branch: %v", err)
|
2023-10-02 23:52:27 +03:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
refspec := gitconfig.RefSpec(fmt.Sprintf("+refs/heads/pkgdash/go_modules/%s-%s:refs/heads/pkgdash/go_modules/%s-%s", path, mod.Version, path, mod.Version))
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "try to push refspec %s", refspec)
|
2023-10-02 22:28:13 +03:00
|
|
|
|
2023-10-02 22:26:02 +03:00
|
|
|
if err = repo.PushContext(ctx, &git.PushOptions{
|
2023-10-02 02:10:13 +03:00
|
|
|
RefSpecs: []gitconfig.RefSpec{refspec},
|
2023-10-02 01:16:16 +03:00
|
|
|
Auth: &httpauth.BasicAuth{Username: envTOKEN, Password: envTOKEN},
|
2023-10-02 02:01:46 +03:00
|
|
|
Force: true,
|
2023-10-02 01:16:16 +03:00
|
|
|
}); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to push repo branch: %v", err)
|
2023-09-30 01:18:30 +03:00
|
|
|
}
|
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
data := map[string]string{
|
|
|
|
"Name": path,
|
|
|
|
"VersionOld": mod.Module.Version,
|
|
|
|
"VersionNew": mod.Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tplTitle.Execute(wTitle, data); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to execute template: %v", err)
|
2023-09-30 00:45:29 +03:00
|
|
|
}
|
|
|
|
if err = tplBody.Execute(wBody, data); err != nil {
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Fatal(ctx, "failed to execute template: %v", err)
|
2023-09-30 00:45:29 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 09:45:50 +03:00
|
|
|
body := map[string]string{
|
2023-10-02 22:26:02 +03:00
|
|
|
"base": branch,
|
2023-09-30 00:45:29 +03:00
|
|
|
"body": wBody.String(),
|
|
|
|
"head": fmt.Sprintf("pkgdash/go_modules/%s-%s", path, mod.Version),
|
|
|
|
"title": wTitle.String(),
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "raw body: %#+v", body)
|
2023-09-30 01:04:31 +03:00
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
buf, err = json.Marshal(body)
|
2023-09-27 09:45:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-30 01:04:31 +03:00
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
log.Info(ctx, "marshal body: %s", buf)
|
2023-09-27 09:45:50 +03:00
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, envAPIURL+"/repos/"+envREPOSITORY+"/pulls?token="+envTOKEN, bytes.NewReader(buf))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2023-09-23 21:16:26 +03:00
|
|
|
|
2023-09-27 09:45:50 +03:00
|
|
|
rsp, err := http.DefaultClient.Do(req)
|
2023-09-23 21:16:26 +03:00
|
|
|
if err != nil {
|
2023-09-27 09:45:50 +03:00
|
|
|
return err
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
2023-09-30 01:05:02 +03:00
|
|
|
if rsp.StatusCode != http.StatusCreated {
|
2023-09-30 01:00:13 +03:00
|
|
|
buf, _ = io.ReadAll(rsp.Body)
|
|
|
|
return fmt.Errorf("unknown error: %s", buf)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
2023-09-30 00:45:29 +03:00
|
|
|
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 09:45:50 +03:00
|
|
|
return nil
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|