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

refactor: migrate members:remove to oclif/core #2678

Merged
merged 4 commits into from
Mar 6, 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
67 changes: 67 additions & 0 deletions packages/cli/src/commands/members/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import color from '@heroku-cli/color'
import {APIClient, Command, flags} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'

const revokeInvite = async (email: string, team: string, heroku: APIClient) => {
ux.action.start(`Revoking invite for ${color.cyan(email)} in ${color.magenta(team)}`)
await heroku.delete<Heroku.TeamInvitation[]>(
`/teams/${team}/invitations/${email}`,
{
headers: {
Accept: 'application/vnd.heroku+json; version=3.team-invitations',
},
})
ux.action.stop()
}

const getTeamInvites = async (team: string, heroku: APIClient) => {
const {body: teamInvites} = await heroku.get<Heroku.TeamInvitation[]>(
`/teams/${team}/invitations`,
{
headers: {
Accept: 'application/vnd.heroku+json; version=3.team-invitations',
},
})
return teamInvites
}

const removeUserMembership = async (email:string, team: string, heroku: APIClient) => {
ux.action.start(`Removing ${color.cyan(email)} from ${color.magenta(team)}`)
await heroku.delete(`/teams/${team}/members/${encodeURIComponent(email)}`)
ux.action.stop()
}

export default class MembersRemove extends Command {
static topic = 'members';
static description = 'removes a user from a team';
static flags = {
team: flags.team({required: true}),
};

static strict = false

public async run(): Promise<void> {
const {flags, argv} = await this.parse(MembersRemove)
const {team} = flags
const email = argv[0] as string
const {body: teamInfo} = await this.heroku.get<Heroku.Team>(`/teams/${team}`)
let teamInviteFeatureEnabled = false
let isInvitedUser = false

if (teamInfo.type === 'team') {
const {body: teamFeatures} = await this.heroku.get<Heroku.TeamFeature[]>(`/teams/${team}/features`)
teamInviteFeatureEnabled = Boolean(teamFeatures.some(feature => feature.name === 'team-invite-acceptance' && feature.enabled))
if (teamInviteFeatureEnabled) {
const invites = await getTeamInvites(team, this.heroku)
isInvitedUser = Boolean(invites.some(m => m.user?.email === email))
}
}

if (teamInviteFeatureEnabled && isInvitedUser) {
await revokeInvite(email, team, this.heroku)
} else {
await removeUserMembership(email, team, this.heroku)
}
}
}
102 changes: 102 additions & 0 deletions packages/cli/test/unit/commands/members/remove.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {stdout, stderr} from 'stdout-stderr'
import {expect} from 'chai'
import * as nock from 'nock'

import Cmd from '../../../../src/commands/members/remove'
import runCommand from '../../../helpers/runCommand'
import {
teamFeatures,
teamInfo,
teamInvites,
} from '../../../helpers/stubs/get'
import {memberFromTeam, teamInvite} from '../../../helpers/stubs/delete'

describe('heroku members:remove', () => {
afterEach(() => nock.cleanAll())

context('from an org', () => {
beforeEach(() => {
teamInfo('enterprise')
})
it('removes a member from an org', () => {
const apiRemoveMemberFromOrg = memberFromTeam()
return runCommand(Cmd, [
'--team',
'myteam',
'[email protected]',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Removing [email protected] from myteam...\nRemoving [email protected] from myteam... done\n').to.eq(stderr.output))
.then(() => apiRemoveMemberFromOrg.done())
})
})
context('from a team', () => {
beforeEach(() => {
teamInfo('team')
})
context('without the feature flag team-invite-acceptance', () => {
beforeEach(() => {
teamFeatures([])
})
it('removes a member from an org', () => {
const apiRemoveMemberFromOrg = memberFromTeam()
return runCommand(Cmd, [
'--team',
'myteam',
'[email protected]',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Removing [email protected] from myteam...\nRemoving [email protected] from myteam... done\n').to.eq(stderr.output))
.then(() => apiRemoveMemberFromOrg.done())
})
})
context('with the feature flag team-invite-acceptance', () => {
let apiGetTeamInvites: nock.Scope
beforeEach(() => {
teamFeatures([{name: 'team-invite-acceptance', enabled: true}])
})
context('with no pending invites', () => {
beforeEach(() => {
apiGetTeamInvites = teamInvites([])
})
it('removes a member', () => {
const apiRemoveMemberFromOrg = memberFromTeam()
return runCommand(Cmd, [
'--team',
'myteam',
'[email protected]',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Removing [email protected] from myteam...\nRemoving [email protected] from myteam... done\n').to.eq(stderr.output))
.then(() => apiGetTeamInvites.done())
.then(() => apiRemoveMemberFromOrg.done())
})
})
context('with pending invites', () => {
it('revokes the invite', () => {
apiGetTeamInvites = teamInvites([
{
invited_by: {
email: '[email protected]',
},
role: 'member',
user: {
email: '[email protected]',
},
},
])
const apiRevokeTeamInvite = teamInvite('[email protected]')
return runCommand(Cmd, [
'--team',
'myteam',
'[email protected]',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Revoking invite for [email protected] in myteam...\nRevoking invite for [email protected] in myteam... done\n').to.eq(stderr.output))
.then(() => apiGetTeamInvites.done())
.then(() => apiRevokeTeamInvite.done())
})
})
})
})
})
68 changes: 0 additions & 68 deletions packages/orgs-v5/commands/members/remove.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/orgs-v5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ exports.commands = flatten([
require('./commands/apps/transfer'),
require('./commands/apps/unlock'),
require('./commands/members/add'),
require('./commands/members/remove'),
])
90 changes: 0 additions & 90 deletions packages/orgs-v5/test/unit/commands/members/remove.unit.test.js

This file was deleted.

Loading