Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
feat: check teams
Browse files Browse the repository at this point in the history
Check team membership.

Fixes OSS-Docs-Tools#13
  • Loading branch information
stoovon committed Sep 25, 2022
1 parent deaa910 commit ada9a11
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ packages/documentation/copy/ja/**/*.ts @sasurau4 @Quramy @Naturalclar @Takepepe
# Collaborators for Portuguese Translation of the Website
packages/playground-examples/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta
packages/playground-examples/copy/pt/**/*.ts @khaosdoctor @danilofuchs @orta
packages/tsconfig-reference/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta
packages/tsconfig-reference/copy/pt/**/*.md @khaosdoctor @danilofuchs @orta @teamA
packages/typescriptlang-org/src/copy/pt/**/*.ts @khaosdoctor @danilofuchs @orta
packages/documentation/copy/pt/**/*.ts @khaosdoctor @danilofuchs @orta

Expand Down
36 changes: 33 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,23 @@ class Actor {
function getFilesNotOwnedByCodeOwner(owner, files, cwd) {
const filesWhichArentOwned = []
const codeowners = new Codeowners(cwd);
const octokit = getOctokit(process.env.GITHUB_TOKEN)

for (const file of files) {
const relative = file.startsWith("/") ? file.slice(1) : file
let owners = codeowners.getOwner(relative);
if (!owners.includes(owner)) {
filesWhichArentOwned.push(file)
if (owners.includes(owner)) {
continue
}

if (owners
.filter(owner => owner.startsWith("@"))
.some(teamowner => getTeamMembers(octokit, teamowner).then(result => result.includes(owner)))
) {
continue
}

filesWhichArentOwned.push(file)
}

return filesWhichArentOwned
Expand All @@ -263,7 +273,21 @@ function getFilesNotOwnedByCodeOwner(owner, files, cwd) {
const codeowners = new Codeowners(cwd);
const contents = readFileSync(codeowners.codeownersFilePath, "utf8").toLowerCase()

return contents.includes("@" + login.toLowerCase() + " ") || contents.includes("@" + login.toLowerCase() + "\n")
if (contents.includes("@" + login.toLowerCase() + " ") || contents.includes("@" + login.toLowerCase() + "\n")) {
return true
}

const regex = /\@[a-zA-Z0-9]+\/[a-zA-Z0-9]+ /g;
const potentialTeams = contents.match(regex);
if (potentialTeams == null || potentialTeams.length == 0) {
return false
}

const octokit = getOctokit(process.env.GITHUB_TOKEN)

return potentialTeams.some(team =>
getTeamMembers(octokit, team.replace(" ","")).then(result => result.includes(login.toLowerCase()))
)
}


Expand Down Expand Up @@ -314,6 +338,12 @@ async function getPRChangedFiles(octokit, repoDeets, prNumber) {
return fileStrings
}

async function getTeamMembers(octokit, teamname) {
const components = teamname.replace("@","").split("/")
const teamMembers = await octokit.paginate('GET /orgs/:org/teams/:team/members', {org: components[0], team: components[1]})
return teamMembers.map(teammember => teammember.login)
}

async function createOrAddLabel(octokit, repoDeets, labelConfig) {
let label = null
const existingLabels = await octokit.paginate('GET /repos/:owner/:repo/labels', { owner: repoDeets.owner, repo: repoDeets.repo })
Expand Down
14 changes: 10 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ test("determine who owns a set of files", () => {
});

test("real world", () => {
// Stoovon is a member of teamA.
const changed = ["/packages/tsconfig-reference/copy/pt/options/files.md"];
const filesNotInCodeowners = findCodeOwnersForChangedFiles(changed, ".");
expect(filesNotInCodeowners.users).toEqual(["@khaosdoctor", "@danilofuchs", "@orta"]);
expect(filesNotInCodeowners.users).toEqual(["@khaosdoctor", "@danilofuchs", "@orta", "@stoovon"]);
});

Or
test("real world 2", () => {
const changed = ["/packages/typescriptlang-org/src/copy/pt/index.ts", "/packages/typescriptlang-org/src/copy/pt/nav.ts"];
const filesNotInCodeowners = findCodeOwnersForChangedFiles(changed, ".");
expect(filesNotInCodeowners.users).toEqual(["@khaosdoctor", "@danilofuchs", "@orta"]);
expect(filesNotInCodeowners.users).toEqual(["@khaosdoctor", "@danilofuchs", "@orta", "@stoovon"]);
});

test("real world with labels", () => {
Expand All @@ -36,10 +37,15 @@ test("deciding if someone has access to merge", () => {
});

describe(githubLoginIsInCodeowners, () => {
test("allows folks found in the codeowners", () => {
test("allows folks found in the codeowners", () => {orta
const ortaIn = githubLoginIsInCodeowners("orta", ".");
expect(ortaIn).toEqual(true);
});
test("allows folks in teams found in the codeowners", () => {
// Stoovon is a member of teamA.
const stoovonIn = githubLoginIsInCodeowners("stoovon", ".");
expect(stoovonIn).toEqual(true);
});
test("ignores case", () => {
const ortaIn = githubLoginIsInCodeowners("OrTa", ".");
expect(ortaIn).toEqual(true);
Expand Down

0 comments on commit ada9a11

Please sign in to comment.