feat(github): add new stargazer trigger

This commit is contained in:
Ali BARIN
2022-10-19 22:56:00 +02:00
parent 486e817a40
commit e4dd7e454a
2 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newStargazers from './new-stargazers';
export default defineTrigger({
name: 'New stargazers',
key: 'newStargazers',
pollInterval: 15,
description: 'Triggers when a user stars a repository',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection'
},
{
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'
}
],
async run($) {
return await newStargazers($);
},
});

View File

@@ -0,0 +1,80 @@
import { DateTime } from 'luxon';
import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
type TResponseDataItem = {
starred_at: string;
user: IJSONObject;
}
const fetchStargazers = async ($: IGlobalVariable) => {
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo as string);
const firstPagePathname = `/repos/${repoOwner}/${repo}/stargazers`;
const requestConfig = {
params: {
per_page: 100,
},
headers: {
// needed to get `starred_at` time
Accept: 'application/vnd.github.star+json',
}
}
const firstPageResponse = await $.http.get<TResponseDataItem[]>(firstPagePathname, requestConfig);
const firstPageLinks = parseLinkHeader(firstPageResponse.headers.link);
// in case there is only single page to fetch
let pathname = firstPageLinks.last?.uri || firstPagePathname;
const stargazers: ITriggerOutput = {
data: [],
};
do {
const response = await $.http.get<TResponseDataItem[]>(pathname, requestConfig);
const links = parseLinkHeader(response.headers.link);
pathname = links.prev?.uri;
if (response.integrationError) {
stargazers.error = response.integrationError;
return stargazers;
}
if (response.data.length) {
for (const starEntry of response.data) {
const { starred_at, user } = starEntry;
const timestamp = DateTime.fromISO(starred_at).toMillis();
if (timestamp <= Number($.flow.lastInternalId) && !$.execution.testRun) return stargazers;
const dataItem = {
raw: user,
meta: {
internalId: timestamp.toString(),
},
};
stargazers.data.push(dataItem);
}
}
} while (pathname && !$.execution.testRun);
return stargazers;
}
const newStargazers = async ($: IGlobalVariable) => {
const stargazers = await fetchStargazers($);
stargazers.data.sort((stargazerA, stargazerB) => {
return Number(stargazerA.meta.internalId) - Number(stargazerB.meta.internalId);
});
return stargazers;
};
export default newStargazers;