forked from bufbuild/buf
/
archive_reader.go
132 lines (122 loc) · 3.32 KB
/
archive_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package githubtesting
import (
"compress/gzip"
"context"
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/bufbuild/buf/private/pkg/filelock"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/storage/storagearchive"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"go.uber.org/multierr"
"go.uber.org/zap"
)
// since we are in testing, we care less about making sure this times out early
// we have had some flaky tests based on this being too low, especially when
// using the race detector
const filelockTimeout = 10 * time.Second
type archiveReader struct {
logger *zap.Logger
storageosProvider storageos.Provider
httpClient *http.Client
lock sync.Mutex
}
func newArchiveReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
httpClient *http.Client,
) *archiveReader {
return &archiveReader{
logger: logger.Named("githubtesting"),
storageosProvider: storageosProvider,
httpClient: httpClient,
}
}
func (a *archiveReader) GetArchive(
ctx context.Context,
outputDirPath string,
owner string,
repository string,
ref string,
) (retErr error) {
a.lock.Lock()
defer a.lock.Unlock()
outputDirPath = normalpath.Unnormalize(outputDirPath)
// creates a file in the same parent directory as outputDirPath
unlocker, err := filelock.Lock(
ctx,
outputDirPath+".lock",
filelock.LockWithTimeout(filelockTimeout),
)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, unlocker.Unlock())
}()
// check if already exists, if so, do nothing
// OK to use os.Stat here
if fileInfo, err := os.Stat(outputDirPath); err == nil {
if !fileInfo.IsDir() {
return fmt.Errorf("expected %s to be a directory", outputDirPath)
}
return nil
}
request, err := http.NewRequestWithContext(
ctx,
"GET",
fmt.Sprintf("https://github.com/%s/%s/archive/%s.tar.gz", owner, repository, ref),
nil,
)
if err != nil {
return err
}
response, err := a.httpClient.Do(request)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, response.Body.Close())
}()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("expected HTTP status code %d to be %d", response.StatusCode, http.StatusOK)
}
gzipReader, err := gzip.NewReader(response.Body)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, gzipReader.Close())
}()
if err := os.MkdirAll(outputDirPath, 0755); err != nil {
return err
}
// do NOT want to read in symlinks
readWriteBucket, err := a.storageosProvider.NewReadWriteBucket(normalpath.Normalize(outputDirPath))
if err != nil {
return err
}
return storagearchive.Untar(
ctx,
gzipReader,
readWriteBucket,
nil,
1,
)
}