feat: add new pull request trigger in GitHub (#301)

* feat: add new pull request trigger in GitHub
This commit is contained in:
Ali BARIN
2022-04-23 01:14:54 +02:00
committed by GitHub
parent a623116204
commit af348714f8
5 changed files with 108 additions and 2 deletions

View File

@@ -65,7 +65,7 @@ export default class NewNotification {
async run(startTime: Date) {
const options = {
since: DateTime.fromJSDate(startTime).toLocaleString(DateTime.TIME_24_SIMPLE),
since: DateTime.fromJSDate(startTime).toISO(),
};
if (this.hasRepo) {

View File

@@ -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;
}
}

View File

@@ -16,7 +16,7 @@ export default class NewRepository {
async run(startTime: Date) {
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);
}