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:add to oclif/core #2673

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
33 changes: 33 additions & 0 deletions packages/cli/src/commands/members/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Command, flags} from '@heroku-cli/command'
import {Args} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import {RoleCompletion} from '@heroku-cli/command/lib/completions'
import {addMemberToTeam, inviteMemberToTeam} from '../../lib/members/util'
export default class MembersAdd extends Command {
static topic = 'members';
static description = 'adds a user to a team';
static flags = {
role: flags.string({char: 'r', required: true, description: 'member role (admin, collaborator, member, owner)', completion: RoleCompletion}),
team: flags.team({required: true}),
};

static args = {
email: Args.string({required: true}),
};

public async run(): Promise<void> {
const {flags, args} = await this.parse(MembersAdd)
const {team, role} = flags
const {body: teamInfo} = await this.heroku.get<Heroku.Team>(`/teams/${team}`)
const email = args.email
const {body: groupFeatures} = await this.heroku.get<Heroku.TeamFeature[]>(`/teams/${team}/features`)

if (teamInfo.type === 'team' && groupFeatures.some(feature => {
return feature.name === 'team-invite-acceptance' && feature.enabled
})) {
await inviteMemberToTeam(email, role, team, this.heroku)
} else {
await addMemberToTeam(email, role, team, this.heroku)
}
}
}
28 changes: 28 additions & 0 deletions packages/cli/src/lib/members/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {APIClient} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import color from '@heroku-cli/color'
import * as Heroku from '@heroku-cli/schema'

export const inviteMemberToTeam = async function (email: string, role: string, team: string, heroku: APIClient) {
ux.action.start(`Inviting ${color.cyan(email)} to ${color.magenta(team)} as ${color.green(role)}`)
await heroku.request<Heroku.TeamInvitation[]>(
`/teams/${team}/invitations`,
{
headers: {
Accept: 'application/vnd.heroku+json; version=3.team-invitations',
}, method: 'PUT',
body: {email, role},
})
ux.action.stop()
}

export const addMemberToTeam = async function (email: string, role: string, groupName: string, heroku: APIClient, method = 'PUT') {
ux.action.start(`Adding ${color.cyan(email)} to ${color.magenta(groupName)} as ${color.green(role)}`)
await heroku.request<Heroku.TeamMember[]>(
`/teams/${groupName}/members`,
{
method: method,
body: {email, role},
})
ux.action.stop()
}
13 changes: 13 additions & 0 deletions packages/cli/test/helpers/stubs/put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as nock from 'nock'

export function sendInvite(email = 'raulb@heroku.com', role = 'admin') {
return nock('https://api.heroku.com:443')
.put('/teams/myteam/invitations', {email, role})
.reply(200)
}

export function updateMemberRole(email = 'raulb@heroku.com', role = 'admin') {
return nock('https://api.heroku.com:443')
.put('/teams/myteam/members', {email, role})
.reply(200)
}
63 changes: 63 additions & 0 deletions packages/cli/test/unit/commands/members/add.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {stdout, stderr} from 'stdout-stderr'
import {expect} from 'chai'
import * as nock from 'nock'
import Cmd from '../../../../src/commands/members/add'
import runCommand from '../../../helpers/runCommand'
import {sendInvite, updateMemberRole} from '../../../helpers/stubs/put'
import {
teamFeatures,
teamInfo,
variableSizeTeamInvites,
variableSizeTeamMembers,
} from '../../../helpers/stubs/get'

describe('heroku members:add', () => {
let apiUpdateMemberRole: nock.Scope
afterEach(() => nock.cleanAll())

context('without the feature flag team-invite-acceptance', () => {
beforeEach(() => {
teamFeatures([])
})
context('and group is an enterprise org', () => {
beforeEach(() => {
teamInfo('enterprise')
variableSizeTeamMembers(1)
})
it('adds a member to an org', () => {
apiUpdateMemberRole = updateMemberRole('foo@foo.com', 'admin')
return runCommand(Cmd, [
'--team',
'myteam',
'--role',
'admin',
'foo@foo.com',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Adding foo@foo.com to myteam as admin...\nAdding foo@foo.com to myteam as admin... done\n').to.eq(stderr.output))
.then(() => apiUpdateMemberRole.done())
})
})
})
context('with the feature flag team-invite-acceptance for a team', () => {
beforeEach(() => {
teamFeatures([{name: 'team-invite-acceptance', enabled: true}])
teamInfo('team')
})
it('sends an invite when adding a new user to the team', () => {
const apiSendInvite = sendInvite('foo@foo.com', 'admin')
variableSizeTeamMembers(1)
variableSizeTeamInvites(0)
return runCommand(Cmd, [
'--role',
'admin',
'--team',
'myteam',
'foo@foo.com',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Inviting foo@foo.com to myteam as admin...\nInviting foo@foo.com to myteam as admin... done\n').to.eq(stderr.output))
.then(() => apiSendInvite.done())
})
})
})
53 changes: 0 additions & 53 deletions packages/orgs-v5/commands/members/add.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 @@ -21,5 +21,4 @@ exports.commands = flatten([
require('./commands/apps/lock'),
require('./commands/apps/transfer'),
require('./commands/apps/unlock'),
require('./commands/members/add'),
])
59 changes: 0 additions & 59 deletions packages/orgs-v5/test/unit/commands/members/add.unit.test.js

This file was deleted.

Loading