-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(workflow): setup sync workflow
- Loading branch information
Showing
14 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
repo/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"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", | ||
"test": "jest" | ||
}, | ||
"lint-staged": { | ||
"*": [ | ||
"prettier --write --ignore-unknown" | ||
], | ||
"*.{ts,tsx}": [ | ||
"eslint --fix", | ||
"prettier --write" | ||
] | ||
}, | ||
"jest": { | ||
"preset": "@workspace/jest-preset" | ||
}, | ||
"dependencies": { | ||
"yaml": "^2.3.4" | ||
}, | ||
"devDependencies": { | ||
"@workspace/jest-preset": "workspace:*", | ||
"@workspace/tsconfig": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@workspace/tsconfig" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters