Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
feat(files): handle all files (not only the 100st) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdelgatte committed Aug 27, 2019
1 parent b981b9a commit 5fb1db2
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { IssuesAddLabelsParams, IssuesAddLabelsResponseItem, IssuesListLabelsOnIssueParams, IssuesListLabelsOnIssueResponse, PullsListFilesParams, PullsListFilesResponse, PullsListFilesResponseItem, Response } from '@octokit/rest';
import { Toolkit, ToolkitOptions } from 'actions-toolkit';
import {
IssuesAddLabelsParams,
IssuesAddLabelsResponseItem,
IssuesListLabelsOnIssueParams,
IssuesListLabelsOnIssueResponse,
PullsListFilesParams,
PullsListFilesResponse,
PullsListFilesResponseItem,
Response
} from '@octokit/rest';
import {Toolkit, ToolkitOptions} from 'actions-toolkit';
// tslint:disable-next-line:no-submodule-imports
import { WebhookPayloadWithRepository } from 'actions-toolkit/lib/context';
import {WebhookPayloadWithRepository} from 'actions-toolkit/lib/context';
// tslint:disable-next-line:no-submodule-imports
import { Exit } from 'actions-toolkit/lib/exit';
import {Exit} from 'actions-toolkit/lib/exit';
// tslint:disable-next-line:no-submodule-imports
import { GitHub } from 'actions-toolkit/lib/github';
import { LoggerFunc, Signale } from 'signale';
import { Filter, Repository } from './types';
import { buildIssueRemoveLabelParams, filterConfiguredIssueLabels, intersectLabels, processListFilesResponses } from './utils';
import {GitHub} from 'actions-toolkit/lib/github';
import {LoggerFunc, Signale} from 'signale';
import {Filter, Repository} from './types';
import {
buildIssueRemoveLabelParams,
filterConfiguredIssueLabels,
intersectLabels,
processListFilesResponses
} from './utils';

const LOGO: string = `
██████╗ ███████╗ ██████╗ █████╗ ████████╗██╗ ██╗██╗ ██████╗ ███╗ ██╗
Expand Down Expand Up @@ -42,19 +56,19 @@ const findRepositoryInformation = (gitHubEventPath: string, log: LoggerFunc & Si
const findIssueLabels = (issuesListLabelsOnIssueParams: IssuesListLabelsOnIssueParams, issues, filters: Filter[]): Promise<string[]> => {
// Find issue labels that are configured in .github/label-pr.yml
return issues.listLabelsOnIssue(issuesListLabelsOnIssueParams)
.then(({ data: labels }: Response<IssuesListLabelsOnIssueResponse>) => labels.reduce((acc, label) => acc.concat(label.name), []))
.then(({data: labels}: Response<IssuesListLabelsOnIssueResponse>) => labels.reduce((acc, label) => acc.concat(label.name), []))
.then(issueLabels => filterConfiguredIssueLabels(issueLabels, filters));
};

// Remove provided labels
const removeIssueLabels = (labels: string[], { log, exit }: Toolkit, repository: Repository, issues): void => {
const removeIssueLabels = (labels: string[], {log, exit}: Toolkit, repository: Repository, issues): void => {
log.info('Labels to remove: ', labels);
buildIssueRemoveLabelParams(repository, labels)
.forEach(value => issues.removeLabel(value).catch(reason => exit.failure(reason)));
};

// Build labels to add
const getLabelsToAdd = (labels: string[], issueLabels: string[], { log, exit }: Toolkit): string[] => {
const getLabelsToAdd = (labels: string[], issueLabels: string[], {log, exit}: Toolkit): string[] => {
const labelsToAdd: string[] = intersectLabels(labels, issueLabels);
log.info('Labels to add: ', labelsToAdd);
if (labelsToAdd.length === 0) {
Expand All @@ -63,6 +77,21 @@ const getLabelsToAdd = (labels: string[], issueLabels: string[], { log, exit }:
return labelsToAdd;
};

// Fetch all files (by recursively calling with page and per_page parameters)
const fetchAllFiles = (listFiles, log, params: PullsListFilesParams, per_page: number, page: number): Promise<PullsListFilesResponseItem[]> => {
log.info(`Listing files (page: ${page} | per_page: ${per_page})...`);
return listFiles({per_page, page, ...params})
.then((response: Response<PullsListFilesResponse>) => {
// If there may be other files to fetch
log.info(`Loaded ${response.data.length} files`);
let pullsListFilesResponseItems: PullsListFilesResponse = response.data;
if (pullsListFilesResponseItems.length >= per_page) {
return fetchAllFiles(listFiles, log, params, per_page, page + 1).then(value => value.concat(pullsListFilesResponseItems));
}
return pullsListFilesResponseItems;
});
};

Toolkit.run(async (toolkit: Toolkit) => {
toolkit.log.info('Open sourced by\n' + LOGO);

Expand All @@ -73,25 +102,24 @@ Toolkit.run(async (toolkit: Toolkit) => {
if (!process.env.GITHUB_EVENT_PATH) {
toolkit.exit.failure('Process env GITHUB_EVENT_PATH is undefined');
} else {
const { owner, issue_number, repo }: IssuesListLabelsOnIssueParams = findRepositoryInformation(process.env.GITHUB_EVENT_PATH, toolkit.log, toolkit.exit);
const { pulls: { listFiles }, issues }: GitHub = toolkit.github;
const {owner, issue_number, repo}: IssuesListLabelsOnIssueParams = findRepositoryInformation(process.env.GITHUB_EVENT_PATH, toolkit.log, toolkit.exit);
const {pulls: {listFiles}, issues}: GitHub = toolkit.github;

// First, we need to retrieve the existing issue labels and filter them over the configured one in config file
const issueLabels: string[] = await findIssueLabels({ issue_number, owner, repo }, issues, filters);
const issueLabels: string[] = await findIssueLabels({issue_number, owner, repo}, issues, filters);

const params: PullsListFilesParams = { owner, pull_number: issue_number, repo };
const params: PullsListFilesParams = {owner, pull_number: issue_number, repo};

await listFiles(params)
.then((response: Response<PullsListFilesResponse>) => response.data)
await fetchAllFiles(listFiles, toolkit.log, params, 100, 1)
.then((files: PullsListFilesResponseItem[]) => {
toolkit.log.info('Checking files...', files.reduce((acc: string[], file: PullsListFilesResponseItem) => acc.concat(file.filename), []));
return files;
})
.then((files: PullsListFilesResponseItem[]) => processListFilesResponses(files, filters))
.then((eligibleFilters: Filter[]) => eligibleFilters.reduce((acc: string[], eligibleFilter: Filter) => acc.concat(eligibleFilter.labels), []))
.then((labels: string[]) => {
removeIssueLabels(intersectLabels(issueLabels, labels), toolkit, { owner, issue_number, repo }, issues);
return { issue_number, labels: getLabelsToAdd(labels, issueLabels, toolkit), owner, repo };
removeIssueLabels(intersectLabels(issueLabels, labels), toolkit, {owner, issue_number, repo}, issues);
return {issue_number, labels: getLabelsToAdd(labels, issueLabels, toolkit), owner, repo};
}
)
.then((addLabelsParams: IssuesAddLabelsParams) => issues.addLabels(addLabelsParams))
Expand Down

0 comments on commit 5fb1db2

Please sign in to comment.