refactor: Use global output with github stargazers

This commit is contained in:
Faruk AYDIN
2022-10-21 19:30:33 +02:00
parent 0f63f6d453
commit 59b9c66a91
4 changed files with 46 additions and 76 deletions

View File

@@ -9,7 +9,7 @@ export default defineTrigger({
substeps: [ substeps: [
{ {
key: 'chooseConnection', key: 'chooseConnection',
name: 'Choose connection' name: 'Choose connection',
}, },
{ {
key: 'chooseTrigger', key: 'chooseTrigger',
@@ -27,10 +27,10 @@ export default defineTrigger({
arguments: [ arguments: [
{ {
name: 'key', name: 'key',
value: 'listRepos' value: 'listRepos',
} },
] ],
} },
}, },
{ {
label: 'Which types of issues should this trigger on?', label: 'Which types of issues should this trigger on?',
@@ -43,25 +43,25 @@ export default defineTrigger({
options: [ options: [
{ {
label: 'Any issue you can see', label: 'Any issue you can see',
value: 'all' value: 'all',
}, },
{ {
label: 'Only issues assigned to you', label: 'Only issues assigned to you',
value: 'assigned' value: 'assigned',
}, },
{ {
label: 'Only issues created by you', label: 'Only issues created by you',
value: 'created' value: 'created',
}, },
{ {
label: `Only issues you're mentioned in`, label: `Only issues you're mentioned in`,
value: 'mentioned' value: 'mentioned',
}, },
{ {
label: `Only issues you're subscribed to`, label: `Only issues you're subscribed to`,
value: 'subscribed' value: 'subscribed',
} },
] ],
}, },
{ {
label: 'Label', label: 'Label',
@@ -77,24 +77,24 @@ export default defineTrigger({
arguments: [ arguments: [
{ {
name: 'key', name: 'key',
value: 'listLabels' value: 'listLabels',
}, },
{ {
name: 'parameters.repo', name: 'parameters.repo',
value: '{parameters.repo}' value: '{parameters.repo}',
} },
] ],
} },
} },
] ],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
async run($) { async run($) {
return await newIssues($); await newIssues($);
}, },
}); });

View File

@@ -1,4 +1,4 @@
import { IGlobalVariable, ITriggerOutput } from '@automatisch/types'; import { IGlobalVariable } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo'; import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link'; import parseLinkHeader from '../../../../helpers/parse-header-link';
@@ -25,26 +25,17 @@ const newIssues = async ($: IGlobalVariable) => {
per_page: 100, per_page: 100,
}; };
const issues: ITriggerOutput = {
data: [],
};
let links; let links;
do { do {
const response = await $.http.get(pathname, { params }); const response = await $.http.get(pathname, { params });
links = parseLinkHeader(response.headers.link); links = parseLinkHeader(response.headers.link);
if (response.httpError) {
issues.error = response.httpError;
return issues;
}
if (response.data.length) { if (response.data.length) {
for (const issue of response.data) { for (const issue of response.data) {
const issueId = issue.id; const issueId = issue.id;
if (issueId <= Number($.flow.lastInternalId) && !$.execution.testRun) if (issueId <= Number($.flow.lastInternalId) && !$.execution.testRun)
return issues; return;
const dataItem = { const dataItem = {
raw: issue, raw: issue,
@@ -53,12 +44,10 @@ const newIssues = async ($: IGlobalVariable) => {
}, },
}; };
issues.data.push(dataItem); $.triggerOutput.data.push(dataItem);
} }
} }
} while (links.next && !$.execution.testRun); } while (links.next && !$.execution.testRun);
return issues;
}; };
export default newIssues; export default newIssues;

View File

@@ -9,7 +9,7 @@ export default defineTrigger({
substeps: [ substeps: [
{ {
key: 'chooseConnection', key: 'chooseConnection',
name: 'Choose connection' name: 'Choose connection',
}, },
{ {
key: 'chooseTrigger', key: 'chooseTrigger',
@@ -27,20 +27,28 @@ export default defineTrigger({
arguments: [ arguments: [
{ {
name: 'key', name: 'key',
value: 'listRepos' value: 'listRepos',
}
]
}
}, },
] ],
},
},
],
}, },
{ {
key: 'testStep', key: 'testStep',
name: 'Test trigger' name: 'Test trigger',
} },
], ],
async run($) { async run($) {
return await newStargazers($); await newStargazers($);
},
sort($) {
$.triggerOutput.data.sort((stargazerA, stargazerB) => {
return (
Number(stargazerA.meta.internalId) - Number(stargazerB.meta.internalId)
);
});
}, },
}); });

View File

@@ -1,9 +1,5 @@
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { import { IGlobalVariable, IJSONObject } from '@automatisch/types';
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo'; import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link'; import parseLinkHeader from '../../../../helpers/parse-header-link';
@@ -12,7 +8,7 @@ type TResponseDataItem = {
user: IJSONObject; user: IJSONObject;
}; };
const fetchStargazers = async ($: IGlobalVariable) => { const newStargazers = async ($: IGlobalVariable) => {
const { repoOwner, repo } = getRepoOwnerAndRepo( const { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string $.step.parameters.repo as string
); );
@@ -36,10 +32,6 @@ const fetchStargazers = async ($: IGlobalVariable) => {
// in case there is only single page to fetch // in case there is only single page to fetch
let pathname = firstPageLinks.last?.uri || firstPagePathname; let pathname = firstPageLinks.last?.uri || firstPagePathname;
const stargazers: ITriggerOutput = {
data: [],
};
do { do {
const response = await $.http.get<TResponseDataItem[]>( const response = await $.http.get<TResponseDataItem[]>(
pathname, pathname,
@@ -48,18 +40,13 @@ const fetchStargazers = async ($: IGlobalVariable) => {
const links = parseLinkHeader(response.headers.link); const links = parseLinkHeader(response.headers.link);
pathname = links.prev?.uri; pathname = links.prev?.uri;
if (response.httpError) {
stargazers.error = response.httpError;
return stargazers;
}
if (response.data.length) { if (response.data.length) {
for (const starEntry of response.data) { for (const starEntry of response.data) {
const { starred_at, user } = starEntry; const { starred_at, user } = starEntry;
const timestamp = DateTime.fromISO(starred_at).toMillis(); const timestamp = DateTime.fromISO(starred_at).toMillis();
if (timestamp <= Number($.flow.lastInternalId) && !$.execution.testRun) if (timestamp <= Number($.flow.lastInternalId) && !$.execution.testRun)
return stargazers; return;
const dataItem = { const dataItem = {
raw: user, raw: user,
@@ -68,24 +55,10 @@ const fetchStargazers = async ($: IGlobalVariable) => {
}, },
}; };
stargazers.data.push(dataItem); $.triggerOutput.data.push(dataItem);
} }
} }
} while (pathname && !$.execution.testRun); } 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; export default newStargazers;