2016-12-29 02:23:39 +02:00
|
|
|
package release
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"github.com/goreleaser/releaser/config"
|
|
|
|
"github.com/goreleaser/releaser/uname"
|
|
|
|
"golang.org/x/oauth2"
|
2016-12-29 15:21:50 +02:00
|
|
|
"os/exec"
|
2016-12-29 03:21:49 +02:00
|
|
|
)
|
2016-12-29 02:23:39 +02:00
|
|
|
|
|
|
|
func Release(version, diff string, config config.ProjectConfig) error {
|
2016-12-29 03:21:49 +02:00
|
|
|
fmt.Println("Creating release", version, "on repo", config.Repo, "...")
|
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: config.Token},
|
|
|
|
)
|
|
|
|
tc := oauth2.NewClient(context.Background(), ts)
|
|
|
|
client := github.NewClient(tc)
|
|
|
|
|
|
|
|
owner := strings.Split(config.Repo, "/")[0]
|
|
|
|
repo := strings.Split(config.Repo, "/")[1]
|
2016-12-29 15:21:50 +02:00
|
|
|
releaseData := &github.RepositoryRelease{
|
2016-12-29 03:21:49 +02:00
|
|
|
Name: github.String(version),
|
|
|
|
TagName: github.String(version),
|
2016-12-29 15:21:50 +02:00
|
|
|
Body: github.String(description(diff)),
|
|
|
|
}
|
|
|
|
r, _, err := client.Repositories.CreateRelease(owner, repo, releaseData)
|
2016-12-29 03:21:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, system := range config.Build.Oses {
|
|
|
|
for _, arch := range config.Build.Arches {
|
|
|
|
if err := upload(client, *r.ID, owner, repo, system, arch, config.BinaryName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 02:23:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-29 03:21:49 +02:00
|
|
|
|
2016-12-29 15:21:50 +02:00
|
|
|
func description(diff string) string {
|
2016-12-29 18:03:34 +02:00
|
|
|
result := "Changelog:\n" + diff + "\n\nAutomated with @goreleaser"
|
2016-12-29 15:21:50 +02:00
|
|
|
cmd := exec.Command("go", "version")
|
|
|
|
bts, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return result + "\nBuilt with " + string(bts)
|
|
|
|
}
|
|
|
|
|
2016-12-29 03:21:49 +02:00
|
|
|
func upload(client *github.Client, releaseID int, owner, repo, system, arch, binaryName string) error {
|
|
|
|
name := binaryName + "_" + uname.FromGo(system) + "_" + uname.FromGo(arch) + ".tar.gz"
|
|
|
|
file, err := os.Open("dist/" + name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
fmt.Println("Uploading", file.Name(), "...")
|
|
|
|
_, _, err = client.Repositories.UploadReleaseAsset(owner, repo, releaseID, &github.UploadOptions{
|
|
|
|
Name: name,
|
|
|
|
}, file)
|
|
|
|
return err
|
|
|
|
}
|