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
6package github
7
8import (
9	"context"
10	"fmt"
11)
12
13// LicensesService handles communication with the license related
14// methods of the GitHub API.
15//
16// GitHub API docs: https://developer.github.com/v3/licenses/
17type LicensesService service
18
19// RepositoryLicense represents the license for a repository.
20type RepositoryLicense struct {
21	Name *string `json:"name,omitempty"`
22	Path *string `json:"path,omitempty"`
23
24	SHA         *string  `json:"sha,omitempty"`
25	Size        *int     `json:"size,omitempty"`
26	URL         *string  `json:"url,omitempty"`
27	HTMLURL     *string  `json:"html_url,omitempty"`
28	GitURL      *string  `json:"git_url,omitempty"`
29	DownloadURL *string  `json:"download_url,omitempty"`
30	Type        *string  `json:"type,omitempty"`
31	Content     *string  `json:"content,omitempty"`
32	Encoding    *string  `json:"encoding,omitempty"`
33	License     *License `json:"license,omitempty"`
34}
35
36func (l RepositoryLicense) String() string {
37	return Stringify(l)
38}
39
40// License represents an open source license.
41type License struct {
42	Key  *string `json:"key,omitempty"`
43	Name *string `json:"name,omitempty"`
44	URL  *string `json:"url,omitempty"`
45
46	SPDXID         *string   `json:"spdx_id,omitempty"`
47	HTMLURL        *string   `json:"html_url,omitempty"`
48	Featured       *bool     `json:"featured,omitempty"`
49	Description    *string   `json:"description,omitempty"`
50	Implementation *string   `json:"implementation,omitempty"`
51	Permissions    *[]string `json:"permissions,omitempty"`
52	Conditions     *[]string `json:"conditions,omitempty"`
53	Limitations    *[]string `json:"limitations,omitempty"`
54	Body           *string   `json:"body,omitempty"`
55}
56
57func (l License) String() string {
58	return Stringify(l)
59}
60
61// List popular open source licenses.
62//
63// GitHub API docs: https://developer.github.com/v3/licenses/#list-all-licenses
64func (s *LicensesService) List(ctx context.Context) ([]*License, *Response, error) {
65	req, err := s.client.NewRequest("GET", "licenses", nil)
66	if err != nil {
67		return nil, nil, err
68	}
69
70	var licenses []*License
71	resp, err := s.client.Do(ctx, req, &licenses)
72	if err != nil {
73		return nil, resp, err
74	}
75
76	return licenses, resp, nil
77}
78
79// Get extended metadata for one license.
80//
81// GitHub API docs: https://developer.github.com/v3/licenses/#get-an-individual-license
82func (s *LicensesService) Get(ctx context.Context, licenseName string) (*License, *Response, error) {
83	u := fmt.Sprintf("licenses/%s", licenseName)
84
85	req, err := s.client.NewRequest("GET", u, nil)
86	if err != nil {
87		return nil, nil, err
88	}
89
90	license := new(License)
91	resp, err := s.client.Do(ctx, req, license)
92	if err != nil {
93		return nil, resp, err
94	}
95
96	return license, resp, nil
97}
98