feat: add new commit trigger in GitHub
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
import ListRepos from './data/list-repos';
|
||||
import ListBranches from './data/list-branches';
|
||||
|
||||
export default class Data {
|
||||
listRepos: ListRepos;
|
||||
listBranches: ListBranches;
|
||||
|
||||
constructor(connectionData: IJSONObject) {
|
||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||
this.listRepos = new ListRepos(connectionData);
|
||||
this.listBranches = new ListBranches(connectionData, parameters);
|
||||
}
|
||||
}
|
||||
|
36
packages/backend/src/apps/github/data/list-branches.ts
Normal file
36
packages/backend/src/apps/github/data/list-branches.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import type { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class ListBranches {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
repo?: string;
|
||||
|
||||
constructor(connectionData: IJSONObject, parameters?: IJSONObject) {
|
||||
if (connectionData.accessToken) {
|
||||
this.client = new Octokit({
|
||||
auth: connectionData.accessToken as string,
|
||||
});
|
||||
}
|
||||
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
return {
|
||||
owner: this.repoOwner,
|
||||
repo: this.repo,
|
||||
};
|
||||
}
|
||||
|
||||
async run() {
|
||||
const branches = await this.client.paginate(this.client.rest.repos.listBranches, this.options);
|
||||
|
||||
return branches.map((branch) => ({
|
||||
value: branch.name,
|
||||
name: branch.name,
|
||||
}));
|
||||
}
|
||||
}
|
@@ -19,7 +19,7 @@ export default class Github implements IService {
|
||||
parameters: IJSONObject
|
||||
) {
|
||||
this.authenticationClient = new Authentication(appData, connectionData);
|
||||
this.data = new Data(connectionData);
|
||||
this.data = new Data(connectionData, parameters);
|
||||
this.triggers = new Triggers(connectionData, parameters);
|
||||
}
|
||||
}
|
||||
|
@@ -599,6 +599,68 @@
|
||||
"name": "Test trigger"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "New commit",
|
||||
"key": "newCommit",
|
||||
"interval": "15m",
|
||||
"description": "Triggers when a new commit 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "Head",
|
||||
"key": "head",
|
||||
"type": "dropdown",
|
||||
"description": "Branch to pull commits from. If unspecified, will use the repository's default branch (usually main or develop).",
|
||||
"required": false,
|
||||
"variables": false,
|
||||
"dependsOn": ["parameters.repo"],
|
||||
"source": {
|
||||
"type": "query",
|
||||
"name": "getData",
|
||||
"arguments": [
|
||||
{
|
||||
"name": "key",
|
||||
"value": "listBranches"
|
||||
},
|
||||
{
|
||||
"name": "parameters.repo",
|
||||
"value": "{parameters.repo}"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"key": "testStep",
|
||||
"name": "Test trigger"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import NewNotification from './triggers/new-notification';
|
||||
import NewPullRequest from './triggers/new-pull-request';
|
||||
import NewWatcher from './triggers/new-watcher';
|
||||
import NewMilestone from './triggers/new-milestone';
|
||||
import NewCommit from './triggers/new-commit';
|
||||
import NewCommitComment from './triggers/new-commit-comment';
|
||||
import NewLabel from './triggers/new-label';
|
||||
import NewCollaborator from './triggers/new-collaborator';
|
||||
@@ -19,6 +20,7 @@ export default class Triggers {
|
||||
newPullRequest: NewPullRequest;
|
||||
newWatcher: NewWatcher;
|
||||
newMilestone: NewMilestone;
|
||||
newCommit: NewCommit;
|
||||
newCommitComment: NewCommitComment;
|
||||
newLabel: NewLabel;
|
||||
newCollaborator: NewCollaborator;
|
||||
@@ -32,6 +34,7 @@ export default class Triggers {
|
||||
this.newPullRequest = new NewPullRequest(connectionData, parameters);
|
||||
this.newWatcher = new NewWatcher(connectionData, parameters);
|
||||
this.newMilestone = new NewMilestone(connectionData, parameters);
|
||||
this.newCommit = new NewCommit(connectionData, parameters);
|
||||
this.newCommitComment = new NewCommitComment(connectionData, parameters);
|
||||
this.newLabel = new NewLabel(connectionData, parameters);
|
||||
this.newCollaborator = new NewCollaborator(connectionData, parameters);
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewBranch {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -13,12 +15,7 @@ export default class NewBranch {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewCollaborator {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -13,12 +15,7 @@ export default class NewCollaborator {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -2,6 +2,8 @@ import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewCommitComment {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -14,12 +16,7 @@ export default class NewCommitComment {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
59
packages/backend/src/apps/github/triggers/new-commit.ts
Normal file
59
packages/backend/src/apps/github/triggers/new-commit.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewCommit {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
repo?: string;
|
||||
head?: string;
|
||||
|
||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||
if (connectionData.accessToken) {
|
||||
this.client = new Octokit({
|
||||
auth: connectionData.accessToken as string,
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.head) {
|
||||
this.head = parameters.head as string;
|
||||
}
|
||||
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
const options = {
|
||||
owner: this.repoOwner,
|
||||
repo: this.repo,
|
||||
};
|
||||
|
||||
if (this.head) {
|
||||
return {
|
||||
...options,
|
||||
sha: this.head,
|
||||
};
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
async run(startTime: Date) {
|
||||
const options = {
|
||||
...this.options,
|
||||
since: DateTime.fromJSDate(startTime).toISO(),
|
||||
};
|
||||
return await this.client.paginate(this.client.rest.repos.listCommits, options);
|
||||
}
|
||||
|
||||
async testRun() {
|
||||
const options = {
|
||||
...this.options,
|
||||
per_page: 1,
|
||||
};
|
||||
|
||||
return (await this.client.rest.repos.listCommits(options)).data;
|
||||
}
|
||||
}
|
@@ -1,6 +1,8 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewLabel {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -13,12 +15,7 @@ export default class NewLabel {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -2,6 +2,8 @@ import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewMilestone {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -14,12 +16,7 @@ export default class NewMilestone {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -2,6 +2,8 @@ import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewNotification {
|
||||
client?: Octokit;
|
||||
connectionData?: IJSONObject;
|
||||
@@ -19,12 +21,7 @@ export default class NewNotification {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get hasRepo() {
|
||||
|
@@ -2,6 +2,8 @@ import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewPullRequest {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -14,12 +16,7 @@ export default class NewPullRequest {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -2,6 +2,8 @@ import { Octokit } from 'octokit';
|
||||
import { DateTime } from 'luxon';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewRelease {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -14,12 +16,7 @@ export default class NewRelease {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = 'facebook' || owner;
|
||||
this.repo = 'react' || repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { Octokit } from 'octokit';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
import { assignOwnerAndRepo } from '../utils';
|
||||
|
||||
export default class NewWatcher {
|
||||
client?: Octokit;
|
||||
repoOwner?: string;
|
||||
@@ -13,12 +15,7 @@ export default class NewWatcher {
|
||||
});
|
||||
}
|
||||
|
||||
if (parameters?.repo) {
|
||||
const [owner, repo] = (parameters.repo as string).split('/');
|
||||
|
||||
this.repoOwner = owner;
|
||||
this.repo = repo;
|
||||
}
|
||||
assignOwnerAndRepo(this, parameters?.repo as string);
|
||||
}
|
||||
|
||||
get options() {
|
||||
|
9
packages/backend/src/apps/github/utils.ts
Normal file
9
packages/backend/src/apps/github/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export function assignOwnerAndRepo<T extends { repoOwner?: string; repo?: string; }>(object: T, repoFullName: string): T {
|
||||
if (object && repoFullName) {
|
||||
const [repoOwner, repo] = repoFullName.split('/');
|
||||
object.repoOwner = repoOwner;
|
||||
object.repo = repo;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
Reference in New Issue
Block a user