Skip to content

Commit

Permalink
GitHub init work
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Aug 21, 2018
1 parent 3466c8a commit 80c2f0a
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 227 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@
"@oclif/errors": "^1.1.2",
"@oclif/plugin-help": "^2",
"@oclif/plugin-not-found": "^1.1.4",
"@octokit/rest": "^15.10.0",
"ascii-table": "0.0.9",
"chalk": "^2.4.1",
"clean-deep": "^3.0.2",
"cli-ux": "^4.7.3",
"configstore": "^3.1.2",
"configstore": "^4.0.0",
"debug": "^3.1.0",
"dot-prop": "^4.2.0",
"find-up": "^3.0.0",
"flush-write-stream": "^1.0.3",
"folder-walker": "^3.1.0",
"from2-array": "0.0.4",
"ghauth": "^3.2.1",
"git-remote-origin-url": "^2.0.0",
"git-repo-info": "^2.0.0",
"hasha": "^3.0.0",
"inquirer": "^6.0.0",
"is-docker": "^1.1.0",
Expand All @@ -89,7 +92,6 @@
"through2-map": "^3.0.0",
"util.promisify": "^1.0.0",
"util.promisify-all": "^1.0.2",
"git-repo-info": "^2.0.0",
"write-file-atomic": "^2.3.0"
},
"devDependencies": {
Expand Down
72 changes: 72 additions & 0 deletions src/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { flags } = require('@oclif/command')
const gitRepoInfo = require('git-repo-info')
const parseGitRemote = require('parse-github-url')
const gitRemoteOriginUrl = require('git-remote-origin-url')

const Command = require('../base')
const createOrFindSite = require('../utils/init/create-or-find-site')
const configManual = require('../utils/init/config-manual')
const configGithub = require('../utils/init/config-github')
const renderShortDesc = require('../utils/renderShortDescription')

class InitCommand extends Command {
async loadRepo() {
const remoteUrl = await gitRemoteOriginUrl()
if (!remoteUrl) this.error('CI requires a git remote. No git remote found.')
const parsedUrl = parseGitRemote(remoteUrl)
const repoInfo = gitRepoInfo()

const repo = {
repo_path: parsedUrl.path,
repo_branch: repoInfo.branch,
allowed_branches: [repoInfo.branch]
}

switch (parsedUrl.host) {
case 'github.com': {
repo.provider = 'github'
break
}
case 'gitlab.com': {
repo.provider = 'gitlab'
break
}
}

return repo
}

async run() {
const { flags } = this.parse(InitCommand)
await this.authenticate()

this.log('Configure continuous integration for a site')
debugger
const repo = await this.loadRepo()
const site = await createOrFindSite(this, flags)

if (flags.manual) {
await configManual(this, site, repo)
} else {
switch (repo.provider) {
case 'github': {
await configGithub(this, site, repo)
break
}
case 'gitlab':
default: {
this.error('No configurator found for the git hosting service')
}
}
}
}
}

InitCommand.description = `${renderShortDesc('Configure continuous deployment')}`

InitCommand.flags = {
manual: flags.boolean(),
force: flags.boolean()
}

module.exports = InitCommand
224 changes: 0 additions & 224 deletions src/commands/init/index.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/commands/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class StatusCommand extends Command {
'Account slug': get(personal, 'slug'),
'Account id': get(personal, 'id'),
Name: get(personal, 'billing_name'),
Email: get(personal, 'billing_email')
Email: get(personal, 'billing_email'),
Github: this.global.get('ghauth.user')
}
const teamsData = {}

Expand Down
32 changes: 32 additions & 0 deletions src/utils/init/config-github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const promisify = require('util.promisify')
const ghauth = promisify(require('ghauth'))
const version = require('../../../package.json').version
const os = require('os')
const octokit = require('@octokit/rest')

const UA = 'Netlify CLI ' + version

module.exports = configGithub
async function configGithub(ctx, site, repo) {
let ghtoken = ctx.global.get('ghauth')

if (!ghtoken) {
const newToken = await ghauth({
noSave: true,
scopes: ['repo'],
userAgent: UA,
note: `Netlify CLI ${os.userInfo().username}@${os.hostname()}`
})
ctx.global.set('ghauth', newToken)
ghtoken = newToken
}

const kit = octokit()

kit.authenticate({
type: 'oauth',
token: ghtoken.token
})

// const key = await ctx.netlify.createDeployKey()
}

0 comments on commit 80c2f0a

Please sign in to comment.