chore(builds): remove github-release tool

This now lives in the jimeh/emacs-builds repo, which focuses on building
and publishing binary releases, using the build-emacs-for-macos script.
This commit is contained in:
2021-05-08 19:10:46 +01:00
parent 1df39fafe6
commit 81a96f4d60
11 changed files with 0 additions and 891 deletions

View File

@@ -1,51 +0,0 @@
package main
import (
"fmt"
"regexp"
"github.com/urfave/cli/v2"
)
var nonAlphaNum = regexp.MustCompile(`[^\w-_]+`)
var checkCmd = &cli.Command{
Name: "check",
Usage: "Check if GitHub release and asset exists",
UsageText: "github-release [global options] check [options]",
Action: actionHandler(checkAction),
}
func checkAction(c *cli.Context, opts *globalOptions) error {
gh := opts.gh
repo := opts.repo
plan, err := LoadPlan(opts.plan)
if err != nil {
return err
}
releaseName := plan.ReleaseName()
fmt.Printf(
"Checking github.com/%s for release: %s\n",
repo.String(), releaseName,
)
release, _, err := gh.Repositories.GetReleaseByTag(
c.Context, repo.Owner, repo.Name, releaseName,
)
if err != nil {
return err
}
filename := plan.ReleaseAsset()
fmt.Printf("checking release for asset: %s\n", filename)
for _, a := range release.Assets {
if a.Name != nil && filename == *a.Name {
return nil
}
}
return fmt.Errorf("release does contain asset: %s", filename)
}

View File

@@ -1,22 +0,0 @@
package main
import (
"context"
"net/http"
"github.com/google/go-github/v35/github"
"golang.org/x/oauth2"
)
func NewGitHubClient(ctx context.Context, token string) *github.Client {
var tc *http.Client
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc = oauth2.NewClient(ctx, ts)
}
return github.NewClient(tc)
}

View File

@@ -1,74 +0,0 @@
package main
import (
"fmt"
"os"
"github.com/google/go-github/v35/github"
"github.com/urfave/cli/v2"
)
var app = &cli.App{
Name: "github-release",
Usage: "manage GitHub releases",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "github-token",
Aliases: []string{"t"},
Usage: "GitHub API Token (read from GITHUB_TOKEN if set)",
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: "Owner/name of GitHub repo to publish releases to",
EnvVars: []string{"GITHUB_REPOSITORY"},
Value: "jimeh/build-emacs-for-macos",
},
&cli.PathFlag{
Name: "plan",
Aliases: []string{"p"},
Usage: "Load plan from `FILE`",
EnvVars: []string{"BUILD_PLAN"},
Required: true,
TakesFile: true,
},
},
Commands: []*cli.Command{
checkCmd,
publishCmd,
planCmd,
},
}
type globalOptions struct {
gh *github.Client
repo *Repo
plan string
}
func actionHandler(
f func(*cli.Context, *globalOptions) error,
) func(*cli.Context) error {
return func(c *cli.Context) error {
token := c.String("github-token")
if t := os.Getenv("GITHUB_TOKEN"); t != "" {
token = t
}
opts := &globalOptions{
gh: NewGitHubClient(c.Context, token),
repo: NewRepo(c.String("repo")),
plan: c.String("plan"),
}
return f(c, opts)
}
}
func main() {
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error())
os.Exit(1)
}
}

View File

@@ -1,44 +0,0 @@
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

@@ -1,53 +0,0 @@
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
type Plan struct {
Ref string `yaml:"ref"`
SHA string `yaml:"sha"`
Date string `yaml:"date"`
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, "-")
if ref == "master" {
ref = "nightly"
}
return fmt.Sprintf("Emacs.%s.%s.%s", s.Date, s.SHA[0:6], ref)
}
func (s *Plan) ReleaseAsset() string {
name := filepath.Base(s.Archive)
ext := filepath.Ext(s.Archive)
name = name[0 : len(name)-len(ext)]
name = regexp.MustCompile(`^Emacs\.app-`).ReplaceAllString(name, "Emacs")
name = regexp.MustCompile(`\.`).ReplaceAllString(name, "-")
name = nonAlphaNum.ReplaceAllString(name, ".")
name = strings.TrimRight(name, ".")
return name + ext
}

View File

@@ -1,77 +0,0 @@
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

@@ -1,101 +0,0 @@
package main
import (
"fmt"
"net/http"
"os"
"github.com/google/go-github/v35/github"
"github.com/urfave/cli/v2"
)
var publishCmd = &cli.Command{
Name: "publish",
Usage: "publish a release",
UsageText: "github-release [global-options] publish [options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "github-sha",
Aliases: []string{"s"},
Usage: "Git SHA of repo to create release on",
EnvVars: []string{"GITHUB_SHA"},
},
&cli.BoolFlag{
Name: "prerelease",
Usage: "Git SHA of repo to create release on",
EnvVars: []string{"RELEASE_PRERELEASE"},
Value: true,
},
},
Action: actionHandler(publishAction),
}
func publishAction(c *cli.Context, opts *globalOptions) error {
gh := opts.gh
repo := opts.repo
plan, err := LoadPlan(opts.plan)
if err != nil {
return err
}
releaseName := plan.ReleaseName()
githubSHA := c.String("github-sha")
prerelease := c.Bool("prerelease")
fmt.Printf("prerelease: %+v\n", prerelease)
assetFile, err := os.Open(plan.Archive)
if err != nil {
return err
}
fmt.Printf("Fetching release %s\n", releaseName)
release, resp, err := gh.Repositories.GetReleaseByTag(
c.Context, repo.Owner, repo.Name, releaseName,
)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
fmt.Printf("Release %s not found, creating...\n", releaseName)
release, _, err = gh.Repositories.CreateRelease(
c.Context, repo.Owner, repo.Name, &github.RepositoryRelease{
Name: &releaseName,
TagName: &releaseName,
TargetCommitish: &githubSHA,
Prerelease: &prerelease,
},
)
if err != nil {
return err
}
} else {
return err
}
}
if release.GetPrerelease() != prerelease {
release.Prerelease = &prerelease
release, _, err = gh.Repositories.EditRelease(
c.Context, repo.Owner, repo.Name, release.GetID(), release,
)
if err != nil {
return err
}
}
assetFilename := plan.ReleaseAsset()
fmt.Printf("Uploading asset %s\n", assetFilename)
_, _, err = gh.Repositories.UploadReleaseAsset(
c.Context, repo.Owner, repo.Name, release.GetID(),
&github.UploadOptions{Name: assetFilename},
assetFile,
)
if err != nil {
return err
}
fmt.Printf("Release published at: %s\n", release.GetHTMLURL())
return nil
}

View File

@@ -1,21 +0,0 @@
package main
import "strings"
type Repo struct {
Owner string
Name string
}
func NewRepo(ownerAndRepo string) *Repo {
parts := strings.SplitN(ownerAndRepo, "/", 2)
return &Repo{
Owner: parts[0],
Name: parts[1],
}
}
func (s *Repo) String() string {
return s.Owner + "/" + s.Name
}