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

Feat: GraphQL #59

Merged
merged 7 commits into from
Feb 17, 2023
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@cp": "dist/cp",
"@clog": "dist/clog",
"@git": "dist/git",
"@gh": "dist/gh",
"@lib": "dist/lib"
"@lib": "dist/lib",
"@utils": "dist/utils"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions src/api/api/checkVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { version } from '@api/cli'

const checkVersion = async () => {
await version({ verbose: true })
}

export default checkVersion
21 changes: 21 additions & 0 deletions src/api/api/createBranch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { mutations } from '@api/graphql'

const createBranch = async (
repoId = '',
oid = '',
name = '',
clientMutationId = '',
) => {
const { error, data } = await mutations.createRef(
repoId,
oid,
name,
clientMutationId,
)

if (error) throw new Error(error)

return data
}

export default createBranch
20 changes: 20 additions & 0 deletions src/api/api/createBranchProtection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { mutations } from '@api/graphql'
import { CreateBranchProtectionRuleInput } from '@api/graphql/mutations/createBranchProtectionRule/types'

const createBranchProtection = async (
repoId: string,
pattern: string,
rules: CreateBranchProtectionRuleInput,
) => {
const { error, data } = await mutations.createBranchProtectionRule(
repoId,
pattern,
rules,
)

if (error) throw new Error(error)

return data
}

export default createBranchProtection
16 changes: 16 additions & 0 deletions src/api/api/createRepo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { create } from '@api/cli/repo'

const createRepo = async (
owner: string,
repo: string,
) => {
return await create(
owner,
repo,
{
verbose: true,
},
)
}

export default createRepo
8 changes: 8 additions & 0 deletions src/api/api/getUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { auth } from '@api/cli'

const getUsername = async () => {
const { username } = await auth.status({ verbose: true })
return username
}

export default getUsername
27 changes: 27 additions & 0 deletions src/api/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import checkVersion from './checkVersion'
import getUsername from './getUsername'
import createRepo from './createRepo'

import queryRepo from './queryRepo'
import createBranchProtection from './createBranchProtection'
import createBranch from './createBranch'

export {
checkVersion,
getUsername,
createRepo,

queryRepo,
createBranchProtection,
createBranch,
}

export default {
checkVersion,
getUsername,
createRepo,

queryRepo,
createBranchProtection,
createBranch,
}
14 changes: 14 additions & 0 deletions src/api/api/queryRepo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { queries } from '@api/graphql'

const queryRepo = async (owner: string, repo: string) => {
const { error, data } = await queries.repository(
owner,
repo,
)

if (error) throw new Error(error)

return data
}

export default queryRepo
File renamed without changes.
11 changes: 8 additions & 3 deletions src/gh/api/refs/delete.ts → src/api/cli/api/refs/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { exec } from '@cp'
import log, { c } from '@clog'
import { Options } from './types'

