Skip to content

Commit 2ba2fe6

Browse files
authored
Add local SVG generator script and GitHub Action for trophy SVGs (#412)
* Wrote a simple render_svg.ts file that generates the svg given an github username, theme and out path. * render_svg.ts generates output dir if not exsists. * Added Section to README.md that explains how to use the render_svg.ts script. * Added an github action that utilizes the render_svg.ts script. * Added Usage of github action to README.md * Added simple CI Check if action runs. * Ran: deno fmt
1 parent 97e9fad commit 2ba2fe6

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Test GitHub Profile Trophy Action
2+
on: [push, pull_request, workflow_dispatch]
3+
4+
jobs:
5+
test-trophy:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout this repo
9+
uses: actions/checkout@v4
10+
- name: Test Action
11+
id: trophy
12+
uses: ./.
13+
with:
14+
username: ${{ github.repository_owner }}
15+
output_path: ./trophy.svg
16+
token: ${{ secrets.GITHUB_TOKEN }}
17+
18+
- name: Verify SVG generated
19+
run: |
20+
if [ -f trophy.svg ]; then
21+
echo "SVG exsists ($(wc -c < trophy.svg) Bytes)"
22+
file trophy.svg
23+
else
24+
echo "SVG failed to generate"
25+
exit 1
26+
fi

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,34 @@ https://github-profile-trophy.vercel.app/?username=ryo-ma&no-frame=true
566566
<img width="936" src="https://github.com/user-attachments/assets/54de15a3-d907-4a50-8117-170aae74d1cd">
567567
</p>
568568

569+
## Generate an svg file localy
570+
571+
Using the render_svg.ts script you can generate your trophys as an svg file
572+
given your username, (Enviroment Vars: See [env-example](env-example)).
573+
574+
Usage:
575+
576+
```bash
577+
deno run --allow-net --allow-env --allow-read --allow-write ./render_svg.ts USERNAME OUTPUT_DIR THEME
578+
```
579+
580+
## Generate an svg inside Github CI (Workflow)
581+
582+
Using the provided github action you can easly generate the trophy inside an
583+
github workflow. This eliminates the needs of an online service running but you
584+
have to manualy update rerun the action to update the file.
585+
586+
Usage:
587+
588+
```yaml
589+
- name: Generate trophy
590+
uses: Erik-Donath/github-profile-trophy@feature/generate-svg
591+
with:
592+
username: your-username
593+
output_path: trophy.svg
594+
token: ${{ secrets.GITHUB_TOKEN }}
595+
```
596+
569597
# Contribution Guide
570598
571599
Check [CONTRIBUTING.md](./CONTRIBUTING.md) for more details.

action.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Generate GitHub Profile Trophy SVG
2+
description: Run the local generator script to produce an SVG.
3+
inputs:
4+
username:
5+
description: "GitHub username to generate the trophy for"
6+
required: true
7+
output_path:
8+
description: "Output path to write the SVG"
9+
required: true
10+
default: "trophy.svg"
11+
token:
12+
description: "PAT or token to use for GitHub API"
13+
required: true
14+
runs:
15+
using: "composite"
16+
steps:
17+
- name: Setup Deno
18+
uses: denoland/setup-deno@v1
19+
with:
20+
deno-version: v1.x
21+
22+
- name: Generate trophy
23+
shell: bash
24+
env:
25+
GITHUB_TOKEN1: ${{ inputs.token }}
26+
run: |
27+
deno run --allow-net --allow-env --allow-read --allow-write $GITHUB_ACTION_PATH/render_svg.ts "${{ inputs.username }}" "${{ inputs.output_path }}"

render_svg.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import "https://deno.land/x/dotenv@v0.5.0/load.ts";
2+
3+
const username = Deno.args[0];
4+
const outputPath = Deno.args[1] ?? "./assets/trophy.svg";
5+
const themeName = Deno.args[2] ?? "default";
6+
7+
if (!username) {
8+
console.error(
9+
"Usage: deno run --allow-net --allow-env --allow-read --allow-write ./render_svg.ts USERNAME [OUTPUT_PATH] [THEME]",
10+
);
11+
Deno.exit(1);
12+
}
13+
14+
import { GithubApiService } from "./src/Services/GithubApiService.ts";
15+
import { Card } from "./src/card.ts";
16+
import { COLORS } from "./src/theme.ts";
17+
18+
async function main() {
19+
console.log("Starting trophy render...");
20+
console.log("Username:", username);
21+
console.log("Output path:", outputPath);
22+
console.log("Theme:", themeName);
23+
24+
const svc = new GithubApiService();
25+
26+
const userInfoOrError = await svc.requestUserInfo(username);
27+
28+
if (
29+
!(userInfoOrError && (userInfoOrError as any).totalCommits !== undefined)
30+
) {
31+
console.error(
32+
"Failed to fetch user info. Check token, username and rate limits.",
33+
);
34+
Deno.exit(2);
35+
}
36+
37+
const userInfo = userInfoOrError as any;
38+
39+
const panelSize = 115;
40+
const maxRow = 10;
41+
const maxColumn = -1; // auto
42+
const marginWidth = 10;
43+
const marginHeight = 10;
44+
const noBackground = false;
45+
const noFrame = false;
46+
47+
const card = new Card(
48+
[],
49+
[],
50+
maxColumn,
51+
maxRow,
52+
panelSize,
53+
marginWidth,
54+
marginHeight,
55+
noBackground,
56+
noFrame,
57+
);
58+
const theme = (COLORS as any)[themeName] ?? (COLORS as any).default;
59+
const svg = card.render(userInfo, theme);
60+
61+
try {
62+
const dir = outputPath.replace(/\/[^/]+$/, "");
63+
if (dir) await Deno.mkdir(dir, { recursive: true });
64+
} catch {
65+
console.error("Failed to create directory. No permission?");
66+
Deno.exit(3);
67+
}
68+
69+
await Deno.writeTextFile(outputPath, svg);
70+
console.log(`Wrote ${outputPath}`);
71+
}
72+
73+
await main();

0 commit comments

Comments
 (0)