Class: GithubContribs

Inherits:
Object
  • Object
show all
Defined in:
lib/github_contribs.rb

Constant Summary collapse

VERSION =
"2.0.1"
QUERY =
"query($userName: String!, $from: DateTime!) {\n  user(login: $userName) {\n    contributionsCollection(from: $from) {\n      contributionCalendar {\n        totalContributions\n        weeks {\n          contributionDays {\n            contributionCount\n            date\n          }\n        }\n      }\n    }\n  }\n}\n".strip
YEARS_QUERY =
"query($userName: String!) {\n  user(login: $userName) {\n    contributionsCollection {\n      contributionYears\n    }\n  }\n}\n".strip

Instance Method Summary collapse

Instance Method Details

#generate(name, last, io = $stdout, testing = false) ⇒ Object



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/github_contribs.rb', line 77

def generate name, last, io = $stdout, testing = false
  last ||= years(name).min
  last = last.to_i # string from cmdline

  unless testing then
    FileUtils.rm_f ".#{name}.#{Time.now.year}.json" # always fetch this fresh
  end

  steps = 16

  # HACK: make it know the years automatically
  contribs = load_all name, last..Time.now.year

  d0, dN = Date.new(last), Date.today

  min, max = contribs.values.minmax

  max1 = Math.log(max+1)

  scale = ->(n) { [n, (steps * Math.log(n) / max1).floor] }

  total_contributions = contribs.values.sum

  range = (min..max) # used for legend below
    .group_by { |n| scale[n].last }
    .transform_values { |ary|
      m, n = ary.minmax
      m == n ? m : Range.new(m, n)
    }

  contribs.transform_values!(&scale)

  years = (d0.year..dN.year).to_a.reverse.to_h { |year|
    d0 = Date.new year
    d1 = Date.new year+1

    d0 -= d0.wday # back it up to sunday to square everything off
    d1 += (7-d1.wday) # unless d1.wday == 0

    days = (d0...d1).map { |d| d.year == year ? [d.to_s, contribs[d.to_s]] : [] }

    by_week = days.each_slice(7).to_a.transpose

    [ year, by_week ]
  }

  def io.td day, code, count
    if code && count then
      print '<td class="entry day green-color-%d tooltip">' % [code]
      print '<div class="right">%s = %s</div>' % [day, count]
      puts '</td>'
    else
      puts '<td class="entry day nocolor"></td>'
    end
  end

  io.puts "<html>"
  io.puts "<head>"
  io.puts "<title>%s's contribution calendar</title>" % [name]
  io.puts "    <style>\n      body { background-color: #ffffff; font-family: system-ui; }\n\n      .entry {\n          font-size: 0.8rem;\n          display: inline-block;\n          margin: 1px;\n          width:  12px;\n          height: 12px;\n          border-radius: 2px;\n          outline: 1px solid rgba(0, 0, 0, 10%);\n          outline-offset: -1px;\n      }\n\n      .entry.day:hover { outline: 2px solid rgba(0, 0, 0, 10%); }\n\n      .non-day { outline: none; } /* cancel out entry's outline for non-days */\n\n      .tooltip {\n          display: inline-block;\n          position: relative;\n      }\n\n      .tooltip .right {\n          font-family: monospace;\n          font-size: 1.2rem;\n          min-width: 11em; # \"yyyy-mm-dd = xyz\" = 16 chars? but em calculation is wack\n          top: 50%;\n          left: 100%;\n          margin-left: 20px;\n          transform: translate(0, -);\n          padding: 5px 5px;\n          color: #444;\n          background-color: #ccc;\n          border-radius: 8px;\n          position: absolute;\n          z-index: 99999999;\n          box-sizing: border-box;\n          border: 1px solid #fff;\n          display: none;\n      }\n\n      .tooltip:hover .right { display: block; }\n\n      .nocolor { background-color: hsl(120, 10%, 99%); }\n\n      .green-color-0  { background-color: hsl(120 70% 95%); }\n      .green-color-1  { background-color: hsl(120 70% 90%); }\n      .green-color-2  { background-color: hsl(120 70% 85%); }\n      .green-color-3  { background-color: hsl(120 70% 80%); }\n      .green-color-4  { background-color: hsl(120 70% 75%); }\n      .green-color-5  { background-color: hsl(120 70% 70%); }\n      .green-color-6  { background-color: hsl(120 70% 65%); }\n      .green-color-7  { background-color: hsl(120 70% 60%); }\n      .green-color-8  { background-color: hsl(120 70% 55%); }\n      .green-color-9  { background-color: hsl(120 70% 50%); }\n      .green-color-10 { background-color: hsl(120 70% 45%); }\n      .green-color-11 { background-color: hsl(120 70% 40%); }\n      .green-color-12 { background-color: hsl(120 70% 35%); }\n      .green-color-13 { background-color: hsl(120 70% 30%); }\n      .green-color-14 { background-color: hsl(120 70% 25%); }\n      .green-color-15 { background-color: hsl(120 70% 20%); }\n    </style>\n  CSS\n  io.puts \"</head>\"\n\n  io.puts \"<body>\"\n\n  io.puts \"<h1>\#{name}'s complete github contributions</h1>\"\n  io.puts \"<p><small>Total contributions = %d</small>\" % [total_contributions]\n\n  io.puts \"<div><small>Legend: </small>\"\n  io.puts \"<table>\"\n  io.puts \"<tr>\"\n  io.puts '<td class=\"entry day nocolor\"></td>'\n  steps.times.each do |code|\n    io.td \"level \#{code}\", code, range[code]\n  end\n  io.puts \"</tr>\"\n  io.puts \"</table>\"\n  io.puts \"</div>\"\n\n  years.each do |year, by_week|\n    total = contribs\n      .select { |date, (count, code)| date.start_with?(year.to_s) && count }\n      .values\n      .map(&:first)\n      .sum\n\n    io.puts \"<h2>%d</h2>\" % [year]\n\n    io.puts '<table class=\"heatmap calendar\">'\n\n    by_week.each_with_index do |weekdays, idx|\n      io.puts \"<!-- \#{year} \#{Date::ABBR_DAYNAMES[idx]} -->\"\n      io.puts \"<tr>\"\n\n      io.puts '<td class=\"entry non-day\">%s</td>' % [[1, 3, 5].include?(idx) ? Date::ABBR_DAYNAMES[idx][0] : nil]\n\n      weekdays.each_with_index do |(day, (count, code)), wday|\n        if day then\n          io.td day, code, count\n        else\n          io.puts '<td class=\"entry non-day\"></td>'\n        end\n      end\n\n      io.puts \"</tr>\"\n    end\n\n    io.puts \"</table>\"\n    io.puts \"<small>%d contributions</small>\" % [total]\n  end # years\n\n  io.puts \"</body>\"\n  io.puts \"</html>\"\nend\n"

