Skip to content

Commit fd12b82

Browse files
authored
feat(initiate-deployment): support merge queue (#724)
Co-authored-by: danadajian <[email protected]>
1 parent 3e7bb29 commit fd12b82

File tree

6 files changed

+185
-21
lines changed

6 files changed

+185
-21
lines changed

dist/264.index.js

Lines changed: 101 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/264.index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43008,11 +43008,13 @@ var map = {
4300843008
"./initiate-deployment": [
4300943009
5264,
4301043010
461,
43011+
366,
4301143012
264
4301243013
],
4301343014
"./initiate-deployment.ts": [
4301443015
5264,
4301543016
461,
43017+
366,
4301643018
264
4301743019
],
4301843020
"./is-user-core-member": [

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers/initiate-deployment.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ limitations under the License.
1212
*/
1313

1414
import { DeploymentState } from '../types/github';
15-
import { GITHUB_OPTIONS } from '../constants';
15+
import { DEFAULT_PIPELINE_STATUS, GITHUB_OPTIONS } from '../constants';
1616
import { HelperInputs } from '../types/generated';
1717
import { context as githubContext } from '@actions/github';
1818
import { octokit } from '../octokit';
19+
import { getMergeQueueCommitHashes } from '../utils/merge-queue';
20+
import { map } from 'bluebird';
1921

2022
export class InitiateDeployment extends HelperInputs {
2123
sha = '';
@@ -24,6 +26,8 @@ export class InitiateDeployment extends HelperInputs {
2426
declare environment_url?: string;
2527
declare description?: string;
2628
declare target_url?: string;
29+
declare context?: string;
30+
declare merge_queue_enabled?: string;
2731
}
2832

2933
export const initiateDeployment = async ({
@@ -32,7 +36,9 @@ export const initiateDeployment = async ({
3236
environment,
3337
environment_url,
3438
description,
35-
target_url
39+
target_url,
40+
context = DEFAULT_PIPELINE_STATUS,
41+
merge_queue_enabled
3642
}: InitiateDeployment) => {
3743
const { data } = await octokit.repos.createDeployment({
3844
ref: sha,
@@ -55,5 +61,17 @@ export const initiateDeployment = async ({
5561
...GITHUB_OPTIONS
5662
});
5763

58-
return deployment_id;
64+
if (merge_queue_enabled === 'true') {
65+
const mergeQueueCommitHashes = await getMergeQueueCommitHashes();
66+
return map(mergeQueueCommitHashes, async sha =>
67+
octokit.repos.createCommitStatus({
68+
sha,
69+
context,
70+
state: 'pending',
71+
description,
72+
target_url,
73+
...githubContext.repo
74+
})
75+
);
76+
}
5977
};

test/helpers/initiate-deployment.test.ts

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
import { GITHUB_OPTIONS } from '../../src/constants';
14+
import { DEFAULT_PIPELINE_STATUS, GITHUB_OPTIONS } from '../../src/constants';
1515
import { Mocktokit } from '../types';
1616
import { context } from '@actions/github';
1717
import { initiateDeployment } from '../../src/helpers/initiate-deployment';
@@ -25,6 +25,7 @@ jest.mock('@actions/github', () => ({
2525
repos: {
2626
createDeployment: jest.fn(),
2727
createDeploymentStatus: jest.fn(),
28+
createCommitStatus: jest.fn(),
2829
listBranches: jest.fn(() => ({ data: [] }))
2930
}
3031
}
@@ -46,13 +47,14 @@ describe('initiateDeployment', () => {
4647
const target_url = 'url';
4748
const auto_merge = false;
4849

49-
it('should call createDeployment with correct params', async () => {
50+
it('should handle non merge queue case', async () => {
5051
await initiateDeployment({
5152
sha,
5253
environment,
5354
description,
5455
target_url
5556
});
57+
5658
expect(octokit.repos.createDeployment).toHaveBeenCalledWith({
5759
ref: sha,
5860
environment,
@@ -61,14 +63,53 @@ describe('initiateDeployment', () => {
6163
...context.repo,
6264
...GITHUB_OPTIONS
6365
});
66+
expect(octokit.repos.createDeploymentStatus).toHaveBeenCalledWith({
67+
state: 'in_progress',
68+
deployment_id,
69+
description,
70+
target_url,
71+
...context.repo,
72+
...GITHUB_OPTIONS
73+
});
74+
expect(octokit.repos.listBranches).not.toHaveBeenCalled();
6475
});
6576

66-
it('should call createDeploymentStatus with correct params', async () => {
77+
it('should handle merge queue case', async () => {
78+
(octokit.repos.listBranches as unknown as Mocktokit).mockImplementation(async ({ page }) =>
79+
page > 1
80+
? { data: [] }
81+
: {
82+
data: [
83+
{
84+
name: 'some-branch',
85+
commit: { sha: 'normal sha 1' }
86+
},
87+
{
88+
name: 'gh-readonly-queue/default-branch/pr-123-79a5ad2b1a46f6b5d77e02573937667979635f27',
89+
commit: { sha: 'merge queue sha 1' }
90+
},
91+
{
92+
name: 'gh-readonly-queue/default-branch/pr-456-79a5ad2b1a46f6b5d77e02573937667979635f27',
93+
commit: { sha: 'merge queue sha 2' }
94+
}
95+
]
96+
}
97+
);
6798
await initiateDeployment({
6899
sha,
69100
environment,
70101
description,
71-
target_url
102+
target_url,
103+
merge_queue_enabled: 'true'
104+
});
105+
106+
expect(octokit.repos.createDeployment).toHaveBeenCalledWith({
107+
ref: sha,
108+
environment,
109+
auto_merge,
110+
required_contexts: [],
111+
...context.repo,
112+
...GITHUB_OPTIONS
72113
});
73114
expect(octokit.repos.createDeploymentStatus).toHaveBeenCalledWith({
74115
state: 'in_progress',
@@ -78,15 +119,21 @@ describe('initiateDeployment', () => {
78119
...context.repo,
79120
...GITHUB_OPTIONS
80121
});
81-
});
82-
83-
it('should return deployment id as output', async () => {
84-
const result = await initiateDeployment({
85-
sha,
86-
environment,
122+
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
123+
sha: 'merge queue sha 1',
124+
context: DEFAULT_PIPELINE_STATUS,
125+
state: 'pending',
87126
description,
88-
target_url
127+
target_url,
128+
...context.repo
129+
});
130+
expect(octokit.repos.createCommitStatus).toHaveBeenCalledWith({
131+
sha: 'merge queue sha 2',
132+
context: DEFAULT_PIPELINE_STATUS,
133+
state: 'pending',
134+
description,
135+
target_url,
136+
...context.repo
89137
});
90-
expect(result).toEqual(deployment_id);
91138
});
92139
});

0 commit comments

Comments
 (0)