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	// TODO: remove custom Accept header when this API fully launches
71	req.Header.Set("Accept", mediaTypeLicensesPreview)
72
73	var licenses []*License
74	resp, err := s.client.Do(ctx, req, &licenses)
75	if err != nil {
76		return nil, resp, err
77	}
78
79	return licenses, resp, nil
80}
81
82// Get extended metadata for one license.
83//
84// GitHub API docs: https://developer.github.com/v3/licenses/#get-an-individual-license
85func (s *LicensesService) Get(ctx context.Context, licenseName string) (*License, *Response, error) {
86	u := fmt.Sprintf("licenses/%s", licenseName)
87
88	req, err := s.client.NewRequest("GET", u, nil)
89	if err != nil {
90		return nil, nil, err
91	}
92
93	// TODO: remove custom Accept header when this API fully launches
94	req.Header.Set("Accept", mediaTypeLicensesPreview)
95
96	license := new(License)
97	resp, err := s.client.Do(ctx, req, license)
98	if err != nil {
99		return nil, resp, err
100	}
101
102	return license, resp, nil
103}
104