Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathgithub_commit_graph.rb
More file actions
Latest commit
94 lines (81 loc) · 2.73 KB
/
Copy pathgithub_commit_graph.rb
File metadata and controls
94 lines (81 loc) · 2.73 KB
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
modulePlugins
classGithubCommitGraph < Base
deflocals
{username:,contributions:,stats: }
end
private
defusername=settings['username']
defcontributions
@contributions ||= begin
query="query($userName:String!) {
user(login: $userName){
contributionsCollection {
contributionCalendar {
totalContributions
weeks {
contributionDays {
contributionCount
date
}
}
}
}
}
}"
body={query: query,variables: {userName: username}}
resp=post('https://api.github.com/graphql',body: body.to_json,headers: headers)
data=resp.dig('data','user','contributionsCollection','contributionCalendar')
ifdata.nil?
{errors: "Cannot fetch data for #{username}"}
else
{
total: data['totalContributions'],
commits: data['weeks'].map(&:deep_symbolize_keys)
}
end
end
end
defheaders
{'authorization'=>"Bearer #{Rails.application.credentials.plugins.github_commit_graph_token}"}
end
# re "Streak" calculations, these may be wrong for some users
# either A), the 'time-zone' header does not work (https://github.com/orgs/community/discussions/20898)
# or B), the time zone is only inferred by the token's owner, us! (https://docs.github.com/en/rest/metrics/statistics?apiVersion=2022-11-28#get-the-hourly-commit-count-for-each-day)
defstats
return{}ifcontributions[:commits].nil?
days=contributions[:commits].flat_map{ |week| week[:contributionDays]}
sorted_days=days.sort_by{ |day| Date.parse(day[:date])}
{
longest_streak: longest_streak(sorted_days),
current_streak: current_streak(sorted_days),
max_contributions: days.map{ |day| day[:contributionCount]}.max,
average_contributions: average_contributions(days)
}
end
defaverage_contributions(days)
total_contributions=days.sum{ |day| day[:contributionCount]}
(total_contributions.to_f / days.size).round(2)
end
deflongest_streak(days)
longest=current=0
days.eachdo |day|
ifday[:contributionCount].positive?
current += 1
longest=[longest,current].max
else
current=0
end
end
longest
end
defcurrent_streak(days)
streak=0
streak += 1ifdays.last[:contributionCount].positive?
days[0..-2].reverse_eachdo |day|
breakifday[:contributionCount].zero?
streak += 1
end
streak
end
end
end