1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00

101 lines
2.5 KiB
Go
Raw Normal View History

2016-12-28 22:23:39 -02:00
package release
2016-12-28 23:21:49 -02:00
import (
"context"
2016-12-30 09:27:35 -02:00
"log"
2016-12-28 23:21:49 -02:00
"os"
2016-12-29 18:10:11 -02:00
"os/exec"
2016-12-28 23:21:49 -02:00
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
2016-12-30 09:48:06 -02:00
"github.com/goreleaser/releaser/split"
2016-12-28 23:21:49 -02:00
"golang.org/x/oauth2"
2016-12-29 14:17:49 -02:00
"golang.org/x/sync/errgroup"
2016-12-28 23:21:49 -02:00
)
2016-12-28 22:23:39 -02:00
2016-12-30 12:41:59 -02:00
// Pipe for github release
2016-12-30 09:27:35 -02:00
type Pipe struct{}
2016-12-30 12:41:59 -02:00
// Name of the pipe
2016-12-30 09:27:35 -02:00
func (Pipe) Name() string {
return "GithubRelease"
}
2016-12-30 12:41:59 -02:00
// Run the pipe
func (Pipe) Run(config config.ProjectConfig) error {
2016-12-28 23:21:49 -02:00
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
2017-01-13 21:42:28 -02:00
r, err := getOrCreateRelease(client, config)
2016-12-28 23:21:49 -02:00
if err != nil {
return err
}
2016-12-29 14:17:49 -02:00
var g errgroup.Group
2016-12-28 23:21:49 -02:00
for _, system := range config.Build.Oses {
for _, arch := range config.Build.Arches {
2016-12-29 14:17:49 -02:00
system := system
arch := arch
g.Go(func() error {
2017-01-06 13:48:25 -02:00
return upload(client, *r.ID, system, arch, config)
2016-12-29 14:17:49 -02:00
})
2016-12-28 23:21:49 -02:00
}
}
2016-12-29 14:17:49 -02:00
return g.Wait()
2016-12-28 22:23:39 -02:00
}
2016-12-28 23:21:49 -02:00
2017-01-13 21:42:28 -02:00
func getOrCreateRelease(client *github.Client, config config.ProjectConfig) (*github.RepositoryRelease, error) {
owner, repo := split.OnSlash(config.Repo)
data := &github.RepositoryRelease{
Name: github.String(config.Git.CurrentTag),
TagName: github.String(config.Git.CurrentTag),
Body: github.String(description(config.Git.Diff)),
}
r, res, err := client.Repositories.GetReleaseByTag(owner, repo, config.Git.CurrentTag)
2017-01-13 21:45:16 -02:00
if err != nil && res.StatusCode == 404 {
2017-01-13 21:42:28 -02:00
log.Println("Creating release", config.Git.CurrentTag, "on", config.Repo, "...")
2017-01-13 21:45:16 -02:00
r, _, err = client.Repositories.CreateRelease(owner, repo, data)
2017-01-13 21:42:28 -02:00
return r, err
}
log.Println("Updating existing release", config.Git.CurrentTag, "on", config.Repo, "...")
r, _, err = client.Repositories.EditRelease(owner, repo, *r.ID, data)
return r, err
}
2016-12-29 11:21:50 -02:00
func description(diff string) string {
2016-12-30 09:56:54 -02:00
result := "## Changelog\n" + diff + "\n\n--\nAutomated with @goreleaser"
2016-12-29 11:21:50 -02:00
cmd := exec.Command("go", "version")
bts, err := cmd.CombinedOutput()
if err != nil {
return result
}
return result + "\nBuilt with " + string(bts)
}
2017-01-06 13:48:25 -02:00
func upload(client *github.Client, releaseID int, system, arch string, config config.ProjectConfig) error {
owner, repo := split.OnSlash(config.Repo)
2017-01-11 14:47:56 -02:00
name, err := config.ArchiveName(system, arch)
2017-01-11 14:28:12 -02:00
if err != nil {
return err
}
name = name + "." + config.Archive.Format
2016-12-28 23:21:49 -02:00
file, err := os.Open("dist/" + name)
if err != nil {
return err
}
2017-01-02 13:20:33 -02:00
defer func() {
_ = file.Close()
}()
2016-12-30 09:27:35 -02:00
log.Println("Uploading", file.Name(), "...")
2017-01-02 13:20:33 -02:00
_, _, err = client.Repositories.UploadReleaseAsset(
owner,
repo,
releaseID,
&github.UploadOptions{Name: name},
file,
)
2016-12-28 23:21:49 -02:00
return err
}