Skip to content

Commit

Permalink
chore: temp
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciNyan committed Aug 15, 2024
1 parent 977dff5 commit 893bb9f
Show file tree
Hide file tree
Showing 14 changed files with 114,606 additions and 19,101 deletions.
10,311 changes: 10,311 additions & 0 deletions action/dist/731.index.js

Large diffs are not rendered by default.

123,107 changes: 104,029 additions & 19,078 deletions action/dist/index.js

Large diffs are not rendered by default.

Binary file added action/dist/resvgjs.darwin-arm64.node
Binary file not shown.
Binary file added action/dist/resvgjs.linux-x64-gnu.node
Binary file not shown.
Binary file added packages/github-action/api/github-stats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 65 additions & 5 deletions packages/github-action/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,72 @@
import { parseOutputs } from './parseOutputs'
import { parseArray, parseBoolean, parseString } from './utils'
import * as core from '@actions/core'
import * as fs from 'fs'
import * as path from 'path'
import { fetchStats, renderStats } from 'pixel-profile'
;(async () => {
try {
const userName = core.getInput('github_user_name')
// const githubToken = process.env.GITHUB_TOKEN ?? core.getInput('github_token')
const githubToken = process.env.GITHUB_TOKEN ?? core.getInput('github_token')

console.log(':::userName', userName)
console.log(':::process.env.GITHUB_TOKEN', process.env.GITHUB_TOKEN)
console.log(":::core.getInput('github_token')", core.getInput('github_token'))
console.log(':::githubToken', githubToken)

const options = parseOutputs([
// eslint-disable-next-line max-len
'api/github-stats?username=LuciNyan&screen_effect=false&theme=fuji&dithering=true&avatar_border=false&pixelate_avatar=false'
])

for (const option of options) {
if (option === null) continue

const {
background,
color,
exclude_repo,
hide,
include_all_commits,
pixelate_avatar,
screen_effect,
show,
username,
theme,
avatar_border,
dithering,
filename
} = option

const showStats = parseArray(show)
const includeAllCommits = parseBoolean(include_all_commits)

const stats: Parameters<typeof renderStats>[0] = await fetchStats(
typeof username === 'string' ? username : '',
includeAllCommits,
parseArray(exclude_repo),
showStats.includes('prs_merged') || showStats.includes('prs_merged_percentage'),
showStats.includes('discussions_started'),
showStats.includes('discussions_answered'),
githubToken
)

console.log(':::stats', stats)

const options = {
background: parseString(background),
color: parseString(color),
hiddenStatsKeys: hide ? parseArray(hide) : undefined,
includeAllCommits,
pixelateAvatar: parseBoolean(pixelate_avatar),
theme: parseString(theme),
screenEffect: parseBoolean(screen_effect),
avatarBorder: parseBoolean(avatar_border),
dithering: parseBoolean(dithering)
}

const result = await renderStats(stats, options)

console.log(`💾 writing to ${filename}`)
fs.mkdirSync(path.dirname(filename), { recursive: true })
fs.writeFileSync(filename, result)
}
} catch (e: any) {
core.setFailed(`Action failed with "${e.message}"`)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/github-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
"name": "pixel-profile-action",
"version": "1.0.0",
"dependencies": {
"@actions/core": "1.10.1"
"@actions/core": "1.10.1",
"@resvg/resvg-js-linux-x64-gnu": "2.6.0",
"ts-known": "^0.1.3",
"pixel-profile": "workspace:*"
},
"devDependencies": {
"@vercel/ncc": "0.38.0",
"dotenv": "16.3.1"
},
"scripts": {
"build": "ncc build --external canvas --external gifsicle --out ../../action/dist ./index.ts",
"run:build": "INPUT_GITHUB_USER_NAME=platane INPUT_OUTPUTS='../../action/dist/out.svg' node dist/index.js"
"run:build": "INPUT_GITHUB_USER_NAME=platane INPUT_OUTPUTS='../../action/dist/out.svg' node ../../action/dist/index.js",
"rerun": "npm run build && npm run run:build"
}
}
75 changes: 75 additions & 0 deletions packages/github-action/parseOutputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
type Options = {
background?: string
color?: string
exclude_repo?: string
hide?: string
include_all_commits?: string
pixelate_avatar?: string
screen_effect?: string
show?: string
username?: string
theme?: string
avatar_border?: string
dithering?: string
filename: string
}

export function parseOutputs(outputs: string[]): Array<Options | null> {
return outputs.map(parseOutput)
}

function parseOutput(output: string): Options | null {
const m = output.trim().match(/^(.+(\.png|))(\?(.*))$/)

if (!m) return null

const [, _filename, , query] = m
const filename = _filename.endsWith('.png') ? _filename : `${_filename}.png`
const sp = new URLSearchParams(query || '')

const {
background,
color,
exclude_repo,
hide = '',
include_all_commits,
pixelate_avatar,
screen_effect,
show,
username,
theme,
avatar_border,
dithering
} = Object.fromEntries(sp)

console.log(filename, query, {
background,
color,
exclude_repo,
hide,
include_all_commits,
pixelate_avatar,
screen_effect,
show,
username,
theme,
avatar_border,
dithering
})

return {
background,
color,
exclude_repo,
hide,
include_all_commits,
pixelate_avatar,
screen_effect,
show,
username,
theme,
avatar_border,
dithering,
filename
}
}
79 changes: 79 additions & 0 deletions packages/github-action/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { isString, objectOf, optional } from 'ts-known'

export const hasMessage = objectOf({
message: isString
})

export const isPATError = objectOf({
response: optional(
objectOf({
data: optional(
objectOf({
message: optional(isString)
})
)
})
)
})

const ONE_MINUTE = 60
const FIVE_MINUTES = 300
const TEN_MINUTES = 600
const FIFTEEN_MINUTES = 900
const THIRTY_MINUTES = 1800
const TWO_HOURS = 7200
const FOUR_HOURS = 14400
const SIX_HOURS = 21600
const EIGHT_HOURS = 28800
const ONE_DAY = 86400

export const CONSTANTS = {
ONE_MINUTE,
FIVE_MINUTES,
TEN_MINUTES,
FIFTEEN_MINUTES,
THIRTY_MINUTES,
TWO_HOURS,
FOUR_HOURS,
SIX_HOURS,
EIGHT_HOURS,
ONE_DAY,
CARD_CACHE_SECONDS: SIX_HOURS,
ERROR_CACHE_SECONDS: TEN_MINUTES
}

export function dateDiff(d1: number | string | Date, d2: number | string | Date) {
const date1 = new Date(d1)
const date2 = new Date(d2)
const diff = date1.getTime() - date2.getTime()

return Math.round(diff / (1000 * 60))
}

export function parseArray(str: string | string[] | undefined): string[] {
if (Array.isArray(str)) {
return str
}

if (!str) {
return []
}

return str.split(',')
}

export function parseBoolean(value: string | string[] | undefined): boolean | undefined {
if (!isString(value)) {
return undefined
}

if (value.toLowerCase() === 'true') {
return true
} else if (value.toLowerCase() === 'false') {
return false
}
}

export function parseString(value: string | string[] | undefined): string | undefined {
return isString(value) ? value : undefined
}
2 changes: 1 addition & 1 deletion packages/pixel-profile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@resvg/resvg-js": "^2.6.0",
"@resvg/resvg-js": "2.6.0",
"axios": "^1.7.3",
"dotenv": "^16.3.1",
"emoji-name-map": "^1.2.8",
Expand Down
7 changes: 7 additions & 0 deletions packages/pixel-profile/src/cards/PressStart2P-Regular.ts

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions packages/pixel-profile/src/cards/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import { getThemeOptions } from '../theme'
import { getBase64FromPixels, getPixelsFromPngBuffer, getPngBufferFromPixels, kFormatter, Rank } from '../utils'
import { getPngBufferFromURL } from '../utils/converter'
import { filterNotEmpty } from '../utils/filter'
import { fontBuffer } from './PressStart2P-Regular'
import { Resvg } from '@resvg/resvg-js'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import satori from 'satori'

export type Stats = {
Expand Down Expand Up @@ -47,6 +46,8 @@ export async function renderStats(stats: Stats, options: Options = {}): Promise<
const { name, username, totalStars, totalCommits, totalIssues, totalPRs, avatarUrl, contributedTo, rank } = stats
let modifiedAvatarUrl = avatarUrl

console.log(':::renderStats', stats)

const {
background,
color,
Expand All @@ -70,12 +71,9 @@ export async function renderStats(stats: Stats, options: Options = {}): Promise<
const width = baseCardSize.CARD_WIDTH
const height = baseCardSize.CARD_HEIGHT

const fontPath = join(process.cwd(), 'packages', 'pixel-profile', 'fonts', 'PressStart2P-Regular.ttf')

const [fontData, avatar] = await Promise.all([
readFile(fontPath),
makeAvatar(modifiedAvatarUrl, pixelateAvatar, applyAvatarBorder)
])
console.log(':::before makeAvatar')
const avatar = await makeAvatar(modifiedAvatarUrl, pixelateAvatar, applyAvatarBorder)
console.log(':::after makeAvatar')

const _stats = {
name,
Expand Down Expand Up @@ -107,7 +105,7 @@ export async function renderStats(stats: Stats, options: Options = {}): Promise<
fonts: [
{
name: 'PressStart2P',
data: fontData,
data: fontBuffer,
weight: 400,
style: 'normal'
}
Expand All @@ -128,7 +126,7 @@ export async function renderStats(stats: Stats, options: Options = {}): Promise<
fonts: [
{
name: 'PressStart2P',
data: fontData,
data: fontBuffer,
weight: 400,
style: 'normal'
}
Expand Down
16 changes: 14 additions & 2 deletions packages/pixel-profile/src/fetchers/stats-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const fetcher = (variables: Record<PropertyKey, unknown>, token: string): Promis
* the 'FETCH_MULTI_PAGE_STARS' environment variable is set to true.
*/
type Variables = {
token?: string
username: string
includeMergedPullRequests: boolean
includeDiscussions: boolean
Expand All @@ -103,6 +104,7 @@ type Variables = {
}

const statsFetcher = async ({
token,
username,
includeMergedPullRequests,
includeDiscussions,
Expand All @@ -122,7 +124,15 @@ const statsFetcher = async ({
includeDiscussionsAnswers,
contributionFrom
}
const res = await retryer(fetcher, variables)

let res: any

if (token) {
res = await fetcher(variables, token)
} else {
res = await retryer(fetcher, variables)
}

if (res.data.errors) {
return res
}
Expand Down Expand Up @@ -208,7 +218,8 @@ export async function fetchStats(
exclude_repo: string[] = [],
include_merged_pull_requests = false,
include_discussions = false,
include_discussions_answers = false
include_discussions_answers = false,
token = ''
): Promise<StatsData> {
if (!username) {
throw new Error('needs a username')
Expand All @@ -233,6 +244,7 @@ export async function fetchStats(
}

const res = await statsFetcher({
token,
username,
includeMergedPullRequests: include_merged_pull_requests,
includeDiscussions: include_discussions,
Expand Down
Loading

0 comments on commit 893bb9f

Please sign in to comment.