const del = async (owner: string, repo: string, branch: string, {
verbose
}: Options = {}) => new Promise<string>((resolve, reject) => {
const del = async (
owner: string,
repo: string,
branch: string,
{
verbose,
}: Options = {}
) => new Promise<string>((resolve, reject) => {
try {
const args = ['api']

Expand Down
11 changes: 8 additions & 3 deletions src/gh/api/refs/get.ts → src/api/cli/api/refs/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { exec } from '@cp'
import log, { c } from '@clog'
import { Data, Options } from './types'

const get = async (owner: string, repo: string, branch: string, {
verbose
}: Options = {}) => new Promise<Data>((resolve, reject) => {
const get = async (
owner: string,
repo: string,
branch: string,
{
verbose,
}: Options = {}
) => new Promise<Data>((resolve, reject) => {
try {
const args = ['api']

Expand Down
2 changes: 1 addition & 1 deletion src/gh/api/refs/index.ts → src/api/cli/api/refs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import refs from './refs'
export {
get,
post,
del
del,
}

export default refs
12 changes: 9 additions & 3 deletions src/gh/api/refs/post.ts → src/api/cli/api/refs/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { exec } from '@cp'
import log, { c } from '@clog'
import { Data, Options } from './types'

const post = async (owner: string, repo: string, sha: string, branch: string, {
verbose
}: Options = {}) => new Promise<Data>((resolve, reject) => {
const post = async (
owner: string,
repo: string,
sha: string,
branch: string,
{
verbose,
}: Options = {}
) => new Promise<Data>((resolve, reject) => {
try {
const args = ['api']

Expand Down
2 changes: 1 addition & 1 deletion src/gh/api/refs/refs.ts → src/api/cli/api/refs/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const refs = async ({ owner, repo }: Props) => {
return {
get,
post,
delete: del
delete: del,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gh/api/refs/types.ts → src/api/cli/api/refs/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Object = {
sha: string,
type: string,
url: string
url: string,
}

export type Data = {
Expand All @@ -13,7 +13,7 @@ export type Data = {

export type Props = {
owner: string,
repo: string
repo: string,
}

export type Options = {
Expand Down
4 changes: 2 additions & 2 deletions src/gh/auth/index.ts → src/api/cli/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import token from './token'

export {
status,
token
token,
}

export default {
status,
token
token,
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import log, { c } from '@clog'
import { Options } from './types'

const status = async ({
verbose
verbose,
}: Options = {}) => {
const cmd = 'gh auth status'

Expand All @@ -22,11 +22,11 @@ const status = async ({
process.exit()
}

const ghUserName = stdall.split('Logged in to github.com as ')[1].split(' ')[0]
const username = stdall.split('Logged in to github.com as ')[1].split(' ')[0]

verbose && log(c.grey(` ${stdall.replace(ghUserName, c.magenta(ghUserName))}`))
verbose && log(c.grey(` ${stdall.replace(username, c.magenta(username))}`))

return { ghUserName }
return { username }
}

export default status
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import log, { c } from '@clog'
import { Options } from './types'

const token = async ({
verbose
verbose,
}: Options = {}) => {
const cli = 'gh auth token'

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/gh/index.ts → src/api/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export {
version,
auth,
repo,
api
api,
}

export default {
version,
auth,
repo,
api
api,
}
32 changes: 17 additions & 15 deletions src/gh/repo/create/create.ts → src/api/cli/repo/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import log, { c } from '@clog'
import { Options, Result } from './types'
import { getAccessibility } from './utils'

const create = async (repoName: string, {
orgName,
const create = async (owner: string, repo: string, {
accessibility,
desciption,
disableIssues,
Expand All @@ -15,11 +14,13 @@ const create = async (repoName: string, {
license,
remote,
team,
verbose
verbose,
}: Options = {}) => {
verbose && log(c.blue(`• Create GitHub repository`))

const args = ['repo', 'create']

const repository = orgName ? `${orgName}/${repoName}` : repoName
const repository = `${owner}/${repo}`

args.push(repository)
args.push(getAccessibility(accessibility))
Expand All @@ -33,29 +34,30 @@ const create = async (repoName: string, {
team && args.push(`-t=${team}`)
args.push('--add-readme')

verbose && log(c.blue(`• ${['gh', ...args].join(' ')}`))

const { stdall } = await spawn('gh', args)

const owner = stdall.split('https://github.com/')[1].split('/')[0].trim()
const name = repoName.trim()
const path = `${owner}/${name}`
const url = stdall.trim()
// HTTP 422: Repository creation failed. (https://api.github.com/user/repos)
// name already exists on this account

if (!(stdall.includes('✓ Created repository') || stdall === `https://github.com/${path}`)) {
if (!(
stdall.includes('✓ Created repository') ||
stdall === `https://github.com/${repository}`
)) {
log(c.red(stdall))
process.exit()
}

verbose && log(c.grey(` https://github.com/${c.magenta(path)}`))
// const repoOwner = stdall.split('https://github.com/')[1].split('/')[0].trim()
// const repoName = repo.trim()

const result: Result = {
owner,
name,
path,
url
repo,
url: stdall.trim(),
}

verbose && log(c.grey(` Created GitHub repository ✓`))

return result
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export enum AccessibilityEnum {
Private = 'private',
Public = 'public',
Internal = 'internal'
Internal = 'internal',
}

export enum GitIgnoreEnum {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AccessibilityEnum,
GitIgnoreEnum,
LicenseEnum
LicenseEnum,
} from './enums'

export type Accessibility =
Expand Down Expand Up @@ -164,7 +164,6 @@ export type License =
`${LicenseEnum.Unlicense}` // 'The Unlicense',

export type Options = {
orgName?: string,
accessibility?: Accessibility,
desciption?: string,
disableIssues?: boolean,
Expand All @@ -179,7 +178,6 @@ export type Options = {

export type Result = {
owner: string,
name: string,
path: string,
repo: string,
url: string,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getAccessibility from './getAccessibility'

export {
getAccessibility
getAccessibility,
}
Loading