Browse Source

go-github: add support for getting content of a repository's license

Fixes #228
Fixes #301

Change-Id: I43d414332baa87b430487ea03d125633f8e40bd5
Glenn Lewis 10 years ago
committed by Will Norris
parent
commit
5808c118ea
2 changed files with 44 additions and 0 deletions
  1. +19
    -0
      github/repos.go
  2. +25
    -0
      github/repos_test.go

+ 19
- 0
github/repos.go View File

@ -538,3 +538,22 @@ func (s *RepositoriesService) EditBranch(owner, repo, branchName string, branch
return b, resp, err
}
// License gets the contents of a repository's license if one is detected.
//
// GitHub API docs: https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license
func (s *RepositoriesService) License(owner, repo string) (*License, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/license", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
r := &Repository{}
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r.License, resp, err
}

+ 25
- 0
github/repos_test.go View File

@ -456,3 +456,28 @@ func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) {
_, _, err := client.Repositories.ListLanguages("%", "%")
testURLParseError(t, err)
}
func TestRepositoriesService_License(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/license", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"license":{"key":"mit","name":"MIT License","url":"https://api.github.com/licenses/mit","featured":true}}`)
})
got, _, err := client.Repositories.License("o", "r")
if err != nil {
t.Errorf("Repositories.License returned error: %v", err)
}
want := &License{
Name: String("MIT License"),
Key: String("mit"),
URL: String("https://api.github.com/licenses/mit"),
Featured: Bool(true),
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Repositories.License returned %+v, want %+v", got, want)
}
}

Loading…
Cancel
Save