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

@@ -42,14 +42,14 @@ const newIssues = async ($: IGlobalVariable) => {
if (response.data.length) {
for (const issue of response.data) {
const issueId = issue.id.toString();
const issueId = issue.id;
if (issueId <= $.flow.lastInternalId && !$.execution.testRun) return issues;
if (issueId <= Number($.flow.lastInternalId) && !$.execution.testRun) return issues;
const dataItem = {
raw: issue,
meta: {
internalId: issueId,
internalId: issueId.toString(),
},
};

View File

@@ -0,0 +1,46 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newPullRequests from './new-pull-requests';
export default defineTrigger({
name: 'New pull requests',
key: 'newPullRequests',
pollInterval: 15,
description: 'Triggers when a new pull request is created',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection'
},
{
key: 'chooseTrigger',
name: 'Set up a trigger',
arguments: [
{
label: 'Repo',
key: 'repo',
type: 'dropdown',
required: true,
variables: false,
source: {
type: 'query',
name: 'getData',
arguments: [
{
name: 'key',
value: 'listRepos'
}
]
}
}
]
},
{
key: 'testStep',
name: 'Test trigger'
}
],
async run($) {
return await newPullRequests($);
},
});

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;