-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-activity.rb
executable file
·75 lines (64 loc) · 1.87 KB
/
fetch-activity.rb
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
#!/usr/bin/env ruby
# SPDX-FileCopyrightText: 2024 Hugo Peixoto <[email protected]>
# SPDX-License-Identifier: AGPL-3.0-only
require 'net/http'
require 'json'
require 'date'
config = JSON.parse(File.read("config.json"))
def github_activity(username, token)
query = "query {
user(login: \"#{username}\") {
contributionsCollection {
contributionCalendar {
weeks {
contributionDays {
date
contributionCount
}
}
}
}
}
}"
URI("https://api.github.com/graphql")
.then { Net::HTTP.post(_1, JSON.dump({ query: }), { "Authorization" => "bearer #{token}" }) }
.then(&:body)
.then { JSON.parse(_1) }
.dig("data", "user", "contributionsCollection", "contributionCalendar", "weeks")
.flat_map { |w| w["contributionDays"] }
.map { |d| [d["date"], d["contributionCount"]] }
.to_h
end
def gitlab_activity(username)
URI("https://gitlab.com/users/#{username}/calendar.json")
.then { Net::HTTP.get(_1) }
.then { JSON.parse(_1) }
end
def gitea_activity(username, url, token)
headers =
if token
{ "Authorization" => "bearer #{token}" }
else
{}
end
URI("https://#{url}/api/v1/users/#{username}/heatmap")
.then { Net::HTTP.get(_1, headers) }
.then { JSON.parse(_1) }
.map { |x| [Time.at(x["timestamp"]).to_date.to_s, x["contributions"]] }
.group_by { |x| x[0] }
.transform_values { |v| v.map(&:last).sum }
end
def activity(forge)
case forge["url"]
when "github.com"
github_activity(forge["username"], forge["token"])
when "gitlab.com"
gitlab_activity(forge["username"])
else
gitea_activity(forge["username"], forge["url"], forge["token"])
end
end
config["forges"]
.map { |forge| { url: forge["url"], activity: activity(forge) } }
.then { JSON.dump(_1) }
.then { File.write("activity.json", _1) }