added setup issue
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
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 { 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'],
|
||||
description: 'Setup everything for a issue (si)',
|
||||
hidden: false,
|
||||
run: async (toolbox: GluegunMenuToolbox) => {
|
||||
|
||||
const {print, system, strings} = toolbox;
|
||||
|
||||
// Load settings
|
||||
const config = await getSettings(toolbox);
|
||||
if (!config) {
|
||||
await toolbox.menu.showMenu('setup', defaultMenuSettings);
|
||||
return;
|
||||
}
|
||||
|
||||
const repos = await getOrgIssues(toolbox, config.gh_token);
|
||||
const issueOptions = repos.flatMap(repo => {
|
||||
return repo.issues.map(issue => `${issue.number}-${repo.repo}-${issue.title}`);
|
||||
})
|
||||
|
||||
const promptIssues = new AutoComplete({
|
||||
name: 'issues',
|
||||
message: 'Select issue',
|
||||
limit: 5,
|
||||
initial: 5,
|
||||
choices: issueOptions
|
||||
});
|
||||
const issue_response = await promptIssues.run();
|
||||
const issue_number = issue_response.split('-')[0];
|
||||
const repo = issue_response.split('-')[1];
|
||||
|
||||
const issueData = await getIssueFromRepo(toolbox, config.gh_token, repo, issue_number);
|
||||
const branchName = issueToBranchName(issueData);
|
||||
|
||||
if (await confirmMessage(`Create new branch ${chalk.yellow(branchName)} from origin/master?`)) {
|
||||
await system.run(`git checkout -b ${branchName} origin/master`, {cwd: config.photowall_repo, trim: true});
|
||||
await system.run(`git push -u origin ${branchName}`, {cwd: config.photowall_repo, trim: true});
|
||||
}
|
||||
|
||||
if (toolbox.fromMenu()) await toolbox.menu.showMenu('workflow', defaultMenuSettings);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
|
||||
import { defaultMenuSettings } from '../../globals';
|
||||
|
||||
/**
|
||||
* Section1 menu
|
||||
*/
|
||||
module.exports = {
|
||||
name: 'workflow',
|
||||
alias: ['wf'],
|
||||
description: 'Workflow tools (wf)',
|
||||
hidden: false,
|
||||
run: async (toolbox: GluegunMenuToolbox) => {
|
||||
await toolbox.menu.showMenu('workflow', defaultMenuSettings);
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import chalk = require("chalk");
|
||||
const os = require("os");
|
||||
var snakeCase = require('lodash.snakecase');
|
||||
|
||||
export const defaultMenuSettings = {
|
||||
showHelp: false
|
||||
@@ -34,3 +35,7 @@ export const getCurrentBranch = async (path: string, toolbox): Promise<string |
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const issueToBranchName = (issue) => {
|
||||
return `${issue.number}-${snakeCase(issue.title).substr(0, 40)}`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { Octokit } from "@octokit/core";
|
||||
|
||||
export const getOrgRepos = async (toolbox, token, org) => {
|
||||
const { print } = toolbox;
|
||||
const octokit = new Octokit({ auth: token });
|
||||
try {
|
||||
const response = await octokit.request("GET /orgs/{org}/repos", {
|
||||
org: org,
|
||||
per_page: 100,
|
||||
});
|
||||
if (response.status === 200) {
|
||||
return response.data;
|
||||
}
|
||||
} catch (e) {
|
||||
print.error('Not a valid organisation for your token access');
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const getOrgIssues = async (toolbox, token) => {
|
||||
// const { print } = toolbox;
|
||||
const repos = await getOrgRepos(toolbox, token, 'Photowall');
|
||||
const repoNames = repos.map(item => item.name);
|
||||
const allIssues = await Promise.all(repoNames.map(async (repoName) => {
|
||||
const issues = await getOpenIssuesFromRepo(toolbox, token, repoName);
|
||||
// console.log(`${repoName} - issues`, issues.length);
|
||||
// return issues.map(item => `${item.number}-${item.title.substring(0, 10)}`);
|
||||
return {repo: repoName, issues: issues};
|
||||
}));
|
||||
return allIssues.flat();
|
||||
}
|
||||
|
||||
export const getOpenIssuesFromRepo = async (toolbox, token, repo) => {
|
||||
const { print } = toolbox;
|
||||
const octokit = new Octokit({ auth: token });
|
||||
try {
|
||||
const response = await octokit.request("GET /repos/{owner}/{repo}/issues", {
|
||||
owner: "Photowall",
|
||||
repo: repo,
|
||||
per_page: 100,
|
||||
});
|
||||
if (response.status === 200) {
|
||||
return response.data;
|
||||
}
|
||||
} catch (e) {
|
||||
print.error('Not a valid issuenumber for this repository');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const getIssueFromRepo = async (toolbox, token, repo, issue_number) => {
|
||||
const { print } = toolbox;
|
||||
const octokit = new Octokit({ auth: token });
|
||||
try {
|
||||
const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", {
|
||||
owner: "Photowall",
|
||||
repo: repo,
|
||||
issue_number: issue_number,
|
||||
});
|
||||
if (response.status === 200) {
|
||||
return response.data;
|
||||
}
|
||||
} catch (e) {
|
||||
print.error('Not a valid issuenumber for this repository');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const createPullRequest = async (toolbox, token: string, repo: string, branch: string) => {
|
||||
const { print } = toolbox;
|
||||
const octokit = new Octokit({ auth: token });
|
||||
try {
|
||||
const response = await octokit.request('POST /repos/{owner}/{repo}/pulls', {
|
||||
owner: 'Photowall',
|
||||
repo: repo,
|
||||
head: branch,
|
||||
base: 'master'
|
||||
});
|
||||
console.log(`response`, response);
|
||||
// if (response.status === 200) {
|
||||
// return response.data;
|
||||
// }
|
||||
} catch (e) {
|
||||
print.error('Not a valid branch');
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user