Skip to content
Draft
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
23 changes: 22 additions & 1 deletion lib/plugins/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports = class Teams extends Diffable {
{ org: this.repo.owner })

this.log.debug(`Response from the call is ${JSON.stringify(resp)}`)
return teams.filter(team => !resp.some(sec => sec.name === team.name))
this.securityManagerTeams = resp
return teams.filter(team => !this.isSecurityManagerTeam(team))
} catch (e) {
if (e.status === 404) {
this.log.debug(`${this.repo.owner} Org does not have Security manager teams set up ${e}`)
Expand All @@ -61,7 +62,19 @@ module.exports = class Teams extends Diffable {
return existing.permission !== attrs.permission
}

isSecurityManagerTeam (team) {
const name = team && (team.name || team.slug || team.team_slug)

return Boolean(name) && (this.securityManagerTeams || []).some(sec => {
return [sec.name, sec.slug, sec.team_slug].some(secName => secName && secName.toLowerCase() === name.toLowerCase())
})
}

update (existing, attrs) {
if (this.isSecurityManagerTeam(existing) || this.isSecurityManagerTeam(attrs)) {
return Promise.resolve()
}

if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint(`PUT ${teamRepoEndpoint}`, this.toParams(existing, attrs)), 'Add Teams to Repo')
Expand All @@ -71,6 +84,10 @@ module.exports = class Teams extends Diffable {
}

add (attrs) {
if (this.isSecurityManagerTeam(attrs)) {
return Promise.resolve()
}

let existing = { team_id: 1 }
this.log.debug(`Getting team with the parms ${JSON.stringify(attrs)}`)
return this.github.rest.teams.getByName({ org: this.repo.owner, team_slug: attrs.name }).then(res => {
Expand Down Expand Up @@ -114,6 +131,10 @@ module.exports = class Teams extends Diffable {
}

remove (existing) {
if (this.isSecurityManagerTeam(existing)) {
return Promise.resolve()
}

if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint(
Expand Down
49 changes: 49 additions & 0 deletions test/unit/lib/plugins/teams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('Teams', () => {
const updatedTeamId = any.integer()
const removedTeamName = 'removed'
const removedTeamId = any.integer()
const securityManagerTeamName = 'security-manager'
const securityManagerTeamId = any.integer()
const unchangedTeamName = 'unchanged'
const unchangedTeamId = any.integer()
const org = 'bkeepers'
Expand Down Expand Up @@ -91,6 +93,53 @@ describe('Teams', () => {
expectTeamDeleted(removedTeamName)
})

it('does not add configured security manager teams', async () => {
github.paginate.mockImplementation(async (fetch, params) => {
if (fetch === 'GET /orgs/{org}/security-managers') {
return [{ name: securityManagerTeamName }]
}

const response = await fetch(params)
return response.data
})
github.rest.repos.listTeams.mockResolvedValue({
data: [
{
id: securityManagerTeamId,
name: securityManagerTeamName,
slug: securityManagerTeamName,
permission: 'admin'
}
]
})
const plugin = configure([
{ name: securityManagerTeamName, permission: 'pull' }
])

await plugin.sync()

expect(github.rest.teams.getByName).not.toHaveBeenCalled()
expect(github.rest.teams.addOrUpdateRepoPermissionsInOrg).not.toHaveBeenCalled()
expect(github.request).not.toHaveBeenCalled()
})

it('does not update or remove security manager teams', async () => {
const plugin = configure()
github.paginate.mockResolvedValue([{ name: securityManagerTeamName }])
const securityManagerTeam = {
id: securityManagerTeamId,
name: securityManagerTeamName,
slug: securityManagerTeamName,
permission: 'admin'
}

await plugin.checkSecurityManager([securityManagerTeam])
await plugin.update(securityManagerTeam, { name: securityManagerTeamName, permission: 'pull' })
await plugin.remove(securityManagerTeam)

expect(github.request).not.toHaveBeenCalled()
})

function expectTeamDeleted (teamSlug) {
expect(github.request).toHaveBeenCalledWith(
'DELETE /orgs/:owner/teams/:team_slug/repos/:owner/:repo',
Expand Down