diff --git a/.github/workflows/test-repository-action.yml b/.github/workflows/test-repository-action.yml new file mode 100644 index 0000000..7fc0a67 --- /dev/null +++ b/.github/workflows/test-repository-action.yml @@ -0,0 +1,26 @@ +name: Test GitHub Profile Trophy Action +on: [push, pull_request, workflow_dispatch] + +jobs: + test-trophy: + runs-on: ubuntu-latest + steps: + - name: Checkout this repo + uses: actions/checkout@v4 + - name: Test Action + id: trophy + uses: ./. + with: + username: ${{ github.repository_owner }} + output_path: ./trophy.svg + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Verify SVG generated + run: | + if [ -f trophy.svg ]; then + echo "SVG exsists ($(wc -c < trophy.svg) Bytes)" + file trophy.svg + else + echo "SVG failed to generate" + exit 1 + fi diff --git a/README.md b/README.md index 4ae558c..142eaf7 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,34 @@ https://github-profile-trophy.vercel.app/?username=ryo-ma&no-frame=true

+## Generate an svg file localy + +Using the render_svg.ts script you can generate your trophys as an svg file +given your username, (Enviroment Vars: See [env-example](env-example)). + +Usage: + +```bash +deno run --allow-net --allow-env --allow-read --allow-write ./render_svg.ts USERNAME OUTPUT_DIR THEME +``` + +## Generate an svg inside Github CI (Workflow) + +Using the provided github action you can easly generate the trophy inside an +github workflow. This eliminates the needs of an online service running but you +have to manualy update rerun the action to update the file. + +Usage: + +```yaml +- name: Generate trophy + uses: Erik-Donath/github-profile-trophy@feature/generate-svg + with: + username: your-username + output_path: trophy.svg + token: ${{ secrets.GITHUB_TOKEN }} +``` + # Contribution Guide Check [CONTRIBUTING.md](./CONTRIBUTING.md) for more details. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..7aecc80 --- /dev/null +++ b/action.yml @@ -0,0 +1,27 @@ +name: Generate GitHub Profile Trophy SVG +description: Run the local generator script to produce an SVG. +inputs: + username: + description: "GitHub username to generate the trophy for" + required: true + output_path: + description: "Output path to write the SVG" + required: true + default: "trophy.svg" + token: + description: "PAT or token to use for GitHub API" + required: true +runs: + using: "composite" + steps: + - name: Setup Deno + uses: denoland/setup-deno@v1 + with: + deno-version: v1.x + + - name: Generate trophy + shell: bash + env: + GITHUB_TOKEN1: ${{ inputs.token }} + run: | + deno run --allow-net --allow-env --allow-read --allow-write $GITHUB_ACTION_PATH/render_svg.ts "${{ inputs.username }}" "${{ inputs.output_path }}" diff --git a/render_svg.ts b/render_svg.ts new file mode 100644 index 0000000..f870eee --- /dev/null +++ b/render_svg.ts @@ -0,0 +1,73 @@ +import "https://deno.land/x/dotenv@v0.5.0/load.ts"; + +const username = Deno.args[0]; +const outputPath = Deno.args[1] ?? "./assets/trophy.svg"; +const themeName = Deno.args[2] ?? "default"; + +if (!username) { + console.error( + "Usage: deno run --allow-net --allow-env --allow-read --allow-write ./render_svg.ts USERNAME [OUTPUT_PATH] [THEME]", + ); + Deno.exit(1); +} + +import { GithubApiService } from "./src/Services/GithubApiService.ts"; +import { Card } from "./src/card.ts"; +import { COLORS } from "./src/theme.ts"; + +async function main() { + console.log("Starting trophy render..."); + console.log("Username:", username); + console.log("Output path:", outputPath); + console.log("Theme:", themeName); + + const svc = new GithubApiService(); + + const userInfoOrError = await svc.requestUserInfo(username); + + if ( + !(userInfoOrError && (userInfoOrError as any).totalCommits !== undefined) + ) { + console.error( + "Failed to fetch user info. Check token, username and rate limits.", + ); + Deno.exit(2); + } + + const userInfo = userInfoOrError as any; + + const panelSize = 115; + const maxRow = 10; + const maxColumn = -1; // auto + const marginWidth = 10; + const marginHeight = 10; + const noBackground = false; + const noFrame = false; + + const card = new Card( + [], + [], + maxColumn, + maxRow, + panelSize, + marginWidth, + marginHeight, + noBackground, + noFrame, + ); + const theme = (COLORS as any)[themeName] ?? (COLORS as any).default; + const svg = card.render(userInfo, theme); + + try { + const dir = outputPath.replace(/\/[^/]+$/, ""); + if (dir) await Deno.mkdir(dir, { recursive: true }); + } catch { + console.error("Failed to create directory. No permission?"); + Deno.exit(3); + } + + await Deno.writeTextFile(outputPath, svg); + console.log(`Wrote ${outputPath}`); +} + +await main();