From 66d1f6c020ecc6f574e7b114a5e55f3c54fb1d63 Mon Sep 17 00:00:00 2001 From: meesh Date: Thu, 18 Jan 2024 11:19:49 -0500 Subject: [PATCH] fixes --- README.md | 4 +++- action.yml | 5 ++++- package-lock.json | 16 ++++++++++++++++ package.json | 1 + src/api.ts | 14 ++++++++------ src/main.ts | 18 +++++++++--------- src/types.ts | 2 ++ 7 files changed, 43 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3076316..75ae3cd 100644 --- a/README.md +++ b/README.md @@ -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' ``` diff --git a/action.yml b/action.yml index b832d40..7e285f0 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/package-lock.json b/package-lock.json index 4330995..6c4fee5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,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", @@ -1657,6 +1658,21 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/lodash": { + "version": "4.14.202", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.202.tgz", + "integrity": "sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==", + "dev": true + }, + "node_modules/@types/lodash.isequal": { + "version": "4.5.8", + "resolved": "https://registry.npmjs.org/@types/lodash.isequal/-/lodash.isequal-4.5.8.tgz", + "integrity": "sha512-uput6pg4E/tj2LGxCZo9+y27JNyB2OZuuI/T5F+ylVDYuqICLG2/ktjxx0v6GvVntAf8TvEzeQLcV0ffRirXuA==", + "dev": true, + "dependencies": { + "@types/lodash": "*" + } + }, "node_modules/@types/node": { "version": "20.11.0", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.0.tgz", diff --git a/package.json b/package.json index 9a5f104..33efd29 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api.ts b/src/api.ts index 2dd6bcf..5d0a8a0 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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, - 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; diff --git a/src/main.ts b/src/main.ts index 2666923..98d7e4f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -85,24 +85,24 @@ async function labeler() { export function getMatchingCodeownerLabels( changedFiles: string[], - entries: string[], + entries: string[][], labelMap: Map ): Set { - 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 = new Set(); 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); } } } diff --git a/src/types.ts b/src/types.ts index 81a9075..37f4878 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1 +1,3 @@ +import * as github from '@actions/github'; + export type ClientType = ReturnType;