1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-18 03:56:52 +02:00

changing repo config

This commit is contained in:
Carlos Alexandro Becker 2017-03-22 21:01:29 -03:00
parent a505f74627
commit 45ec67efb3
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
13 changed files with 120 additions and 125 deletions

View File

@ -247,7 +247,9 @@ archive:
release:
# Repo in which the release will be created.
# Default is extracted from the origin remote URL.
repo: user/repo
github:
owner: user
name: repo
```
### Homebrew tap customization
@ -259,7 +261,9 @@ Check [the Homebrew documentation](https://github.com/Homebrew/brew/blob/master/
# goreleaser.yml
brew:
# Reporitory to push the tap to.
repo: user/homebrew-tap
github:
owner: user
name: homebrew-tap
# Folder inside the repository to put the formula.
# Default is the root folder.

View File

@ -6,9 +6,21 @@ import (
yaml "gopkg.in/yaml.v1"
)
// Repo represents any kind of repo (github, gitlab, etc)
type Repo struct {
Owner string
Name string
}
// String of ther repo, e.g. owner/name
func (r Repo) String() string {
return r.Owner + "/" + r.Name
}
// Homebrew contains the brew section
type Homebrew struct {
Repo string
Repo string // deprecated!
GitHub Repo
Folder string
Caveats string
Plist string
@ -44,7 +56,8 @@ type Archive struct {
// Release config used for the GitHub release
type Release struct {
Repo string
Repo string // deprecated!
GitHub Repo
}
// FPM config

View File

@ -13,21 +13,14 @@ type GitInfo struct {
Diff string
}
// Repo owner/name pair
type Repo struct {
Owner, Name string
}
// Context carries along some data through the pipes
type Context struct {
ctx.Context
Config config.Project
Token string
Git GitInfo
ReleaseRepo Repo
BrewRepo Repo
Archives map[string]string
Version string
Config config.Project
Token string
Git GitInfo
Archives map[string]string
Version string
}
// New context

View File

@ -15,8 +15,6 @@ import (
"github.com/goreleaser/goreleaser/pipeline/fpm"
"github.com/goreleaser/goreleaser/pipeline/git"
"github.com/goreleaser/goreleaser/pipeline/release"
"github.com/goreleaser/goreleaser/pipeline/repos"
"github.com/goreleaser/goreleaser/pipeline/source"
"github.com/urfave/cli"
)
@ -66,14 +64,13 @@ func main() {
func pipes(buildOnly bool) []pipeline.Pipe {
var pipes = []pipeline.Pipe{
defaults.Pipe{}, // load default configs
repos.Pipe{}, // split repos into owner/name pairs
}
if !buildOnly {
pipes = append(
pipes,
git.Pipe{}, // get current tag info
env.Pipe{}, // load and validate environment variables
source.Pipe{}, // validate current git state
git.Pipe{}, // get current tag info
env.Pipe{}, // load and validate environment variables
// source.Pipe{}, // validate current git state
)
}
pipes = append(

View File

@ -10,6 +10,7 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/goreleaser/clients"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/sha256sum"
)
@ -21,7 +22,7 @@ var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build")
const formula = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
version "{{ .Version }}"
sha256 "{{ .SHA256 }}"
@ -64,7 +65,7 @@ type templateData struct {
Name string
Desc string
Homepage string
Repo string
Repo config.Repo // FIXME: will not work for anything but github right now.
Tag string
Version string
BinaryName string
@ -88,7 +89,16 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Brew.Repo == "" {
// TODO: remove this block in next release cycle
if ctx.Config.Brew.Repo != "" {
log.Println("The `brew.repo` syntax is deprecated and will soon be removed. Please check the README for more info.")
var ss = strings.Split(ctx.Config.Brew.Repo, "/")
ctx.Config.Brew.GitHub = config.Repo{
Owner: ss[0],
Name: ss[1],
}
}
if ctx.Config.Brew.GitHub.Name == "" {
return nil
}
client := clients.GitHub(ctx)
@ -96,7 +106,7 @@ func (Pipe) Run(ctx *context.Context) error {
ctx.Config.Brew.Folder, ctx.Config.Build.BinaryName+".rb",
)
log.Println("Pushing", path, "to", ctx.Config.Brew.Repo)
log.Println("Pushing", path, "to", ctx.Config.Brew.GitHub.String())
out, err := buildFormula(ctx, client)
if err != nil {
return err
@ -113,19 +123,31 @@ func (Pipe) Run(ctx *context.Context) error {
),
}
owner := ctx.BrewRepo.Owner
repo := ctx.BrewRepo.Name
file, _, res, err := client.Repositories.GetContents(
ctx, owner, repo, path, &github.RepositoryContentGetOptions{},
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
&github.RepositoryContentGetOptions{},
)
if err != nil && res.StatusCode == 404 {
_, _, err = client.Repositories.CreateFile(
ctx, owner, repo, path, options,
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return err
}
options.SHA = file.SHA
_, _, err = client.Repositories.UpdateFile(ctx, owner, repo, path, options)
_, _, err = client.Repositories.UpdateFile(
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return err
}
@ -153,7 +175,9 @@ func dataFor(
var homepage string
var description string
rep, _, err := client.Repositories.Get(
ctx, ctx.ReleaseRepo.Owner, ctx.ReleaseRepo.Name,
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
)
if err != nil {
return
@ -180,7 +204,7 @@ func dataFor(
Name: formulaNameFor(ctx.Config.Build.BinaryName),
Desc: description,
Homepage: homepage,
Repo: ctx.Config.Release.Repo,
Repo: ctx.Config.Release.GitHub,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
BinaryName: ctx.Config.Build.BinaryName,

View File

@ -3,6 +3,7 @@ package brew
import (
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/stretchr/testify/assert"
)
@ -23,7 +24,7 @@ var defaultTemplateData = templateData{
Desc: "Some desc",
Homepage: "https://google.com",
Name: "Test",
Repo: "caarlos0/test",
Repo: config.Repo{"caarlos0", "test"},
Tag: "v0.1.3",
Version: "0.1.3",
File: "test_Darwin_x86_64",

View File

@ -3,6 +3,7 @@ package defaults
import (
"errors"
"io/ioutil"
"log"
"strings"
"github.com/goreleaser/goreleaser/context"
@ -20,16 +21,21 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Release.Repo == "" {
// TODO: remove this block in next release cycle
if ctx.Config.Release.Repo != "" {
log.Println("The `release.repo` syntax is deprecated and will soon be removed. Please check the README for more info.")
ctx.Config.Release.GitHub = toRepo(ctx.Config.Release.Repo)
}
if ctx.Config.Release.GitHub.Name == "" {
repo, err := remoteRepo()
ctx.Config.Release.Repo = repo
ctx.Config.Release.GitHub = repo
if err != nil {
return errors.New("failed reading repo from git: " + err.Error())
}
}
if ctx.Config.Build.BinaryName == "" {
ctx.Config.Build.BinaryName = strings.Split(ctx.Config.Release.Repo, "/")[1]
ctx.Config.Build.BinaryName = ctx.Config.Release.GitHub.Name
}
if ctx.Config.Build.Main == "" {
ctx.Config.Build.Main = "."

View File

@ -17,7 +17,8 @@ func TestFillBasicData(t *testing.T) {
assert.NoError(Pipe{}.Run(ctx))
assert.Equal("goreleaser/goreleaser", ctx.Config.Release.Repo)
assert.Equal("goreleaser", ctx.Config.Release.GitHub.Owner)
assert.Equal("goreleaser", ctx.Config.Release.GitHub.Name)
assert.Equal("goreleaser", ctx.Config.Build.BinaryName)
assert.Equal(".", ctx.Config.Build.Main)
assert.Equal("tar.gz", ctx.Config.Archive.Format)

View File

@ -4,19 +4,21 @@ import (
"errors"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/config"
)
// remoteRepo gets the repo name from the Git config.
func remoteRepo() (result string, err error) {
func remoteRepo() (result config.Repo, err error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
bts, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(err.Error() + ": " + string(bts))
return result, errors.New(err.Error() + ": " + string(bts))
}
return extractRepoFromURL(string(bts)), nil
}
func extractRepoFromURL(s string) string {
func extractRepoFromURL(s string) config.Repo {
for _, r := range []string{
"git@github.com:",
".git",
@ -25,5 +27,13 @@ func extractRepoFromURL(s string) string {
} {
s = strings.Replace(s, r, "", -1)
}
return s
return toRepo(s)
}
func toRepo(s string) config.Repo {
var ss = strings.Split(s, "/")
return config.Repo{
Owner: ss[0],
Name: ss[1],
}
}

View File

@ -8,19 +8,19 @@ import (
func TestRepoName(t *testing.T) {
assert := assert.New(t)
name, err := remoteRepo()
repo, err := remoteRepo()
assert.NoError(err)
assert.Equal("goreleaser/goreleaser", name)
assert.Equal("goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromGitURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("git@github.com:goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", url)
repo := extractRepoFromURL("git@github.com:goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromHttpsURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", url)
repo := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", repo.String())
}

View File

@ -45,21 +45,35 @@ func (Pipe) Run(ctx *context.Context) error {
}
func getOrCreateRelease(client *github.Client, ctx *context.Context) (*github.RepositoryRelease, error) {
owner := ctx.ReleaseRepo.Owner
repo := ctx.ReleaseRepo.Name
data := &github.RepositoryRelease{
Name: github.String(ctx.Git.CurrentTag),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(description(ctx.Git.Diff)),
}
r, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, ctx.Git.CurrentTag)
r, _, err := client.Repositories.GetReleaseByTag(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
ctx.Git.CurrentTag,
)
if err != nil {
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
r, _, err = client.Repositories.CreateRelease(ctx, owner, repo, data)
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
r, _, err = client.Repositories.CreateRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
data,
)
return r, err
}
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
r, _, err = client.Repositories.EditRelease(ctx, owner, repo, *r.ID, data)
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
r, _, err = client.Repositories.EditRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
*r.ID,
data,
)
return r, err
}
@ -94,8 +108,8 @@ func upload(ctx *context.Context, client *github.Client, releaseID int, archive,
log.Println("Uploading", file.Name())
_, _, err = client.Repositories.UploadReleaseAsset(
ctx,
ctx.ReleaseRepo.Owner,
ctx.ReleaseRepo.Name,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
releaseID,
&github.UploadOptions{Name: archive},
file,

View File

@ -1,38 +0,0 @@
package repos
import (
"strings"
"github.com/goreleaser/goreleaser/context"
)
// Pipe for brew deployment
type Pipe struct{}
// Description of the pipe
func (Pipe) Description() string {
return "Setting repositories"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
owner, name := split(ctx.Config.Release.Repo)
ctx.ReleaseRepo = context.Repo{
Owner: owner,
Name: name,
}
owner, name = split(ctx.Config.Brew.Repo)
ctx.BrewRepo = context.Repo{
Owner: owner,
Name: name,
}
return
}
func split(pair string) (string, string) {
parts := strings.Split(pair, "/")
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}

View File

@ -1,30 +0,0 @@
package repos
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSplit(t *testing.T) {
assert := assert.New(t)
a, b := split("a/b")
assert.Equal("a", a)
assert.Equal("b", b)
a, b = split("")
assert.Equal("", a)
assert.Equal("", b)
a, b = split("a")
assert.Equal("a", a)
assert.Equal("", b)
a, b = split("a/")
assert.Equal("a", a)
assert.Equal("", b)
a, b = split("/b")
assert.Equal("", a)
assert.Equal("b", b)
}