-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
276 lines (257 loc) · 9.03 KB
/
main.py
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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import datetime
import math
import typer
from typing import List
from typing_extensions import Annotated
from src.fonts.default import nitram_micro_mono_CP437
from src.github import GitHub, Contribution, Visibility
from src.window import Window
from src.util import (
next_saturday_of_date,
sunday_of_date,
next_saturday,
prev_sunday_52_weeks_ago,
HAlign,
VAlign,
Color,
Pixel,
)
app = typer.Typer()
def print_contribs(contribs: List[Contribution], width: int, height: int = 7):
window = Window(
width=width,
height=height,
empty_pixel=Pixel(Color(0)),
padding=(0, 0, 0, 0),
)
min_contrib = min(contribs, key=lambda c: c.count).count
max_contrib = max(contribs, key=lambda c: c.count).count
quarter = (max_contrib) // 4
print(
f"Quartiles (width={quarter}): ",
max_contrib - quarter,
max_contrib - 2 * quarter,
max_contrib - 3 * quarter,
min_contrib,
)
# experimentally, this seems to be how the graph is colored
for i, contrib in enumerate(reversed(contribs)):
if contrib.count >= max_contrib - quarter:
window.buf[i] = Pixel(Color(4))
elif contrib.count >= max_contrib - 2 * quarter:
window.buf[i] = Pixel(Color(3))
elif contrib.count >= max_contrib - 3 * quarter:
window.buf[i] = Pixel(Color(2))
elif contrib.count >= min_contrib:
window.buf[i] = Pixel(Color(1))
else:
window.buf[i] = Pixel(Color(0))
print(window)
@app.command()
def simulate(
user: Annotated[
str,
typer.Argument(
help="GitHub user to simulate contribution banner for (e.g. 'tbrockman'). Used to retrieve existing contributions",
envvar="INPUT_USER",
),
],
token: Annotated[
str,
typer.Option(
help="GitHub personal access token (used for creating/deleting repos, pushing commits, and getting user contribution history)",
envvar="INPUT_TOKEN",
),
],
start: Annotated[
datetime.datetime,
typer.Option(
help="The start of the date range to generate the contribution banner for (will be rounded to the start of the previous Sunday)",
envvar="INPUT_START",
),
] = prev_sunday_52_weeks_ago,
end: Annotated[
datetime.datetime,
typer.Option(
help="The end of the date range to generate the contribution banner for (will be rounded to the start of next Saturday)",
envvar="INPUT_END",
),
] = next_saturday,
):
git = GitHub(token)
contribs = git.get_user_contributions(user, start, end)
width = math.ceil((end - start).days / 7)
print_contribs(contribs, width)
@app.command()
def draw(
text: Annotated[
str,
typer.Argument(
help="Text to display on the contribution graph (not guaranteed to fit).",
envvar="INPUT_TEXT",
),
],
token: Annotated[
str,
typer.Option(
help="GitHub personal access token (used for creating/deleting repos, pushing commits, and getting user contribution history).",
envvar="INPUT_TOKEN",
),
],
user: Annotated[
str,
typer.Option(
help="GitHub user to generate the contribution banner for (e.g. 'tbrockman'). Used to retrieve existing contributions. Defaults to the GH user of the token.",
envvar="INPUT_USER",
),
] = "",
git_name: Annotated[
str,
typer.Option(
help="Name for git user (defaults to name in GitHub profile of token user).",
envvar="INPUT_GIT_NAME",
),
] = "",
git_email: Annotated[
str,
typer.Option(
help="Email for git user (defaults to email in GitHub profile of token user).",
envvar="INPUT_GIT_EMAIL",
),
] = "",
repo: Annotated[
str,
typer.Option(
help="The name of the repo to create fake commits in (must be owned by the user). Also used as the name of the subdirectory to initialize the repo in. Specify an organization name to create the repo under an organization (ex. 'org/github-painted').",
envvar="INPUT_REPO",
),
] = "github-painted",
visiblity: Annotated[
Visibility,
typer.Option(
help="The visibility of the generated GitHub repository.",
envvar="INPUT_VISIBILITY",
),
] = Visibility.PUBLIC,
start: Annotated[
datetime.datetime,
typer.Option(
help="The start of the date range to generate the contribution banner for (will be rounded to the start of the previous Sunday).",
envvar="INPUT_START",
),
] = prev_sunday_52_weeks_ago,
end: Annotated[
datetime.datetime,
typer.Option(
help="The end of the date range to generate the contribution banner for (will be rounded to the start of next Saturday).",
envvar="INPUT_END",
),
] = next_saturday,
separator: Annotated[
str,
typer.Option(
help="An optional string to use to separate the text (if repeating).",
envvar="INPUT_SEPARATOR",
),
] = "|",
inverse: Annotated[
bool,
typer.Option(
help="Whether to use the inverse color scheme (empty text cells surrounded by filled).",
envvar="INPUT_INVERSE",
),
] = False,
repeat: Annotated[
bool,
typer.Option(
help="Whether to repeat the text across the entire width of the window (as much as possible).",
envvar="INPUT_REPEAT",
),
] = False,
padding: Annotated[
tuple[int, int, int, int],
typer.Option(
help="Padding to add to the window (top, right, bottom, left).",
envvar="INPUT_PADDING",
),
] = (0, 0, 0, 0),
h_align: Annotated[
HAlign,
typer.Option(
help="The alignment of the text within the window.",
envvar="INPUT_HALIGN",
),
] = HAlign.CENTER,
v_align: Annotated[
VAlign,
typer.Option(
help="The alignment of the text within the window.",
envvar="INPUT_VALIGN",
),
] = VAlign.CENTER,
force_date: Annotated[
bool,
typer.Option(
help="Whether to force the chosen date range without applying the default rounding.",
envvar="INPUT_FORCE_DATE",
),
] = False,
dry_run: Annotated[
bool,
typer.Option(
help="Whether or not to actually push the commits to the remote repository (useful for testing).",
envvar="INPUT_DRY_RUN",
),
] = False,
):
"""
Given a GitHub user, a string of text, we generate fake Git commits to display the desired text on the contribution graph within a given range.
---
GitHub's contribution graph is a 53x7 grid where each cell represents a day (in UTC),
with 5 shades of green indicating relatively (as a heatmap) how many contributions were made on each day. By default (filtering to a specific time range will change this) the first cell is the previous Sunday of this week last year, and the last visible cell is today.
Based on one GitHub dev comment (https://github.com/orgs/community/discussions/23261#discussioncomment-3239758), the shade is determined by the distribution of commits in a given time-period, where each shade matches a given quartile.
The "quartile's" are not _actual_ quartiles however, the ranges seem to be divided by taking the maximum number of commits on a single day in a given time period, divided by 4.
"""
empty_pixel = Pixel(Color(1) if not inverse else Color(4))
height = 7
if not force_date:
end = next_saturday_of_date(end)
start = sunday_of_date(start)
weeks = math.ceil((end - start).days / 7)
window = Window(
width=weeks,
height=height,
empty_pixel=empty_pixel,
padding=padding,
)
git = GitHub(token)
if not user or not git_name or not git_email:
github_user = git.get_user()
user = user or github_user["login"]
git_name = git_name or github_user["name"]
git_email = git_email or github_user["email"]
# note: contribs are in reverse order (most recent first)
contribs = git.get_user_contributions(
user,
start,
end,
)
window.draw_text(
text,
nitram_micro_mono_CP437,
repeat=repeat,
separator=separator,
inverse=inverse,
h_align=h_align,
v_align=v_align,
)
print(window)
deltas = git.calc_necessary_contrib_deltas(window.buf[::-1], repo, contribs)
print("Commit delta mask (darker=more commits, lighter=less):")
print_contribs(deltas, weeks)
if not dry_run:
git.make_necessary_commits(repo, deltas, git_name, git_email, visiblity)
else:
print("Dry run, not committing or pushing to GitHub.")
if __name__ == "__main__":
app()