diff --git a/packages/backend/src/apps/github/authentication.ts b/packages/backend/src/apps/github/authentication.ts index 190e54ed..490e180f 100644 --- a/packages/backend/src/apps/github/authentication.ts +++ b/packages/backend/src/apps/github/authentication.ts @@ -13,7 +13,11 @@ import { export default class Authentication implements IAuthentication { appData: IApp; connectionData: IJSONObject; - scopes: string[] = ['repo']; + scopes: string[] = [ + 'read:org', + 'repo', + 'user', + ]; client: { getWebFlowAuthorizationUrl: typeof getWebFlowAuthorizationUrl; exchangeWebFlowCode: typeof exchangeWebFlowCode; diff --git a/packages/backend/src/apps/github/info.json b/packages/backend/src/apps/github/info.json index 140d1f35..5ac59d66 100644 --- a/packages/backend/src/apps/github/info.json +++ b/packages/backend/src/apps/github/info.json @@ -231,6 +231,22 @@ "name": "Test trigger" } ] + }, + { + "name": "New organization", + "key": "newOrganization", + "interval": "15m", + "description": "Triggers when a new organization is created", + "substeps": [ + { + "key": "chooseAccount", + "name": "Choose account" + }, + { + "key": "testStep", + "name": "Test trigger" + } + ] } ] } diff --git a/packages/backend/src/apps/github/triggers.ts b/packages/backend/src/apps/github/triggers.ts index 2a41008b..313e26e4 100644 --- a/packages/backend/src/apps/github/triggers.ts +++ b/packages/backend/src/apps/github/triggers.ts @@ -1,10 +1,13 @@ import { IJSONObject } from '@automatisch/types'; import NewRepository from './triggers/new-repository'; +import NewOrganization from './triggers/new-organization'; export default class Triggers { newRepository: NewRepository; + newOrganization: NewOrganization; constructor(connectionData: IJSONObject, parameters: IJSONObject) { this.newRepository = new NewRepository(connectionData); + this.newOrganization = new NewOrganization(connectionData); } } diff --git a/packages/backend/src/apps/github/triggers/new-organization.ts b/packages/backend/src/apps/github/triggers/new-organization.ts new file mode 100644 index 00000000..da5b10f7 --- /dev/null +++ b/packages/backend/src/apps/github/triggers/new-organization.ts @@ -0,0 +1,32 @@ +import { Octokit } from 'octokit'; +import { IJSONObject } from '@automatisch/types'; + +export default class NewOrganization { + client?: Octokit; + baseOptions = { + per_page: 100, + }; + + constructor(connectionData: IJSONObject) { + if ( + connectionData.consumerKey && + connectionData.consumerSecret && + connectionData.accessToken + ) { + this.client = new Octokit({ + auth: connectionData.accessToken as string, + }); + } + } + + async run() { + // TODO: implement pagination on undated entires + return await this.client.paginate(this.client.rest.orgs.listForAuthenticatedUser); + } + + async testRun() { + const { data: orgs } = await this.client.rest.orgs.listForAuthenticatedUser({ per_page: 1 }); + + return orgs; + } +}