Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit e088d76

Browse files
authored
Support private dependencies (#348)
1 parent fd09464 commit e088d76

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

buildserver/deps.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,20 @@ func FetchCommonDeps() {
496496
var NewDepRepoVFS = func(ctx context.Context, cloneURL *url.URL, rev string, zipURLTemplate *string) (ctxvfs.FileSystem, error) {
497497
if zipURLTemplate != nil {
498498
zipURL := fmt.Sprintf(*zipURLTemplate, path.Join(cloneURL.Host, cloneURL.Path), rev)
499-
response, err := http.Head(zipURL)
500-
if err == nil && response.StatusCode == http.StatusOK {
501-
return vfsutil.NewZipVFS(zipURL, depZipFetch.Inc, depZipFetchFailed.Inc, false)
499+
archive, err := vfsutil.NewZipVFS(ctx, zipURL, depZipFetch.Inc, depZipFetchFailed.Inc, false)
500+
if archive != nil && err == nil {
501+
return archive, nil
502502
}
503503
}
504504

505505
// Fast-path for GitHub repos, which we can fetch on-demand from
506506
// GitHub's repo .zip archive download endpoint.
507507
if cloneURL.Host == "github.com" {
508508
fullName := cloneURL.Host + strings.TrimSuffix(cloneURL.Path, ".git") // of the form "github.com/foo/bar"
509-
return vfsutil.NewGitHubRepoVFS(fullName, rev)
509+
archive, err := vfsutil.NewGitHubRepoVFS(ctx, fullName, rev)
510+
if archive != nil && err == nil {
511+
return archive, nil
512+
}
510513
}
511514

512515
// Fall back to a full git clone for non-github.com repos.

buildserver/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func useGithubForVFS() func() {
275275
if u.Rev() == "" {
276276
return nil, errors.Errorf("rev is required in uri: %s", initializeParams.OriginalRootURI)
277277
}
278-
return vfsutil.NewGitHubRepoVFS(string(u.Repo()), u.Rev())
278+
return vfsutil.NewGitHubRepoVFS(ctx, string(u.Repo()), u.Rev())
279279
}
280280

281281
return func() {

buildserver/vfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var RemoteFS = func(ctx context.Context, initializeParams lspext.InitializeParam
2727
return url
2828
}()
2929
if zipURL != "" {
30-
return vfsutil.NewZipVFS(zipURL, zipFetch.Inc, zipFetchFailed.Inc, true)
30+
return vfsutil.NewZipVFS(ctx, zipURL, zipFetch.Inc, zipFetchFailed.Inc, true)
3131
}
3232
return nil, errors.Errorf("no zipURL was provided in the initializeOptions")
3333
}

vfsutil/github_archive.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vfsutil
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
67

@@ -9,13 +10,13 @@ import (
910

1011
// NewGitHubRepoVFS creates a new VFS backed by a GitHub downloadable
1112
// repository archive.
12-
func NewGitHubRepoVFS(repo, rev string) (*ArchiveFS, error) {
13+
func NewGitHubRepoVFS(ctx context.Context, repo, rev string) (*ArchiveFS, error) {
1314
if !githubRepoRx.MatchString(repo) {
1415
return nil, fmt.Errorf(`invalid GitHub repo %q: must be "github.com/user/repo"`, repo)
1516
}
1617

1718
url := fmt.Sprintf("https://codeload.%s/zip/%s", repo, rev)
18-
return NewZipVFS(url, ghFetch.Inc, ghFetchFailed.Inc, false)
19+
return NewZipVFS(ctx, url, ghFetch.Inc, ghFetchFailed.Inc, false)
1920
}
2021

2122
var githubRepoRx = regexp.MustCompile(`^github\.com/[\w.-]{1,100}/[\w.-]{1,100}$`)

vfsutil/github_archive_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vfsutil
22

33
import (
4+
"context"
45
"io/ioutil"
56
"os"
67
"testing"
@@ -12,7 +13,7 @@ func TestGitHubRepoVFS(t *testing.T) {
1213
defer cleanup()
1314

1415
// Any public repo will work.
15-
fs, err := NewGitHubRepoVFS("github.com/gorilla/schema", "0164a00ab4cd01d814d8cd5bf63fd9fcea30e23b")
16+
fs, err := NewGitHubRepoVFS(context.Background(), "github.com/gorilla/schema", "0164a00ab4cd01d814d8cd5bf63fd9fcea30e23b")
1617
if err != nil {
1718
t.Fatal(err)
1819
}

vfsutil/zip.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package vfsutil
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"fmt"
57
"io"
68
"net/http"
9+
"os"
10+
"strings"
711

812
"golang.org/x/net/context/ctxhttp"
913

14+
"github.com/fhs/go-netrc/netrc"
1015
"github.com/pkg/errors"
1116

1217
opentracing "github.com/opentracing/opentracing-go"
@@ -15,7 +20,20 @@ import (
1520

1621
// NewZipVFS downloads a zip archive from a URL (or fetches from the local cache
1722
// on disk) and returns a new VFS backed by that zip archive.
18-
func NewZipVFS(url string, onFetchStart, onFetchFailed func(), evictOnClose bool) (*ArchiveFS, error) {
23+
func NewZipVFS(ctx context.Context, url string, onFetchStart, onFetchFailed func(), evictOnClose bool) (*ArchiveFS, error) {
24+
request, err := http.NewRequest("HEAD", url, nil)
25+
if err != nil {
26+
return nil, errors.Wrapf(err, "failed to construct a new request with URL %s", url)
27+
}
28+
setAuthFromNetrc(request)
29+
response, err := ctxhttp.Do(ctx, nil, request)
30+
if err != nil {
31+
return nil, err
32+
}
33+
if response.StatusCode != http.StatusOK {
34+
return nil, nil
35+
}
36+
1937
fetch := func(ctx context.Context) (ar *archiveReader, err error) {
2038
span, ctx := opentracing.StartSpanFromContext(ctx, "zip Fetch")
2139
ext.Component.Set(span, "zipvfs")
@@ -35,6 +53,7 @@ func NewZipVFS(url string, onFetchStart, onFetchFailed func(), evictOnClose bool
3553
return nil, errors.Wrapf(err, "failed to construct a new request with URL %s", url)
3654
}
3755
request.Header.Add("Accept", "application/zip")
56+
setAuthFromNetrc(request)
3857
resp, err := ctxhttp.Do(ctx, nil, request)
3958
if err != nil {
4059
return nil, errors.Wrapf(err, "failed to fetch zip archive from %s", url)
@@ -71,3 +90,19 @@ func NewZipVFS(url string, onFetchStart, onFetchFailed func(), evictOnClose bool
7190

7291
return &ArchiveFS{fetch: fetch, EvictOnClose: evictOnClose}, nil
7392
}
93+
94+
func setAuthFromNetrc(req *http.Request) {
95+
host := req.URL.Host
96+
if i := strings.Index(host, ":"); i != -1 {
97+
host = host[:i]
98+
}
99+
netrcFile := os.ExpandEnv("$HOME/.netrc")
100+
if _, err := os.Stat(netrcFile); os.IsNotExist(err) {
101+
return
102+
}
103+
machine, err := netrc.FindMachine(netrcFile, host)
104+
if err != nil || machine == nil {
105+
return
106+
}
107+
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", machine.Login, machine.Password))))
108+
}

0 commit comments

Comments
 (0)