Files
gluetun/internal/version/github.go
T
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly
- only use sentinel errors when it has to be checked using `errors.Is`
- replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error
- exclude the sentinel error definition requirement from .golangci.yml
- update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
2026-05-02 03:29:46 +00:00

81 lines
2.0 KiB
Go

package version
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type githubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Prerelease bool `json:"prerelease"`
PublishedAt time.Time `json:"published_at"`
}
type githubCommit struct {
Sha string `json:"sha"`
Commit struct {
Committer struct {
Date time.Time `json:"date"`
} `json:"committer"`
} `json:"commit"`
}
func getGithubReleases(ctx context.Context, client *http.Client) (releases []githubRelease, err error) {
// Define a timeout since the default client has a large timeout and we don't
// want to wait too long.
const timeout = 15 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
const url = "https://api.github.com/repos/qdm12/gluetun/releases"
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad response HTTP status code: %d %s",
response.StatusCode, response.Status)
}
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&releases); err != nil {
return nil, err
}
return releases, nil
}
func getGithubCommits(ctx context.Context, client *http.Client) (commits []githubCommit, err error) {
// Define a timeout since the default client has a large timeout and we don't
// want to wait too long.
const timeout = 15 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
const url = "https://api.github.com/repos/qdm12/gluetun/commits"
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&commits); err != nil {
return nil, err
}
return commits, nil
}