Skip to content

Commit

Permalink
add parametros de entrada e helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Lariel committed Aug 16, 2024
1 parent 5151a55 commit a8c5cf2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ Executar `npm i -g fetch-pullrequests` para instalar o projeto.

### Execução
Após a instalação global, executar `fetch-pullrequests` em qualquer local.
Executar `fetch-pullrequests -h` para obter ajuda.
86 changes: 83 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#! /usr/bin/env node
const https = require('https');
const pat=process.env.AZURE_PAT; //Token gerado no Azure > PAT _usersSettings/tokens
const versionInfo = require('../package').version;
//console.log('AZURE_PAT: ',pat);
const token64=Buffer.from(':'+pat).toString('base64');
const { P_HELP, P_VERSION, P_DEBUG } = require('./params');
const { REPOS, ORGANIZATION, PROJECT, REVIEWER } = require(`${process.env.FETCH_PR}/configs`);
const paramsList = process.argv.slice(2);
const apiVersion='api-version=7.1-preview.1';
const baseUrl=`https://dev.azure.com/${ORGANIZATION}/${PROJECT}`;
//console.log('baseUrl: ',baseUrl);
const INVALID_PARAMS = `Invalid params.`;

const colVoteWidth = 8;
const colAuthorWidth = 20;
Expand All @@ -33,7 +37,11 @@ const options = {
}
};

getPullRequests();
let helpParam;
let versionParam;
let debugParam;

handleParams(paramsList);

function ellipsis(text, limit) {
return text.length <= limit ? text : text.substring(0, limit-3).trim()+'...';
Expand All @@ -44,8 +52,11 @@ function formatResult(pr) {
const author = ` | ${ellipsis(pr.createdBy.displayName, colAuthorWidth).padEnd(colAuthorWidth, ' ')}`;
const title = ` | ${ellipsis(pr.title, colTitleWidth).padEnd(colTitleWidth, ' ')}`;
const url = ` | ${baseUrl}/_git/${pr.repository.name}/pullrequest/${pr.pullRequestId}`;
console.log(`${vote}${author}${title}${url}`);
//console.log(pr);
if (debugParam){
console.log(pr);
} else {
console.log(`${vote}${author}${title}${url}`);
}
}

function filterPullRequest(pullRequest) {
Expand Down Expand Up @@ -87,4 +98,73 @@ function getPullRequests() {
});
}
);
}

function handleHelpParam(param) {

const HELP_TEXT = `
------------------------------------------------------------------------------------------------------------------
| Help: |
| Usage: |
| $ fetch-pullrequests
| |
| Options: |
| ${P_HELP.value} ${P_HELP.helpText} [${P_HELP.type}] |
| ${P_VERSION.value} ${P_VERSION.helpText} [${P_VERSION.type}] |
| ${P_DEBUG.value} ${P_DEBUG.helpText} [${P_DEBUG.type}] |
| |
| Configs found: |
| Repos: ${reposFilter.length} |
| Revisor: ${REVIEWER} |
| |
| |
------------------------------------------------------------------------------------------------------------------
`

const doHelp = () => {
helpParam = true;
console.log(HELP_TEXT);
}

P_HELP.values.includes(param) ? doHelp() : null;
}

function handleVersionParam(param) {

const showVersion = () => {
versionParam = true;
console.log(` Version: ${versionInfo}`);
}

P_VERSION.values.includes(param) ? showVersion() : null;
}

function handleDebugParam(param) {

const activeDebug = () => {
debugParam = true;
console.log(' Debug mode active!');
console.log(` Repos: ${reposFilter.length}`);
console.log(` Revisor: ${REVIEWER}`);
}

P_DEBUG.values.includes(param) ? activeDebug() : null;
}

function handleParams(paramsList) {

paramsList.forEach(param => {
handleHelpParam(param);
handleVersionParam(param);
handleDebugParam(param);
});

if (((!envParam && !serviceParam) || (tagParam && serviceParam))
&& !helpParam && !versionParam) {
console.error(INVALID_PARAMS +GREETINGS);
return;
}

getPullRequests();

}
17 changes: 17 additions & 0 deletions bin/params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const P_HELP = {
values: ['-h','-help'],
helpText: 'Show this help',
type: 'boolean'
};
const P_VERSION = {
values: ['-v','-version'],
helpText: 'Show version information',
type: 'boolean'
};
const P_DEBUG = {
values: ['-debug'],
helpText: 'Shows more information while running',
type: 'boolean'
};

module.exports = {P_HELP, P_VERSION, P_DEBUG};
3 changes: 2 additions & 1 deletion bin/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const GREETINGS = `
Welcome!`
Welcome!
To show the help information, type $ mon -help`;
console.log(GREETINGS);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-pullrequests",
"version": "0.1.1",
"version": "0.2.0",
"description": "CLI para buscar pull requests no Azure DevOps",
"main": "bin/index.js",
"repository": {
Expand Down

0 comments on commit a8c5cf2

Please sign in to comment.