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

Improve CODEOWNER style glob support #3

Merged
merged 6 commits into from
Jan 18, 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
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Repository CODEOWNERS

* @mdzhang
/src @mdzhang
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 28 additions & 11 deletions dist/index.js

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

29 changes: 26 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ export async function getCodeowners(
// @ts-expect-error false positive
result.data.encoding
).toString()
core.debug(`codeowners fileContent is:\n${fileContent}`)
} catch (error: any) {
core.warning(`Could not find pull request #${prNumber}, skipping`)
core.warning(
`Could not find pull request #${prNumber}, skipping (${error.message})`
)
return []
}

Expand All @@ -102,8 +103,30 @@ export async function getCodeowners(
.filter(l => l.trim().length > 0)
.filter(l => !l.startsWith('#'))
.map(l => l.split(' '))
.filter(([glob, team]) => {
if (team === undefined) {
core.warning(`CODEOWNERS had glob ${glob} w/o matching team`)
}

core.debug(`codeowners is ${codeowners}`)
return team !== undefined
})
.map(([glob, team]) => {
// do some munging to support CODEOWNER format globs
let finalGlob = glob

// convert directories like foo/ to foo/**
if (finalGlob.endsWith('/')) {
finalGlob += '**'
} else {
// convert directories like foo to foo/**
const last = finalGlob.split('\\').pop()?.split('/').pop()
if (!last?.includes('.')) {
finalGlob += '/**'
}
}

return [finalGlob, team]
})

if (!codeowners.length) {
core.warning(`Pull request #${prNumber} has no codeowners`)
Expand Down
12 changes: 4 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function labeler() {

core.debug(`labelsToOwner is ${labelsToOwner}`)
const labelMap: Map<any, any> = flip(JSON.parse(labelsToOwner))
core.debug(`labelMap is ${labelMap}`)
core.debug(`labelMap is ${JSON.stringify(Object.fromEntries(labelMap))}`)
const preexistingLabels = pullRequest.data.labels.map(
(l: { name: string }) => l.name
)
Expand Down Expand Up @@ -109,21 +109,17 @@ export function getMatchingCodeownerLabels(
entries: string[][],
labelMap: Map<string, string>
): Set<string> {
// 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 = changedFile.blob_url.replace(repoUrlPrefix, '');
// const i = refPath.indexOf('/');
// const [_, path] = [refPath.slice(0,i), refPath.slice(i+1)];

core.debug(`checking path ${changedFile}`)
for (const entry of entries) {
const [glob, team] = entry
core.debug(`-- checking glob ${glob}, team ${team}`)
if (minimatch(changedFile, glob)) {
if (minimatch(`/${changedFile}`, glob)) {
core.debug(`-- matched glob ${glob}, team ${team}`)
const label = labelMap.get(team)
if (label !== undefined) {
core.debug(`-- adding label ${label}`)
allLabels.add(label)
}
}
Expand Down
Loading