Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labeler general refactoring (ver. 4) #691

Merged
merged 6 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
782 changes: 610 additions & 172 deletions dist/index.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/api/get-changed-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import {ClientType} from './types';

export const getChangedFiles = async (
client: ClientType,
prNumber: number
): Promise<string[]> => {
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});

const listFilesResponse = await client.paginate(listFilesOptions);
const changedFiles = listFilesResponse.map((f: any) => f.filename);

core.debug('found changed files:');
for (const file of changedFiles) {
core.debug(' ' + file);
}

return changedFiles;
};
38 changes: 38 additions & 0 deletions src/api/get-changed-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import {getChangedFiles} from './get-changed-files';
import {ClientType} from './types';

export async function* getPullRequests(
client: ClientType,
prNumbers: number[]
) {
for (const prNumber of prNumbers) {
core.debug(`looking for pr #${prNumber}`);
let prData: any;
try {
const result = await client.rest.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
prData = result.data;
} catch (error: any) {
core.warning(`Could not find pull request #${prNumber}, skipping`);
continue;
}

core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber);
if (!changedFiles.length) {
core.warning(`Pull request #${prNumber} has no changed files, skipping`);
continue;
}

yield {
data: prData,
number: prNumber,
changedFiles
};
}
}
16 changes: 16 additions & 0 deletions src/api/get-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as github from '@actions/github';
import {ClientType} from './types';

export const getContent = async (
client: ClientType,
repoPath: string
): Promise<string> => {
const response: any = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha
});

return Buffer.from(response.data.content, response.data.encoding).toString();
};
70 changes: 70 additions & 0 deletions src/api/get-label-globs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as core from '@actions/core';
import * as yaml from 'js-yaml';
import fs from 'fs';
import {ClientType} from './types';
import {getContent} from './get-content';

export interface MatchConfig {
all?: string[];
any?: string[];
}

export type StringOrMatchConfig = string | MatchConfig;

export const getLabelGlobs = (
client: ClientType,
configurationPath: string
): Promise<Map<string, StringOrMatchConfig[]>> =>
Promise.resolve()
.then(() => {
if (!fs.existsSync(configurationPath)) {
core.info(
`The configuration file (path: ${configurationPath}) was not found locally, fetching via the api`
);

return getContent(client, configurationPath);
}

core.info(
`The configuration file (path: ${configurationPath}) was found locally, reading from the file`
);

return fs.readFileSync(configurationPath, {
encoding: 'utf8'
});
})
.catch(error => {
if (error.name == 'HttpError' || error.name == 'NotFound') {
core.warning(
`The config file was not found at ${configurationPath}. Make sure it exists and that this action has the correct access rights.`
);
}
return Promise.reject(error);
})
.then(configuration => {
// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
const configObject: any = yaml.load(configuration);

// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
return getLabelGlobMapFromObject(configObject);
});

function getLabelGlobMapFromObject(
configObject: any
): Map<string, StringOrMatchConfig[]> {
const labelGlobs: Map<string, StringOrMatchConfig[]> = new Map();

for (const label in configObject) {
if (typeof configObject[label] === 'string') {
labelGlobs.set(label, [configObject[label]]);
} else if (configObject[label] instanceof Array) {
labelGlobs.set(label, configObject[label]);
} else {
throw Error(
`found unexpected type for label ${label} (should be string or array of globs)`
);
}
}

return labelGlobs;
}
6 changes: 6 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './get-changed-files';
export * from './get-changed-pull-requests';
export * from './get-content';
export * from './get-label-globs';
export * from './set-labels';
export * from './types';
15 changes: 15 additions & 0 deletions src/api/set-labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as github from '@actions/github';
import {ClientType} from './types';

export const setLabels = async (
client: ClientType,
prNumber: number,
labels: string[]
) => {
await client.rest.issues.setLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});
};
2 changes: 2 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as github from '@actions/github';
export type ClientType = ReturnType<typeof github.getOctokit>;
10 changes: 10 additions & 0 deletions src/get-inputs/get-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as core from '@actions/core';
import {getPrNumbers} from './get-pr-numbers';

export const getInputs = () => ({
token: core.getInput('repo-token'),
configPath: core.getInput('configuration-path', {required: true}),
syncLabels: !!core.getInput('sync-labels'),
dot: core.getBooleanInput('dot'),
prNumbers: getPrNumbers()
});
28 changes: 28 additions & 0 deletions src/get-inputs/get-pr-numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as core from '@actions/core';
import * as github from '@actions/github';

const getPrNumberFromContext = () =>
github.context.payload.pull_request?.number;

export const getPrNumbers = (): number[] => {
const prInput = core.getMultilineInput('pr-number');

if (!prInput?.length) {
return [getPrNumberFromContext()].filter(Boolean) as number[];
}

const result: number[] = [];

for (const line of prInput) {
const prNumber = parseInt(line, 10);

if (isNaN(prNumber) && prNumber <= 0) {
core.warning(`'${prNumber}' is not a valid pull request number`);
continue;
}

result.push(prNumber);
}

return result;
};
1 change: 1 addition & 0 deletions src/get-inputs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-inputs';
Loading