|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type licensesReply struct { |
| 10 | + Key string `json:"key"` |
| 11 | + Name string `json:"name"` |
| 12 | + URL string `json:"url"` |
| 13 | + Featured bool `json:"featured"` |
| 14 | +} |
| 15 | + |
| 16 | +type repositoryReply struct { |
| 17 | + License licensesReply `json:"license"` |
| 18 | +} |
| 19 | + |
| 20 | +type licenseReply struct { |
| 21 | + Body string `json:"body"` |
| 22 | +} |
| 23 | + |
| 24 | +// To update, use: |
| 25 | +// curl -s -H 'Accept: application/vnd.github.drax-preview+json' https://api.github.com/licenses | jq '.[].key' |
| 26 | +// then compare with https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/#license-specification |
| 27 | +var githubLicenseToDebianLicense = map[string]string{ |
| 28 | + //"agpl-3.0" (not in debian?) |
| 29 | + "apache-2.0": "Apache-2.0", |
| 30 | + "artistic-2.0": "Artistic-2.0", |
| 31 | + "bsd-2-clause": "BSD-2-clause", |
| 32 | + "bsd-3-clause": "BSD-3-clause", |
| 33 | + "cc0-1.0": "CC0-1.0", |
| 34 | + //"epl-1.0" (eclipse public license) |
| 35 | + "gpl-2.0": "GPL-2.0", // TODO: is this GPL-2.0+? |
| 36 | + "gpl-3.0": "GPL-3.0", |
| 37 | + "isc": "ISC", |
| 38 | + "lgpl-2.1": "LGPL-2.1", |
| 39 | + "lgpl-3.0": "LGPL-3.0", |
| 40 | + //"mit" - expat? |
| 41 | + //"mpl-2.0" (only 1.1 is in debian) |
| 42 | + //"unlicense" (not in debian) |
| 43 | +} |
| 44 | + |
| 45 | +var debianLicenseText = map[string]string{ |
| 46 | + "Apache-2.0": ` Licensed under the Apache License, Version 2.0 (the "License"); |
| 47 | + you may not use this file except in compliance with the License. |
| 48 | + You may obtain a copy of the License at |
| 49 | + . |
| 50 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 51 | + . |
| 52 | + Unless required by applicable law or agreed to in writing, software |
| 53 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 54 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 55 | + See the License for the specific language governing permissions and |
| 56 | + limitations under the License. |
| 57 | + . |
| 58 | + On Debian systems, the complete text of the Apache version 2.0 license |
| 59 | + can be found in "/usr/share/common-licenses/Apache-2.0". |
| 60 | +`, |
| 61 | +} |
| 62 | + |
| 63 | +func getLicenseForGopkg(gopkg string) (string, string, error) { |
| 64 | + if !strings.HasPrefix(gopkg, "github.com/") { |
| 65 | + return "", "", nil |
| 66 | + } |
| 67 | + req, err := http.NewRequest("GET", "https://api.github.com/repos/"+gopkg[len("github.com/"):], nil) |
| 68 | + if err != nil { |
| 69 | + return "", "", err |
| 70 | + } |
| 71 | + req.Header.Set("Accept", "application/vnd.github.drax-preview+json") |
| 72 | + resp, err := http.DefaultClient.Do(req) |
| 73 | + if err != nil { |
| 74 | + return "", "", err |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + var r repositoryReply |
| 78 | + if err := json.NewDecoder(resp.Body).Decode(&r); err != nil { |
| 79 | + return "", "", err |
| 80 | + } |
| 81 | + if deblicense, ok := githubLicenseToDebianLicense[r.License.Key]; ok { |
| 82 | + fulltext := debianLicenseText[deblicense] |
| 83 | + if fulltext == "" { |
| 84 | + fulltext = "TODO" |
| 85 | + } |
| 86 | + return deblicense, fulltext, nil |
| 87 | + } else { |
| 88 | + return "TODO", "TODO", nil |
| 89 | + } |
| 90 | +} |
0 commit comments