licenses.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // LicensesService handles communication with the license related
  11. // methods of the GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/licenses/
  14. type LicensesService service
  15. // RepositoryLicense represents the license for a repository.
  16. type RepositoryLicense struct {
  17. Name *string `json:"name,omitempty"`
  18. Path *string `json:"path,omitempty"`
  19. SHA *string `json:"sha,omitempty"`
  20. Size *int `json:"size,omitempty"`
  21. URL *string `json:"url,omitempty"`
  22. HTMLURL *string `json:"html_url,omitempty"`
  23. GitURL *string `json:"git_url,omitempty"`
  24. DownloadURL *string `json:"download_url,omitempty"`
  25. Type *string `json:"type,omitempty"`
  26. Content *string `json:"content,omitempty"`
  27. Encoding *string `json:"encoding,omitempty"`
  28. License *License `json:"license,omitempty"`
  29. }
  30. func (l RepositoryLicense) String() string {
  31. return Stringify(l)
  32. }
  33. // License represents an open source license.
  34. type License struct {
  35. Key *string `json:"key,omitempty"`
  36. Name *string `json:"name,omitempty"`
  37. URL *string `json:"url,omitempty"`
  38. SPDXID *string `json:"spdx_id,omitempty"`
  39. HTMLURL *string `json:"html_url,omitempty"`
  40. Featured *bool `json:"featured,omitempty"`
  41. Description *string `json:"description,omitempty"`
  42. Implementation *string `json:"implementation,omitempty"`
  43. Permissions *[]string `json:"permissions,omitempty"`
  44. Conditions *[]string `json:"conditions,omitempty"`
  45. Limitations *[]string `json:"limitations,omitempty"`
  46. Body *string `json:"body,omitempty"`
  47. }
  48. func (l License) String() string {
  49. return Stringify(l)
  50. }
  51. // List popular open source licenses.
  52. //
  53. // GitHub API docs: https://developer.github.com/v3/licenses/#list-all-licenses
  54. func (s *LicensesService) List(ctx context.Context) ([]*License, *Response, error) {
  55. req, err := s.client.NewRequest("GET", "licenses", nil)
  56. if err != nil {
  57. return nil, nil, err
  58. }
  59. // TODO: remove custom Accept header when this API fully launches
  60. req.Header.Set("Accept", mediaTypeLicensesPreview)
  61. var licenses []*License
  62. resp, err := s.client.Do(ctx, req, &licenses)
  63. if err != nil {
  64. return nil, resp, err
  65. }
  66. return licenses, resp, nil
  67. }
  68. // Get extended metadata for one license.
  69. //
  70. // GitHub API docs: https://developer.github.com/v3/licenses/#get-an-individual-license
  71. func (s *LicensesService) Get(ctx context.Context, licenseName string) (*License, *Response, error) {
  72. u := fmt.Sprintf("licenses/%s", licenseName)
  73. req, err := s.client.NewRequest("GET", u, nil)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. // TODO: remove custom Accept header when this API fully launches
  78. req.Header.Set("Accept", mediaTypeLicensesPreview)
  79. license := new(License)
  80. resp, err := s.client.Do(ctx, req, license)
  81. if err != nil {
  82. return nil, resp, err
  83. }
  84. return license, resp, nil
  85. }