feat: add new branch trigger in GitHub
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
6bee6bafa8
commit
3551d12e7d
10
packages/backend/src/apps/github/data.ts
Normal file
10
packages/backend/src/apps/github/data.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
23
packages/backend/src/apps/github/data/list-repos.ts
Normal file
23
packages/backend/src/apps/github/data/list-repos.ts
Normal 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,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
@@ -6,10 +6,12 @@ import {
|
|||||||
} from '@automatisch/types';
|
} from '@automatisch/types';
|
||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
import Triggers from './triggers';
|
import Triggers from './triggers';
|
||||||
|
import Data from './data';
|
||||||
|
|
||||||
export default class Github implements IService {
|
export default class Github implements IService {
|
||||||
authenticationClient: IAuthentication;
|
authenticationClient: IAuthentication;
|
||||||
triggers: Triggers;
|
triggers: Triggers;
|
||||||
|
data: Data;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
appData: IApp,
|
appData: IApp,
|
||||||
@@ -17,6 +19,7 @@ export default class Github implements IService {
|
|||||||
parameters: IJSONObject
|
parameters: IJSONObject
|
||||||
) {
|
) {
|
||||||
this.authenticationClient = new Authentication(appData, connectionData);
|
this.authenticationClient = new Authentication(appData, connectionData);
|
||||||
|
this.data = new Data(connectionData);
|
||||||
this.triggers = new Triggers(connectionData, parameters);
|
this.triggers = new Triggers(connectionData, parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -247,6 +247,45 @@
|
|||||||
"name": "Test trigger"
|
"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"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,16 @@
|
|||||||
import { IJSONObject } from '@automatisch/types';
|
import { IJSONObject } from '@automatisch/types';
|
||||||
import NewRepository from './triggers/new-repository';
|
import NewRepository from './triggers/new-repository';
|
||||||
import NewOrganization from './triggers/new-organization';
|
import NewOrganization from './triggers/new-organization';
|
||||||
|
import NewBranch from './triggers/new-branch';
|
||||||
|
|
||||||
export default class Triggers {
|
export default class Triggers {
|
||||||
newRepository: NewRepository;
|
newRepository: NewRepository;
|
||||||
newOrganization: NewOrganization;
|
newOrganization: NewOrganization;
|
||||||
|
newBranch: NewBranch;
|
||||||
|
|
||||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||||
this.newRepository = new NewRepository(connectionData);
|
this.newRepository = new NewRepository(connectionData);
|
||||||
this.newOrganization = new NewOrganization(connectionData);
|
this.newOrganization = new NewOrganization(connectionData);
|
||||||
|
this.newBranch = new NewBranch(connectionData, parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
45
packages/backend/src/apps/github/triggers/new-branch.ts
Normal file
45
packages/backend/src/apps/github/triggers/new-branch.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@@ -5,30 +5,17 @@ import { IJSONObject } from '@automatisch/types';
|
|||||||
export default class NewRepository {
|
export default class NewRepository {
|
||||||
client?: Octokit;
|
client?: Octokit;
|
||||||
connectionData?: IJSONObject;
|
connectionData?: IJSONObject;
|
||||||
baseOptions = {
|
|
||||||
visibility: 'all' as const,
|
|
||||||
affiliation: 'owner,organization_member,collaborator',
|
|
||||||
sort: 'created' as const,
|
|
||||||
per_page: 100,
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor(connectionData: IJSONObject) {
|
constructor(connectionData: IJSONObject) {
|
||||||
if (
|
if (connectionData.accessToken) {
|
||||||
connectionData.consumerKey &&
|
|
||||||
connectionData.consumerSecret &&
|
|
||||||
connectionData.accessToken
|
|
||||||
) {
|
|
||||||
this.client = new Octokit({
|
this.client = new Octokit({
|
||||||
auth: connectionData.accessToken as string,
|
auth: connectionData.accessToken as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(startTime: Date) {
|
async run(startTime: Date) {
|
||||||
const options = {
|
const options = {
|
||||||
...this.baseOptions,
|
|
||||||
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
|
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
|
||||||
};
|
};
|
||||||
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
|
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
|
||||||
@@ -36,7 +23,6 @@ export default class NewRepository {
|
|||||||
|
|
||||||
async testRun() {
|
async testRun() {
|
||||||
const options = {
|
const options = {
|
||||||
...this.baseOptions,
|
|
||||||
per_page: 1,
|
per_page: 1,
|
||||||
};
|
};
|
||||||
const { data: repos } = await this.client.rest.repos.listForAuthenticatedUser(options);
|
const { data: repos } = await this.client.rest.repos.listForAuthenticatedUser(options);
|
||||||
|
Reference in New Issue
Block a user