Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdzhang committed Jan 18, 2024
1 parent 4ae4a97 commit 66d1f6c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ steps:
uses: mdzhang/pr-codeowner-autolabel
with:
# default is ./CODEOWNERS
filepath: ./.github/CODEOWNERS
filePath: ./.github/CODEOWNERS
labelsToOwner:
frontend: '@myteam/@frontend-guild'

```

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ branding:
color: "red"

inputs:
filepath:
filePath:
description: "Relative path to CODEOWNERS in repo"
required: false
default: CODEOWNERS
labelsToOwner:
description: "Keys are labels to add, values are potential owners in CODEOWNERS"
required: true

runs:
using: node20
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/lodash.isequal": "^4.5.8",
"@types/node": "^20.11.0",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
Expand Down
14 changes: 8 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,31 @@ export async function getCodeowners(
filePath: string = 'CODEOWNERS'
) {
core.debug(`fetching codeowners for pr #${prNumber} from path ${filePath}`);
let fileContent: any;
let fileContent: string;

try {
const result = client.rest.repos.getContent({
const result = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: filePath,
});
console.log(result.data)
fileContent = atob(result.data.content);
} catch (error: any) {
core.warning(`Could not find pull request #${prNumber}, skipping`);
return;
return [];
}

// rm newlines & comments; convert to array of 2-tupes, <glob, team>
const codeowners: string[] = fileContent
const codeowners: string[][] = fileContent
.split(/\r?\n/)
.filter(l => l.trim().length > 0)
.filter(l => !l.startsWith('#')).split(' ');
.filter(l => !l.startsWith('#'))
.map(l => l.split(' '));

if (!codeowners.length) {
core.warning(`Pull request #${prNumber} has no codeowners`);
return;
return [];
}

return codeowners;
Expand Down
18 changes: 9 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ async function labeler() {

export function getMatchingCodeownerLabels(
changedFiles: string[],
entries: string[],
entries: string[][],
labelMap: Map<string, string>
): Set<string> {
const repoUrlPrefix = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/blob`;
// const repoUrlPrefix = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/blob`;
const allLabels: Set<string> = new Set<string>();

for (const changedFile of changedFiles) {
const refPath = split(changedFile.blob_url.replace(repoUrlPrefix, ''));
const i = refPath.indexOf('/');
const [_, path] = [refPath.slice(0,i), refPath.slice(i+1)];
// const refPath = changedFile.blob_url.replace(repoUrlPrefix, '');
// const i = refPath.indexOf('/');
// const [_, path] = [refPath.slice(0,i), refPath.slice(i+1)];

core.debug(`checking path ${path}`);
core.debug(`checking path ${changedFile}`);
for (const entry of entries) {
const [glob, team] = entry
if minimatch(path, glob) {
if (minimatch(changedFile, glob)) {
const label = labelMap.get(team);
if (label) {}
allLabels.add(label);
if (label !== undefined) {
allLabels.add(label as string);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import * as github from '@actions/github';

export type ClientType = ReturnType<typeof github.getOctokit>;

0 comments on commit 66d1f6c

Please sign in to comment.