feat(github): add new pull requests trigger

This commit is contained in:
Ali BARIN
2022-10-20 20:44:02 +02:00
parent 1613ab503c
commit 97f88d6c4a
3 changed files with 115 additions and 3 deletions

View File

@@ -0,0 +1,66 @@
import {
IGlobalVariable,
ITriggerOutput,
} from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
const fetchPullRequests = async ($: IGlobalVariable) => {
const repoParameter = $.step.parameters.repo as string;
if (!repoParameter) throw new Error('A repo must be set!');
const { repoOwner, repo } = getRepoOwnerAndRepo(repoParameter);
const pathname = `/repos/${repoOwner}/${repo}/pulls`;
const params = {
state: 'all',
sort: 'created',
direction: 'desc',
per_page: 100,
};
const pullRequests: ITriggerOutput = {
data: [],
};
let links;
do {
const response = await $.http.get(pathname, { params });
links = parseLinkHeader(response.headers.link);
if (response.integrationError) {
pullRequests.error = response.integrationError;
return pullRequests;
}
if (response.data.length) {
for (const pullRequest of response.data) {
const pullRequestId = pullRequest.id;
if (pullRequestId <= Number($.flow.lastInternalId) && !$.execution.testRun) return pullRequests;
const dataItem = {
raw: pullRequest,
meta: {
internalId: pullRequestId.toString(),
},
};
pullRequests.data.push(dataItem);
}
}
} while (links.next && !$.execution.testRun);
return pullRequests;
}
const newPullRequests = async ($: IGlobalVariable) => {
const pullRequests = await fetchPullRequests($);
pullRequests.data.reverse();
return pullRequests;
};
export default newPullRequests;