From af348714f8d918b8f23b0fef3d80bf9db4cac17a Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Sat, 23 Apr 2022 01:14:54 +0200 Subject: [PATCH] feat: add new pull request trigger in GitHub (#301) * feat: add new pull request trigger in GitHub --- packages/backend/src/apps/github/info.json | 39 +++++++++++ packages/backend/src/apps/github/triggers.ts | 3 + .../apps/github/triggers/new-notification.ts | 2 +- .../apps/github/triggers/new-pull-request.ts | 64 +++++++++++++++++++ .../apps/github/triggers/new-repository.ts | 2 +- 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/apps/github/triggers/new-pull-request.ts diff --git a/packages/backend/src/apps/github/info.json b/packages/backend/src/apps/github/info.json index 5eaa3887..73595f68 100644 --- a/packages/backend/src/apps/github/info.json +++ b/packages/backend/src/apps/github/info.json @@ -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" + } + ] } ] } diff --git a/packages/backend/src/apps/github/triggers.ts b/packages/backend/src/apps/github/triggers.ts index 1e2eeb7a..4b00f576 100644 --- a/packages/backend/src/apps/github/triggers.ts +++ b/packages/backend/src/apps/github/triggers.ts @@ -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); } } diff --git a/packages/backend/src/apps/github/triggers/new-notification.ts b/packages/backend/src/apps/github/triggers/new-notification.ts index 2ffbf18a..7009be99 100644 --- a/packages/backend/src/apps/github/triggers/new-notification.ts +++ b/packages/backend/src/apps/github/triggers/new-notification.ts @@ -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) { diff --git a/packages/backend/src/apps/github/triggers/new-pull-request.ts b/packages/backend/src/apps/github/triggers/new-pull-request.ts new file mode 100644 index 00000000..eb1a249a --- /dev/null +++ b/packages/backend/src/apps/github/triggers/new-pull-request.ts @@ -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; + } +} diff --git a/packages/backend/src/apps/github/triggers/new-repository.ts b/packages/backend/src/apps/github/triggers/new-repository.ts index d0be3c1f..2652de37 100644 --- a/packages/backend/src/apps/github/triggers/new-repository.ts +++ b/packages/backend/src/apps/github/triggers/new-repository.ts @@ -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); }