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

feat: check teams #38

Closed
wants to merge 1 commit into from
Closed
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 .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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orta in order to test this properly I think I need either of the following please:

  • membership of OSS-Docs-Tools/teamA (can be temporary if you like); or
  • details of a substitute team within the organisation (such team not containing khaosdoctor danilofuchs nor orta)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orta is this something which can be done? Keen to merge this in if possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests need to mock the github API calls and be moved to use real async code, as this PR doesn't look like it would work to me

That said - @TeamA has the account @dangermcshane set up - so we can verify post-merge, https://github.com/orgs/OSS-Docs-Tools/teams/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
10 changes: 8 additions & 2 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"]);
});

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 @@ -40,6 +41,11 @@ describe(githubLoginIsInCodeowners, () => {
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