diff --git a/config/config.go b/config/config.go index 6df5579f3..5b1174475 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -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:"-"` diff --git a/docs/150-github-enterprise.md b/docs/150-github-enterprise.md new file mode 100644 index 000000000..9962c3e1f --- /dev/null +++ b/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. diff --git a/internal/client/github.go b/internal/client/github.go index 9929a36c4..31bee0685 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -2,6 +2,7 @@ package client import ( "bytes" + "net/url" "os" "github.com/apex/log" @@ -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( diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 954b2154a..31602b909 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -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 { @@ -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, diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index e1961db77..fab9a891d 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -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", @@ -46,10 +47,10 @@ var defaultTemplateData = templateData{ func assertDefaultTemplateData(t *testing.T, formulae string) { assert := assert.New(t) assert.Contains(formulae, "class Test < Formula") - assert.Contains(formulae, "homepage \"https://google.com\"") - assert.Contains(formulae, "url \"https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz\"") - assert.Contains(formulae, "sha256 \"1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68\"") - assert.Contains(formulae, "version \"0.1.3\"") + assert.Contains(formulae, `homepage "https://google.com"`) + assert.Contains(formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`) + assert.Contains(formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`) + assert.Contains(formulae, `version "0.1.3"`) } func TestFullFormulae(t *testing.T) { @@ -65,10 +66,9 @@ func TestFullFormulae(t *testing.T) { assert.NoError(err) formulae := out.String() - f, err := os.Open("testdata/test.rb") - assert.NoError(err) - bts, err := ioutil.ReadAll(f) + bts, err := ioutil.ReadFile("testdata/test.rb") assert.NoError(err) + // ioutil.WriteFile("testdata/test.rb", []byte(formulae), 0644) assert.Equal(string(bts), formulae) } @@ -105,6 +105,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", @@ -133,10 +139,9 @@ func TestRunPipe(t *testing.T) { assert.NoError(doRun(ctx, client)) assert.True(client.CreatedFile) - f, err := os.Open("testdata/run_pipe.rb") - assert.NoError(err) - bts, err := ioutil.ReadAll(f) + bts, err := ioutil.ReadFile("testdata/run_pipe.rb") assert.NoError(err) + // assert.NoError(ioutil.WriteFile("testdata/run_pipe.rb", []byte(client.Content), 0644)) assert.Equal(string(bts), client.Content) } diff --git a/pipeline/brew/template.go b/pipeline/brew/template.go index 87e5b74de..ca26a839f 100644 --- a/pipeline/brew/template.go +++ b/pipeline/brew/template.go @@ -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 @@ -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 }}" diff --git a/pipeline/brew/testdata/run_pipe.rb b/pipeline/brew/testdata/run_pipe.rb index 0fb1100b8..bb488bea5 100644 --- a/pipeline/brew/testdata/run_pipe.rb +++ b/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" diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 1d245e5e7..65c2a62f0 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -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 {