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 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 diff --git a/src/index.ts b/src/index.ts index e6efd31..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 @@ -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, {