fetchLatestVersion fetches the latest version of Rill from Github releases.
(ctx context.Context)
| 109 | |
| 110 | // fetchLatestVersion fetches the latest version of Rill from Github releases. |
| 111 | func fetchLatestVersion(ctx context.Context) (*githubReleaseInfo, error) { |
| 112 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, versionCheckURI, http.NoBody) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | req.Header.Set("Content-Type", "application/json; charset=utf-8") |
| 118 | req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 119 | |
| 120 | getToken := func() string { |
| 121 | if t := os.Getenv("GH_TOKEN"); t != "" { |
| 122 | return t |
| 123 | } |
| 124 | return os.Getenv("GITHUB_TOKEN") |
| 125 | } |
| 126 | |
| 127 | if token := getToken(); token != "" { |
| 128 | req.Header.Set("Authorization", fmt.Sprintf("token %s", token)) |
| 129 | } |
| 130 | |
| 131 | client := &http.Client{Timeout: time.Second * 15} |
| 132 | resp, err := client.Do(req) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | defer resp.Body.Close() |
| 137 | |
| 138 | out, err := io.ReadAll(resp.Body) |
| 139 | if err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | |
| 143 | success := resp.StatusCode >= 200 && resp.StatusCode < 300 |
| 144 | if !success { |
| 145 | return nil, fmt.Errorf("error fetching latest release: %s", string(out)) |
| 146 | } |
| 147 | |
| 148 | var info *githubReleaseInfo |
| 149 | err = json.Unmarshal(out, &info) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | |
| 154 | return info, nil |
| 155 | } |
no test coverage detected