1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

87 lines
2.2 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 (
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"
2017-01-14 14:50:46 -02:00
"github.com/goreleaser/releaser/clients"
2017-01-14 12:55:52 -02:00
"github.com/goreleaser/releaser/context"
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
2017-01-14 15:14:35 -02:00
func (Pipe) Description() string {
return "Releasing to GitHub..."
2016-12-30 09:27:35 -02:00
}
2016-12-30 12:41:59 -02:00
// Run the pipe
2017-01-14 14:06:57 -02:00
func (Pipe) Run(ctx *context.Context) error {
2017-01-14 14:50:46 -02:00
client := clients.Github(*ctx.Token)
2016-12-28 23:21:49 -02:00
2017-01-14 14:06:57 -02:00
r, err := getOrCreateRelease(client, ctx)
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
2017-01-14 14:06:57 -02:00
for _, archive := range ctx.Archives {
2017-01-14 12:55:52 -02:00
archive := archive
g.Go(func() error {
2017-01-14 14:06:57 -02:00
return upload(client, *r.ID, archive, ctx)
2017-01-14 12:55:52 -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-14 14:06:57 -02:00
func getOrCreateRelease(client *github.Client, ctx *context.Context) (*github.RepositoryRelease, error) {
owner := ctx.Repo.Owner
repo := ctx.Repo.Name
2017-01-13 21:42:28 -02:00
data := &github.RepositoryRelease{
2017-01-14 14:06:57 -02:00
Name: github.String(ctx.Git.CurrentTag),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(description(ctx.Git.Diff)),
2017-01-13 21:42:28 -02:00
}
2017-01-14 14:06:57 -02:00
r, _, err := client.Repositories.GetReleaseByTag(owner, repo, ctx.Git.CurrentTag)
if err != nil {
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.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
}
2017-01-14 14:06:57 -02:00
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Repo, "...")
2017-01-13 21:42:28 -02:00
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-14 14:06:57 -02:00
func upload(client *github.Client, releaseID int, archive string, ctx *context.Context) error {
archive = archive + "." + ctx.Config.Archive.Format
2017-01-14 12:55:52 -02:00
file, err := os.Open("dist/" + archive)
2016-12-28 23:21:49 -02:00
if err != nil {
return err
}
2017-01-14 12:55:52 -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(
2017-01-14 14:06:57 -02:00
ctx.Repo.Owner,
ctx.Repo.Name,
2017-01-02 13:20:33 -02:00
releaseID,
2017-01-14 12:55:52 -02:00
&github.UploadOptions{Name: archive},
2017-01-02 13:20:33 -02:00
file,
)
2016-12-28 23:21:49 -02:00
return err
}