HasInvalidGitHubToken checks if the response indicates an invalid GitHub token. This is possible if we get 401 or 404 response.
(response any)
| 151 | // HasInvalidGitHubToken checks if the response indicates an invalid GitHub token. |
| 152 | // This is possible if we get 401 or 404 response. |
| 153 | func HasInvalidGitHubToken(response any) error { |
| 154 | var httpResp *http.Response |
| 155 | switch r := response.(type) { |
| 156 | case *github.Response: |
| 157 | if r == nil { |
| 158 | return nil |
| 159 | } |
| 160 | httpResp = r.Response |
| 161 | case *http.Response: |
| 162 | httpResp = r |
| 163 | default: |
| 164 | return nil |
| 165 | } |
| 166 | if httpResp == nil || httpResp.Request == nil { |
| 167 | return nil |
| 168 | } |
| 169 | if !isGitHubURL(httpResp.Request.URL.String()) { |
| 170 | return nil |
| 171 | } |
| 172 | _, tokenVariable := GetGitHubToken() |
| 173 | if tokenVariable == "" { |
| 174 | return nil |
| 175 | } |
| 176 | if httpResp.StatusCode == http.StatusUnauthorized { |
| 177 | return fmt.Errorf("using %s, which is invalid or lacks required permissions", tokenVariable) |
| 178 | } |
| 179 | if httpResp.StatusCode == http.StatusNotFound { |
| 180 | return fmt.Errorf("using %s, which may be invalid or lack permissions", tokenVariable) |
| 181 | } |
| 182 | return nil |
| 183 | } |
no test coverage detected