From 17205e9e36e58038ff3de5afe3e4744b1b7a31bc Mon Sep 17 00:00:00 2001 From: baiwusanyu-c <740132583@qq.com> Date: Fri, 24 Mar 2023 10:58:46 +0800 Subject: [PATCH 1/3] feat: Automatically set username --- packages/core/gitApi.ts | 8 ++++++++ packages/core/index.ts | 18 +++++++++++++++--- packages/core/storage.ts | 1 - 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/core/gitApi.ts b/packages/core/gitApi.ts index f0972fc..be600a0 100644 --- a/packages/core/gitApi.ts +++ b/packages/core/gitApi.ts @@ -149,3 +149,11 @@ export default class GitApi { } } } + +export async function getUserName(token: string) { + const octokit = new Octokit({ + auth: token, + }) + const { data } = await octokit.request('GET /user') + return data +} diff --git a/packages/core/index.ts b/packages/core/index.ts index 44141db..e8e780b 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -4,6 +4,7 @@ import { cac } from 'cac' import { version } from '../../package.json' import { clearStorage, loadStorage, saveStorage } from './storage' import { runtimeStart } from './runtime' +import { getUserName } from './gitApi' import type { Storage } from './storage' const cli = cac('pr-checker') export const run = async() => { @@ -19,11 +20,18 @@ export const run = async() => { cli.option('-g, --get', 'get git config') cli.command('run', 'check your pr').action(async() => { - if (!storage.token || !storage.username) { + if (!storage.token) { log('error', 'use `pr-checker -t ` to set your token') process.exit(1) return } + + if (!storage.username) { + log('info', 'You have not set a username, ' + + 'it has been automatically set for you according to the token') + const { login } = await getUserName(storage.token) + await setUserName(login) + } await runtimeStart(storage as Storage) }) cli.help() @@ -68,8 +76,12 @@ export const run = async() => { // set username if ((u && typeof u === 'string') || (username && typeof username === 'string')) { - storage.username = u || username - await saveStorage() + await setUserName() log('success', 'username set successfully') } + + async function setUserName(name?: string) { + storage.username = name || u || username + await saveStorage() + } } diff --git a/packages/core/storage.ts b/packages/core/storage.ts index 2eb257d..5d259eb 100644 --- a/packages/core/storage.ts +++ b/packages/core/storage.ts @@ -16,7 +16,6 @@ const storagePath = resolve(storageDir, '_pr_checker_storage.json') export async function loadStorage( fn?: (storage: Storage) => Promise | boolean, ) { - console.log(storagePath) if (!storage) { storage = existsSync(storagePath) ? JSON.parse((await fs.readFile(storagePath, 'utf-8')) || '{}') || {} From ec2c4e76a0fa3b089137af6ba1e276fba0320adb Mon Sep 17 00:00:00 2001 From: baiwusanyu-c <740132583@qq.com> Date: Fri, 24 Mar 2023 12:54:59 +0800 Subject: [PATCH 2/3] chore: play test --- package.json | 4 ++-- play/index.ts | 4 ++++ play/package.json | 3 +-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c547a25..decd157 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,8 @@ "scripts": { "init": "pnpm i", "lint:fix": "eslint --fix ./ --ext .vue,.js,.ts,.jsx,.tsx,.json ", - "dev": "cross-env RUNTIME_ENV=development esno watch play/index.ts", - "build": "cross-env RUNTIME_ENV=production pnpm run --filter @pr-checker/build build", + "dev": "pnpm run --filter @pr-checker/play play", + "build": "pnpm run --filter @pr-checker/build build", "prepublishOnly": "pnpm run build", "release": "bumpp package.json --commit --push --tag", "publish": "pnpm run release && pnpm run prepublishOnly && cd dist && pnpm run publish", diff --git a/play/index.ts b/play/index.ts index 2a9973a..d1391f5 100644 --- a/play/index.ts +++ b/play/index.ts @@ -1,3 +1,4 @@ +/* import { cac } from 'cac' const cli = cac('pr-checker') @@ -18,3 +19,6 @@ cli.help() cli.version('0.0.0') const asd = cli.parse() console.log(asd.options.t) +*/ +import { run } from '@pr-checker/entry' +run() diff --git a/play/package.json b/play/package.json index 6192018..50bea2b 100644 --- a/play/package.json +++ b/play/package.json @@ -15,8 +15,7 @@ "test:get": "pr-checker -g", "test:username": "pr-checker -u baiwusanyu-c", "test:token": "pr-checker -t baiwusanyu-c", - "test:token:script": "esno token.ts", - "test:runtime": "esno index.ts -t adwqdwq" + "play": "esno index.ts run" }, "devDependencies": { From 28b1a19f6fa64b2f9dfb166ba92a27a8cb9066e5 Mon Sep 17 00:00:00 2001 From: baiwusanyu-c <740132583@qq.com> Date: Fri, 24 Mar 2023 13:06:05 +0800 Subject: [PATCH 3/3] docs: update README.md --- README.ZH-CN.md | 14 ++++++++++---- README.md | 13 ++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.ZH-CN.md b/README.ZH-CN.md index df7e8a9..94ecffe 100644 --- a/README.ZH-CN.md +++ b/README.ZH-CN.md @@ -30,14 +30,20 @@ yarn install pr-checker --global #### 2.运行命令 `pr-checker` +* 设置你的 GitHub Token +> 首次使用请设置 GitHub Token +```bash +pr-checker -t # // set github token +``` -首次使用请设置 GitHub Token 和用户名。请设置 GitHub Token 和用户名。 - +* 首次使用请设置 GitHub用户名。 +> v1.1.1 版本以及更低的版本首次使用需要设置 GitHub 用户名 +v1.1.1以上版本会根据 GitHub Token 自动设置用户名 ```bash -pr-checker -t # -pr-checker -u # +pr-checker -u # // set github username ``` +* 运行 run 命令来检查你的 pr ```` shell pr-checker run ```` diff --git a/README.md b/README.md index 2a37bcc..439fd4a 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,20 @@ yarn install pr-checker --global #### 2.Run command to use `pr-checker` -Set GitHub Token and username at first time. +* Set up your GitHub token +> Please set GitHub Token for the first use +```bash +pr-checker -t # // set github token +``` +* Please set your GitHub username for the first use. +> For v1.1.1 and lower versions, you need to set the GitHub username for the first use +Versions above v1.1.1 will automatically set the username according to the GitHub Token ```bash -pr-checker -t # -pr-checker -u # +pr-checker -u # // set github username ``` +* Run the run command to check your pr ```` shell pr-checker run ````