#get(name, year) ⇒ Object



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
# File 'lib/github_contribs.rb', line 30

def get name, year
  path = ".#{name}.#{year}.json"

  unless File.exist? path then
    warn "#{name} #{year}" if $v

    data = graphql("--paginate", "--slurp",
                   "-F", "userName=#{name}",
                   "-F", "from=#{year}-01-01T00:00:00",
                   "-f", "query=%s" % [QUERY])
      .dig(0,
           "data",
           "user", # JFC
           "contributionsCollection",
           "contributionCalendar",
           "weeks")
      .map { |h| h["contributionDays"] }
      .map { |a| a.to_h { |h| [h["date"], h["contributionCount"]] } }
      .reduce(&:merge).sort.to_h
      .select { |k,v| k.start_with? "#{year}" } # edges of calendar
      .select { |k,v| v > 0 }

    File.write path, JSON.pretty_generate(data)
  end

  JSON.parse File.read path
end

#graphql(*args) ⇒ Object



8
9
10
# File 'lib/github_contribs.rb', line 8

def graphql(*args)
  IO.popen(["gh", "api", "graphql", *args]) { |io| JSON.parse(io.read) }
end

#load_all(name, years) ⇒ Object



73
74
75
# File 'lib/github_contribs.rb', line 73

def load_all name, years
  years.map { |year| get name, year }.reduce(&:merge)
end

#years(name) ⇒ Object



68
69
70
71
# File 'lib/github_contribs.rb', line 68

def years name
  graphql("-F", "userName=#{name}", "-f", "query=%s" % [YEARS_QUERY])
    .dig("data", "user", "contributionsCollection", "contributionYears")
end