Merge pull request #2433 from timja/support-user-or-org

scaffolder: Allow user or org, remove hard coded isOrg
This commit is contained in:
Ben Lambert
2020-09-15 09:17:25 +02:00
committed by GitHub
6 changed files with 35 additions and 16 deletions
@@ -19,6 +19,9 @@ export const mockGithubClient = {
createInOrg: jest.fn(),
createForAuthenticatedUser: jest.fn(),
},
users: {
getByUsername: jest.fn(),
},
};
export class Octokit {
@@ -19,11 +19,18 @@ jest.mock('nodegit');
import { Octokit } from '@octokit/rest';
import * as NodeGit from 'nodegit';
import { OctokitResponse, ReposCreateInOrgResponseData } from '@octokit/types';
import {
OctokitResponse,
ReposCreateInOrgResponseData,
UsersGetByUsernameResponseData,
} from '@octokit/types';
import { GithubPublisher } from './github';
const { mockGithubClient } = require('@octokit/rest') as {
mockGithubClient: { repos: jest.Mocked<Octokit['repos']> };
mockGithubClient: {
repos: jest.Mocked<Octokit['repos']>;
users: jest.Mocked<Octokit['users']>;
};
};
const {
@@ -59,10 +66,14 @@ describe('GitHub Publisher', () => {
clone_url: 'mockclone',
},
} as OctokitResponse<ReposCreateInOrgResponseData>);
mockGithubClient.users.getByUsername.mockResolvedValue({
data: {
type: 'Organization',
},
} as OctokitResponse<UsersGetByUsernameResponseData>);
await publisher.publish({
values: {
isOrg: true,
storePath: 'blam/test',
owner: 'bob',
},
@@ -81,6 +92,11 @@ describe('GitHub Publisher', () => {
clone_url: 'mockclone',
},
} as OctokitResponse<ReposCreateInOrgResponseData>);
mockGithubClient.users.getByUsername.mockResolvedValue({
data: {
type: 'User',
},
} as OctokitResponse<UsersGetByUsernameResponseData>);
await publisher.publish({
values: {
@@ -112,6 +128,12 @@ describe('GitHub Publisher', () => {
clone_url: 'mockclone',
},
} as OctokitResponse<ReposCreateInOrgResponseData>);
mockGithubClient.users.getByUsername.mockResolvedValue({
data: {
type: 'Organization',
},
} as OctokitResponse<UsersGetByUsernameResponseData>);
it('should call init on the repo with the directory', async () => {
await publisher.publish({
values,
@@ -45,9 +45,12 @@ export class GithubPublisher implements PublisherBase {
) {
const [owner, name] = values.storePath.split('/');
const repoCreationPromise = values.isOrg
? this.client.repos.createInOrg({ name, org: owner })
: this.client.repos.createForAuthenticatedUser({ name });
const user = await this.client.users.getByUsername({ username: owner });
const repoCreationPromise =
user.data.type === 'Organization'
? this.client.repos.createInOrg({ name, org: owner })
: this.client.repos.createForAuthenticatedUser({ name });
const { data } = await repoCreationPromise;
+1 -2
View File
@@ -46,8 +46,7 @@ export class ScaffolderApi {
headers: {
'Content-Type': 'application/json',
},
// TODO(shmidt-i): when repo picker is implemented, take isOrg from it
body: JSON.stringify({ template, values: { ...values, isOrg: true } }),
body: JSON.stringify({ template, values: { ...values } }),
});
if (response.status !== 201) {