Skip to content

Commit 47ffb32

Browse files
authored
Local backend: setup clone step respects context (#6030)
1 parent 6a76851 commit 47ffb32

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pipeline/backend/local/clone.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (e *local) loadClone() {
4747
}
4848

4949
// setupClone prepare the clone environment before exec.
50-
func (e *local) setupClone(state *workflowState) error {
50+
func (e *local) setupClone(ctx context.Context, state *workflowState) error {
5151
if e.pluginGitBinary != "" {
5252
state.pluginGitBinary = e.pluginGitBinary
5353
return nil
@@ -58,7 +58,7 @@ func (e *local) setupClone(state *workflowState) error {
5858
if e.os == "windows" {
5959
state.pluginGitBinary += ".exe"
6060
}
61-
return e.downloadLatestGitPluginBinary(state.pluginGitBinary)
61+
return e.downloadLatestGitPluginBinary(ctx, state.pluginGitBinary)
6262
}
6363

6464
// execClone executes a clone-step locally.
@@ -67,7 +67,7 @@ func (e *local) execClone(ctx context.Context, step *types.Step, state *workflow
6767
return fmt.Errorf("check for git clone capabilities failed: %w", err)
6868
}
6969

70-
if err := e.setupClone(state); err != nil {
70+
if err := e.setupClone(ctx, state); err != nil {
7171
return fmt.Errorf("setup clone step failed: %w", err)
7272
}
7373

@@ -137,7 +137,7 @@ func (e *local) writeNetRC(step *types.Step, state *workflowState) (string, erro
137137

138138
// downloadLatestGitPluginBinary download the latest plugin-git binary based on runtime OS and Arch
139139
// and saves it to dest.
140-
func (e *local) downloadLatestGitPluginBinary(dest string) error {
140+
func (e *local) downloadLatestGitPluginBinary(ctx context.Context, dest string) error {
141141
type asset struct {
142142
Name string
143143
BrowserDownloadURL string `json:"browser_download_url"`
@@ -148,7 +148,7 @@ func (e *local) downloadLatestGitPluginBinary(dest string) error {
148148
}
149149

150150
// get latest release
151-
req, _ := http.NewRequest(http.MethodGet, "https://api.github.com/repos/woodpecker-ci/plugin-git/releases/latest", nil)
151+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/repos/woodpecker-ci/plugin-git/releases/latest", nil)
152152
req.Header.Set("Accept", "application/vnd.github+json")
153153
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
154154
resp, err := http.DefaultClient.Do(req)
@@ -164,19 +164,23 @@ func (e *local) downloadLatestGitPluginBinary(dest string) error {
164164

165165
for _, at := range rel.Assets {
166166
if strings.Contains(at.Name, e.os) && strings.Contains(at.Name, e.arch) {
167-
resp2, err := http.Get(at.BrowserDownloadURL)
167+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, at.BrowserDownloadURL, nil)
168+
if err != nil {
169+
return err
170+
}
171+
assetResp, err := http.DefaultClient.Do(req)
168172
if err != nil {
169173
return fmt.Errorf("could not download plugin-git: %w", err)
170174
}
171-
defer resp2.Body.Close()
175+
defer assetResp.Body.Close()
172176

173177
file, err := os.Create(dest)
174178
if err != nil {
175179
return fmt.Errorf("could not create plugin-git: %w", err)
176180
}
177181
defer file.Close()
178182

179-
if _, err := io.Copy(file, resp2.Body); err != nil {
183+
if _, err := io.Copy(file, assetResp.Body); err != nil {
180184
return fmt.Errorf("could not download plugin-git: %w", err)
181185
}
182186
if err := os.Chmod(dest, 0o755); err != nil {

0 commit comments

Comments
 (0)