MCPcopy
hub / github.com/syncthing/syncthing / getLicenseText

Function getLicenseText

script/copyrights.go:392–429  ·  view source on GitHub ↗
(owner, repo string)

Source from the content-addressed store, hash-verified

390}
391
392func getLicenseText(owner, repo string) string {
393 url := fmt.Sprintf("https://api.github.com/repos/%s/%s/license", owner, repo)
394 req, _ := http.NewRequest("GET", url, nil)
395 req.Header.Set("Accept", "application/vnd.github.v3+json")
396
397 if token := os.Getenv("GITHUB_TOKEN"); token != "" {
398 req.Header.Set("Authorization", "Bearer "+token)
399 }
400
401 resp, err := http.DefaultClient.Do(req)
402 if err != nil {
403 log.Fatal(err)
404 }
405 defer resp.Body.Close()
406 if resp.StatusCode == 404 {
407 return ""
408 }
409 var result struct {
410 Content string `json:"content"`
411 Encoding string `json:"encoding"`
412 }
413 body, _ := io.ReadAll(resp.Body)
414 err = json.Unmarshal(body, &result)
415 if err != nil {
416 log.Fatal(err)
417 }
418
419 if result.Encoding != "base64" {
420 log.Fatal(fmt.Sprintf("unexpected encoding: %q", result.Encoding))
421 }
422
423 decoded, err := base64.StdEncoding.DecodeString(result.Content)
424 if err != nil {
425 log.Fatal(err)
426 }
427
428 return string(decoded)
429}
430
431func extractCopyrights(license string, notice CopyrightNotice) []string {
432 lines := strings.Split(license, "\n")

Callers 1

mainFunction · 0.85

Calls 5

DoMethod · 0.80
FatalMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected