Disallow update checker delaying the gh process (#6978) · cli/cli@626c639 · GitHub
Skip to content

Commit 626c639

Browse files
authored
Disallow update checker delaying the gh process (#6978)
This ensures that checking for newer versions of gh happens in the background of the main operation that the user requested, and that when that operation is completed, the gh process should immediately exit without being delayed by the update checker goroutine.
1 parent d4c9890 commit 626c639

3 files changed

Lines changed: 44 additions & 24 deletions

File tree

‎cmd/gh/main.go‎

Lines changed: 18 additions & 13 deletions

‎internal/update/update.go‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package update
22

33
import (
4+
"context"
5+
"encoding/json"
46
"fmt"
7+
"io"
8+
"net/http"
59
"os"
610
"path/filepath"
711
"regexp"
812
"strconv"
913
"strings"
1014
"time"
1115

12-
"github.com/cli/cli/v2/api"
13-
"github.com/cli/cli/v2/internal/ghinstance"
1416
"github.com/hashicorp/go-version"
1517
"gopkg.in/yaml.v3"
1618
)
@@ -30,13 +32,13 @@ type StateEntry struct {
3032
}
3133

3234
// CheckForUpdate checks whether this software has had a newer release on GitHub
33-
func CheckForUpdate(client *api.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) {
35+
func CheckForUpdate(ctx context.Context, client *http.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) {
3436
stateEntry, _ := getStateEntry(stateFilePath)
3537
if stateEntry != nil && time.Since(stateEntry.CheckedForUpdateAt).Hours() < 24 {
3638
return nil, nil
3739
}
3840

39-
releaseInfo, err := getLatestReleaseInfo(client, repo)
41+
releaseInfo, err := getLatestReleaseInfo(ctx, client, repo)
4042
if err != nil {
4143
return nil, err
4244
}
@@ -53,13 +55,27 @@ func CheckForUpdate(client *api.Client, stateFilePath, repo, currentVersion stri
5355
return nil, nil
5456
}
5557

56-
func getLatestReleaseInfo(client *api.Client, repo string) (*ReleaseInfo, error) {
57-
var latestRelease ReleaseInfo
58-
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("repos/%s/releases/latest", repo), nil, &latestRelease)
58+
func getLatestReleaseInfo(ctx context.Context, client *http.Client, repo string) (*ReleaseInfo, error) {
59+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo), nil)
5960
if err != nil {
6061
return nil, err
6162
}
62-
63+
res, err := client.Do(req)
64+
if err != nil {
65+
return nil, err
66+
}
67+
defer func() {
68+
_, _ = io.Copy(io.Discard, res.Body)
69+
res.Body.Close()
70+
}()
71+
if res.StatusCode != 200 {
72+
return nil, fmt.Errorf("unexpected HTTP %d", res.StatusCode)
73+
}
74+
dec := json.NewDecoder(res.Body)
75+
var latestRelease ReleaseInfo
76+
if err := dec.Decode(&latestRelease); err != nil {
77+
return nil, err
78+
}
6379
return &latestRelease, nil
6480
}
6581

‎internal/update/update_test.go‎

Lines changed: 2 additions & 3 deletions

0 commit comments

Comments
 (0)