fix(release): attempt to fix issue with talking to GitHub API

This commit is contained in:
2021-05-07 01:12:57 +01:00
parent d684cf560f
commit 272a3000a1
8 changed files with 173 additions and 33 deletions

View File

@@ -19,7 +19,11 @@ var checkCmd = &cli.Command{
func checkAction(c *cli.Context, opts *globalOptions) error {
gh := opts.gh
repo := opts.repo
plan := opts.plan
plan, err := LoadPlan(opts.plan)
if err != nil {
return err
}
releaseName := plan.ReleaseName()
fmt.Printf(

View File

@@ -6,7 +6,6 @@ import (
"github.com/google/go-github/v35/github"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
var app = &cli.App{
@@ -37,30 +36,20 @@ var app = &cli.App{
Commands: []*cli.Command{
checkCmd,
publishCmd,
planCmd,
},
}
type globalOptions struct {
gh *github.Client
repo *Repo
plan *Plan
plan string
}
func actionHandler(
f func(*cli.Context, *globalOptions) error,
) func(*cli.Context) error {
return func(c *cli.Context) error {
b, err := os.ReadFile(c.Path("plan"))
if err != nil {
return err
}
plan := &Plan{}
err = yaml.Unmarshal(b, plan)
if err != nil {
return err
}
token := c.String("github-token")
if t := os.Getenv("GITHUB_TOKEN"); t != "" {
token = t
@@ -69,7 +58,7 @@ func actionHandler(
opts := &globalOptions{
gh: NewGitHubClient(c.Context, token),
repo: NewRepo(c.String("repo")),
plan: plan,
plan: c.String("plan"),
}
return f(c, opts)

View File

@@ -0,0 +1,44 @@
package main
import (
"os/exec"
"strings"
)
var OSInfo = &osInfo{}
type osInfo struct {
version string
arch string
}
func (s *osInfo) Arch() string {
if s.arch != "" {
return s.arch
}
cmd, err := exec.Command("uname", "-m").CombinedOutput()
if err != nil {
panic(err)
}
s.arch = strings.TrimSpace(string(cmd))
return s.arch
}
func (s *osInfo) Version() string {
if s.version != "" {
return s.version
}
cmd, err := exec.Command("sw_vers", "-productVersion").CombinedOutput()
if err != nil {
panic(err)
}
parts := strings.Split(string(cmd), ".")
s.version = strings.Join(parts[0:2], ".")
return s.version
}

View File

@@ -2,9 +2,12 @@ package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
type Plan struct {
@@ -14,6 +17,18 @@ type Plan struct {
Archive string `yaml:"archive"`
}
func LoadPlan(filename string) (*Plan, error) {
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
plan := &Plan{}
err = yaml.Unmarshal(b, plan)
return plan, err
}
func (s *Plan) ReleaseName() string {
ref := nonAlphaNum.ReplaceAllString(s.Ref, "-")
ref = regexp.MustCompile(`\.`).ReplaceAllString(ref, "-")

View File

@@ -0,0 +1,77 @@
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
var planCmd = &cli.Command{
Name: "plan",
Usage: "Plan if GitHub release and asset exists",
UsageText: "github-release [global options] plan [<gif-ref>]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "emacs-mirror-repo",
Usage: "Github owner/repo to get Emacs commit info from",
Aliases: []string{"e"},
EnvVars: []string{"EMACS_MIRROR_REPO"},
Value: "emacs-mirror/emacs",
},
},
Action: actionHandler(planAction),
}
func planAction(c *cli.Context, opts *globalOptions) error {
gh := opts.gh
planFile := opts.plan
emacsRepo := NewRepo(c.String("emacs-mirror-repo"))
ref := c.Args().Get(0)
if ref == "" {
ref = "master"
}
commit, _, err := gh.Repositories.GetCommit(
c.Context, emacsRepo.Owner, emacsRepo.Name, ref,
)
if err != nil {
return err
}
commitDate := commit.GetCommit().Committer.GetDate()
date := commitDate.Format("2006-01-02")
sha := commit.GetSHA()
archive := fmt.Sprintf(
"Emacs.app-[%s][%s][%s][%s][%s].tbz",
nonAlphaNum.ReplaceAllString(ref, "-"),
date,
sha[0:7],
"macOS-"+OSInfo.Version(),
OSInfo.Arch(),
)
rootDir, err := os.Getwd()
if err != nil {
return err
}
buildsDir := filepath.Join(rootDir, "builds")
plan := &Plan{
Ref: ref,
SHA: sha,
Date: date,
Archive: filepath.Join(buildsDir, archive),
}
b, err := yaml.Marshal(plan)
if err != nil {
return err
}
return os.WriteFile(planFile, b, 0666)
}

View File

@@ -33,7 +33,10 @@ var publishCmd = &cli.Command{
func publishAction(c *cli.Context, opts *globalOptions) error {
gh := opts.gh
repo := opts.repo
plan := opts.plan
plan, err := LoadPlan(opts.plan)
if err != nil {
return err
}
releaseName := plan.ReleaseName()
githubSHA := c.String("github-sha")