summaryrefslogtreecommitdiff
path: root/cmd/generate/forge_forgejo.go
blob: 62234b956461e0271f048a5c4d22173964edf376 (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
package main

import (
	"fmt"
	"regexp"
	"time"
)

var reForgejoPR = regexp.MustCompile(`^https://([^/]+)/([^/?#]+)/([^/?#]+)/pulls/([0-9]+)(?:\?[^#]*)?(?:#.*)?$`)

type Forgejo struct {
	Authority string
}

var _ Forge = Forgejo{}

func (f Forgejo) FetchStatus(urls []string) (string, error) {
	for _, u := range urls {
		m := reForgejoPR.FindStringSubmatch(u)
		if m == nil || m[1] != f.Authority {
			continue
		}
		authority := m[1]
		user := m[2]
		repo := m[3]
		prnum := m[4]

		urlStr := "https://" + authority + "/api/v1/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://"+authority+"/"+user+"/"+repo, obj.MergeCommitSha)
			if err != nil {
				return "", err
			}
			if tag != "" {
				ret = fmt.Sprintf(statusReleasedFmt, tag)
			}
		}

		return ret, nil
	}
	return "", nil
}

func (f Forgejo) FetchSubmittedAt(urls []string) (time.Time, error) {
	for _, u := range urls {
		m := reForgejoPR.FindStringSubmatch(u)
		if m == nil || m[1] != f.Authority {
			continue
		}
		authority := m[1]
		user := m[2]
		repo := m[3]
		prnum := m[4]

		urlStr := "https://" + authority + "/api/v1/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
	}
	return time.Time{}, nil
}

func (f Forgejo) FetchLastUpdated(urls []string) (time.Time, User, error) {
	for _, u := range urls {
		m := reForgejoPR.FindStringSubmatch(u)
		if m == nil || m[1] != f.Authority {
			continue
		}
		authority := m[1]
		user := m[2]
		repo := m[3]
		prnum := m[4]

		urlStr := "https://" + authority + "/api/v1/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
	}
	return time.Time{}, User{}, nil
}