From c79622aefd22404c3f97301e4590a3453a9c64ba Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 22 Apr 2022 17:57:42 +0200 Subject: [PATCH] feat: add new commit comment trigger in GitHub --- packages/backend/src/apps/github/info.json | 39 ++++++++++++ packages/backend/src/apps/github/triggers.ts | 3 + .../github/triggers/new-commit-comment.ts | 62 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 packages/backend/src/apps/github/triggers/new-commit-comment.ts diff --git a/packages/backend/src/apps/github/info.json b/packages/backend/src/apps/github/info.json index e7f23bf5..7ad8935b 100644 --- a/packages/backend/src/apps/github/info.json +++ b/packages/backend/src/apps/github/info.json @@ -443,6 +443,45 @@ "name": "Test trigger" } ] + }, + { + "name": "New commit comment", + "key": "newCommitComment", + "interval": "15m", + "description": "Triggers when a new commit comment 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 a07be0b1..c31cb341 100644 --- a/packages/backend/src/apps/github/triggers.ts +++ b/packages/backend/src/apps/github/triggers.ts @@ -6,6 +6,7 @@ import NewNotification from './triggers/new-notification'; import NewPullRequest from './triggers/new-pull-request'; import NewWatcher from './triggers/new-watcher'; import NewMilestone from './triggers/new-milestone'; +import NewCommitComment from './triggers/new-commit-comment'; export default class Triggers { newRepository: NewRepository; @@ -15,6 +16,7 @@ export default class Triggers { newPullRequest: NewPullRequest; newWatcher: NewWatcher; newMilestone: NewMilestone; + newCommitComment: NewCommitComment; constructor(connectionData: IJSONObject, parameters: IJSONObject) { this.newRepository = new NewRepository(connectionData); @@ -24,5 +26,6 @@ export default class Triggers { this.newPullRequest = new NewPullRequest(connectionData, parameters); this.newWatcher = new NewWatcher(connectionData, parameters); this.newMilestone = new NewMilestone(connectionData, parameters); + this.newCommitComment = new NewCommitComment(connectionData, parameters); } } diff --git a/packages/backend/src/apps/github/triggers/new-commit-comment.ts b/packages/backend/src/apps/github/triggers/new-commit-comment.ts new file mode 100644 index 00000000..b21e479b --- /dev/null +++ b/packages/backend/src/apps/github/triggers/new-commit-comment.ts @@ -0,0 +1,62 @@ +import { Octokit } from 'octokit'; +import { DateTime } from 'luxon'; +import { IJSONObject } from '@automatisch/types'; + +export default class NewCommitComment { + 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, + }; + } + + async run(startTime: Date) { + const iterator = await this.client.paginate.iterator(this.client.rest.repos.listCommitCommentsForRepo, this.options); + const newCommitComments = []; + + const startTimeDateObject = DateTime.fromJSDate(startTime); + + commitCommentIterator: + for await (const { data: commitComments } of iterator) { + for (const commitComment of commitComments) { + const createdAtDateObject = DateTime.fromISO(commitComment.created_at); + + if (createdAtDateObject < startTimeDateObject) { + break commitCommentIterator; + } + + newCommitComments.push(commitComment); + } + } + + return newCommitComments; + } + + async testRun() { + const options = { + ...this.options, + per_page: 1, + }; + + return (await this.client.rest.repos.listCommitCommentsForRepo(options)).data; + } +}