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

Improvements. #22

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 15 additions & 15 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import * as github from '@actions/github';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import { minimatch } from 'minimatch';

import { Octokit } from '@octokit/rest';

const context = github.context;

interface LabelConfig {
Expand All @@ -22,13 +23,15 @@ interface LabelConfig {
description?: string;
}

async function getChangedFiles(octokit: any, owner: string, repo: string, prNumber: number): Promise<string[]> {
async function getChangedFiles(octokit: Octokit, owner: string, repo: string, prNumber: number): Promise<string[]> {
const { data: files } = await octokit.rest.pulls.listFiles({
owner,
repo,
pull_number: prNumber,
});
return files.map((file: any) => file.filename);
const filenames = files.map((file: any) => file.filename);
core.debug(`Changed files: ${filenames.join(', ')}`);
return filenames;
}

function parseConfigFile(filePath: string): Array<LabelConfig> {
Expand All @@ -43,10 +46,12 @@ function parseConfigFile(filePath: string): Array<LabelConfig> {
}

if (typeof parsedData === 'object' && parsedData !== null) {
return Object.entries(parsedData).map(([label, patterns]) => ({
label,
match: patterns as string[],
}));
return Object.entries(parsedData).map(([label, patterns]) => {
if (!Array.isArray(patterns)) {
throw new Error(`Patterns for label "${label}" should be an array.`);
}
return { label, match: patterns as string[] };
});
} else {
throw new Error(`Parsed data from ${filePath} is not an object or is empty.`);
}
Expand Down Expand Up @@ -97,6 +102,7 @@ function getRandomColor() {
function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels: Array<T>): Array<{ label: string; description?: string }> | undefined {
const matchedLabels: Array<{ label: string; description?: string }> = [];
labels.forEach(({ label, match, description }) => {
core.debug(`Checking label "${label}" with patterns: ${match.join(', ')}`);
if (match.some(pattern => content.some(item => minimatch(item, pattern)))) {
matchedLabels.push({ label, description });
}
Expand All @@ -108,7 +114,7 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
try {
const token = core.getInput('github-token');
const octokit = github.getOctokit(token);
const debugMode = core.getInput('debug') || false;
const debugMode = core.getBooleanInput('debug');

const { owner: contextOwner, repo: contextRepo } = github.context.repo;
const owner = core.getInput('owner') || contextOwner;
Expand All @@ -121,10 +127,6 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
const labelsToApply: string[] = [];
let targetNumber;

if (debugMode) {
core.debug('Debug mode enabled.');
}

if (eventType === 'pull_request' && context.payload.pull_request) {
const prNumber = context.payload.pull_request.number;
targetNumber = prNumber;
Expand All @@ -134,7 +136,7 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
return;
}

const changedFiles = await getChangedFiles(octokit, owner, repo, prNumber);
const changedFiles = await getChangedFiles(octokit as unknown as Octokit, owner, repo, prNumber);
const fileLabelMapping = parseConfigFile(prConfigPath);

const matchedLabels = getMatchedLabels(changedFiles, fileLabelMapping);
Expand Down Expand Up @@ -193,7 +195,7 @@ function getMatchedLabels<T extends LabelConfig>(content: Array<string>, labels:
`);

} catch (error: any) {
console.dir(error);
core.error(`Error: ${error.message}`);
core.setFailed(`Failed to label PR based on file changes: \n${error.message}`);
}
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@octokit/rest": "^21.0.2",
"js-yaml": "^4.1.0",
"minimatch": "^10.0.1",
"override.ps1": "^4.0.0",
Expand All @@ -30,4 +31,4 @@
"@types/node": "^22.7.4",
"esbuild": "^0.23.1"
}
}
}
126 changes: 126 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading