Skip to content

Commit

Permalink
feat(get-merge-queue-position): add merge queue position helper (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Dec 18, 2024
1 parent 9a90ec4 commit b309052
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 12 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/get-merge-queue-position.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Get Merge Queue Position

on:
pull_request:
branches: [main]
paths:
- 'src/helpers/get-merge-queue-position.ts'

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./
with:
helper: get-merge-queue-position
pull_number: ${{ github.event.pull_request.number }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ with:

Each of the following helpers are defined in a file of the same name in `src/helpers`:

### [get-merge-queue-position](.github/workflows/get-merge-queue-position.yml)

- Returns the current position of a given PR in the GitHub merge queue

### [are-reviewers-required](.github/workflows/are-reviewers-required.yml)

- Returns true if all teams specified are requested for review on a pull request
Expand Down
Binary file modified bun.lockb
Binary file not shown.
122 changes: 122 additions & 0 deletions dist/604.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/604.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42973,6 +42973,16 @@ var map = {
461,
338
],
"./get-merge-queue-position": [
3604,
461,
604
],
"./get-merge-queue-position.ts": [
3604,
461,
604
],
"./initiate-deployment": [
5264,
461,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@actions/core": "1.11.1",
"@actions/github": "6.0.0",
"@adobe/node-fetch-retry": "2.2.0",
"@octokit/graphql-schema": "15.25.0",
"@octokit/rest": "21.0.2",
"axios": "1.7.9",
"bluebird": "3.7.2",
Expand Down
43 changes: 43 additions & 0 deletions src/helpers/get-merge-queue-position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { HelperInputs } from '../types/generated';
import { context } from '@actions/github';
import { octokitGraphql } from '../octokit';
import { Repository } from '@octokit/graphql-schema';

export class GetMergeQueuePosition extends HelperInputs {
pull_number = '';
max_queue_size?: string;
}

export const getMergeQueuePosition = async ({ pull_number, max_queue_size = '10' }: GetMergeQueuePosition) => {
const data = await octokitGraphql<{ repository: Repository }>(`
query {
repository(owner: "${context.repo.owner}", name: "${context.repo.repo}") {
mergeQueue {
entries(first: ${max_queue_size}) {
nodes {
pullRequest {
number
}
position
}
}
}
}
}
`);
const mergeQueueEntries = data.repository.mergeQueue?.entries?.nodes;
return mergeQueueEntries?.find(entry => entry?.pullRequest?.number === Number(pull_number))?.position;
};
4 changes: 2 additions & 2 deletions templates/helper.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { context } from '@actions/github';
import { octokit } from '../octokit';

export class {{ properCase helper }} extends HelperInputs {
requiredInput = '';
optionalInput?: string;
requiredInput = '';
optionalInput?: string;
}

export const {{ camelCase helper }} = async ({ requiredInput, optionalInput }: {{ properCase helper }}) => {
Expand Down
4 changes: 2 additions & 2 deletions templates/test.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({
rest: {
rest: {

}
}
}))
}));

Expand Down
14 changes: 7 additions & 7 deletions templates/workflow.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ on:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./
with:
helper: {{ dashCase helper }}
- uses: ./
with:
helper: {{ dashCase helper }}
61 changes: 61 additions & 0 deletions test/helpers/get-merge-queue-position.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Mocktokit } from '../types';
import { getMergeQueuePosition } from '../../src/helpers/get-merge-queue-position';
import { octokitGraphql } from '../../src/octokit';
import { MergeQueueEntry } from '@octokit/graphql-schema';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({
graphql: jest.fn()
}))
}));

type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
function mockGraphQLResponse(mergeQueueEntries: RecursivePartial<MergeQueueEntry>[]) {
(octokitGraphql as unknown as Mocktokit).mockImplementation(async () => ({
repository: {
mergeQueue: {
entries: {
nodes: mergeQueueEntries
}
}
}
}));
}

describe('getMergeQueuePosition', () => {
it('should return 1 for PR 1st in the queue', async () => {
mockGraphQLResponse([
{ position: 1, pullRequest: { number: 123 } },
{ position: 2, pullRequest: { number: 456 } }
]);
const result = await getMergeQueuePosition({ pull_number: '123' });
expect(result).toBe(1);
});

it('should return 3 for PR 3rd in the queue', async () => {
mockGraphQLResponse([
{ position: 1, pullRequest: { number: 123 } },
{ position: 2, pullRequest: { number: 456 } },
{ position: 3, pullRequest: { number: 789 } }
]);
const result = await getMergeQueuePosition({ pull_number: '789' });
expect(result).toBe(3);
});
});

0 comments on commit b309052

Please sign in to comment.