rename cli

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-09-27 09:45:50 +03:00
parent cfb7cb0f26
commit 3564c55f3d

View File

@ -3,7 +3,9 @@ package main
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"text/template" "text/template"
@ -130,33 +132,23 @@ func main() {
modules.Updates(updateOptions) modules.Updates(updateOptions)
data := &Data{Modules: updateOptions.Modules}
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)
if err = tplTitle.Execute(wTitle, data); err != nil {
logger.Fatalf(ctx, "failed to execute template: %v", err)
}
tplBody, err := template.New("pull_request_title").Parse(cfg.PullRequestTitle)
if err != nil {
logger.Fatalf(ctx, "failed to parse template: %v", err)
}
wBody := bytes.NewBuffer(nil)
if err = tplBody.Execute(wBody, data); err != nil {
logger.Fatalf(ctx, "failed to execute template: %v", err)
}
for path, u := range mvs { for path, u := range mvs {
fmt.Printf("%s from %s to %s\n", path, u.Module.Version, u.Version) fmt.Printf("%s from %s to %s\n", path, u.Module.Version, u.Version)
} }
repoMgmt := getRepoMgmt()
if repoMgmt == "unknown" {
logger.Fatalf(ctx, "failed to get repo management")
}
switch repoMgmt {
case "gitea":
err = giteaPullRequest(ctx, mvs)
}
if err != nil {
logger.Fatalf(ctx, "failed to create pr: %v", err)
}
} }
func getRepoMgmt() string { func getRepoMgmt() string {
@ -173,5 +165,75 @@ func getRepoMgmt() string {
return "unknown" return "unknown"
} }
func giteaPullRequest(mods []module.Version) { func giteaPullRequest(ctx context.Context, cfg *Config, mods []module.Version) error {
envAPIURL := os.Getenv("GITHUB_API_URL")
envREPOSITORY := os.Getenv("GITHUB_REPOSITORY")
envTOKEN := os.Getenv("GITHUB_TOKEN")
var buf []byte
var err error
data := &Data{Modules: mods}
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)
if err = tplTitle.Execute(wTitle, data); err != nil {
logger.Fatalf(ctx, "failed to execute template: %v", err)
}
tplBody, err := template.New("pull_request_title").Parse(cfg.PullRequestBody)
if err != nil {
logger.Fatalf(ctx, "failed to parse template: %v", err)
}
wBody := bytes.NewBuffer(nil)
if err = tplBody.Execute(wBody, data); err != nil {
logger.Fatalf(ctx, "failed to execute template: %v", err)
}
for _, mod := range mods {
body := map[string]string{
"base": "",
"body": "",
"head": "",
"title": "",
}
buf, err := json.Marshal(body)
if err != nil {
return err
}
/*
'https://try.gitea.io/api/v1/repos/org/repo/pulls?token=myaccesstoken' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"base": "main",
"body": "This is a PR",
"head": "username:feature-branch",
"title": "PR title"
*/
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")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unknown error")
}
}
return nil
} }