Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Add support for private repos using env var or netrc #78

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

[[constraint]]
name = "github.com/goreleaser/goreleaser"
version = ">=0.66.0"
version = ">=0.77.1"
[prune]
go-tests = true
unused-packages = true
59 changes: 56 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
Expand All @@ -17,7 +18,10 @@ import (

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/bgentry/go-netrc/netrc"
"github.com/client9/codegen/shell"
"github.com/mitchellh/go-homedir"
"golang.org/x/oauth2"
"gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -112,7 +116,7 @@ func loadURLs(path, configPath string) (*config.Project, error) {
if file == "" {
continue
}
url := fmt.Sprintf("%s/%s", path, file)
url := fmt.Sprintf("%s/%s?ref=master", path, file)
log.Infof("reading %s", url)
project, err := loadURL(url)
if err != nil {
Expand All @@ -126,7 +130,16 @@ func loadURLs(path, configPath string) (*config.Project, error) {
}

func loadURL(file string) (*config.Project, error) {
resp, err := http.Get(file)
client, err := buildSecureHTTPClient(file)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", file, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github.raw")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -144,6 +157,46 @@ func loadURL(file string) (*config.Project, error) {
return &p, err
}

func buildSecureHTTPClient(file string) (*http.Client, error) {
// Read GITHUB_TOKEN env var
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return buildHTTPClient(token), nil
}

// Read ~/.netrc login info
netrcFile, err := homedir.Expand("~/.netrc")
if err != nil {
return nil, err
}
if _, err = os.Stat(netrcFile); !os.IsNotExist(err) {
fileURL, err := url.Parse(file)
if err != nil {
return nil, err
}
m, err := netrc.FindMachine(netrcFile, fileURL.Host)
if err != nil {
return nil, err
}
if m.Password != "" {
token = m.Password
} else if m.Login != "" {
token = m.Login
}
if token != "" {
return buildHTTPClient(token), nil
}
}

return http.DefaultClient, nil
}

func buildHTTPClient(token string) *http.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client := oauth2.NewClient(nil, ts)
return client
}

func loadFile(file string) (*config.Project, error) {
p, err := config.Load(file)
return &p, err
Expand All @@ -158,7 +211,7 @@ func Load(repo, configPath, file string) (project *config.Project, err error) {
repo = normalizeRepo(repo)
log.Infof("reading repo %q on github", repo)
project, err = loadURLs(
fmt.Sprintf("https://raw.githubusercontent.com/%s/master", repo),
fmt.Sprintf("https://api.github.com/repos/%s/contents", repo),
configPath,
)
} else {
Expand Down
10 changes: 5 additions & 5 deletions shell_godownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ parse_args() {
execute() {
tmpdir=$(mktmpdir)
log_debug "downloading files into ${tmpdir}"
http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}" "Accept:application/octet-stream"
http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}" "Accept:application/octet-stream"
hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
{{- if .Archive.WrapInDirectory }}
srcdir="${tmpdir}/${NAME}"
Expand Down Expand Up @@ -185,7 +185,6 @@ log_prefix() {
echo "$PREFIX"
}
PLATFORM="${OS}/${ARCH}"
GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download

uname_os_check "$OS"
uname_arch_check "$ARCH"
Expand All @@ -205,10 +204,11 @@ adjust_arch
log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"

{{ .Archive.NameTemplate }}
GITHUB_DOWNLOAD=https://api.github.com/repos/${OWNER}/${REPO}/releases/tags/${TAG}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will hit rate limits on travis & cia...

TARBALL=${NAME}.${FORMAT}
TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
TARBALL_URL=$(curl -n -sL ${GITHUB_DOWNLOAD} | grep -B 3 "\"name\": \"${TARBALL}\"" | grep '"url"' | sed 's/.*"url": "\(.*\)",/\1/')
{{ .Checksum.NameTemplate }}
CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
CHECKSUM_URL=$(curl -n -sL ${GITHUB_DOWNLOAD} | grep -B 3 "\"name\": \"${CHECKSUM}\"" | grep '"url"' | sed 's/.*"url": "\(.*\)",/\1/')


execute
Expand Down
8 changes: 4 additions & 4 deletions shellfn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.