feat: add new pull request trigger in GitHub (#301)
* feat: add new pull request trigger in GitHub
This commit is contained in:
@@ -326,6 +326,45 @@
|
|||||||
"name": "Test trigger"
|
"name": "Test trigger"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "New pull request",
|
||||||
|
"key": "newPullRequest",
|
||||||
|
"interval": "15m",
|
||||||
|
"description": "Triggers when a new pull request 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -3,17 +3,20 @@ import NewRepository from './triggers/new-repository';
|
|||||||
import NewOrganization from './triggers/new-organization';
|
import NewOrganization from './triggers/new-organization';
|
||||||
import NewBranch from './triggers/new-branch';
|
import NewBranch from './triggers/new-branch';
|
||||||
import NewNotification from './triggers/new-notification';
|
import NewNotification from './triggers/new-notification';
|
||||||
|
import NewPullRequest from './triggers/new-pull-request';
|
||||||
|
|
||||||
export default class Triggers {
|
export default class Triggers {
|
||||||
newRepository: NewRepository;
|
newRepository: NewRepository;
|
||||||
newOrganization: NewOrganization;
|
newOrganization: NewOrganization;
|
||||||
newBranch: NewBranch;
|
newBranch: NewBranch;
|
||||||
newNotification: NewNotification;
|
newNotification: NewNotification;
|
||||||
|
newPullRequest: NewPullRequest;
|
||||||
|
|
||||||
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);
|
this.newBranch = new NewBranch(connectionData, parameters);
|
||||||
this.newNotification = new NewNotification(connectionData, parameters);
|
this.newNotification = new NewNotification(connectionData, parameters);
|
||||||
|
this.newPullRequest = new NewPullRequest(connectionData, parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -65,7 +65,7 @@ export default class NewNotification {
|
|||||||
|
|
||||||
async run(startTime: Date) {
|
async run(startTime: Date) {
|
||||||
const options = {
|
const options = {
|
||||||
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
|
since: DateTime.fromJSDate(startTime).toISO(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.hasRepo) {
|
if (this.hasRepo) {
|
||||||
|
@@ -0,0 +1,64 @@
|
|||||||
|
import { Octokit } from 'octokit';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
import { IJSONObject } from '@automatisch/types';
|
||||||
|
|
||||||
|
export default class NewPullRequest {
|
||||||
|
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,
|
||||||
|
sort: 'created' as const,
|
||||||
|
direction: 'desc' as const,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(startTime: Date) {
|
||||||
|
const iterator = await this.client.paginate.iterator(this.client.rest.pulls.list, this.options);
|
||||||
|
const newPullRequests = [];
|
||||||
|
|
||||||
|
const startTimeDateObject = DateTime.fromJSDate(startTime);
|
||||||
|
|
||||||
|
pullRequestIterator:
|
||||||
|
for await (const { data: pullRequests } of iterator) {
|
||||||
|
for (const pullRequest of pullRequests) {
|
||||||
|
const createdAtDateObject = DateTime.fromISO(pullRequest.created_at);
|
||||||
|
|
||||||
|
if (createdAtDateObject < startTimeDateObject) {
|
||||||
|
break pullRequestIterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
newPullRequests.push(pullRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPullRequests;
|
||||||
|
}
|
||||||
|
|
||||||
|
async testRun() {
|
||||||
|
const options = {
|
||||||
|
...this.options,
|
||||||
|
per_page: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
return (await this.client.rest.pulls.list(options)).data;
|
||||||
|
}
|
||||||
|
}
|
@@ -16,7 +16,7 @@ export default class NewRepository {
|
|||||||
|
|
||||||
async run(startTime: Date) {
|
async run(startTime: Date) {
|
||||||
const options = {
|
const options = {
|
||||||
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
|
since: DateTime.fromJSDate(startTime).toISO(),
|
||||||
};
|
};
|
||||||
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
|
return await this.client.paginate(this.client.rest.repos.listForAuthenticatedUser, options);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user