This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create-repo): enabled creation within an org
as long as the authenticated user is a member for #1
- Loading branch information
Showing
7 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
import chalk from 'chalk'; | ||
import {factory} from './github-client-factory'; | ||
|
||
export default async function (name, visibility) { | ||
async function authenticatedUserIsMemberOfRequestedOrganization(account, octokit) { | ||
const {data: organizations} = await octokit.orgs.listForAuthenticatedUser(); | ||
|
||
return organizations.reduce((acc, organization) => acc || account === organization.login, false); | ||
} | ||
|
||
export default async function (name, owner, visibility) { | ||
console.error(chalk.grey('Creating repository on GitHub')); // eslint-disable-line no-console | ||
|
||
const {data: {ssh_url: sshUrl, html_url: htmlUrl}} = await factory().repos.createForAuthenticatedUser({ | ||
name, | ||
private: 'Private' === visibility | ||
}); | ||
const octokit = factory(); | ||
const {data: {login: authenticatedUser}} = await octokit.users.getAuthenticated(); | ||
|
||
if (owner === authenticatedUser) { | ||
const {data: {ssh_url: sshUrl, html_url: htmlUrl}} = await octokit.repos.createForAuthenticatedUser({ | ||
name, | ||
private: 'Private' === visibility | ||
}); | ||
|
||
console.error(chalk.grey(`Repository created for user ${name} at ${htmlUrl}`)); // eslint-disable-line no-console | ||
|
||
return {sshUrl, htmlUrl}; | ||
} | ||
|
||
if (await authenticatedUserIsMemberOfRequestedOrganization(owner, octokit)) { | ||
const {data: {ssh_url: sshUrl, html_url: htmlUrl}} = await octokit.repos.createInOrg({ | ||
org: owner, | ||
name, | ||
private: 'Private' === visibility | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.error(chalk.grey(`Repository created for organization ${name} at ${htmlUrl}`)); | ||
|
||
console.error(chalk.grey(`Repository created at ${htmlUrl}`)); // eslint-disable-line no-console | ||
return {sshUrl, htmlUrl}; | ||
} | ||
|
||
return {sshUrl, htmlUrl}; | ||
throw new Error(`User ${authenticatedUser} does not have access to create a repository in the ${owner} account`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters