diff --git a/packages/backend/src/apps/github/info.json b/packages/backend/src/apps/github/info.json index 7ad8935b..fb70cd97 100644 --- a/packages/backend/src/apps/github/info.json +++ b/packages/backend/src/apps/github/info.json @@ -482,6 +482,45 @@ "name": "Test trigger" } ] + }, + { + "name": "New label", + "key": "newLabel", + "interval": "15m", + "description": "Triggers when a new label 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 c31cb341..d11e0860 100644 --- a/packages/backend/src/apps/github/triggers.ts +++ b/packages/backend/src/apps/github/triggers.ts @@ -7,6 +7,7 @@ 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'; +import NewLabel from './triggers/new-label'; export default class Triggers { newRepository: NewRepository; @@ -17,6 +18,7 @@ export default class Triggers { newWatcher: NewWatcher; newMilestone: NewMilestone; newCommitComment: NewCommitComment; + newLabel: NewLabel; constructor(connectionData: IJSONObject, parameters: IJSONObject) { this.newRepository = new NewRepository(connectionData); @@ -27,5 +29,6 @@ export default class Triggers { this.newWatcher = new NewWatcher(connectionData, parameters); this.newMilestone = new NewMilestone(connectionData, parameters); this.newCommitComment = new NewCommitComment(connectionData, parameters); + this.newLabel = new NewLabel(connectionData, parameters); } } diff --git a/packages/backend/src/apps/github/triggers/new-label.ts b/packages/backend/src/apps/github/triggers/new-label.ts new file mode 100644 index 00000000..21fa61f8 --- /dev/null +++ b/packages/backend/src/apps/github/triggers/new-label.ts @@ -0,0 +1,47 @@ +import { Octokit } from 'octokit'; +import { IJSONObject } from '@automatisch/types'; + +export default class NewLabel { + 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 entires + return await this.client.paginate( + this.client.rest.issues.listLabelsForRepo, + this.options + ); + } + + async testRun() { + const options = { + ...this.options, + per_page: 1, + }; + + return (await this.client.rest.issues.listLabelsForRepo(options)).data; + } +}