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"
|
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"
|
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
|
|
|
|
2024-02-14 23:03:30 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
|
2023-09-23 21:16:26 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/modules"
|
2024-03-15 21:18:58 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/source"
|
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"
|
2023-09-23 21:16:26 +03:00
|
|
|
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
|
|
|
|
envconfig "go.unistack.org/micro-config-env/v4"
|
|
|
|
fileconfig "go.unistack.org/micro-config-file/v4"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
"golang.org/x/mod/modfile"
|
2024-03-15 21:18:58 +03:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
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
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Data struct {
|
2023-09-30 00:45:29 +03:00
|
|
|
Modules map[string]modules.Update
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-03-15 21:18:58 +03:00
|
|
|
fmt.Println("Start...")
|
2023-09-23 21:16:26 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2023-10-02 23:52:27 +03:00
|
|
|
if err = logger.DefaultLogger.Init(logger.WithCallerSkipCount(3)); err != nil {
|
|
|
|
logger.Errorf(ctx, "logger init error: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:18:58 +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 {
|
|
|
|
logger.Fatalf(ctx, "failed to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, configDir := range configDirs {
|
|
|
|
for _, configFile := range configFiles {
|
|
|
|
c := fileconfig.NewConfig(
|
|
|
|
config.AllowFail(true),
|
|
|
|
config.Struct(cfg),
|
|
|
|
options.Codec(yamlcodec.NewCodec()),
|
|
|
|
fileconfig.Path(filepath.Join(configDir, configFile)),
|
|
|
|
)
|
|
|
|
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to load config: %v", err)
|
|
|
|
}
|
2024-03-15 21:18:58 +03:00
|
|
|
logger.Infof(ctx, "c: %v", c)
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:18:58 +03:00
|
|
|
file, err := os.Open(".gitea/pkgdashcli.yaml")
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "file open: %s", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
d := yaml.NewDecoder(file)
|
|
|
|
if err = d.Decode(&cfg); err != nil {
|
|
|
|
logger.Fatalf(ctx, "Decode err: %s", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof(ctx, "Load config... %s", cfg)
|
|
|
|
|
2023-09-30 01:07:50 +03:00
|
|
|
if cfg.PullRequestBody == "" {
|
|
|
|
cfg.PullRequestBody = DefaultPullRequestBody
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PullRequestTitle == "" {
|
|
|
|
cfg.PullRequestTitle = DefaultPullRequestTitle
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
mfile, err := modfile.Parse(name, buf, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mvs := make(map[string]modules.Update)
|
|
|
|
|
|
|
|
updateOptions := modules.UpdateOptions{
|
|
|
|
Pre: false,
|
|
|
|
Major: false,
|
|
|
|
Cached: true,
|
|
|
|
OnUpdate: func(u modules.Update) {
|
|
|
|
if u.Err != nil {
|
|
|
|
logger.Errorf(ctx, "%s: failed: %v", u.Module.Path, u.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mvs[u.Module.Path] = u
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, req := range mfile.Require {
|
|
|
|
updateOptions.Modules = append(updateOptions.Modules, req.Mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
modules.Updates(updateOptions)
|
|
|
|
|
2024-03-15 21:18:58 +03:00
|
|
|
/*var repoGit, tokenGit string // nameGit = gitea, repoGit = pkgdash, tokenGit = {xxx}, machine??
|
2024-02-14 23:03:30 +03:00
|
|
|
if cfg.Source != nil {
|
|
|
|
repoGit = cfg.Source.TypeGit
|
|
|
|
tokenGit = cfg.Source.Token
|
|
|
|
} else {
|
|
|
|
repoGit = getRepoMgmt()
|
|
|
|
if repoGit == "unknown" {
|
2024-03-15 21:18:58 +03:00
|
|
|
logger.Fatalf(ctx, "pkgdash/main failed to get repo management")
|
2024-02-14 23:03:30 +03:00
|
|
|
}
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
2024-03-15 21:18:58 +03:00
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf(ctx, "pkgdash/main can t get info user: %s", err)
|
|
|
|
} else {
|
|
|
|
n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf(ctx, "pkgdash/main can t parse .netrc: %s", err)
|
|
|
|
}
|
|
|
|
tokenGit = n.Machine(repoGit).Get("password")
|
|
|
|
}
|
2023-09-27 09:45:50 +03:00
|
|
|
|
2024-02-14 23:03:30 +03:00
|
|
|
switch repoGit {
|
2023-09-27 09:45:50 +03:00
|
|
|
case "gitea":
|
2023-10-03 01:01:31 +03:00
|
|
|
for _, branch := range cfg.Branches {
|
|
|
|
err = giteaPullRequest(ctx, cfg, branch, mvs)
|
|
|
|
}
|
2024-03-15 21:18:58 +03:00
|
|
|
}*/
|
|
|
|
|
|
|
|
logger.Infof(ctx, "cfg: %v", cfg)
|
|
|
|
|
|
|
|
gitSource := source.NewSourceControl(*cfg)
|
|
|
|
for _, branch := range cfg.Branches {
|
|
|
|
for pathMod, mod := range mvs {
|
|
|
|
err = gitSource.RequestOpen(ctx, branch, pathMod, mod)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to create pr: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to create pr: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoMgmt() string {
|
2023-09-30 00:45:29 +03:00
|
|
|
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
|
|
|
|
}
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
2023-09-30 00:45:29 +03:00
|
|
|
if p == "/" {
|
|
|
|
return "unknown"
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
2023-09-30 00:45:29 +03:00
|
|
|
p = filepath.Clean(filepath.Join(p, ".."))
|
2023-09-27 09:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:03:30 +03:00
|
|
|
func giteaPullRequest(ctx context.Context, 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 {
|
|
|
|
logger.Fatalf(ctx, "failed to parse template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
logger.Fatalf(ctx, "failed to parse template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wBody := bytes.NewBuffer(nil)
|
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
repo, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true})
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to open repo: %v", err)
|
|
|
|
}
|
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 {
|
|
|
|
logger.Fatalf(ctx, "failed to fetch repo: %v", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-10-03 01:01:31 +03:00
|
|
|
logger.Fatalf(ctx, "failed to get branches: %v", err)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
ref, err := refIter.Next()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ref.Name().String() == branch {
|
|
|
|
headRef = ref
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refIter.Close()
|
|
|
|
|
|
|
|
if headRef == nil {
|
|
|
|
logger.Fatalf(ctx, "failed to get repo branch head")
|
2023-09-23 21:16:26 +03:00
|
|
|
}
|
2023-10-02 02:04:45 +03:00
|
|
|
|
2023-10-02 22:49:22 +03:00
|
|
|
logger.Infof(ctx, "repo head %s", headRef)
|
|
|
|
|
2023-10-02 02:00:19 +03:00
|
|
|
wtree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to get worktree: %v", err)
|
|
|
|
}
|
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 {
|
2023-10-02 19:49:18 +03:00
|
|
|
logger.Fatalf(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 {
|
2023-10-02 19:43:48 +03:00
|
|
|
logger.Infof(ctx, "skip %s as pr already exists %s", path, pull.URL)
|
|
|
|
delete(mods, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 00:45:29 +03:00
|
|
|
for path, mod := range mods {
|
|
|
|
wTitle.Reset()
|
|
|
|
wBody.Reset()
|
|
|
|
|
2023-10-02 22:26:02 +03:00
|
|
|
logger.Infof(ctx, "update %s from %s to %s", path, mod.Module.Version, mod.Version)
|
2023-09-30 00:45:29 +03:00
|
|
|
|
2023-10-02 02:04:45 +03:00
|
|
|
logger.Infof(ctx, "reset worktree")
|
2023-10-02 22:26:02 +03:00
|
|
|
if err = wtree.Reset(&git.ResetOptions{Mode: git.HardReset}); err != nil {
|
2023-10-02 02:00:19 +03:00
|
|
|
logger.Fatalf(ctx, "failed to reset repo branch: %v", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-10-03 00:26:33 +03:00
|
|
|
logger.Fatalf(ctx, "failed to pull repo: %v", err)
|
|
|
|
}
|
2023-10-03 00:01:45 +03:00
|
|
|
|
2023-10-02 23:52:27 +03:00
|
|
|
logger.Infof(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 {
|
2023-10-02 23:52:27 +03:00
|
|
|
logger.Fatalf(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 {
|
|
|
|
logger.Fatalf(ctx, "failed to find go command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
logger.Fatalf(ctx, "failed to run go mod edit: %s err: %v", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.CommandContext(ctx, epath, "mod", "tidy")
|
|
|
|
if out, err = cmd.CombinedOutput(); err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to run go mod tidy: %s err: %v", out, err)
|
|
|
|
}
|
|
|
|
|
2023-10-02 02:06:06 +03:00
|
|
|
logger.Infof(ctx, "worktree add go.mod")
|
2023-10-01 16:28:03 +03:00
|
|
|
if _, err = wtree.Add("go.mod"); err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to add file: %v", err)
|
|
|
|
}
|
2023-10-02 02:06:06 +03:00
|
|
|
|
|
|
|
logger.Infof(ctx, "worktree add go.sum")
|
2023-10-01 16:28:03 +03:00
|
|
|
if _, err = wtree.Add("go.sum"); err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to add file: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-10-02 02:06:06 +03:00
|
|
|
logger.Infof(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 {
|
|
|
|
logger.Fatalf(ctx, "failed to commit: %v", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
logger.Fatalf(ctx, "failed to create repo branch: %v", err)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
logger.Infof(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 {
|
2023-09-30 01:18:30 +03:00
|
|
|
logger.Fatalf(ctx, "failed to push repo branch: %v", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
logger.Fatalf(ctx, "failed to execute template: %v", err)
|
|
|
|
}
|
|
|
|
if err = tplBody.Execute(wBody, data); err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed to execute template: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-09-30 01:04:31 +03:00
|
|
|
logger.Infof(ctx, "raw body: %#+v", body)
|
|
|
|
|
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
|
|
|
|
|
|
|
logger.Infof(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
|
|
|
}
|