-
Notifications
You must be signed in to change notification settings - Fork 0
/
steps.go
165 lines (131 loc) · 4.51 KB
/
steps.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
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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"image"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log"
"net/http"
)
func ListContributors(repo string, token string) ([]ContributorInfo, error) {
// Connect endpoint URL
endpointUrl := fmt.Sprintf("https://api.github.com/repos/%s/contributors", repo)
// Prepare request
req, err := http.NewRequest("GET", endpointUrl, nil)
if err != nil {
// Failed to create contributors request
return nil, fmt.Errorf("creating request: %w", err)
}
// Set request headers
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
// Do request
res, err := (&http.Client{}).Do(req)
if err != nil {
// Failed to do request
return nil, fmt.Errorf("sending request: %w", err)
}
defer res.Body.Close()
// Check response
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
// Decode response
var contributors []ContributorInfo
err = json.NewDecoder(res.Body).Decode(&contributors)
if err != nil {
return nil, fmt.Errorf("decoding response: %w", err)
}
return contributors, nil
}
func FilterUsersOnly(contributors []ContributorInfo) []ContributorInfo {
var userContributors []ContributorInfo
for _, contributor := range contributors {
if contributor.Type == "User" &&
contributor.Login != "fossabot" { // They are using a user account for bot actions
userContributors = append(userContributors, contributor)
}
}
return userContributors
}
func DownloadInfo(contributors []ContributorInfo) []ContributorInfoDownload {
contributorsDl := make([]ContributorInfoDownload, len(contributors))
for index, contributor := range contributors {
log.Printf("Download %d of %d...", index+1, len(contributors))
contributorsDl[index].Login = contributor.Login
contributorsDl[index].Id = contributor.Id
// Download image
avatarImage, err := (&http.Client{}).Get(contributor.AvatarUrl)
if err != nil {
log.Printf("Error downloading avatar image %s: %v", contributor.AvatarUrl, err)
continue
}
avatarBytes, err := io.ReadAll(avatarImage.Body)
if err != nil {
log.Printf("Error reading avatar image %s: %v", contributor.AvatarUrl, err)
continue
}
avatarImage.Body.Close()
var img image.Image
contentType := http.DetectContentType(avatarBytes)
switch contentType {
case "image/png":
img, err = png.Decode(bytes.NewReader(avatarBytes))
case "image/jpeg":
img, err = jpeg.Decode(bytes.NewReader(avatarBytes))
case "image/gif":
img, err = gif.Decode(bytes.NewReader(avatarBytes))
default:
log.Printf("Unknown content type: %s", contentType)
continue
}
cropped := CropImage(img)
var avatarBuf bytes.Buffer
err = png.Encode(&avatarBuf, cropped)
if err != nil {
log.Printf("Error encoding image %s: %v", contributor.AvatarUrl, err)
continue
}
contributorsDl[index].AvatarBuf = avatarBuf.Bytes()
}
return contributorsDl
}
func CropImage(img image.Image) *image.RGBA {
b := img.Bounds()
dx := b.Dx()
dy := b.Dy()
dst := image.NewRGBA(image.Rect(0, 0, dx, dy))
// draw.Draw(dst, b, img, b.Min, draw.Src)
draw.DrawMask(dst, dst.Bounds(), img, image.Point{}, &circle{image.Pt(dx/2, dy/2), min(dx, dy) / 2}, image.Point{}, draw.Over)
return dst
}
func GenerateGraph(contributors []ContributorInfoDownload) string {
// Generate SVG with template
avatarSpace := AvatarSize + 2*AvatarMargin
linesCount := len(contributors) / AvatarsPerLine
if len(contributors)%AvatarsPerLine > 0 {
linesCount++
}
startTemplate := fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%d" height="%d">`, AvatarsPerLine*avatarSpace, linesCount*avatarSpace)
styleTemplate := `<style></style>`
internalContent := ""
for index, contributor := range contributors {
row := index % AvatarsPerLine
line := index / AvatarsPerLine
internalContent += fmt.Sprintf(`<a xlink:href="https://github.com/%s" class="kr-contributors" target="_blank" rel="nofollow" id="github-user-%d"><image x="%d" y="%d" width="%d" height="%d" clip-path="url(#clip)" xlink:href="data:image/png;base64,%s" /><span>@%s</span></a>`,
contributor.Login, contributor.Id,
row*avatarSpace+AvatarMargin, line*avatarSpace+AvatarMargin,
AvatarSize, AvatarSize,
base64.StdEncoding.EncodeToString(contributor.AvatarBuf), contributor.Login,
)
}
endTemplate := "</svg>"
return startTemplate + styleTemplate + internalContent + endTemplate
}