Update github_commit_graph plugin from core repo

This commit is contained in:
Ryan Kulp
2025-09-12 11:36:26 -04:00
parent 7983607a53
commit a4f3060ca6
14 changed files with 543 additions and 139 deletions
+22 -20
View File
@@ -25,19 +25,18 @@ module Plugins
}
}
}"
body = {
query: query,
variables: { userName: settings['username'] }
}
url = 'https://api.github.com/graphql'
resp = HTTParty.post(url, body: body.to_json, headers: headers)
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')
{
total: data['totalContributions'],
commits: data['weeks']
}
if data.nil?
{ errors: "Cannot fetch data for #{username}" }
else
{
total: data['totalContributions'],
commits: data['weeks'].map(&:deep_symbolize_keys)
}
end
end
end
@@ -45,27 +44,32 @@ module Plugins
{ '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)
def stats
days = contributions[:commits].flat_map { |week| week['contributionDays'] }
sorted_days = days.sort_by { |day| Date.parse(day['date']) }
return {} if contributions[: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,
max_contributions: days.map { |day| day[:contributionCount] }.max,
average_contributions: average_contributions(days)
}
end
def average_contributions(days)
total_contributions = days.sum { |day| day['contributionCount'] }
total_contributions = days.sum { |day| day[:contributionCount] }
(total_contributions.to_f / days.size).round(2)
end
def longest_streak(days)
longest = current = 0
days.each do |day|
if (day['contributionCount']).positive?
if day[:contributionCount].positive?
current += 1
longest = [longest, current].max
else
@@ -77,12 +81,10 @@ module Plugins
def current_streak(days)
streak = 0
# The current day can count towards the streak but it shouldn't break the streak
streak += 1 if days.last[:contributionCount].positive?
days[0..-2].reverse_each do |day|
break if (day['contributionCount']).zero?
break if day[:contributionCount].zero?
streak += 1
end