Skip to content

Commit

Permalink
GitHub: improve error message when SSH key resolution fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
n-g committed Nov 3, 2023
1 parent bcac85e commit 8115e62
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions pkg/github/sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,9 @@ func ResolveSSHKeys(ctx context.Context, usernames []string) (map[string][]strin
for _, username := range usernames {
t := time.Now()

resp, err := http.Get(fmt.Sprintf("https://github.com/%s.keys", username))
keys, err := fetchKeys(username)
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

contents, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}

var keys []string
for _, line := range strings.Split(strings.TrimSpace(string(contents)), "\n") {
keys = append(keys, strings.TrimSpace(line))
return nil, fmt.Errorf("failed to fetch SSH keys for GitHub user %q: %w", username, err)
}

m[username] = keys
Expand All @@ -44,3 +30,26 @@ func ResolveSSHKeys(ctx context.Context, usernames []string) (map[string][]strin

return m, nil
}

func fetchKeys(username string) ([]string, error) {
resp, err := http.Get(fmt.Sprintf("https://github.com/%s.keys", username))
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

contents, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}

var keys []string
for _, line := range strings.Split(strings.TrimSpace(string(contents)), "\n") {
keys = append(keys, strings.TrimSpace(line))
}

return keys, nil
}

0 comments on commit 8115e62

Please sign in to comment.