(httpClient httpClient, hostname, authToken string)
| 250 | } |
| 251 | |
| 252 | func GetCurrentLogin(httpClient httpClient, hostname, authToken string) (string, error) { |
| 253 | query := `query UserCurrent{viewer{login}}` |
| 254 | reqBody, err := json.Marshal(map[string]interface{}{"query": query}) |
| 255 | if err != nil { |
| 256 | return "", err |
| 257 | } |
| 258 | result := struct { |
| 259 | Data struct{ Viewer struct{ Login string } } |
| 260 | }{} |
| 261 | apiEndpoint := ghinstance.GraphQLEndpoint(hostname) |
| 262 | req, err := http.NewRequest("POST", apiEndpoint, bytes.NewBuffer(reqBody)) |
| 263 | if err != nil { |
| 264 | return "", err |
| 265 | } |
| 266 | req.Header.Set("Authorization", "token "+authToken) |
| 267 | res, err := httpClient.Do(req) |
| 268 | if err != nil { |
| 269 | return "", err |
| 270 | } |
| 271 | defer res.Body.Close() |
| 272 | if res.StatusCode > 299 { |
| 273 | return "", api.HandleHTTPError(res) |
| 274 | } |
| 275 | decoder := json.NewDecoder(res.Body) |
| 276 | err = decoder.Decode(&result) |
| 277 | if err != nil { |
| 278 | return "", err |
| 279 | } |
| 280 | return result.Data.Viewer.Login, nil |
| 281 | } |
no test coverage detected