feat: add new branch trigger in GitHub

This commit is contained in:
Ali BARIN
2022-04-20 21:56:01 +02:00
committed by Ömer Faruk Aydın
parent 6bee6bafa8
commit 3551d12e7d
7 changed files with 124 additions and 15 deletions

View File

@@ -0,0 +1,10 @@
import { IJSONObject } from '@automatisch/types';
import ListRepos from './data/list-repos';
export default class Data {
listRepos: ListRepos;
constructor(connectionData: IJSONObject) {
this.listRepos = new ListRepos(connectionData);
}
}

View File

@@ -0,0 +1,23 @@
import { Octokit } from 'octokit';
import type { IJSONObject } from '@automatisch/types';
export default class ListRepos {
client?: Octokit;
constructor(connectionData: IJSONObject) {
if (connectionData.accessToken) {
this.client = new Octokit({
auth: connectionData.accessToken as string,
});
}
}
async run() {
const repos = await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser);
return repos.map((repo) => ({
value: repo.full_name,
name: repo.full_name,
}));
}
}

View File

@@ -6,10 +6,12 @@ import {
} from '@automatisch/types';
import Authentication from './authentication';
import Triggers from './triggers';
import Data from './data';
export default class Github implements IService {
authenticationClient: IAuthentication;
triggers: Triggers;
data: Data;
constructor(
appData: IApp,
@@ -17,6 +19,7 @@ export default class Github implements IService {
parameters: IJSONObject
) {
this.authenticationClient = new Authentication(appData, connectionData);
this.data = new Data(connectionData);
this.triggers = new Triggers(connectionData, parameters);
}
}

View File

@@ -247,6 +247,45 @@
"name": "Test trigger"
}
]
},
{
"name": "New branch",
"key": "newBranch",
"interval": "15m",
"description": "Triggers when a new branch 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"
}
]
}
]
}

View File

@@ -1,13 +1,16 @@
import { IJSONObject } from '@automatisch/types';
import NewRepository from './triggers/new-repository';
import NewOrganization from './triggers/new-organization';
import NewBranch from './triggers/new-branch';
export default class Triggers {
newRepository: NewRepository;
newOrganization: NewOrganization;
newBranch: NewBranch;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.newRepository = new NewRepository(connectionData);
this.newOrganization = new NewOrganization(connectionData);
this.newBranch = new NewBranch(connectionData, parameters);
}
}

View File

@@ -0,0 +1,45 @@
import { Octokit } from 'octokit';
import { IJSONObject } from '@automatisch/types';
export default class NewBranch {
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.repos.listBranches, this.options);
}
async testRun() {
const options = {
...this.options,
per_page: 1,
};
const { data: branches } = await this.client.rest.repos.listBranches(options);
return branches;
}
}

View File

@@ -5,30 +5,17 @@ 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
) {
if (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);
@@ -36,7 +23,6 @@ export default class NewRepository {
async testRun() {
const options = {
...this.baseOptions,
per_page: 1,
};
const { data: repos } = await this.client.rest.repos.listForAuthenticatedUser(options);