CurrentUserID fetches the ID of the current user. It caches the result in ~/.rill, along with a hash of the current admin token for cache invalidation in case of login/logout.
(ctx context.Context)
| 282 | // CurrentUserID fetches the ID of the current user. |
| 283 | // It caches the result in ~/.rill, along with a hash of the current admin token for cache invalidation in case of login/logout. |
| 284 | func (h *Helper) CurrentUserID(ctx context.Context) (string, error) { |
| 285 | if h.AdminToken() == "" { |
| 286 | return "", nil |
| 287 | } |
| 288 | |
| 289 | newHash := hashStr(h.AdminToken(), h.AdminURL()) |
| 290 | |
| 291 | oldHash, err := h.DotRill.GetUserCheckHash() |
| 292 | if err != nil { |
| 293 | return "", err |
| 294 | } |
| 295 | |
| 296 | if oldHash == newHash { |
| 297 | userID, err := h.DotRill.GetUserID() |
| 298 | if err != nil { |
| 299 | return "", err |
| 300 | } |
| 301 | return userID, nil |
| 302 | } |
| 303 | |
| 304 | c, err := h.Client() |
| 305 | if err != nil { |
| 306 | return "", err |
| 307 | } |
| 308 | |
| 309 | res, err := c.GetCurrentUser(ctx, &adminv1.GetCurrentUserRequest{}) |
| 310 | if err != nil { |
| 311 | return "", err |
| 312 | } |
| 313 | |
| 314 | var userID string |
| 315 | if res.User != nil { |
| 316 | userID = res.User.Id |
| 317 | } |
| 318 | |
| 319 | err = h.DotRill.SetUserID(userID) |
| 320 | if err != nil { |
| 321 | return "", err |
| 322 | } |
| 323 | |
| 324 | err = h.DotRill.SetUserCheckHash(newHash) |
| 325 | if err != nil { |
| 326 | return "", err |
| 327 | } |
| 328 | |
| 329 | return userID, nil |
| 330 | } |
| 331 | |
| 332 | func (h *Helper) ProjectNamesByGitRemote(ctx context.Context, org, remote, subPath string) ([]string, error) { |
| 333 | if org == "" || remote == "" { |
no test coverage detected