feat: add new pull request trigger in GitHub (#301)

* feat: add new pull request trigger in GitHub
This commit is contained in:
Ali BARIN
2022-04-23 01:14:54 +02:00
committed by GitHub
parent a623116204
commit af348714f8
5 changed files with 108 additions and 2 deletions

View File

@@ -326,6 +326,45 @@
"name": "Test trigger"
}
]
},
{
"name": "New pull request",
"key": "newPullRequest",
"interval": "15m",
"description": "Triggers when a new pull request is created",
"substeps": [
{
"key": "chooseAccount",
"name": "Choose account"
},
{
"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"
}
]
}
]
}

View File

@@ -3,17 +3,20 @@ import NewRepository from './triggers/new-repository';
import NewOrganization from './triggers/new-organization';
import NewBranch from './triggers/new-branch';
import NewNotification from './triggers/new-notification';
import NewPullRequest from './triggers/new-pull-request';
export default class Triggers {
newRepository: NewRepository;
newOrganization: NewOrganization;
newBranch: NewBranch;
newNotification: NewNotification;
newPullRequest: NewPullRequest;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.newRepository = new NewRepository(connectionData);
this.newOrganization = new NewOrganization(connectionData);
this.newBranch = new NewBranch(connectionData, parameters);
this.newNotification = new NewNotification(connectionData, parameters);
this.newPullRequest = new NewPullRequest(connectionData, parameters);
}
}

View File

@@ -65,7 +65,7 @@ export default class NewNotification {
async run(startTime: Date) {
const options = {
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
since: DateTime.fromJSDate(startTime).toISO(),
};
if (this.hasRepo) {

View File

@@ -0,0 +1,64 @@
import { Octokit } from 'octokit';
import { DateTime } from 'luxon';
import { IJSONObject } from '@automatisch/types';
export default class NewPullRequest {
client?: Octokit;
repoOwner?: string;
repo?: string;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
if (connectionData.accessToken) {
this.client = new Octokit({
auth: connectionData.accessToken as string,
});
}
if (parameters?.repo) {
const [owner, repo] = (parameters.repo as string).split('/');
this.repoOwner = owner;
this.repo = repo;
}
}
get options() {
return {
owner: this.repoOwner,
repo: this.repo,
sort: 'created' as const,
direction: 'desc' as const,
};
}
async run(startTime: Date) {
const iterator = await this.client.paginate.iterator(this.client.rest.pulls.list, this.options);
const newPullRequests = [];
const startTimeDateObject = DateTime.fromJSDate(startTime);
pullRequestIterator:
for await (const { data: pullRequests } of iterator) {
for (const pullRequest of pullRequests) {
const createdAtDateObject = DateTime.fromISO(pullRequest.created_at);
if (createdAtDateObject < startTimeDateObject) {
break pullRequestIterator;
}
newPullRequests.push(pullRequest);
}
}
return newPullRequests;
}
async testRun() {
const options = {
...this.options,
per_page: 1,
};
return (await this.client.rest.pulls.list(options)).data;
}
}

View File

@@ -16,7 +16,7 @@ export default class NewRepository {
async run(startTime: Date) {
const options = {
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
since: DateTime.fromJSDate(startTime).toISO(),
};
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
}