mirror of
https://github.com/jimeh/build-emacs-for-macos.git
synced 2026-02-19 13:06:38 +00:00
feat(release): initial attempt at providing automatic builds
This commit is contained in:
47
cmd/github-release/check_cmd.go
Normal file
47
cmd/github-release/check_cmd.go
Normal file
@@ -0,0 +1,47 @@
|
||||
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 := opts.plan
|
||||
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)
|
||||
}
|
||||
22
cmd/github-release/github.go
Normal file
22
cmd/github-release/github.go
Normal file
@@ -0,0 +1,22 @@
|
||||
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)
|
||||
}
|
||||
85
cmd/github-release/main.go
Normal file
85
cmd/github-release/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/go-github/v35/github"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
type globalOptions struct {
|
||||
gh *github.Client
|
||||
repo *Repo
|
||||
plan *Plan
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
opts := &globalOptions{
|
||||
gh: NewGitHubClient(c.Context, token),
|
||||
repo: NewRepo(c.String("repo")),
|
||||
plan: 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)
|
||||
}
|
||||
}
|
||||
38
cmd/github-release/plan.go
Normal file
38
cmd/github-release/plan.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Plan struct {
|
||||
Ref string `yaml:"ref"`
|
||||
SHA string `yaml:"sha"`
|
||||
Date string `yaml:"date"`
|
||||
Archive string `yaml:"archive"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
98
cmd/github-release/publish_cmd.go
Normal file
98
cmd/github-release/publish_cmd.go
Normal file
@@ -0,0 +1,98 @@
|
||||
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 := opts.plan
|
||||
|
||||
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
|
||||
}
|
||||
21
cmd/github-release/repo.go
Normal file
21
cmd/github-release/repo.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user