1
0

config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package github
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net/http"
  6. "strings"
  7. "github.com/cockroachdb/errors"
  8. "github.com/google/go-github/github"
  9. )
  10. // Config contains configuration for GitHub authentication.
  11. //
  12. // ⚠️ WARNING: Change to the field name must preserve the INI key name for backward compatibility.
  13. type Config struct {
  14. // the GitHub service endpoint, e.g. https://api.github.com/.
  15. APIEndpoint string
  16. SkipVerify bool
  17. }
  18. func (c *Config) doAuth(login, password string) (fullname, email, location, website string, err error) {
  19. tp := github.BasicAuthTransport{
  20. Username: strings.TrimSpace(login),
  21. Password: strings.TrimSpace(password),
  22. Transport: &http.Transport{
  23. TLSClientConfig: &tls.Config{InsecureSkipVerify: c.SkipVerify},
  24. },
  25. }
  26. client, err := github.NewEnterpriseClient(c.APIEndpoint, c.APIEndpoint, tp.Client())
  27. if err != nil {
  28. return "", "", "", "", errors.Wrap(err, "create new client")
  29. }
  30. user, _, err := client.Users.Get(context.Background(), "")
  31. if err != nil {
  32. return "", "", "", "", errors.Wrap(err, "get user info")
  33. }
  34. if user.Name != nil {
  35. fullname = *user.Name
  36. }
  37. if user.Email != nil {
  38. email = *user.Email
  39. } else {
  40. email = login + "+github@local"
  41. }
  42. if user.Location != nil {
  43. location = strings.ToUpper(*user.Location)
  44. }
  45. if user.HTMLURL != nil {
  46. website = strings.ToLower(*user.HTMLURL)
  47. }
  48. return fullname, email, location, website, nil
  49. }