feat: Capture unhandled errors by restructuring apps

This commit is contained in:
Faruk AYDIN
2022-10-21 19:03:24 +02:00
parent 525472d3e0
commit 9a743fb4a8
16 changed files with 92 additions and 76 deletions

View File

@@ -1,12 +1,11 @@
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';
function getPathname($: IGlobalVariable) {
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo as string);
const { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string
);
if (repoOwner && repo) {
return `/repos/${repoOwner}/${repo}/issues`;
@@ -35,8 +34,8 @@ const newIssues = async ($: IGlobalVariable) => {
const response = await $.http.get(pathname, { params });
links = parseLinkHeader(response.headers.link);
if (response.integrationError) {
issues.error = response.integrationError;
if (response.httpError) {
issues.error = response.httpError;
return issues;
}
@@ -44,7 +43,8 @@ const newIssues = async ($: IGlobalVariable) => {
for (const issue of response.data) {
const issueId = issue.id;
if (issueId <= Number($.flow.lastInternalId) && !$.execution.testRun) return issues;
if (issueId <= Number($.flow.lastInternalId) && !$.execution.testRun)
return issues;
const dataItem = {
raw: issue,

View File

@@ -10,10 +10,12 @@ 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 { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string
);
const firstPagePathname = `/repos/${repoOwner}/${repo}/stargazers`;
const requestConfig = {
params: {
@@ -22,10 +24,13 @@ const fetchStargazers = async ($: IGlobalVariable) => {
headers: {
// needed to get `starred_at` time
Accept: 'application/vnd.github.star+json',
}
}
},
};
const firstPageResponse = await $.http.get<TResponseDataItem[]>(firstPagePathname, requestConfig);
const firstPageResponse = await $.http.get<TResponseDataItem[]>(
firstPagePathname,
requestConfig
);
const firstPageLinks = parseLinkHeader(firstPageResponse.headers.link);
// in case there is only single page to fetch
@@ -36,12 +41,15 @@ const fetchStargazers = async ($: IGlobalVariable) => {
};
do {
const response = await $.http.get<TResponseDataItem[]>(pathname, requestConfig);
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;
if (response.httpError) {
stargazers.error = response.httpError;
return stargazers;
}
@@ -50,7 +58,8 @@ const fetchStargazers = async ($: IGlobalVariable) => {
const { starred_at, user } = starEntry;
const timestamp = DateTime.fromISO(starred_at).toMillis();
if (timestamp <= Number($.flow.lastInternalId) && !$.execution.testRun) return stargazers;
if (timestamp <= Number($.flow.lastInternalId) && !$.execution.testRun)
return stargazers;
const dataItem = {
raw: user,
@@ -65,13 +74,15 @@ const fetchStargazers = async ($: IGlobalVariable) => {
} 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 (
Number(stargazerA.meta.internalId) - Number(stargazerB.meta.internalId)
);
});
return stargazers;