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

feat: Automatically set username #16

Merged
merged 4 commits into from
Mar 24, 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
14 changes: 10 additions & 4 deletions README.ZH-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ yarn install pr-checker --global

#### 2.运行命令 `pr-checker`

* 设置你的 GitHub Token
> 首次使用请设置 GitHub Token
```bash
pr-checker -t #<GH_TOKEN> // set github token
```

首次使用请设置 GitHub Token 和用户名。请设置 GitHub Token 和用户名。

* 首次使用请设置 GitHub用户名。
> v1.1.1 版本以及更低的版本首次使用需要设置 GitHub 用户名
v1.1.1以上版本会根据 GitHub Token 自动设置用户名
```bash
pr-checker -t #<GH_TOKEN>
pr-checker -u #<GH_USERNAME>
pr-checker -u #<GH_USERNAME> // set github username
```

* 运行 run 命令来检查你的 pr
```` shell
pr-checker run
````
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 #<GH_TOKEN> // 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 #<GH_TOKEN>
pr-checker -u #<GH_USERNAME>
pr-checker -u #<GH_USERNAME> // set github username
```

* Run the run command to check your pr
```` shell
pr-checker run
````
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/gitApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 12 additions & 5 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() => {
Expand All @@ -23,11 +24,13 @@ export const run = async() => {
log('error', 'use `pr-checker -t <TOKEN>` to set your token')
process.exit(1)
}

if (!storage.username) {
log('error', 'use `pr-checker -u <USERNAME>` to set your username')
process.exit(1)
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()
Expand Down Expand Up @@ -72,8 +75,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()
}
}
1 change: 0 additions & 1 deletion packages/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const storagePath = resolve(storageDir, '_pr_checker_storage.json')
export async function loadStorage(
fn?: (storage: Storage) => Promise<boolean> | boolean,
) {
console.log(storagePath)
if (!storage) {
storage = existsSync(storagePath)
? JSON.parse((await fs.readFile(storagePath, 'utf-8')) || '{}') || {}
Expand Down
4 changes: 4 additions & 0 deletions play/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*

import { cac } from 'cac'
const cli = cac('pr-checker')
Expand All @@ -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()
3 changes: 1 addition & 2 deletions play/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down