From 024bb731d4eac33bd591ded0f81273a3d4317704 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 22 Apr 2022 18:51:00 +0200 Subject: [PATCH] feat: add new collaborator trigger in GitHub --- packages/backend/src/apps/github/info.json | 39 +++++++++++++++ packages/backend/src/apps/github/triggers.ts | 3 ++ .../apps/github/triggers/new-collaborator.ts | 47 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 packages/backend/src/apps/github/triggers/new-collaborator.ts diff --git a/packages/backend/src/apps/github/info.json b/packages/backend/src/apps/github/info.json index fb70cd97..215dd9db 100644 --- a/packages/backend/src/apps/github/info.json +++ b/packages/backend/src/apps/github/info.json @@ -521,6 +521,45 @@ "name": "Test trigger" } ] + }, + { + "name": "New collaborator", + "key": "newCollaborator", + "interval": "15m", + "description": "Triggers when a new collaborator is added to a repo", + "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 d11e0860..e8246764 100644 --- a/packages/backend/src/apps/github/triggers.ts +++ b/packages/backend/src/apps/github/triggers.ts @@ -8,6 +8,7 @@ import NewWatcher from './triggers/new-watcher'; import NewMilestone from './triggers/new-milestone'; import NewCommitComment from './triggers/new-commit-comment'; import NewLabel from './triggers/new-label'; +import NewCollaborator from './triggers/new-collaborator'; export default class Triggers { newRepository: NewRepository; @@ -19,6 +20,7 @@ export default class Triggers { newMilestone: NewMilestone; newCommitComment: NewCommitComment; newLabel: NewLabel; + newCollaborator: NewCollaborator; constructor(connectionData: IJSONObject, parameters: IJSONObject) { this.newRepository = new NewRepository(connectionData); @@ -30,5 +32,6 @@ export default class Triggers { this.newMilestone = new NewMilestone(connectionData, parameters); this.newCommitComment = new NewCommitComment(connectionData, parameters); this.newLabel = new NewLabel(connectionData, parameters); + this.newCollaborator = new NewCollaborator(connectionData, parameters); } } diff --git a/packages/backend/src/apps/github/triggers/new-collaborator.ts b/packages/backend/src/apps/github/triggers/new-collaborator.ts new file mode 100644 index 00000000..b6b5888a --- /dev/null +++ b/packages/backend/src/apps/github/triggers/new-collaborator.ts @@ -0,0 +1,47 @@ +import { Octokit } from 'octokit'; +import { IJSONObject } from '@automatisch/types'; + +export default class NewCollaborator { + 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() { + // TODO: implement pagination on undated entries + return await this.client.paginate( + this.client.rest.repos.listCollaborators, + this.options + ); + } + + async testRun() { + const options = { + ...this.options, + per_page: 1, + }; + + return (await this.client.rest.repos.listCollaborators(options)).data; + } +}