You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 KiB

  1. package client
  2. import (
  3. "context"
  4. "github.com/google/go-github/github"
  5. "golang.org/x/oauth2"
  6. )
  7. // TrimmedRepository represents a cut-down version of github.Repository with only the keys the end-user is
  8. // likely to want.
  9. type TrimmedRepository struct {
  10. Name *string `json:"name"`
  11. Description *string `json:"description"`
  12. Private *bool `json:"private"`
  13. HTMLURL *string `json:"html_url"`
  14. CreatedAt *github.Timestamp `json:"created_at"`
  15. UpdatedAt *github.Timestamp `json:"updated_at"`
  16. PushedAt *github.Timestamp `json:"pushed_at"`
  17. Fork *bool `json:"fork"`
  18. FullName *string `json:"full_name"`
  19. Permissions *map[string]bool `json:"permissions"`
  20. }
  21. // TrimRepository trims a github repo into important fields only.
  22. func TrimRepository(repo *github.Repository) TrimmedRepository {
  23. return TrimmedRepository{
  24. Name: repo.Name,
  25. Description: repo.Description,
  26. Private: repo.Private,
  27. HTMLURL: repo.HTMLURL,
  28. CreatedAt: repo.CreatedAt,
  29. UpdatedAt: repo.UpdatedAt,
  30. PushedAt: repo.PushedAt,
  31. Permissions: repo.Permissions,
  32. Fork: repo.Fork,
  33. FullName: repo.FullName,
  34. }
  35. }
  36. // New returns a github Client which can perform Github API operations.
  37. // If `token` is empty, a non-authenticated client will be created. This should be
  38. // used sparingly where possible as you only get 60 requests/hour like that (IP locked).
  39. func New(token string) *github.Client {
  40. var tokenSource oauth2.TokenSource
  41. if token != "" {
  42. tokenSource = oauth2.StaticTokenSource(
  43. &oauth2.Token{AccessToken: token},
  44. )
  45. }
  46. httpCli := oauth2.NewClient(context.TODO(), tokenSource)
  47. return github.NewClient(httpCli)
  48. }