feat: add new repository trigger in GitHub

This commit is contained in:
Ali BARIN
2022-04-18 22:39:21 +02:00
committed by Ömer Faruk Aydın
parent 4f7bef83ea
commit 96ede071cf
6 changed files with 287 additions and 10 deletions

View File

@@ -1,15 +1,22 @@
import Authentication from './authentication';
import {
IService,
IAuthentication,
IApp,
IJSONObject,
} from '@automatisch/types';
import Authentication from './authentication';
import Triggers from './triggers';
export default class Github implements IService {
authenticationClient: IAuthentication;
triggers: Triggers;
constructor(appData: IApp, connectionData: IJSONObject) {
constructor(
appData: IApp,
connectionData: IJSONObject,
parameters: IJSONObject
) {
this.authenticationClient = new Authentication(appData, connectionData);
this.triggers = new Triggers(connectionData, parameters);
}
}

View File

@@ -214,5 +214,23 @@
}
]
}
],
"triggers": [
{
"name": "New repository",
"key": "newRepository",
"interval": "15m",
"description": "Triggers when a new repository is created",
"substeps": [
{
"key": "chooseAccount",
"name": "Choose account"
},
{
"key": "testStep",
"name": "Test trigger"
}
]
}
]
}

View File

@@ -0,0 +1,10 @@
import { IJSONObject } from '@automatisch/types';
import NewRepository from './triggers/new-repository';
export default class Triggers {
newRepository: NewRepository;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.newRepository = new NewRepository(connectionData);
}
}

View File

@@ -0,0 +1,46 @@
import { Octokit } from 'octokit';
import { DateTime } from 'luxon';
import { IJSONObject } from '@automatisch/types';
export default class NewRepository {
client?: Octokit;
connectionData?: IJSONObject;
baseOptions = {
visibility: 'all' as const,
affiliation: 'owner,organization_member,collaborator',
sort: 'created' as const,
per_page: 100,
};
constructor(connectionData: IJSONObject) {
if (
connectionData.consumerKey &&
connectionData.consumerSecret &&
connectionData.accessToken
) {
this.client = new Octokit({
auth: connectionData.accessToken as string,
});
this.connectionData = connectionData;
}
}
async run(startTime: Date) {
const options = {
...this.baseOptions,
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
};
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
}
async testRun() {
const options = {
...this.baseOptions,
per_page: 1,
};
const { data: repos } = await this.client.rest.repos.listForAuthenticatedUser(options);
return repos;
}
}