From 1c8955bacbfc32af3daf7eca00ff28a713369ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstro=CC=88m?= Date: Fri, 4 Feb 2022 14:28:10 +0100 Subject: [PATCH] new branch select on create server --- .../deployment/testservers/create/create.ts | 44 +++++----------- .../deployment/testservers/logs/logs.ts | 52 +++++++++++++++++++ .../workflow/setupissue/setupissue.ts | 16 +----- src/globals.ts | 15 ++++++ src/services/github_rest.ts | 47 +++++++++++++++++ 5 files changed, 128 insertions(+), 46 deletions(-) create mode 100644 src/commands/deployment/testservers/logs/logs.ts diff --git a/src/commands/deployment/testservers/create/create.ts b/src/commands/deployment/testservers/create/create.ts index 4cdca78..c8fcddd 100644 --- a/src/commands/deployment/testservers/create/create.ts +++ b/src/commands/deployment/testservers/create/create.ts @@ -1,9 +1,10 @@ import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu' const chalk = require('chalk'); -const { Input, Confirm } = require('enquirer'); +const { AutoComplete, Input, Confirm } = require('enquirer'); import { Octokit } from "@octokit/core"; import { GluegunPrint } from 'gluegun'; import { defaultMenuSettings, getSettings } from '../../../../globals'; +import { getRepoBranches } from '../../../../services/github_rest'; const os = require("os"); const STACK_DEFAULTS = { @@ -69,32 +70,6 @@ const getCreateStackCommand = (stackName: string, branch: string, templateFile: return cmd.join(' '); }; -const getBranchName = async (print: GluegunPrint, token: string) => { - const input = new Input({ - type: 'input', - name: 'branch', - message: 'What github branch should be deployed?' - }); - const responseBranch = await input.run(); - - // First ask what branch should be deployed - const octokit = new Octokit({ auth: token }); - - try { - const response = await octokit.request("GET /repos/{owner}/{repo}/branches/{branch}", { - owner: "photowall", - repo: "photowall", - branch: responseBranch - }); - if (response.status === 200) { - return responseBranch; - } - } catch (e) { - print.error('Not a valid branch on photowall repo'); - return null; - } -}; - const getConfirmation = async (branchName: string, stackName: string) => { const url = stackName.replace('photowall-', ''); const confirm = new Confirm({ @@ -125,10 +100,17 @@ module.exports = { return; } - let branchName = null; - while (!branchName) { - branchName = await getBranchName(print, config.gh_token); - } + const branches = await getRepoBranches(toolbox, config.gh_token, 'Photowall', 'photowall'); + const branchOptions = branches.map(item => item.name); + + const promptBranch = new AutoComplete({ + name: 'branchName', + message: 'Select a branch', + limit: 10, + initial: 10, + choices: branchOptions + }); + const branchName = await promptBranch.run(); const stacks = JSON.parse( strings.trim(await system.run(`aws cloudformation list-stacks`)) diff --git a/src/commands/deployment/testservers/logs/logs.ts b/src/commands/deployment/testservers/logs/logs.ts new file mode 100644 index 0000000..42e3acf --- /dev/null +++ b/src/commands/deployment/testservers/logs/logs.ts @@ -0,0 +1,52 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu' +import chalk = require('chalk'); +import { confirmMessage, defaultMenuSettings } from '../../../../globals'; +const { AutoComplete } = require('enquirer'); +const { spawn } = require('child_process'); + +module.exports = { + name: 'logs', + alias: ['l'], + description: 'Tail a log in cloudwatch (l)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + + const { system, strings } = toolbox + + // aws logs tail /aws/codebuild/photowall-test-arwid-1 --follow + // aws logs describe-log-groups --log-group-name-prefix codepipeline + + // const groups = JSON.parse(strings.trim(await system.run(`aws logs describe-log-groups`))); + + // const options = groups.logGroups.map(item => { + // return item.logGroupName; + // }); + + // const prompt = new AutoComplete({ + // name: 'logGroups', + // message: 'Choose a logGroup', + // limit: 30, + // initial: options.length - 1, + // choices: options + // }); + + // const logGroup = await prompt.run(); + const logGroup = 'top'; + + if (await confirmMessage(`Tail logs from ${chalk.yellow(logGroup)}?`)) { + // system.run(`aws logs tail ${logGroup} --follow`); + // const child = spawn(`aws logs tail ${logGroup} --follow`); + const child = spawn(`top`); + child.stdout.setEncoding('utf8'); + child.stdout.on('data', function(data) { + //Here is where the output goes + console.clear(); + console.log(data); + }); + } + + if (toolbox.fromMenu()) { + await toolbox.menu.showMenu('deployment testservers', defaultMenuSettings); + } + } +} diff --git a/src/commands/workflow/setupissue/setupissue.ts b/src/commands/workflow/setupissue/setupissue.ts index 3a20fc7..94ae369 100644 --- a/src/commands/workflow/setupissue/setupissue.ts +++ b/src/commands/workflow/setupissue/setupissue.ts @@ -2,24 +2,10 @@ import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import { Octokit } from '@octokit/core'; import chalk = require('chalk'); import { system } from 'gluegun'; -import { defaultMenuSettings, getSettings, issueToBranchName } from '../../../globals'; +import { confirmMessage, defaultMenuSettings, getSettings, issueToBranchName } from '../../../globals'; import { createPullRequest, getIssueFromRepo, getOpenIssuesFromRepo, getOrgIssues, getOrgRepos } from '../../../services/github_rest'; const { AutoComplete, Confirm, Input } = require('enquirer'); -const confirmMessage = async (message: string) => { - const confirm = new Confirm({ - name: 'confirm', - message: message - }); - - try { - return await confirm.run(); - } catch (e) { - console.error(e); - return false; - } -}; - module.exports = { name: 'SetupIssue', alias: ['si'], diff --git a/src/globals.ts b/src/globals.ts index a2133d0..3bf0d1b 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -1,6 +1,7 @@ import chalk = require("chalk"); const os = require("os"); var snakeCase = require('lodash.snakecase'); +const { AutoComplete, Confirm, Input } = require('enquirer'); export const defaultMenuSettings = { showHelp: false @@ -39,3 +40,17 @@ export const getCurrentBranch = async (path: string, toolbox): Promise { return `${issue.number}-${snakeCase(issue.title).substr(0, 40)}`; } + +export const confirmMessage = async (message: string) => { + const confirm = new Confirm({ + name: 'confirm', + message: message + }); + + try { + return await confirm.run(); + } catch (e) { + console.error(e); + return false; + } +}; diff --git a/src/services/github_rest.ts b/src/services/github_rest.ts index b0828c7..76e249c 100644 --- a/src/services/github_rest.ts +++ b/src/services/github_rest.ts @@ -17,6 +17,53 @@ export const getOrgRepos = async (toolbox, token, org) => { } }; +export const getRepo = async (toolbox, token, owner, repo) => { + const { print } = toolbox; + const octokit = new Octokit({ auth: token }); + try { + const response = await octokit.request("GET /repos/{owner}/{repo}", { + owner: owner, + repo: repo, + }); + if (response.status === 200) { + return response.data; + } + } catch (e) { + print.error('Not a valid organisation for your token access'); + return null; + } +} + +export const getRepoBranches = async (toolbox, token, owner, repo) => { + const { print } = toolbox; + const octokit = new Octokit({ auth: token }); + + const getBranches = async (toolbox, token, owner, repo, page) => { + const response = await octokit.request("GET /repos/{owner}/{repo}/branches", { + owner: owner, + repo: repo, + page: page, + per_page: 100, + }); + return response; + }; + + let allBranches = []; + let response = null; + let page = 0; + while (true) { + response = await getBranches(toolbox, token, owner, repo, page); + if (response.data.length > 0) { + allBranches = [...allBranches, ...response.data]; + } else { + break; + } + page += 1; + } + + return allBranches; +} + export const getOrgIssues = async (toolbox, token) => { // const { print } = toolbox; const repos = await getOrgRepos(toolbox, token, 'Photowall');