This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
136 lines (121 loc) · 3.55 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
// GitHubInfo is a subset of the GH API info.
type GitHubInfo struct {
ID int
Name string
Login string
}
// FetchGitHubInfo fetches information about the GitHub user associated
// with who. If no such user exists, it returns nil.
func FetchGitHubInfo(who *acLine) (*GitHubInfo, error) {
id, err := fetchGitHubUserID(who)
if err != nil {
return nil, err
}
if id == "" {
// There is no GitHub user associated with who.
return nil, nil
}
cacheDir, err := githubCacheDir()
if err != nil {
return nil, err
}
cacheFile := filepath.Join(cacheDir, fmt.Sprintf("user-id-%s", id))
if slurp, err := ioutil.ReadFile(cacheFile); err == nil {
res := &GitHubInfo{}
if err := json.Unmarshal(slurp, res); err != nil {
return nil, fmt.Errorf("%s: %v", cacheFile, err)
}
return res, nil
}
jsonURL := fmt.Sprintf("https://api.github.com/user/%s", id) // undocumented but it works
req, _ := http.NewRequest("GET", jsonURL, nil)
if token, err := ioutil.ReadFile(githubTokenFile()); err == nil {
req.Header.Set("Authorization", "token "+strings.TrimSpace(string(token)))
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("%s: %v", jsonURL, res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("%s: %v", jsonURL, err)
}
jres := &GitHubInfo{}
if err := json.Unmarshal(body, jres); err != nil {
return nil, fmt.Errorf("%s: %v", jsonURL, err)
}
if jres.ID == 0 {
return nil, fmt.Errorf("%s: malformed response", jsonURL)
}
os.MkdirAll(cacheDir, 0700)
ioutil.WriteFile(cacheFile, body, 0600)
return jres, nil
}
// fetchGitHubUserID fetches the ID of the GitHub user associated
// with who. If no such user exists, it returns the empty string.
func fetchGitHubUserID(who *acLine) (string, error) {
org, repo := githubOrgRepo(who.firstRepo)
cacheDir, err := githubCacheDir()
if err != nil {
return "", err
}
cacheFile := filepath.Join(cacheDir, fmt.Sprintf("%s-%s-%s-id", org, repo, who.firstCommit))
if slurp, err := ioutil.ReadFile(cacheFile); err == nil {
return string(slurp), nil
}
jsonURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits/%s", org, repo, who.firstCommit)
req, _ := http.NewRequest("GET", jsonURL, nil)
if token, err := ioutil.ReadFile(githubTokenFile()); err == nil {
req.Header.Set("Authorization", "token "+strings.TrimSpace(string(token)))
}
var jres struct {
Author struct {
ID int
}
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return "", fmt.Errorf("%s: %v", jsonURL, res.Status)
}
if err := json.NewDecoder(res.Body).Decode(&jres); err != nil {
return "", fmt.Errorf("%s: %v", jsonURL, err)
}
if jres.Author.ID == 0 {
return "", nil // not a registered GitHub user
}
os.MkdirAll(cacheDir, 0700)
ioutil.WriteFile(cacheFile, []byte(strconv.Itoa(jres.Author.ID)), 0600)
return strconv.Itoa(jres.Author.ID), nil
}
func githubCacheDir() (string, error) {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(userCacheDir, "updatecontrib-github"), nil
}
func githubTokenFile() string {
return filepath.Join(os.Getenv("HOME"), ".github-updatecontrib-token")
}