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.

51 lines
1.7 KiB

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