Skip to content

Commit

Permalink
add triggerWorkflow
Browse files Browse the repository at this point in the history
  • Loading branch information
yaf committed Dec 17, 2024
1 parent f2be424 commit 8b68f94
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
22 changes: 10 additions & 12 deletions build/services/merge-queue.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { config } from '../../config.js';

const GITHUB_WORKFLOW_DISPATCH_URL = `https://api.github.com/repos/${config.github.automerge.repositoryName}/actions/workflows/${config.github.automerge.workflowId}/dispatches`;
const GITHUB_WORKFLOW_REF = config.github.automerge.workflowRef;

import * as _pullRequestRepository from '../repositories/pull-request-repository.js';
import { httpAgent as _httpAgent } from '../../common/http-agent.js';
import _githubService from '../../common/services/github.js';

export async function mergeQueue({ pullRequestRepository = _pullRequestRepository, httpAgent = _httpAgent } = {}) {
export async function mergeQueue({ pullRequestRepository = _pullRequestRepository, githubService = _githubService } = {}) {
const isAtLeastOneMergeInProgress = await pullRequestRepository.isAtLeastOneMergeInProgress();
if (isAtLeastOneMergeInProgress) {
return;
Expand All @@ -21,13 +18,14 @@ export async function mergeQueue({ pullRequestRepository = _pullRequestRepositor
...pr,
isMerging: true,
});
await httpAgent.post({
url: GITHUB_WORKFLOW_DISPATCH_URL,
payload: {
ref: GITHUB_WORKFLOW_REF,
inputs: {
pullRequest: `${pr.repositoryName}/${pr.number}`,
},
await githubService.triggerWorkflow({
workflow: {
id: config.github.automerge.workflowId,
repositoryName: config.github.automerge.repositoryName,
ref: config.github.automerge.workflowRef,
},
inputs: {
pullRequest: `${pr.repositoryName}/${pr.number}`,
},
});
}
8 changes: 8 additions & 0 deletions common/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ const github = {
});
return response.status === 204;
},

async triggerWorkflow({ workflow, inputs }) {
const octokit = _createOctokit();
const response = await octokit.request(`POST /repos/${workflow.repositoryName}/actions/workflows/${workflow.id}/dispatches`, {
ref: workflow.ref,
inputs,
});
}
};

export default github;
18 changes: 18 additions & 0 deletions test/unit/build/services/github_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,22 @@ describe('Unit | Build | github-test', function () {
});
});
});
describe('#triggerWorkflow', function () {
it('calls github API', async function () {
const workflow = {
id: "lo",
repositoryName: 'aName',
ref: 'aRef',
}
const inputs = { pullRequest: "aName/1234"}

const githubNock = nock('https://api.github.com').post(`/repos/${workflow.repositoryName}/actions/workflows/${workflow.id}/dispatches`, {
ref: workflow.ref,
inputs: inputs
}).reply(200);

await githubService.triggerWorkflow({workflow, inputs});
expect(githubNock.isDone()).to.be.true;
});
});
});
16 changes: 10 additions & 6 deletions test/unit/run/services/merge-queue_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ describe('Unit | Build | Services | merge-queue', function () {
pullRequestRepository.isAtLeastOneMergeInProgress.resolves(false);
pullRequestRepository.getOldest.resolves(pr);

const httpAgent = {
post: sinon.stub(),
const githubService = {
triggerWorkflow: sinon.stub(),
};

await mergeQueue({ pullRequestRepository, httpAgent });
await mergeQueue({ pullRequestRepository, githubService });

expect(pullRequestRepository.isAtLeastOneMergeInProgress).to.have.been.called;
expect(pullRequestRepository.update).to.have.been.calledWithExactly({
number: 1,
repositoryName: 'foo/pix-project',
isMerging: true,
});
expect(httpAgent.post).to.have.been.calledWithExactly({
url: `https://api.github.com/repos/${config.github.automerge.repositoryName}/actions/workflows/${config.github.automerge.workflowId}/dispatches`,
payload: { ref: 'main', inputs: { pullRequest: 'foo/pix-project/1' } },
expect(githubService.triggerWorkflow).to.have.been.calledWithExactly({
workflow: {
id: config.github.automerge.workflowId,
repositoryName: config.github.automerge.repositoryName,
ref: config.github.automerge.workflowRef,
},
inputs: {pullRequest: 'foo/pix-project/1'}
});
});
});
Expand Down

0 comments on commit 8b68f94

Please sign in to comment.