Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(workflow): setup sync workflow #2

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/sync-trustwallet-assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Sync

on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *'

concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: write
pull-requests: write

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
node-version-file: '.nvmrc'

- run: corepack enable pnpm

- run: pnpm install --frozen-lockfile

- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
path: workspace/sync-trustwallet-assets/repo
repository: trustwallet/assets
ref: master

- run: pnpm turbo run sync
working-directory: workspace/sync-trustwallet-assets

- uses: peter-evans/create-pull-request@153407881ec5c347639a548ade7d8ad1d6740e38 # v5.0.2
with:
commit-message: 'sync(assets): trustwallet/assets'
title: 'sync(assets): trustwallet/assets'
committer: Frontmatter Bot <${{ github.actor }}@users.noreply.github.com>
author: Frontmatter Bot <${{ github.actor }}@users.noreply.github.com>
body: |
#### What this PR does / why we need it:

Sync latest changes from `trustwallet/assets` repository.

branch: sync/trustwallet/assets
2 changes: 2 additions & 0 deletions .idea/frontmatter.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"repository": {
"url": "git+https://github.com/levaintech/frontmatter"
},
"type": "module",
"main": "./dist/index.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions workspace/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module.exports = [
{
ignores: ['**/dist/*'],
},
require('@eslint/js').configs.recommended,
{
files: ['**/*.ts', '**/*.tsx'],
plugins: {
'@typescript-eslint': require('@typescript-eslint/eslint-plugin'),
},
rules: require('@typescript-eslint/eslint-plugin').configs.recommended.rules,
languageOptions: {
parser: require('@typescript-eslint/parser'),
},
Expand Down
1 change: 1 addition & 0 deletions workspace/sync-trustwallet-assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo/
26 changes: 26 additions & 0 deletions workspace/sync-trustwallet-assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@workspace/sync-trustwallet-assets",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "tsc --project tsconfig.build.json",
"clean": "rm -rf dist",
"lint": "eslint .",
"sync": "node dist/index.js"
},
"lint-staged": {
"*": [
"prettier --write --ignore-unknown"
],
"*.{ts,tsx}": [
"eslint --fix",
"prettier --write"
]
},
"dependencies": {
"yaml": "^2.3.4"
},
"devDependencies": {
"@workspace/tsconfig": "workspace:*"
}
}
17 changes: 17 additions & 0 deletions workspace/sync-trustwallet-assets/src/gitlog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { exec } from 'node:child_process';

export function getAuthorName(filepath: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(`git log -1 --pretty=format:"%ae" ${filepath}`, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
reject(stderr);
return;
}
resolve(stdout);
});
});
}
12 changes: 12 additions & 0 deletions workspace/sync-trustwallet-assets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { join } from 'node:path';
import process from 'node:process';

import { Eip155Sync } from './sync';

export async function sync(): Promise<void> {
const cwd = process.cwd();

await new Eip155Sync(join(cwd, 'repo/blockchains/ethereum/assets'), join(cwd, '../../packages/eip155/1')).sync();
}

void sync();
110 changes: 110 additions & 0 deletions workspace/sync-trustwallet-assets/src/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { copyFile, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';

import { stringify } from 'yaml';

import { getAuthorName } from './gitlog';

interface Info {
name: string;
website: string;
description: string;
explorer: string;
type: string;
symbol: string;
decimals: number;
status: string;
id: string;
tags: string[];
links: {
name: string;
url: string;
}[];
}

export abstract class Sync {
constructor(
protected readonly from: string,
protected readonly to: string,
) {}

async sync() {
const dirs = await readdir(this.from);
// TODO(fuxingloh): Limit to first 100 files for testing
for (const dir of dirs.slice(0, 100)) {
const info = JSON.parse(
await readFile(`${this.from}/${dir}/info.json`, {
encoding: 'utf-8',
}),
) as Partial<Info>;

if (!(await this.shouldWrite(dir, info))) continue;
await this.write(dir, info);
}
}

async shouldWrite(dir: string, info: Partial<Info>): Promise<boolean> {
// Make sure logo.png and info.json exists
const fromLogoStats = await stat(`${this.from}/${dir}/logo.png`);
const fromInfoStats = await stat(`${this.from}/${dir}/info.json`);
if (!fromLogoStats.isFile() || !fromInfoStats.isFile()) return false;

// If README.md and logo.png do not exist on the other side, write it
const namespace = this.getNamespace(info);
const toLogoStats = await stat(`${this.to}/${namespace}/logo.png`);
const toReadmeStats = await stat(`${this.to}/${namespace}/README.md`);
if (!toLogoStats.isFile() || !toReadmeStats.isFile()) return true;

// Otherwise, allow overwriting if the author is Frontmatter Bot
const name = await getAuthorName(`${this.to}/${namespace}/README.md`);
return name === 'Frontmatter Bot';
}

abstract write(dir: string, info: Partial<Info>): Promise<void>;

abstract getNamespace(info: Partial<Info>): string;

createLinks(info: Partial<Info>): Info['links'] {
const links: Info['links'] = [];
if (info.website) links.push({ name: 'website', url: info.website });
if (info.explorer) links.push({ name: 'explorer', url: info.explorer });

if (info.links) {
for (const link of info.links) {
if (link.name === 'website' || link.name === 'explorer') continue;
if (!link.url?.startsWith('https://')) continue;
if (!link.name) continue;
links.push(link);
}
}
return links;
}
}

export class Eip155Sync extends Sync {
async write(dir: string, info: Partial<Info>): Promise<void> {
const namespace = this.getNamespace(info);
await mkdir(`${this.to}/${namespace}`, { recursive: true });
await copyFile(`${this.from}/${dir}/logo.png`, `${this.to}/${namespace}/logo.png`);
await writeFile(
`${this.to}/${namespace}/README.md`,
[
`---`,
stringify({
symbol: info.symbol,
decimals: info.decimals,
tags: info.tags,
links: this.createLinks(info),
}),
`---`,
'',
`# ${info.name}`,
'',
info.description,
].join('\n'),
);
}

getNamespace(info: Partial<Info>): string {
return info.type!.toLowerCase() + '/' + info.id;
}
}
8 changes: 8 additions & 0 deletions workspace/sync-trustwallet-assets/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["**/*.unit.ts"]
}
3 changes: 3 additions & 0 deletions workspace/sync-trustwallet-assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@workspace/tsconfig"
}
9 changes: 9 additions & 0 deletions workspace/sync-trustwallet-assets/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://turborepo.org/schema.json",
"extends": ["//"],
"pipeline": {
"sync": {
"dependsOn": ["build"]
}
}
}
2 changes: 1 addition & 1 deletion workspace/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"compilerOptions": {
"lib": ["ES2021"],
"target": "ES2020",
"module": "ES2020",
"module": "CommonJS",
"allowJs": true,
"composite": false,
"declaration": true,
Expand Down