feat(github): add new-issues trigger
This commit is contained in:
100
packages/backend/src/apps/github/triggers/new-issues/index.ts
Normal file
100
packages/backend/src/apps/github/triggers/new-issues/index.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import defineTrigger from '../../../../helpers/define-trigger';
|
||||||
|
import newIssues from './new-issues';
|
||||||
|
|
||||||
|
export default defineTrigger({
|
||||||
|
name: 'New issue',
|
||||||
|
key: 'newIssues',
|
||||||
|
pollInterval: 15,
|
||||||
|
description: 'Triggers when a new issue is created',
|
||||||
|
substeps: [
|
||||||
|
{
|
||||||
|
key: 'chooseConnection',
|
||||||
|
name: 'Choose connection'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'chooseTrigger',
|
||||||
|
name: 'Set up a trigger',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
label: 'Repo',
|
||||||
|
key: 'repo',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
variables: false,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listRepos'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Which types of issues should this trigger on?',
|
||||||
|
key: 'issueType',
|
||||||
|
type: 'dropdown',
|
||||||
|
description: 'Defaults to any issue you can see.',
|
||||||
|
required: true,
|
||||||
|
variables: false,
|
||||||
|
value: 'all',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
label: 'Any issue you can see',
|
||||||
|
value: 'all'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Only issues assigned to you',
|
||||||
|
value: 'assigned'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Only issues created by you',
|
||||||
|
value: 'created'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: `Only issues you're mentioned in`,
|
||||||
|
value: 'mentioned'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: `Only issues you're subscribed to`,
|
||||||
|
value: 'subscribed'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Label',
|
||||||
|
key: 'label',
|
||||||
|
type: 'dropdown',
|
||||||
|
description: 'Only trigger on issues when this label is added.',
|
||||||
|
required: false,
|
||||||
|
variables: false,
|
||||||
|
dependsOn: ['parameters.repo'],
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listLabels'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'parameters.repo',
|
||||||
|
value: '{parameters.repo}'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'testStep',
|
||||||
|
name: 'Test trigger'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
async run($) {
|
||||||
|
return await newIssues($);
|
||||||
|
},
|
||||||
|
});
|
@@ -0,0 +1,64 @@
|
|||||||
|
import {
|
||||||
|
IGlobalVariable,
|
||||||
|
ITriggerOutput,
|
||||||
|
} from '@automatisch/types';
|
||||||
|
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
|
||||||
|
import parseLinkHeader from '../../../../helpers/parse-header-link';
|
||||||
|
|
||||||
|
function getPathname($: IGlobalVariable) {
|
||||||
|
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo as string);
|
||||||
|
|
||||||
|
if (repoOwner && repo) {
|
||||||
|
return `/repos/${repoOwner}/${repo}/issues`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '/issues';
|
||||||
|
}
|
||||||
|
|
||||||
|
const newIssues = async ($: IGlobalVariable) => {
|
||||||
|
const pathname = getPathname($);
|
||||||
|
const params = {
|
||||||
|
labels: $.step.parameters.label,
|
||||||
|
filter: 'all',
|
||||||
|
state: 'all',
|
||||||
|
sort: 'created',
|
||||||
|
direction: 'desc',
|
||||||
|
per_page: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
const issues: ITriggerOutput = {
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
let links;
|
||||||
|
do {
|
||||||
|
const response = await $.http.get(pathname, { params });
|
||||||
|
links = parseLinkHeader(response.headers.link);
|
||||||
|
|
||||||
|
if (response.integrationError) {
|
||||||
|
issues.error = response.integrationError;
|
||||||
|
return issues;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.data.length) {
|
||||||
|
for (const issue of response.data) {
|
||||||
|
const issueId = issue.id.toString();
|
||||||
|
|
||||||
|
if (issueId <= $.flow.lastInternalId && !$.execution.testRun) return issues;
|
||||||
|
|
||||||
|
const dataItem = {
|
||||||
|
raw: issue,
|
||||||
|
meta: {
|
||||||
|
internalId: issueId,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
issues.data.push(dataItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (links.next && !$.execution.testRun);
|
||||||
|
|
||||||
|
return issues;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default newIssues;
|
Reference in New Issue
Block a user