Skip to content

Commit

Permalink
Merge branch 'master' into lint
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Sep 27, 2017
2 parents d85a900 + c468c1d commit fe36819
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
10 changes: 10 additions & 0 deletions config/config.go
Expand Up @@ -13,6 +13,13 @@ import (
yaml "gopkg.in/yaml.v2"
)

// GitHubURLs holds the URLs to be used when using github enterprise
type GitHubURLs struct {
API string `yaml:"api,omitempty"`
Upload string `yaml:"upload,omitempty"`
Download string `yaml:"download,omitempty"`
}

// Repo represents any kind of repo (github, gitlab, etc)
type Repo struct {
Owner string `yaml:",omitempty"`
Expand Down Expand Up @@ -190,6 +197,9 @@ type Project struct {
// this is a hack ¯\_(ツ)_/¯
SingleBuild Build `yaml:"build,omitempty"`

// should be set if using github enterprise
GitHubURLs GitHubURLs `yaml:"github_urls,omitempty"`

// test only property indicating the path to the dist folder
Dist string `yaml:"-"`

Expand Down
16 changes: 16 additions & 0 deletions docs/150-github-enterprise.md
@@ -0,0 +1,16 @@
---
title: GitHub Enterprise Support
---

You can use GitHub Enteprise within GoReleaser by providing its URLs in
the `.goreleaer.yml` configuration file:

```yaml
# .goreleaser.yml
github_urls:
api: api.github.foo.bar
upload: uploads.github.foo.bar
download: github.foo.bar
```

If none is set, it will default to the public GitHub's URLs.
19 changes: 16 additions & 3 deletions internal/client/github.go
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"net/url"
"os"

"github.com/apex/log"
Expand All @@ -15,13 +16,25 @@ type githubClient struct {
}

// NewGitHub returns a github client implementation
func NewGitHub(ctx *context.Context) Client {
func NewGitHub(ctx *context.Context) (Client, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ctx.Token},
)
return &githubClient{
client: github.NewClient(oauth2.NewClient(ctx, ts)),
client := github.NewClient(oauth2.NewClient(ctx, ts))
if ctx.Config.GitHubURLs.API != "" {
api, err := url.Parse(ctx.Config.GitHubURLs.API)
if err != nil {
return &githubClient{}, err
}
upload, err := url.Parse(ctx.Config.GitHubURLs.Upload)
if err != nil {
return &githubClient{}, err
}
client.BaseURL = api
client.UploadURL = upload
}

return &githubClient{client}, nil
}

func (c *githubClient) CreateFile(
Expand Down
11 changes: 10 additions & 1 deletion pipeline/brew/brew.go
Expand Up @@ -33,7 +33,11 @@ func (Pipe) Description() string {

// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
return doRun(ctx, client.NewGitHub(ctx))
client, err := client.NewGitHub(ctx)
if err != nil {
return err
}
return doRun(ctx, client)
}

func doRun(ctx *context.Context, client client.Client) error {
Expand Down Expand Up @@ -93,8 +97,13 @@ func dataFor(ctx *context.Context, client client.Client, folder string) (result
if err != nil {
return
}
var url = "https://github.com"
if ctx.Config.GitHubURLs.Download != "" {
url = ctx.Config.GitHubURLs.Download
}
return templateData{
Name: formulaNameFor(ctx.Config.ProjectName),
DownloadURL: url,
Desc: ctx.Config.Brew.Description,
Homepage: ctx.Config.Brew.Homepage,
Repo: ctx.Config.Release.GitHub,
Expand Down
34 changes: 20 additions & 14 deletions pipeline/brew/brew_test.go
Expand Up @@ -30,9 +30,10 @@ func TestSimpleName(t *testing.T) {
}

var defaultTemplateData = templateData{
Desc: "Some desc",
Homepage: "https://google.com",
Name: "Test",
Desc: "Some desc",
Homepage: "https://google.com",
DownloadURL: "https://github.com",
Name: "Test",
Repo: config.Repo{
Owner: "caarlos0",
Name: "test",
Expand All @@ -45,10 +46,10 @@ var defaultTemplateData = templateData{

func assertDefaultTemplateData(t *testing.T, formulae string) {
assert.Contains(t, formulae, "class Test < Formula")
assert.Contains(t, formulae, "homepage \"https://google.com\"")
assert.Contains(t, formulae, "url \"https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz\"")
assert.Contains(t, formulae, "sha256 \"1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68\"")
assert.Contains(t, formulae, "version \"0.1.3\"")
assert.Contains(t, formulae, `homepage "https://google.com"`)
assert.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`)
assert.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`)
assert.Contains(t, formulae, `version "0.1.3"`)
}

func TestFullFormulae(t *testing.T) {
Expand All @@ -63,11 +64,10 @@ func TestFullFormulae(t *testing.T) {
assert.NoError(t, err)
formulae := out.String()

f, err := os.Open("testdata/test.rb")
bts, err := ioutil.ReadFile("testdata/test.rb")
assert.NoError(t, err)
bts, err := ioutil.ReadAll(f)
assert.NoError(t, err)

// ioutil.WriteFile("testdata/test.rb", []byte(formulae), 0644)

assert.Equal(t, string(bts), formulae)
}

Expand Down Expand Up @@ -100,6 +100,12 @@ func TestRunPipe(t *testing.T) {
Archive: config.Archive{
Format: "tar.gz",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
Brew: config.Homebrew{
GitHub: config.Repo{
Owner: "test",
Expand Down Expand Up @@ -128,10 +134,10 @@ func TestRunPipe(t *testing.T) {
assert.NoError(t, doRun(ctx, client))
assert.True(t, client.CreatedFile)

f, err := os.Open("testdata/run_pipe.rb")
assert.NoError(t, err)
bts, err := ioutil.ReadAll(f)
bts, err := ioutil.ReadFile("testdata/run_pipe.rb")
assert.NoError(t, err)
// assert.NoError(ioutil.WriteFile("testdata/run_pipe.rb", []byte(client.Content), 0644))

assert.Equal(t, string(bts), client.Content)
}

Expand Down
3 changes: 2 additions & 1 deletion pipeline/brew/template.go
Expand Up @@ -6,6 +6,7 @@ type templateData struct {
Name string
Desc string
Homepage string
DownloadURL string
Repo config.Repo // FIXME: will not work for anything but github right now.
Tag string
Version string
Expand All @@ -22,7 +23,7 @@ type templateData struct {
const formulaTemplate = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}"
url "{{ .DownloadURL }}/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}"
version "{{ .Version }}"
sha256 "{{ .SHA256 }}"
Expand Down
2 changes: 1 addition & 1 deletion pipeline/brew/testdata/run_pipe.rb
@@ -1,7 +1,7 @@
class RunPipe < Formula
desc "A run pipe test formula"
homepage "https://github.com/goreleaser"
url "https://github.com///releases/download/v1.0.1/bin.tar.gz"
url "https://github.com/test/test/releases/download/v1.0.1/bin.tar.gz"
version "1.0.1"
sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
6 changes: 5 additions & 1 deletion pipeline/release/release.go
Expand Up @@ -23,7 +23,11 @@ func (Pipe) Description() string {

// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
return doRun(ctx, client.NewGitHub(ctx))
client, err := client.NewGitHub(ctx)
if err != nil {
return err
}
return doRun(ctx, client)
}

func doRun(ctx *context.Context, client client.Client) error {
Expand Down

0 comments on commit fe36819

Please sign in to comment.