refactor: Restructure apps with new data pushing logic

This commit is contained in:
Faruk AYDIN
2022-10-22 19:29:02 +02:00
parent bcff9f5a9e
commit a56135ca57
19 changed files with 115 additions and 136 deletions

View File

@@ -1,11 +1,8 @@
import {
IGlobalVariable,
ITriggerOutput,
} from '@automatisch/types';
import { IGlobalVariable, ITriggerOutput } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
const fetchWatchers = async ($: IGlobalVariable) => {
const newWatchers = async ($: IGlobalVariable) => {
const repoParameter = $.step.parameters.repo as string;
if (!repoParameter) throw new Error('A repo must be set!');
@@ -15,9 +12,9 @@ const fetchWatchers = async ($: IGlobalVariable) => {
const firstPagePathname = `/repos/${repoOwner}/${repo}/subscribers`;
const requestConfig = {
params: {
per_page: 100
per_page: 100,
},
}
};
const firstPageResponse = await $.http.get(firstPagePathname, requestConfig);
const firstPageLinks = parseLinkHeader(firstPageResponse.headers.link);
@@ -25,20 +22,11 @@ const fetchWatchers = async ($: IGlobalVariable) => {
// in case there is only single page to fetch
let pathname = firstPageLinks.last?.uri || firstPagePathname;
const watchers: ITriggerOutput = {
data: [],
};
do {
const response = await $.http.get(pathname, requestConfig);
const links = parseLinkHeader(response.headers.link);
pathname = links.prev?.uri;
if (response.integrationError) {
watchers.error = response.integrationError;
return watchers;
}
if (response.data.length) {
// to iterate reverse-chronologically
response.data.reverse();
@@ -46,7 +34,8 @@ const fetchWatchers = async ($: IGlobalVariable) => {
for (const watcher of response.data) {
const watcherId = watcher.id.toString();
if ($.flow.isAlreadyProcessed(watcherId) && !$.execution.testRun) return watchers;
if ($.flow.isAlreadyProcessed(watcherId) && !$.execution.testRun)
return;
const dataItem = {
raw: watcher,
@@ -55,21 +44,10 @@ const fetchWatchers = async ($: IGlobalVariable) => {
},
};
watchers.data.push(dataItem);
$.pushTriggerItem(dataItem);
}
}
} while (pathname && !$.execution.testRun === false);
return watchers;
}
const newWatchers = async ($: IGlobalVariable) => {
const watchers = await fetchWatchers($);
// to process chronologically
watchers.data.reverse();
return watchers;
};
export default newWatchers;