From 91ca3d2b14978c370c3d3637aab97d365bc5af2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sindri=20Gu=C3=B0mundsson?= Date: Tue, 24 Jan 2023 09:58:26 +0000 Subject: [PATCH] Allow configuration of fetch count (#364) --- README.md | 12 +++++++++--- action.yml | 12 ++++++++++++ src/main.ts | 9 +++++++-- src/pullRequest.ts | 19 +++++++++---------- src/type.ts | 7 +++++++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6b34033..c1318eb 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ Merge your pull request in order when enabled the `Require branches to be up to Inspired by [Merge Queue feature of Mergify](https://mergify.io/features/merge-queue). > Safety -> +> > Do not merge broken pull requests. By merging your pull requests serially using a queue, your code is safe. Each pull request is tested with the latest CI code. > Save CI time -> +> > Rather than overconsuming your CI time by trying to merge multiple pull requests, just run it once before the pull request gets merged. ## Quick Start @@ -18,7 +18,7 @@ Inspired by [Merge Queue feature of Mergify](https://mergify.io/features/merge-q 1. Enable `Allow auto-merge` in `Settings/Options`. -3. Create a workflow file (`.github/workflow/update-branch.yaml`): +2. Create a workflow file (`.github/workflow/update-branch.yaml`): Example: @@ -59,6 +59,12 @@ jobs: requiredStatusChecks: | build_pr WIP + # Optionally set the maximum amount of pull requests to match against (default: 50) + fetchMaxPr: 50 + # Optionally set the maximum amount of pull request checks to fetch (default: 100) + fetchMaxPrChecks: 100 + # Optionally set the maximum amount of pull request labels to fetch (default: 10) + fetchMaxPrLabels: 10 ``` If you are using a personal access token and it has permission to access branch protection rules, you can set your jobs like: diff --git a/action.yml b/action.yml index cf8ad29..e507efd 100644 --- a/action.yml +++ b/action.yml @@ -31,6 +31,18 @@ inputs: required: false description: 'The name pattern of GitHub branch protection rules to apply. The default behavior is to find the name pattern of main or master. Require personal access token to let this feature work.' default: '' + fetchMaxPr: + required: false + description: 'The maximum amount of pull request fetch when searching for eligible pull requests.' + default: '50' + fetchMaxPrChecks: + required: false + description: 'The maximum amount of pull request checks to fetch when searching for requiredStatusChecks.' + default: '100' + fetchMaxPrLabels: + required: false + description: 'The maximum amount of pull request labels to fetch when searching for requiredLabels.' + default: '10' runs: using: 'node16' main: 'dist/index.js' diff --git a/src/main.ts b/src/main.ts index 11d36b6..0a1c578 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { mergePullRequest, updateBranch } from './pullRequest' -import {Condition, GhContext, IssueInfo, RecordBody} from './type' +import {Condition, FetchConfig, GhContext, IssueInfo, RecordBody} from './type' import {getViewerName} from './user' import { createIssueBody, @@ -39,10 +39,15 @@ async function run(): Promise { const protectedBranchNamePattern = core.getInput( 'protectedBranchNamePattern' ) + const fetchConfig: FetchConfig = { + prs: parseInt(core.getInput('fetchMaxPr')), + checks: parseInt(core.getInput('fetchMaxPrChecks')), + labels: parseInt(core.getInput('fetchMaxPrLabels')) + } const octokit = github.getOctokit(token) const {owner, repo} = github.context.repo - const ctx: GhContext = {octokit, owner, repo, autoMergeMethod} + const ctx: GhContext = {octokit, owner, repo, autoMergeMethod, fetchConfig} const branchProtectionRule = await getBranchProtectionRule( ctx, diff --git a/src/pullRequest.ts b/src/pullRequest.ts index d7217a9..e2097eb 100644 --- a/src/pullRequest.ts +++ b/src/pullRequest.ts @@ -1,5 +1,6 @@ import retry from 'async-retry' import { + FetchConfig, GhContext, PullRequestInfo, RepositoryGetPullRequest, @@ -14,7 +15,7 @@ export async function getPullRequest( `query ($owner: String!, $repo: String!, $num: Int!) { repository(name: $repo, owner: $owner) { pullRequest(number: $num) { - ${pullRequestFragment} + ${getPullRequestFragment(ctx.fetchConfig)} } } }`, @@ -96,9 +97,9 @@ async function listPullRequests(ctx: GhContext): Promise { const result: RepositoryListPullRequest = await ctx.octokit.graphql( `query ($owner: String!, $repo: String!) { repository(name: $repo, owner: $owner) { - pullRequests(first: ${pullRequestCount}, states: OPEN) { + pullRequests(first: ${ctx.fetchConfig.prs}, states: OPEN) { nodes { - ${pullRequestFragment} + ${getPullRequestFragment(ctx.fetchConfig)} } } } @@ -114,11 +115,8 @@ async function listPullRequests(ctx: GhContext): Promise { return result.repository.pullRequests.nodes } -const pullRequestCount = 50 -const checkCount = 100 -const labelCount = 10 - -const pullRequestFragment = ` +function getPullRequestFragment(cfg: FetchConfig): string { + return ` id title baseRefName @@ -132,7 +130,7 @@ const pullRequestFragment = ` reviewRequests { totalCount } - labels(first: ${labelCount}) { + labels(first: ${cfg.labels}) { nodes { name } @@ -141,7 +139,7 @@ const pullRequestFragment = ` nodes { commit { statusCheckRollup { - contexts(first: ${checkCount}) { + contexts(first: ${cfg.checks}) { nodes { ... on CheckRun { name @@ -158,3 +156,4 @@ const pullRequestFragment = ` } } }` +} diff --git a/src/type.ts b/src/type.ts index ec45997..0e8f9b1 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1,10 +1,17 @@ import {Octokit} from '@octokit/core' +export interface FetchConfig { + prs: number + labels: number + checks: number +} + export interface GhContext { octokit: Octokit owner: string repo: string autoMergeMethod: string + fetchConfig: FetchConfig } export interface Condition {