From 55a1a653049b2468580e726ec1c30ec7abaf7af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=B5=B7?= Date: Mon, 25 Dec 2023 02:00:28 +0800 Subject: [PATCH 1/4] feat: getTokens retry --- src/index.ts | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index e6efd31..c470234 100644 --- a/src/index.ts +++ b/src/index.ts @@ -322,20 +322,38 @@ export class MediaWikiApi { } /** Token Handler */ - async getTokens(type: MwTokenName[] = ['csrf']) { + async getTokens(type: MwTokenName[] = ['csrf'], retry: number = 3): Promise> { + if (retry < 1) { + throw new WikiSaikouError( + WikiSaikouErrorCode.TOKEN_RETRY_LIMIT_EXCEEDED, + 'The limit of the number of times to automatically re-acquire the token has been exceeded' + ) + } this.defaultOptions.credentials = 'include' - const { data } = await this.get({ - action: 'query', - meta: 'tokens', - type, - }) - this.tokens = { ...this.tokens, ...data.query.tokens } - return this.tokens + try { + const { data } = await this.get({ + action: 'query', + meta: 'tokens', + type, + }) + this.tokens = { ...this.tokens, ...data.query.tokens } + return this.tokens + } catch (err) { + if (retry < 1) { + throw new WikiSaikouError( + WikiSaikouErrorCode.HTTP_ERROR, + "The server returns an error, but it doesn't seem to be caused by MediaWiki", + err + ) + } else { + return this.getTokens(type, retry - 1); + } + } } - async token(type: MwTokenName = 'csrf', noCache = false) { + async token(type: MwTokenName = 'csrf', noCache = false, retry: number = 3) { if (!this.tokens[`${type}token`] || noCache) { delete this.tokens[`${type}token`] - await this.getTokens([type]) + await this.getTokens([type], retry) } return this.tokens[`${type}token`] } @@ -343,9 +361,9 @@ export class MediaWikiApi { async postWithToken( tokenType: MwTokenName, body: MwApiParams, - options?: { tokenName?: string; retry?: number; noCache?: boolean } + options?: { tokenName?: string; retry?: number; noCache?: boolean; tokenRetry?: number } ): Promise> { - const { tokenName = 'token', retry = 3, noCache = false } = options || {} + const { tokenName = 'token', retry = 3, noCache = false, tokenRetry = 3 } = options || {} if (retry < 1) { throw new WikiSaikouError( @@ -354,7 +372,7 @@ export class MediaWikiApi { ) } - const token = await this.token(tokenType, noCache) + const token = await this.token(tokenType, noCache, tokenRetry) const doRetry = () => this.postWithToken(tokenType, body, { From 552b70a9ef2e088afeb0e5415130866566ebaacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=B5=B7?= Date: Mon, 25 Dec 2023 02:21:23 +0800 Subject: [PATCH 2/4] feat: logintoken retry --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index c470234..349b423 100644 --- a/src/index.ts +++ b/src/index.ts @@ -227,7 +227,7 @@ export class MediaWikiApi { lgname: string, lgpassword: string, params?: MwApiParams, - postOptions?: { retry?: number; noCache?: boolean } + postOptions?: { retry?: number; noCache?: boolean, tokenRetry?: number } ): Promise<{ result: 'Success' | 'NeedToken' | 'WrongToken' | 'Failed' token?: string From 6efc42d9f8416511bb0a48483d9f7a18e466f9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=B5=B7?= Date: Mon, 25 Dec 2023 02:45:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?chore:=20=E5=9C=A8approved=E5=90=8E?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/unit-test.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 2a8eada..8eeb326 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -2,8 +2,8 @@ name: Unit test for MediaWikiApi core on: push: branches: [master, dev, actions] - pull_request: - branches: [master, dev, actions] + pull_request_review: + types: [submitted] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: @@ -11,6 +11,8 @@ jobs: # The type of runner that the job will run on runs-on: ubuntu-latest + if: github.event_name == 'push' || (github.event_name == 'pull_request_review' && github.event.review.state == 'APPROVED') + # Steps represent a sequence of tasks that will be executed as part of the job steps: - name: Checkout From 1a686e0d1a68301102074bb28b2a5ba59b5f85a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=B5=B7?= Date: Mon, 25 Dec 2023 03:04:22 +0800 Subject: [PATCH 4/4] feat: auto assign MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 没有assign权限。。。 --- .github/auto_assign.yml | 9 +++++++++ .github/workflows/auto_assign_pull_request.yml | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 .github/auto_assign.yml create mode 100644 .github/workflows/auto_assign_pull_request.yml diff --git a/.github/auto_assign.yml b/.github/auto_assign.yml new file mode 100644 index 0000000..dde0730 --- /dev/null +++ b/.github/auto_assign.yml @@ -0,0 +1,9 @@ +addReviewers: true + +addAssignees: true + +reviewers: + - dragon-fish + +assignees: + - dragon-fish \ No newline at end of file diff --git a/.github/workflows/auto_assign_pull_request.yml b/.github/workflows/auto_assign_pull_request.yml new file mode 100644 index 0000000..1772ca6 --- /dev/null +++ b/.github/workflows/auto_assign_pull_request.yml @@ -0,0 +1,13 @@ +name: 'Auto Assign' +on: + pull_request: + types: [opened, ready_for_review] + +jobs: + add-reviews: + runs-on: ubuntu-latest + steps: + - uses: kentaro-m/auto-assign-action@v1.2.5 + with: + configuration-path: '.github/auto_assign.yml' # Only needed if you use something other than .github/auto_assign.yml + repo-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file