summaryrefslogtreecommitdiff
path: root/cmd/generate/forge_github.go
blob: 9f475a3e05d969c8da3dcc77aac5c6fbf90edd97 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main

import (
	"fmt"
	"net/url"
	"regexp"
	"time"
)

var reGitHubPR = regexp.MustCompile(`^https://github\.com/([^/?#]+)/([^/?#]+)/pull/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)

func githubPagination(i int) url.Values {
	params := make(url.Values)
	params.Set("page", fmt.Sprintf("%v", i+1))
	return params
}

type GitHub struct{}

var _ Forge = GitHub{}

func (GitHub) FetchStatus(urls []string) (string, error) {
	for _, u := range urls {
		if reGoogleGerritCL.MatchString(u) {
			return "", nil
		}
	}
	return fetchPerURLStatus(urls, func(u string) (string, error) {
		m := reGitHubPR.FindStringSubmatch(u)
		if m == nil {
			return "", nil
		}
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			// State values are "open" and "closed".
			State          string `json:"state"`
			Merged         bool   `json:"merged"`
			MergeCommitSha string `json:"merge_commit_sha"`
		}
		if err := httpGetJSON(urlStr, nil, &obj); err != nil {
			return "", err
		}
		ret := obj.State
		if obj.Merged {
			ret = statusMerged
			tag, err := getGitTagThatContainsAll("https://github.com/"+user+"/"+repo, obj.MergeCommitSha)
			if err != nil {
				return "", err
			}
			if tag != "" {
				ret = fmt.Sprintf(statusReleasedFmt, tag)
			}
		}

		return ret, nil
	})
}

func (GitHub) FetchSubmittedAt(urls []string) (time.Time, error) {
	return fetchPerURLSubmittedAt(urls, func(u string) (time.Time, error) {
		m := reGitHubPR.FindStringSubmatch(u)
		if m == nil {
			return time.Time{}, nil
		}
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			CreatedAt time.Time `json:"created_at"`
		}
		if err := httpGetJSON(urlStr, nil, &obj); err != nil {
			return time.Time{}, err
		}
		return obj.CreatedAt, nil
	})
}

func (GitHub) FetchLastUpdated(urls []string) (time.Time, User, error) {
	for _, u := range urls {
		if reGoogleGerritCL.MatchString(u) {
			return time.Time{}, User{}, nil
		}
	}
	return fetchPerURLLastUpdated(urls, func(u string) (time.Time, User, error) {
		m := reGitHubPR.FindStringSubmatch(u)
		if m == nil {
			return time.Time{}, User{}, nil
		}
		user := m[1]
		repo := m[2]
		prnum := m[3]

		urlStr := "https://api.github.com/repos/" + user + "/" + repo + "/pulls/" + prnum

		var obj struct {
			UpdatedAt time.Time `json:"updated_at"`

			CreatedAt time.Time `json:"created_at"`
			CreatedBy struct {
				Login   string `json:"login"`
				HTMLURL string `json:"html_url"`
			} `json:"user"`

			MergedAt time.Time `json:"merged_at"`
			MergedBy struct {
				Login   string `json:"login"`
				HTMLURL string `json:"html_url"`
			} `json:"merged_by"`
		}
		if err := httpGetJSON(urlStr, nil, &obj); err != nil {
			return time.Time{}, User{}, err
		}

		retUpdatedAt := obj.UpdatedAt
		var retUser User

		if retUser == (User{}) && withinOneSecond(obj.CreatedAt, retUpdatedAt) {
			retUser.Name = obj.CreatedBy.Login
			retUser.URL = obj.CreatedBy.HTMLURL
		}
		if retUser == (User{}) && withinOneSecond(obj.MergedAt, retUpdatedAt) {
			retUser.Name = obj.MergedBy.Login
			retUser.URL = obj.MergedBy.HTMLURL
		}
		if retUser == (User{}) {
			// "normal" comments
			var comments []struct {
				UpdatedAt time.Time `json:"updated_at"`
				User      struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"user"`
			}
			if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/comments", nil, &comments, githubPagination); err != nil {
				return time.Time{}, User{}, err
			}
			for _, comment := range comments {
				if withinOneSecond(comment.UpdatedAt, retUpdatedAt) {
					retUser.Name = comment.User.Login
					retUser.URL = comment.User.HTMLURL
					break
				}
			}
		}
		if retUser == (User{}) {
			// comments on a specific part of the diff
			var reviewComments []struct {
				UpdatedAt time.Time `json:"updated_at"`
				User      struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"user"`
			}
			if err := httpGetPaginatedJSON("https://api.github.com/repos/"+user+"/"+repo+"/pulls/"+prnum+"/comments", nil, &reviewComments, githubPagination); err != nil {
				return time.Time{}, User{}, err
			}
			for _, comment := range reviewComments {
				if withinOneSecond(comment.UpdatedAt, retUpdatedAt) {
					retUser.Name = comment.User.Login
					retUser.URL = comment.User.HTMLURL
					break
				}
			}
		}
		if retUser == (User{}) {
			var events []struct {
				CreatedAt time.Time `json:"created_at"`
				Actor     struct {
					Login   string `json:"login"`
					HTMLURL string `json:"html_url"`
				} `json:"actor"`
			}
			if err := httpGetJSON("https://api.github.com/repos/"+user+"/"+repo+"/issues/"+prnum+"/events", nil, &events); err != nil {
				return time.Time{}, User{}, err
			}
			for _, event := range events {
				if withinOneSecond(event.CreatedAt, retUpdatedAt) {
					retUser.Name = event.Actor.Login
					retUser.URL = event.Actor.HTMLURL
					break
				}
			}
		}

		return retUpdatedAt, retUser, nil
	})
}