mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-18 03:49:50 -06:00
Add a function that asks GitHub for the latest lazygit version
Unlike the current update code, it returns the version as a VersionNumber, so that we can compare versions properly instead of only checking whether they are equal. It also takes a context, so that the caller can give it a timeout; the current code waits forever if the network hangs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2877762745
commit
463aba33ba
2 files changed
+110
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
// fetchLatestVersion asks GitHub for the version of the latest lazygit release.
|
||||
// We use the release page instead of GitHub's REST API, because the API limits
|
||||
// the number of unauthenticated requests per IP address.
|
||||
func fetchLatestVersion(ctx context.Context, client *http.Client, repoURL string) (*types.VersionNumber, error) {
|
||||
var data struct {
|
||||
TagName string `json:"tag_name"`
|
||||
}
|
||||
if err := fetchJSON(ctx, client, repoURL+"/releases/latest", &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.ParseVersionNumber(data.TagName)
|
||||
}
|
||||
|
||||
func fetchJSON(ctx context.Context, client *http.Client, url string, data any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s returned %s", url, resp.Status)
|
||||
}
|
||||
|
||||
return json.NewDecoder(resp.Body).Decode(data)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFetchLatestVersion(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
status int
|
||||
body string
|
||||
expectedVersion *types.VersionNumber
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "release",
|
||||
status: http.StatusOK,
|
||||
body: `{"id":1,"tag_name":"v0.67.0","update_url":"/jesseduffield/lazygit/releases/tag/v0.67.0"}`,
|
||||
expectedVersion: &types.VersionNumber{Major: 0, Minor: 67, Patch: 0},
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
status: http.StatusNotFound,
|
||||
body: `{"error":"Not Found"}`,
|
||||
expectedError: "/releases/latest returned 404 Not Found",
|
||||
},
|
||||
{
|
||||
name: "unexpected tag",
|
||||
status: http.StatusOK,
|
||||
body: `{"tag_name":"nightly"}`,
|
||||
expectedError: "unexpected version format: nightly",
|
||||
},
|
||||
{
|
||||
name: "invalid json",
|
||||
status: http.StatusOK,
|
||||
body: `<html>`,
|
||||
expectedError: "invalid character '<' looking for beginning of value",
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
t.Run(s.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/releases/latest", r.URL.Path)
|
||||
assert.Equal(t, "application/json", r.Header.Get("Accept"))
|
||||
w.WriteHeader(s.status)
|
||||
_, _ = w.Write([]byte(s.body))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
version, err := fetchLatestVersion(context.Background(), server.Client(), server.URL)
|
||||
if s.expectedError != "" {
|
||||
assert.ErrorContains(t, err, s.expectedError)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, s.expectedVersion, version)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user