@stats-forge/github-stats-forge-core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/build/api/api-result.d.ts +44 -0
- package/build/api/api-result.d.ts.map +1 -0
- package/build/api/api-result.js +33 -0
- package/build/api/gist.d.ts +29 -0
- package/build/api/gist.d.ts.map +1 -0
- package/build/api/gist.js +52 -0
- package/build/api/index.d.ts +13 -0
- package/build/api/index.d.ts.map +1 -0
- package/build/api/index.js +8 -0
- package/build/api/params.d.ts +93 -0
- package/build/api/params.d.ts.map +1 -0
- package/build/api/params.js +145 -0
- package/build/api/pin.d.ts +47 -0
- package/build/api/pin.d.ts.map +1 -0
- package/build/api/pin.js +87 -0
- package/build/api/stats.d.ts +42 -0
- package/build/api/stats.d.ts.map +1 -0
- package/build/api/stats.js +132 -0
- package/build/api/top-langs.d.ts +37 -0
- package/build/api/top-langs.d.ts.map +1 -0
- package/build/api/top-langs.js +101 -0
- package/build/api/wakatime.d.ts +34 -0
- package/build/api/wakatime.d.ts.map +1 -0
- package/build/api/wakatime.js +91 -0
- package/build/calculateRank.d.ts +29 -0
- package/build/calculateRank.d.ts.map +1 -0
- package/build/calculateRank.js +58 -0
- package/build/cards/gist.d.ts +16 -0
- package/build/cards/gist.d.ts.map +1 -0
- package/build/cards/gist.js +119 -0
- package/build/cards/index.d.ts +11 -0
- package/build/cards/index.d.ts.map +1 -0
- package/build/cards/index.js +7 -0
- package/build/cards/options.d.ts +19 -0
- package/build/cards/options.d.ts.map +1 -0
- package/build/cards/options.js +1 -0
- package/build/cards/repo.d.ts +25 -0
- package/build/cards/repo.d.ts.map +1 -0
- package/build/cards/repo.js +276 -0
- package/build/cards/stats.d.ts +37 -0
- package/build/cards/stats.d.ts.map +1 -0
- package/build/cards/stats.js +457 -0
- package/build/cards/top-languages.d.ts +153 -0
- package/build/cards/top-languages.d.ts.map +1 -0
- package/build/cards/top-languages.js +805 -0
- package/build/cards/wakatime.d.ts +31 -0
- package/build/cards/wakatime.d.ts.map +1 -0
- package/build/cards/wakatime.js +376 -0
- package/build/common/Card.d.ts +121 -0
- package/build/common/Card.d.ts.map +1 -0
- package/build/common/Card.js +305 -0
- package/build/common/I18n.d.ts +27 -0
- package/build/common/I18n.d.ts.map +1 -0
- package/build/common/I18n.js +35 -0
- package/build/common/color.d.ts +112 -0
- package/build/common/color.d.ts.map +1 -0
- package/build/common/color.js +188 -0
- package/build/common/config.d.ts +53 -0
- package/build/common/config.d.ts.map +1 -0
- package/build/common/config.js +87 -0
- package/build/common/constants.d.ts +7 -0
- package/build/common/constants.d.ts.map +1 -0
- package/build/common/constants.js +9 -0
- package/build/common/date.d.ts +29 -0
- package/build/common/date.d.ts.map +1 -0
- package/build/common/date.js +23 -0
- package/build/common/error.d.ts +87 -0
- package/build/common/error.d.ts.map +1 -0
- package/build/common/error.js +108 -0
- package/build/common/fmt.d.ts +28 -0
- package/build/common/fmt.d.ts.map +1 -0
- package/build/common/fmt.js +68 -0
- package/build/common/html.d.ts +8 -0
- package/build/common/html.d.ts.map +1 -0
- package/build/common/html.js +14 -0
- package/build/common/http.d.ts +53 -0
- package/build/common/http.d.ts.map +1 -0
- package/build/common/http.js +47 -0
- package/build/common/icons.d.ts +28 -0
- package/build/common/icons.d.ts.map +1 -0
- package/build/common/icons.js +77 -0
- package/build/common/languageColors.d.ts +11 -0
- package/build/common/languageColors.d.ts.map +1 -0
- package/build/common/languageColors.js +15 -0
- package/build/common/languageColors.json +678 -0
- package/build/common/log.d.ts +9 -0
- package/build/common/log.d.ts.map +1 -0
- package/build/common/log.js +8 -0
- package/build/common/ops.d.ts +59 -0
- package/build/common/ops.d.ts.map +1 -0
- package/build/common/ops.js +115 -0
- package/build/common/render.d.ts +196 -0
- package/build/common/render.d.ts.map +1 -0
- package/build/common/render.js +474 -0
- package/build/common/retryer.d.ts +36 -0
- package/build/common/retryer.d.ts.map +1 -0
- package/build/common/retryer.js +67 -0
- package/build/fetchers/gist.d.ts +15 -0
- package/build/fetchers/gist.d.ts.map +1 -0
- package/build/fetchers/gist.js +68 -0
- package/build/fetchers/index.d.ts +13 -0
- package/build/fetchers/index.d.ts.map +1 -0
- package/build/fetchers/index.js +10 -0
- package/build/fetchers/repo.d.ts +27 -0
- package/build/fetchers/repo.d.ts.map +1 -0
- package/build/fetchers/repo.js +100 -0
- package/build/fetchers/stats.d.ts +74 -0
- package/build/fetchers/stats.d.ts.map +1 -0
- package/build/fetchers/stats.js +498 -0
- package/build/fetchers/top-languages.d.ts +23 -0
- package/build/fetchers/top-languages.d.ts.map +1 -0
- package/build/fetchers/top-languages.js +89 -0
- package/build/fetchers/types.d.ts +130 -0
- package/build/fetchers/types.d.ts.map +1 -0
- package/build/fetchers/types.js +1 -0
- package/build/fetchers/wakatime.d.ts +19 -0
- package/build/fetchers/wakatime.d.ts.map +1 -0
- package/build/fetchers/wakatime.js +36 -0
- package/build/graphql/contributionsDocument.d.ts +18 -0
- package/build/graphql/contributionsDocument.d.ts.map +1 -0
- package/build/graphql/contributionsDocument.js +30 -0
- package/build/graphql/generated/common.d.ts +42 -0
- package/build/graphql/generated/common.d.ts.map +1 -0
- package/build/graphql/generated/common.js +2 -0
- package/build/graphql/generated/gist.d.ts +44 -0
- package/build/graphql/generated/gist.d.ts.map +1 -0
- package/build/graphql/generated/gist.js +27 -0
- package/build/graphql/generated/repo.d.ts +67 -0
- package/build/graphql/generated/repo.d.ts.map +1 -0
- package/build/graphql/generated/repo.js +30 -0
- package/build/graphql/generated/stats.d.ts +150 -0
- package/build/graphql/generated/stats.d.ts.map +1 -0
- package/build/graphql/generated/stats.js +95 -0
- package/build/graphql/generated/top-languages.d.ts +54 -0
- package/build/graphql/generated/top-languages.d.ts.map +1 -0
- package/build/graphql/generated/top-languages.js +27 -0
- package/build/graphql/graphqlDocument.d.ts +22 -0
- package/build/graphql/graphqlDocument.d.ts.map +1 -0
- package/build/graphql/graphqlDocument.js +8 -0
- package/build/graphql/reposContributedToDocument.d.ts +27 -0
- package/build/graphql/reposContributedToDocument.d.ts.map +1 -0
- package/build/graphql/reposContributedToDocument.js +59 -0
- package/build/index.d.ts +15 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +10 -0
- package/build/themes/index.d.ts +505 -0
- package/build/themes/index.d.ts.map +1 -0
- package/build/themes/index.js +505 -0
- package/build/translations.d.ts +1153 -0
- package/build/translations.d.ts.map +1 -0
- package/build/translations.js +1156 -0
- package/package.json +114 -0
- package/src/_emoji-name-map.d.ts +10 -0
- package/src/_github-username-regex.d.ts +4 -0
- package/src/api/api-result.ts +69 -0
- package/src/api/gist.ts +66 -0
- package/src/api/index.ts +16 -0
- package/src/api/params.ts +221 -0
- package/src/api/pin.ts +126 -0
- package/src/api/stats.ts +195 -0
- package/src/api/top-langs.ts +150 -0
- package/src/api/wakatime.ts +130 -0
- package/src/calculateRank.ts +91 -0
- package/src/cards/gist.ts +169 -0
- package/src/cards/index.ts +21 -0
- package/src/cards/options.ts +18 -0
- package/src/cards/repo.ts +376 -0
- package/src/cards/stats.ts +615 -0
- package/src/cards/top-languages.ts +1077 -0
- package/src/cards/wakatime.ts +501 -0
- package/src/common/Card.ts +364 -0
- package/src/common/I18n.ts +51 -0
- package/src/common/color.ts +296 -0
- package/src/common/config.ts +118 -0
- package/src/common/constants.ts +12 -0
- package/src/common/date.ts +34 -0
- package/src/common/error.ts +163 -0
- package/src/common/fmt.ts +86 -0
- package/src/common/html.ts +17 -0
- package/src/common/http.ts +98 -0
- package/src/common/icons.ts +80 -0
- package/src/common/languageColors.json +678 -0
- package/src/common/languageColors.ts +19 -0
- package/src/common/log.ts +12 -0
- package/src/common/ops.ts +145 -0
- package/src/common/render.ts +641 -0
- package/src/common/retryer.ts +111 -0
- package/src/fetchers/gist.ts +82 -0
- package/src/fetchers/index.ts +25 -0
- package/src/fetchers/repo.ts +140 -0
- package/src/fetchers/stats.ts +742 -0
- package/src/fetchers/top-languages.ts +134 -0
- package/src/fetchers/types.ts +136 -0
- package/src/fetchers/wakatime.ts +56 -0
- package/src/graphql/contributionsDocument.ts +44 -0
- package/src/graphql/generated/common.ts +26 -0
- package/src/graphql/generated/gist.ts +56 -0
- package/src/graphql/generated/repo.ts +79 -0
- package/src/graphql/generated/stats.ts +180 -0
- package/src/graphql/generated/top-languages.ts +66 -0
- package/src/graphql/graphqlDocument.ts +25 -0
- package/src/graphql/queries/gist.graphql +25 -0
- package/src/graphql/queries/repo.graphql +28 -0
- package/src/graphql/queries/stats.graphql +113 -0
- package/src/graphql/queries/top-languages.graphql +27 -0
- package/src/graphql/reposContributedToDocument.ts +83 -0
- package/src/index.ts +18 -0
- package/src/themes/index.ts +519 -0
- package/src/translations.ts +1169 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import { getCardColors, isPrefixedHexColor } from './color.js';
|
|
2
|
+
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from './error.js';
|
|
3
|
+
import { kFormatter } from './fmt.js';
|
|
4
|
+
import { encodeHTML } from './html.js';
|
|
5
|
+
import { clampValue } from './ops.js';
|
|
6
|
+
/**
|
|
7
|
+
* Auto layout utility, allows us to layout things vertically or horizontally with
|
|
8
|
+
* proper gaping.
|
|
9
|
+
*
|
|
10
|
+
* The caller must ensure that the passed `items` are properly sanitized!
|
|
11
|
+
*
|
|
12
|
+
* @param props Function properties.
|
|
13
|
+
* @param props.items Array of sanitized items to layout.
|
|
14
|
+
* @param props.gap Gap between items.
|
|
15
|
+
* @param props.direction Direction to layout items.
|
|
16
|
+
* @param props.sizes Array of sizes for each item.
|
|
17
|
+
* @returns Array of items with proper layout.
|
|
18
|
+
*/
|
|
19
|
+
const flexLayout = ({ items, gap, direction, sizes = [], }) => {
|
|
20
|
+
if (sizes.some((size) => !Number.isFinite(size))) {
|
|
21
|
+
throw new Error('flexLayout: `sizes` must contain only numbers');
|
|
22
|
+
}
|
|
23
|
+
let lastSize = 0;
|
|
24
|
+
// filter() for filtering out empty strings
|
|
25
|
+
return items.filter(Boolean).map((item, i) => {
|
|
26
|
+
const size = sizes[i] || 0;
|
|
27
|
+
let transform = `translate(${lastSize}, 0)`;
|
|
28
|
+
if (direction === 'column') {
|
|
29
|
+
transform = `translate(0, ${lastSize})`;
|
|
30
|
+
}
|
|
31
|
+
lastSize += size + gap;
|
|
32
|
+
return `<g transform="${transform}">${item}</g>`;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Creates a node to display the primary programming language of the repository/gist.
|
|
37
|
+
*
|
|
38
|
+
* @param langName Language name.
|
|
39
|
+
* @param langColor Language color.
|
|
40
|
+
* @returns Language display SVG object.
|
|
41
|
+
*/
|
|
42
|
+
const createLanguageNode = (langName, langColor) => {
|
|
43
|
+
if (!isPrefixedHexColor(langColor)) {
|
|
44
|
+
throw new Error(`Invalid language color: "${langColor}"`);
|
|
45
|
+
}
|
|
46
|
+
return `
|
|
47
|
+
<g data-testid="primary-lang">
|
|
48
|
+
<circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
|
|
49
|
+
<text data-testid="lang-name" class="gray" x="15">${encodeHTML(langName)}</text>
|
|
50
|
+
</g>
|
|
51
|
+
`;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Create a node to indicate progress in percentage along a horizontal line.
|
|
55
|
+
*
|
|
56
|
+
* @param params Object that contains the createProgressNode parameters.
|
|
57
|
+
* @param params.x X-axis position.
|
|
58
|
+
* @param params.y Y-axis position.
|
|
59
|
+
* @param params.width Width of progress bar.
|
|
60
|
+
* @param params.color Optional foreground color as an inline fill fallback.
|
|
61
|
+
* When omitted, the color must be set via a `.lang-progress` CSS rule.
|
|
62
|
+
* @param params.progress Progress value.
|
|
63
|
+
* @param params.delay Delay before animation starts.
|
|
64
|
+
* @returns Progress node.
|
|
65
|
+
*/
|
|
66
|
+
const createProgressNode = ({ x, y, width, color, progress, delay, }) => {
|
|
67
|
+
if (color !== undefined && !isPrefixedHexColor(color)) {
|
|
68
|
+
throw new Error(`Invalid progress color: "${color}"`);
|
|
69
|
+
}
|
|
70
|
+
if (!Number.isFinite(width)) {
|
|
71
|
+
throw new Error(`Invalid width: "${width}"`);
|
|
72
|
+
}
|
|
73
|
+
if (!Number.isFinite(x)) {
|
|
74
|
+
throw new Error(`Invalid x: "${x}"`);
|
|
75
|
+
}
|
|
76
|
+
if (!Number.isFinite(y)) {
|
|
77
|
+
throw new Error(`Invalid y: "${y}"`);
|
|
78
|
+
}
|
|
79
|
+
if (!Number.isFinite(delay)) {
|
|
80
|
+
throw new Error(`Invalid delay: "${delay}"`);
|
|
81
|
+
}
|
|
82
|
+
const progressPercentage = clampValue(progress, 2, 100);
|
|
83
|
+
return `
|
|
84
|
+
<svg width="${width}" x="${x}" y="${y}">
|
|
85
|
+
<rect data-testid="progress-background" class="progress-background" rx="5" ry="5" x="0" y="0" width="${width}" height="8"></rect>
|
|
86
|
+
<svg data-testid="lang-progress" width="${progressPercentage}%">
|
|
87
|
+
<rect
|
|
88
|
+
height="8"
|
|
89
|
+
${color === undefined ? '' : `fill="${color}"`}
|
|
90
|
+
rx="5" ry="5" x="0" y="0"
|
|
91
|
+
class="lang-progress"
|
|
92
|
+
style="animation-delay: ${delay}ms;"
|
|
93
|
+
/>
|
|
94
|
+
</svg>
|
|
95
|
+
</svg>
|
|
96
|
+
`;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Renders multi-line text via a `foreignObject` so the browser performs
|
|
100
|
+
* native, font-aware wrapping. Content overflowing `lineCount` lines is
|
|
101
|
+
* clipped (with an ellipsis on the last visible line) by CSS line-clamp.
|
|
102
|
+
*
|
|
103
|
+
* @param props Function properties.
|
|
104
|
+
* @param props.text Text to render (will be HTML-encoded).
|
|
105
|
+
* @param props.x X position of the foreignObject.
|
|
106
|
+
* @param props.y Y position of the foreignObject.
|
|
107
|
+
* @param props.width Width of the wrap box.
|
|
108
|
+
* @param props.height Height of the wrap box.
|
|
109
|
+
* @param props.lineCount Maximum number of lines to display.
|
|
110
|
+
* @param props.className CSS class applied to the inner element.
|
|
111
|
+
* @param props.testId Optional test id for the inner element.
|
|
112
|
+
* @returns foreignObject SVG node.
|
|
113
|
+
*/
|
|
114
|
+
const wrappedTextNode = ({ text, x, y, width, height, lineCount, className, testId, }) => {
|
|
115
|
+
if (!Number.isFinite(x)) {
|
|
116
|
+
throw new Error(`Invalid x: "${x}"`);
|
|
117
|
+
}
|
|
118
|
+
if (!Number.isFinite(y)) {
|
|
119
|
+
throw new Error(`Invalid y: "${y}"`);
|
|
120
|
+
}
|
|
121
|
+
if (!Number.isFinite(width)) {
|
|
122
|
+
throw new Error(`Invalid width: "${width}"`);
|
|
123
|
+
}
|
|
124
|
+
if (!Number.isFinite(height)) {
|
|
125
|
+
throw new Error(`Invalid height: "${height}"`);
|
|
126
|
+
}
|
|
127
|
+
if (!Number.isFinite(lineCount)) {
|
|
128
|
+
throw new Error(`Invalid lineCount: "${lineCount}"`);
|
|
129
|
+
}
|
|
130
|
+
const testIdAttr = testId ? ` data-testid="${encodeHTML(testId)}"` : '';
|
|
131
|
+
return `
|
|
132
|
+
<foreignObject x="${x}" y="${y}" width="${width}" height="${height}">
|
|
133
|
+
<div xmlns="http://www.w3.org/1999/xhtml" class="${encodeHTML(className)}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(text)}</div>
|
|
134
|
+
</foreignObject>
|
|
135
|
+
`;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* CSS rules used to render multi-line text inside a `foreignObject`. Apply this
|
|
139
|
+
* to a CSS class (e.g. `.description`) shared with `wrappedTextNode` so the
|
|
140
|
+
* browser handles wrapping and the line count is taken from the `--lines`
|
|
141
|
+
* custom property set on the element.
|
|
142
|
+
*
|
|
143
|
+
* @param color Text color (CSS `color` property).
|
|
144
|
+
* @returns CSS rules block (without the surrounding selector).
|
|
145
|
+
*/
|
|
146
|
+
const wrappedTextStyles = (color) => {
|
|
147
|
+
if (!isPrefixedHexColor(color)) {
|
|
148
|
+
throw new Error(`Invalid text color: "${color}"`);
|
|
149
|
+
}
|
|
150
|
+
return `
|
|
151
|
+
color: ${color};
|
|
152
|
+
margin: 0;
|
|
153
|
+
line-height: 1.2;
|
|
154
|
+
overflow-wrap: anywhere;
|
|
155
|
+
word-break: break-word;
|
|
156
|
+
display: -webkit-box;
|
|
157
|
+
-webkit-box-orient: vertical;
|
|
158
|
+
-webkit-line-clamp: var(--lines);
|
|
159
|
+
line-clamp: var(--lines);
|
|
160
|
+
overflow: hidden;
|
|
161
|
+
text-overflow: ellipsis;
|
|
162
|
+
padding-bottom: 0.15em;
|
|
163
|
+
`;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Creates an icon with label to display repository/gist stats like forks, stars, etc.
|
|
167
|
+
*
|
|
168
|
+
* The caller must ensure that the passed `icon` is properly sanitized!
|
|
169
|
+
*
|
|
170
|
+
* @param icon The sanitized icon to display.
|
|
171
|
+
* @param label The label to display.
|
|
172
|
+
* @param testid The testid to assign to the label.
|
|
173
|
+
* @param iconSize The size of the icon.
|
|
174
|
+
* @returns Icon with label SVG object.
|
|
175
|
+
*/
|
|
176
|
+
const iconWithLabel = (icon, label, testid, iconSize) => {
|
|
177
|
+
if (typeof label === 'number' && label <= 0) {
|
|
178
|
+
return '';
|
|
179
|
+
}
|
|
180
|
+
if (!Number.isFinite(iconSize)) {
|
|
181
|
+
throw new Error(`Invalid iconSize: "${iconSize}"`);
|
|
182
|
+
}
|
|
183
|
+
const iconSvg = `
|
|
184
|
+
<svg
|
|
185
|
+
class="icon"
|
|
186
|
+
y="-12"
|
|
187
|
+
viewBox="0 0 16 16"
|
|
188
|
+
version="1.1"
|
|
189
|
+
width="${iconSize}"
|
|
190
|
+
height="${iconSize}"
|
|
191
|
+
>
|
|
192
|
+
${icon}
|
|
193
|
+
</svg>
|
|
194
|
+
`;
|
|
195
|
+
const text = `<text data-testid="${encodeHTML(testid)}" class="gray">${encodeHTML(String(label))}</text>`;
|
|
196
|
+
return flexLayout({ items: [iconSvg, text], gap: 20 }).join('');
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Create a labelled stat text item
|
|
200
|
+
* (a label and its value, optionally with an icon and link).
|
|
201
|
+
* Shared by the stats and repo cards.
|
|
202
|
+
*
|
|
203
|
+
* The caller must ensure that the passed `icon` and `link` are properly sanitized!
|
|
204
|
+
*
|
|
205
|
+
* @param params Object that contains the createTextNode parameters.
|
|
206
|
+
* @param params.icon The sanitized icon to display.
|
|
207
|
+
* @param params.label The label to display.
|
|
208
|
+
* @param params.value The value to display.
|
|
209
|
+
* @param params.id The id of the stat.
|
|
210
|
+
* @param params.unitSymbol The unit symbol of the stat.
|
|
211
|
+
* @param params.index The index of the stat.
|
|
212
|
+
* @param params.showIcons Whether to show icons.
|
|
213
|
+
* @param params.shiftValuePos Number of pixels the value has to be shifted to the right.
|
|
214
|
+
* @param params.bold Whether to bold the label.
|
|
215
|
+
* @param params.numberFormat The format of numbers on card.
|
|
216
|
+
* @param params.numberPrecision The precision of numbers on card.
|
|
217
|
+
* @param params.link Sanitized url to link to.
|
|
218
|
+
* @param params.labelXOffset horizontal offset for label.
|
|
219
|
+
* @returns The stats card text item SVG object.
|
|
220
|
+
*/
|
|
221
|
+
const createTextNode = ({ icon, label, value, id, unitSymbol, index, showIcons, shiftValuePos, bold, numberFormat, numberPrecision, link, labelXOffset = 25, }) => {
|
|
222
|
+
if (!Number.isFinite(labelXOffset)) {
|
|
223
|
+
throw new Error(`Invalid labelXOffset: "${labelXOffset}"`);
|
|
224
|
+
}
|
|
225
|
+
if (!Number.isFinite(shiftValuePos)) {
|
|
226
|
+
throw new Error(`Invalid shiftValuePos: "${shiftValuePos}"`);
|
|
227
|
+
}
|
|
228
|
+
if (!Number.isFinite(index)) {
|
|
229
|
+
throw new Error(`Invalid index: "${index}"`);
|
|
230
|
+
}
|
|
231
|
+
const precision = numberPrecision !== undefined && Number.isFinite(numberPrecision)
|
|
232
|
+
? clampValue(numberPrecision, 0, 2)
|
|
233
|
+
: undefined;
|
|
234
|
+
// `long` format and the percentage stat keep the raw value; everything else
|
|
235
|
+
// (always a number here) is abbreviated via kFormatter.
|
|
236
|
+
const rawValue = numberFormat.toLowerCase() === 'long' || id === 'prs_merged_percentage';
|
|
237
|
+
const kValue = rawValue || typeof value !== 'number' ? value : kFormatter(value, precision);
|
|
238
|
+
const staggerDelay = (index + 3) * 150;
|
|
239
|
+
const boldClass = bold ? ' bold' : 'not_bold';
|
|
240
|
+
const valueX = (showIcons ? 140 : 120) + (bold ? 5 : 0) + shiftValuePos;
|
|
241
|
+
const unit = unitSymbol ? ` ${unitSymbol}` : '';
|
|
242
|
+
const labelOffset = showIcons ? `x="${labelXOffset}"` : '';
|
|
243
|
+
const iconSvg = showIcons
|
|
244
|
+
? `
|
|
245
|
+
<svg data-testid="icon" class="icon" viewBox="0 0 16 16" version="1.1" width="16" height="16">
|
|
246
|
+
${icon}
|
|
247
|
+
</svg>
|
|
248
|
+
`
|
|
249
|
+
: '';
|
|
250
|
+
const content = `
|
|
251
|
+
${iconSvg}
|
|
252
|
+
<text class="stat ${boldClass}" ${labelOffset} y="12.5">${encodeHTML(label)}:</text>
|
|
253
|
+
<text
|
|
254
|
+
class="stat ${boldClass}"
|
|
255
|
+
x="${valueX}"
|
|
256
|
+
y="12.5"
|
|
257
|
+
data-testid="${id}"
|
|
258
|
+
>${kValue}${unit}</text>`;
|
|
259
|
+
const inner = link ? `<a href="${link}">${content}</a>` : content;
|
|
260
|
+
return `
|
|
261
|
+
<g class="stagger" style="animation-delay: ${staggerDelay}ms" transform="translate(25, 0)">${inner}
|
|
262
|
+
</g>
|
|
263
|
+
`;
|
|
264
|
+
};
|
|
265
|
+
// Script parameters.
|
|
266
|
+
const ERROR_CARD_LENGTH = 576.5;
|
|
267
|
+
const UPSTREAM_API_ERRORS = [TRY_AGAIN_LATER, SECONDARY_ERROR_MESSAGES.rate_limited];
|
|
268
|
+
/**
|
|
269
|
+
* Renders error message on the card.
|
|
270
|
+
*
|
|
271
|
+
* @param args Function arguments.
|
|
272
|
+
* @param args.message Main error message.
|
|
273
|
+
* @param args.secondaryMessage The secondary error message.
|
|
274
|
+
* @param args.renderOptions Render options.
|
|
275
|
+
* @param args.renderOptions.title_color Card title color.
|
|
276
|
+
* @param args.renderOptions.text_color Card text color.
|
|
277
|
+
* @param args.renderOptions.bg_color Card background color.
|
|
278
|
+
* @param args.renderOptions.border_color Card border color.
|
|
279
|
+
* @param args.renderOptions.theme Card theme.
|
|
280
|
+
* @param args.renderOptions.show_repo_link Whether to show repo link or not.
|
|
281
|
+
* @returns The SVG markup.
|
|
282
|
+
*/
|
|
283
|
+
const renderError = ({ message, secondaryMessage = '', renderOptions = {}, }) => {
|
|
284
|
+
const { title_color, text_color, bg_color, border_color, theme = 'default', show_repo_link = true, } = renderOptions;
|
|
285
|
+
const { titleColor, textColor, bgColor, borderColor } = getCardColors({
|
|
286
|
+
title_color,
|
|
287
|
+
text_color,
|
|
288
|
+
icon_color: '',
|
|
289
|
+
bg_color,
|
|
290
|
+
border_color,
|
|
291
|
+
ring_color: '',
|
|
292
|
+
theme,
|
|
293
|
+
});
|
|
294
|
+
return `
|
|
295
|
+
<svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${String(bgColor)}" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="titleId descId">
|
|
296
|
+
<title id="titleId">${encodeHTML(message)}</title>
|
|
297
|
+
<desc id="descId">${encodeHTML(secondaryMessage)}</desc>
|
|
298
|
+
<style>
|
|
299
|
+
.text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
|
|
300
|
+
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
|
|
301
|
+
.gray { fill: #858585 }
|
|
302
|
+
</style>
|
|
303
|
+
<rect x="0.5" y="0.5" width="${ERROR_CARD_LENGTH - 1}" height="99%" rx="4.5" fill="${String(bgColor)}" stroke="${borderColor}"/>
|
|
304
|
+
<text x="25" y="45" class="text">Something went wrong!${UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
|
|
305
|
+
? ''
|
|
306
|
+
: ' file an issue at https://tinyurl.com/github-stats'}</text>
|
|
307
|
+
<text data-testid="message" x="25" y="55" class="text small">
|
|
308
|
+
<tspan x="25" dy="18">${encodeHTML(message)}</tspan>
|
|
309
|
+
<tspan x="25" dy="18" class="gray">${encodeHTML(secondaryMessage)}</tspan>
|
|
310
|
+
</text>
|
|
311
|
+
</svg>
|
|
312
|
+
`;
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* Retrieve text length based on Segoe UI font.
|
|
316
|
+
*
|
|
317
|
+
* @see https://stackoverflow.com/a/48172630/10629172
|
|
318
|
+
* @param str String to measure.
|
|
319
|
+
* @param fontSize Font size.
|
|
320
|
+
* @returns Text length.
|
|
321
|
+
*/
|
|
322
|
+
const measureText = (str, fontSize = 10) => {
|
|
323
|
+
// prettier-ignore
|
|
324
|
+
const widths = [
|
|
325
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
326
|
+
0, 0, 0, 0, 0, 0, 0, 0.2733333110809326, 0.28499999046325686,
|
|
327
|
+
0.39166667461395266, 0.5900000095367431, 0.5383333206176758,
|
|
328
|
+
0.8183333396911621, 0.8, 0.22999999523162842, 0.30166666507720946,
|
|
329
|
+
0.30166666507720946, 0.41666665077209475, 0.6833333492279052,
|
|
330
|
+
0.21666667461395264, 0.4, 0.21666667461395264, 0.3900000095367432,
|
|
331
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
332
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
333
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
334
|
+
0.5383333206176758, 0.21666667461395264, 0.21666667461395264,
|
|
335
|
+
0.6833333492279052, 0.6833333492279052, 0.6833333492279052,
|
|
336
|
+
0.4483333110809326, 0.9550000190734863, 0.6449999809265137,
|
|
337
|
+
0.5733333110809327, 0.6183333396911621, 0.7016666889190674,
|
|
338
|
+
0.5066666603088379, 0.48833332061767576, 0.6866666793823242,
|
|
339
|
+
0.7099999904632568, 0.26666667461395266, 0.35666666030883787,
|
|
340
|
+
0.5800000190734863, 0.4699999809265137, 0.8983333587646485,
|
|
341
|
+
0.7483333110809326, 0.753333330154419, 0.5599999904632569,
|
|
342
|
+
0.753333330154419, 0.5983333110809326, 0.5316666603088379,
|
|
343
|
+
0.5233333110809326, 0.6866666793823242, 0.621666669845581,
|
|
344
|
+
0.9333333015441895, 0.5900000095367431, 0.553333330154419,
|
|
345
|
+
0.5699999809265137, 0.30166666507720946, 0.37833333015441895,
|
|
346
|
+
0.30166666507720946, 0.6833333492279052, 0.41500000953674315,
|
|
347
|
+
0.26833333969116213, 0.5083333492279053, 0.5883333206176757,
|
|
348
|
+
0.4616666793823242, 0.5883333206176757, 0.5233333110809326,
|
|
349
|
+
0.3133333444595337, 0.5883333206176757, 0.5666666507720948,
|
|
350
|
+
0.24166667461395264, 0.24166667461395264, 0.49666666984558105,
|
|
351
|
+
0.24166667461395264, 0.8616666793823242, 0.5666666507720948,
|
|
352
|
+
0.5866666793823242, 0.5883333206176757, 0.5883333206176757,
|
|
353
|
+
0.3483333349227905, 0.425, 0.33833334445953367, 0.5666666507720948,
|
|
354
|
+
0.4783333301544189, 0.7233333110809326, 0.45833334922790525,
|
|
355
|
+
0.4833333492279053, 0.45166668891906736, 0.30166666507720946,
|
|
356
|
+
0.24000000953674316, 0.30166666507720946, 0.6833333492279052,
|
|
357
|
+
];
|
|
358
|
+
const avg = 0.5131403493881227;
|
|
359
|
+
// CJK character range: U+3000–U+9FFF (CJK Symbols/Punctuation, Hiragana,
|
|
360
|
+
// Katakana, CJK Unified Ideographs incl. Extension A) plus U+FF00–U+FFEF
|
|
361
|
+
// (Halfwidth/Fullwidth Forms — fullwidth ASCII, fullwidth punctuation).
|
|
362
|
+
const cjkRange = /[\u3000-\u9FFF\uFF00-\uFFEF]/;
|
|
363
|
+
return (str
|
|
364
|
+
.split('')
|
|
365
|
+
.map((c) => {
|
|
366
|
+
if (cjkRange.test(c) || c === '\u3000') {
|
|
367
|
+
// CJK glyphs and U+3000 IDEOGRAPHIC SPACE are full-width by default;
|
|
368
|
+
return 1;
|
|
369
|
+
}
|
|
370
|
+
if (c.charCodeAt(0) < widths.length) {
|
|
371
|
+
return widths[c.charCodeAt(0)] ?? avg;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
return avg;
|
|
375
|
+
}
|
|
376
|
+
})
|
|
377
|
+
.reduce((cur, acc) => acc + cur) * fontSize);
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Split text into the lines it would wrap to when laid out greedily at the
|
|
381
|
+
* given font size inside a box of width `maxWidth`. Uses `measureText` so the
|
|
382
|
+
* estimate reflects actual font metrics rather than a fixed character count.
|
|
383
|
+
* The browser still does the real wrap inside the foreignObject; this is only
|
|
384
|
+
* used to size the SVG.
|
|
385
|
+
*
|
|
386
|
+
* @param text Text to split.
|
|
387
|
+
* @param fontSize Font size in px (matches `measureText`).
|
|
388
|
+
* @param maxWidth Available wrap width in px.
|
|
389
|
+
* @returns Estimated wrapped lines.
|
|
390
|
+
*/
|
|
391
|
+
const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
392
|
+
if (!text) {
|
|
393
|
+
return [];
|
|
394
|
+
}
|
|
395
|
+
// Tokenize the text into atoms representing line-break opportunities:
|
|
396
|
+
// - ASCII whitespace runs (collapsed/dropped at line edges per CSS rules);
|
|
397
|
+
// - non-ASCII whitespaces
|
|
398
|
+
// - a single CJK codepoint (browsers break between any two CJK chars,
|
|
399
|
+
// punctuation or not, so each one is its own atom and ~1 em wide);
|
|
400
|
+
// - a run of non-whitespace non-CJK characters (a "word" that can only
|
|
401
|
+
// break at its boundaries, like Latin script).
|
|
402
|
+
// Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
|
|
403
|
+
// because Korean wraps at word boundaries by default in HTML.
|
|
404
|
+
// ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
|
|
405
|
+
const normalizedText = text.replace(/[\t\n\r ]+/g, ' ');
|
|
406
|
+
const tokens = normalizedText.match(/\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g);
|
|
407
|
+
if (!tokens) {
|
|
408
|
+
return [];
|
|
409
|
+
}
|
|
410
|
+
const takeFittingSegment = (token, availableWidth) => {
|
|
411
|
+
const characters = token.split('');
|
|
412
|
+
let segment = '';
|
|
413
|
+
let width = 0;
|
|
414
|
+
for (const character of characters) {
|
|
415
|
+
const characterWidth = measureText(character, fontSize);
|
|
416
|
+
if (segment && width + characterWidth > availableWidth) {
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
segment += character;
|
|
420
|
+
width += characterWidth;
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
segment,
|
|
424
|
+
width,
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
const lines = [''];
|
|
428
|
+
let currentWidth = 0;
|
|
429
|
+
for (const token of tokens) {
|
|
430
|
+
if (token === ' ') {
|
|
431
|
+
// Whitespace at the start of a line is dropped by browsers.
|
|
432
|
+
if (currentWidth === 0) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? '') + token;
|
|
436
|
+
currentWidth += measureText(token, fontSize);
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
let remaining = token;
|
|
440
|
+
while (remaining) {
|
|
441
|
+
const w = measureText(remaining, fontSize);
|
|
442
|
+
if (currentWidth + w <= maxWidth) {
|
|
443
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? '') + remaining;
|
|
444
|
+
currentWidth += w;
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
if (currentWidth > 0) {
|
|
448
|
+
lines.push('');
|
|
449
|
+
currentWidth = 0;
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
// An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
|
|
453
|
+
const { segment, width } = takeFittingSegment(remaining, maxWidth);
|
|
454
|
+
lines[lines.length - 1] = (lines[lines.length - 1] ?? '') + segment;
|
|
455
|
+
currentWidth = width;
|
|
456
|
+
remaining = remaining.slice(segment.length);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return lines.map((line) => line.replace(/[\t\n\r ]+$/, ''));
|
|
460
|
+
};
|
|
461
|
+
/**
|
|
462
|
+
* Estimate how many lines a string will wrap to when laid out greedily at the
|
|
463
|
+
* given font size inside a box of width `maxWidth`, capped at `maxLines`.
|
|
464
|
+
*
|
|
465
|
+
* @param text Text to estimate.
|
|
466
|
+
* @param fontSize Font size in px (matches `measureText`).
|
|
467
|
+
* @param maxWidth Available wrap width in px.
|
|
468
|
+
* @param maxLines Cap on the returned line count.
|
|
469
|
+
* @returns Estimated line count, at least 1, at most `maxLines`.
|
|
470
|
+
*/
|
|
471
|
+
const countWrappedLines = (text, fontSize, maxWidth, maxLines) => {
|
|
472
|
+
return Math.min(Math.max(1, splitWrappedText(text, fontSize, maxWidth).length), maxLines);
|
|
473
|
+
};
|
|
474
|
+
export { renderError, createLanguageNode, createProgressNode, createTextNode, iconWithLabel, flexLayout, measureText, splitWrappedText, countWrappedLines, wrappedTextNode, wrappedTextStyles, };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CardConfig } from './config.js';
|
|
2
|
+
import type { FetcherContext, HttpResponse } from './http.js';
|
|
3
|
+
/**
|
|
4
|
+
* Error-detection fields the retryer inspects to detect rate-limiting and credential failures.
|
|
5
|
+
* Every fetcher's payload is intersected with
|
|
6
|
+
* this, so the retryer can read `errors`/`message` regardless of the payload's own shape.
|
|
7
|
+
*/
|
|
8
|
+
interface ResponseErrors {
|
|
9
|
+
errors?: Array<{
|
|
10
|
+
type?: string;
|
|
11
|
+
message?: string;
|
|
12
|
+
}>;
|
|
13
|
+
message?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A fetcher's response. `TData` is the shape of `response.data`,
|
|
17
|
+
* which is intersected with {@link ResponseErrors} so the retryer can inspect
|
|
18
|
+
* `errors`/`message`.
|
|
19
|
+
* Defaults to `unknown` (error fields only) for callers that don't care about the payload.
|
|
20
|
+
*/
|
|
21
|
+
type FetcherResponse<TData = unknown> = HttpResponse<TData & ResponseErrors>;
|
|
22
|
+
type FetcherFunction<TData = unknown, TVariables = Record<string, unknown>> = (variables: TVariables, token: string, context: FetcherContext) => Promise<FetcherResponse<TData>>;
|
|
23
|
+
/**
|
|
24
|
+
* Try to execute the fetcher function until it succeeds or the max number of retries is reached.
|
|
25
|
+
*
|
|
26
|
+
* @template TData Shape of `response.data` returned by the fetcher.
|
|
27
|
+
* @template TVariables Variables the fetcher accepts.
|
|
28
|
+
* @param fetcher The fetcher function.
|
|
29
|
+
* @param variables Object with arguments to pass to the fetcher function.
|
|
30
|
+
* @param config Deployment config supplying the PAT pool and the transport.
|
|
31
|
+
* @returns The response from the fetcher function.
|
|
32
|
+
*/
|
|
33
|
+
declare const retryer: <TData = unknown, TVariables = Record<string, unknown>>(fetcher: FetcherFunction<TData, TVariables>, variables: TVariables, config: CardConfig) => Promise<FetcherResponse<TData>>;
|
|
34
|
+
export { retryer };
|
|
35
|
+
export type { FetcherResponse };
|
|
36
|
+
//# sourceMappingURL=retryer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryer.d.ts","sourceRoot":"","sources":["../../src/common/retryer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAG9D;;;;GAIG;AACH,UAAU,cAAc;IACtB,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACpD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAgBD;;;;;GAKG;AACH,KAAK,eAAe,CAAC,KAAK,GAAG,OAAO,IAAI,YAAY,CAAC,KAAK,GAAG,cAAc,CAAC,CAAC;AAE7E,KAAK,eAAe,CAAC,KAAK,GAAG,OAAO,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAC5E,SAAS,EAAE,UAAU,EACrB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,cAAc,KACpB,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;AAErC;;;;;;;;;GASG;AACH,QAAA,MAAM,OAAO,GAAU,KAAK,GAAG,OAAO,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1E,SAAS,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,EAC3C,WAAW,UAAU,EACrB,QAAQ,UAAU,KACjB,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAkDhC,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC;AACnB,YAAY,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { CardError } from './error.js';
|
|
2
|
+
import { logger } from './log.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns a random integer from 0 (inclusive) to `max` (exclusive).
|
|
5
|
+
*
|
|
6
|
+
* The value is generated using `Math.random()` and uniformly distributed
|
|
7
|
+
* across the range.
|
|
8
|
+
*
|
|
9
|
+
* @param max The upper bound (exclusive). Must be a positive number.
|
|
10
|
+
*
|
|
11
|
+
* @returns A random integer `n` such that `0 <= n < max`.
|
|
12
|
+
*/
|
|
13
|
+
function getRandomInt(max) {
|
|
14
|
+
return Math.floor(Math.random() * max);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Try to execute the fetcher function until it succeeds or the max number of retries is reached.
|
|
18
|
+
*
|
|
19
|
+
* @template TData Shape of `response.data` returned by the fetcher.
|
|
20
|
+
* @template TVariables Variables the fetcher accepts.
|
|
21
|
+
* @param fetcher The fetcher function.
|
|
22
|
+
* @param variables Object with arguments to pass to the fetcher function.
|
|
23
|
+
* @param config Deployment config supplying the PAT pool and the transport.
|
|
24
|
+
* @returns The response from the fetcher function.
|
|
25
|
+
*/
|
|
26
|
+
const retryer = async (fetcher, variables, config) => {
|
|
27
|
+
const PATs = config.pats;
|
|
28
|
+
if (!PATs.length) {
|
|
29
|
+
throw new CardError('No GitHub API tokens found', { code: 'no_tokens' });
|
|
30
|
+
}
|
|
31
|
+
const startPAT = getRandomInt(PATs.length);
|
|
32
|
+
for (let retries = 0; retries < PATs.length; retries++) {
|
|
33
|
+
const currentPAT = PATs[(startPAT + retries) % PATs.length];
|
|
34
|
+
if (!currentPAT) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
// a non-2xx comes back as a response, so only a transport failure throws — and that is fatal
|
|
38
|
+
const response = await fetcher(variables, currentPAT.value, {
|
|
39
|
+
fetch: config.fetch,
|
|
40
|
+
retries,
|
|
41
|
+
});
|
|
42
|
+
// react on both type and message-based rate-limit signals.
|
|
43
|
+
// https://github.com/anuraghazra/github-readme-stats/issues/4425
|
|
44
|
+
const errors = response.data.errors;
|
|
45
|
+
const errorType = errors?.[0]?.type;
|
|
46
|
+
const errorMsg = errors?.[0]?.message ?? '';
|
|
47
|
+
const isRateLimited = (!!errors && errorType === 'RATE_LIMITED') || /rate limit/i.test(errorMsg);
|
|
48
|
+
if (isRateLimited) {
|
|
49
|
+
logger.log(`${currentPAT.name} Failed due to rate limiting`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
// also checking for bad credentials if any tokens gets invalidated
|
|
53
|
+
const message = response.data.message;
|
|
54
|
+
const isBadCredential = message === 'Bad credentials';
|
|
55
|
+
const isAccountSuspended = message === 'Sorry. Your account was suspended.';
|
|
56
|
+
if (isBadCredential || isAccountSuspended) {
|
|
57
|
+
logger.log(`${currentPAT.name} Failed due to bad credentials`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// anything else — including an HTTP error — is the caller's to interpret
|
|
61
|
+
return response;
|
|
62
|
+
}
|
|
63
|
+
throw new CardError('Downtime due to GitHub API rate limiting', {
|
|
64
|
+
code: 'rate_limited',
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
export { retryer };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CardConfig } from '../common/config.js';
|
|
2
|
+
import type { GistData } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Fetch GitHub gist information by given username and ID.
|
|
5
|
+
*
|
|
6
|
+
* @param props Fetcher props.
|
|
7
|
+
* @param props.id GitHub gist ID.
|
|
8
|
+
* @param config Deployment config supplying the PAT pool.
|
|
9
|
+
* @returns Gist data.
|
|
10
|
+
*/
|
|
11
|
+
declare const fetchGist: ({ id }: {
|
|
12
|
+
id: string | undefined;
|
|
13
|
+
}, config: CardConfig) => Promise<GistData>;
|
|
14
|
+
export { fetchGist };
|
|
15
|
+
//# sourceMappingURL=gist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gist.d.ts","sourceRoot":"","sources":["../../src/fetchers/gist.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAOtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AA+B3C;;;;;;;GAOG;AACH,QAAA,MAAM,SAAS,GACb,QAAQ;IAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,EAClC,QAAQ,UAAU,KACjB,OAAO,CAAC,QAAQ,CA8BlB,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { CardError, GIST_NOT_FOUND } from '../common/error.js';
|
|
2
|
+
import { createGraphQLFetcher } from '../common/http.js';
|
|
3
|
+
import { retryer } from '../common/retryer.js';
|
|
4
|
+
import { GistInfoDocument } from '../graphql/generated/gist.js';
|
|
5
|
+
const fetcher = createGraphQLFetcher(GistInfoDocument, 'token');
|
|
6
|
+
/**
|
|
7
|
+
* This function calculates the primary language of a gist by files size.
|
|
8
|
+
*
|
|
9
|
+
* @param files Files.
|
|
10
|
+
* @returns Primary language, or `null` when no file has a language.
|
|
11
|
+
*/
|
|
12
|
+
const calculatePrimaryLanguage = (files) => {
|
|
13
|
+
const languages = {};
|
|
14
|
+
for (const file of files) {
|
|
15
|
+
if (file.language) {
|
|
16
|
+
languages[file.language.name] = (languages[file.language.name] ?? 0) + (file.size ?? 0);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
let primaryLanguage = null;
|
|
20
|
+
let maxSize = -1;
|
|
21
|
+
for (const [language, size] of Object.entries(languages)) {
|
|
22
|
+
if (size > maxSize) {
|
|
23
|
+
maxSize = size;
|
|
24
|
+
primaryLanguage = language;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return primaryLanguage;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Fetch GitHub gist information by given username and ID.
|
|
31
|
+
*
|
|
32
|
+
* @param props Fetcher props.
|
|
33
|
+
* @param props.id GitHub gist ID.
|
|
34
|
+
* @param config Deployment config supplying the PAT pool.
|
|
35
|
+
* @returns Gist data.
|
|
36
|
+
*/
|
|
37
|
+
const fetchGist = async ({ id }, config) => {
|
|
38
|
+
if (!id) {
|
|
39
|
+
throw CardError.missingParam(['id'], '/api/gist?id=GIST_ID');
|
|
40
|
+
}
|
|
41
|
+
const res = await retryer(fetcher, { gistName: id }, config);
|
|
42
|
+
if (res.data.errors) {
|
|
43
|
+
throw new CardError(res.data.errors[0]?.message ?? 'Could not fetch gist.', {
|
|
44
|
+
code: 'upstream',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const gist = res.data.data.viewer.gist;
|
|
48
|
+
if (!gist) {
|
|
49
|
+
throw new CardError('Gist not found', {
|
|
50
|
+
code: 'not_found',
|
|
51
|
+
secondaryMessage: GIST_NOT_FOUND,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const firstFile = gist.files?.[0];
|
|
55
|
+
if (!firstFile?.name) {
|
|
56
|
+
// A gist with nothing to render will not gain a file on a retry.
|
|
57
|
+
throw new CardError('Gist has no files', { code: 'not_found' });
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
name: firstFile.name,
|
|
61
|
+
nameWithOwner: `${gist.owner?.login ?? ''}/${firstFile.name}`,
|
|
62
|
+
description: gist.description,
|
|
63
|
+
language: calculatePrimaryLanguage(gist.files?.filter((file) => !!file) ?? []),
|
|
64
|
+
starsCount: gist.stargazerCount,
|
|
65
|
+
forksCount: gist.forks.totalCount,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export { fetchGist };
|