-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissues.js
172 lines (150 loc) · 4.6 KB
/
issues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const shell = require('shelljs');
const fs = require('fs');
const path = require('path');
// Check environment
/**
* Show an error and exit the process.
* @param {number} error code error
*/
function showError(error) {
if (error) {
console.error(`Error!: ${error}`);
process.exit(1);
}
}
/**
* Check is current folder is under control version
*/
function isGitFolder() {
let isGitFolder = shell.exec("git rev-parse --is-inside-work-tree", {silent: true});
if (!isGitFolder || !fs.existsSync(".git")) {
showError('The current folder must be the root of a git repo when running this command!');
}
}
// Execute gh command
/**
* Makes a GitHub API request and return the result or exit the process if it fails
* @param {...any} args arguments for the request
* @returns {string} response
*/
function gh(...args) {
let command = `gh ${args.join('')}`;
let result = shell.exec(command, { silent: true, stdio: "inherit" }); // silent option don't echo program output to console
if (result.code != 0) {
shell.echo(`Sorry! This issue or repository does not exits`);
shell.exit(result.code);
}
return result.stdout.replace(/\s+$/,'');
}
// Access gh api data
/**
* Ask GitHUb API who is logged
* @returns {string} response
*/
function getUserLogin() {
let command = "api 'user' --jq .login";
return gh(command);
}
/**
* Ask GitHUb API for the current repo issues
* @param {string} state state of the issues the request is looking for
* @returns {string} response
*/
function getThisRepoIssues(state) {
isGitFolder();
let command = `api repos/:owner/:repo/issues?state=${state} --template \"$(cat ${path.join(__dirname, 'templates', 'repo.gotemplate')})\"`;
return gh(command);
}
/**
* Ask GitHUb API for issues of an specific owner, repo and state
* @param {string} owner
* @param {string} repo
* @param {string} state
* @returns {string} response
*/
function getRepoIssues(owner, repo, state) {
let command =`api /repos/${owner}/${repo}/issues?state=${state} --template \"$(cat ${path.join(__dirname, 'templates', 'repo.gotemplate')})\"`;
return gh(command);
}
/**
* Ask GitHUb API for an specific issue from its id of an specific owner and repo
* @param {string} owner
* @param {string} repo
* @param {string} number
* @returns {string} response
*/
function getIssue(owner, repo, number) {
if (repo == ":repo")
isGitFolder();
let command =`api /repos/${owner}/${repo}/issues/${number} --template \"$(cat ${path.join(__dirname, 'templates', 'issue.gotemplate')})\"`;
return gh(command);
}
/**
* Get the assigned issues of a user
* @param {string} state the states of the issues the request is asking for
* @returns {string} response
*/
function getAssignedIssuesByUser(state) {
let command =`api /issues?state=${state} --template \"$(cat ${path.join(__dirname, 'templates', 'assignee.gotemplate')})\"`;
return gh(command);
}
/**
* Get the assigned issues in an organization
* @param {string} org
* @param {string} state
* @returns {string} response
*/
function getAssignedIssuesByOrg(org, state) {
let command =`api /orgs/${org}/issues?state=${state} --template \"$(cat ${path.join(__dirname, 'templates', 'assignee.gotemplate')})\"`;
return gh(command);
}
/**
* Open a new issue with some params
* @param {string} title
* @param {string} body
* @param {string} owner
* @param {string*} repo
* @returns {string} successful message
*/
function openIssue(title, body, owner, repo) {
isGitFolder();
gh(`api -X POST /repos/${owner}/${repo}/issues -f title="${title}" -f body="${body}"`);
return `A new issue has been created`;
}
/**
* Update an issue state from an id
* @param {string} owner
* @param {string} repo
* @param {string} number
* @param {string} state
* @returns {string} response
*/
function updateIssueState(owner, repo, number, state) {
isGitFolder();
gh(`api -X PATCH /repos/${owner}/${repo}/issues/${number} -f state=${state}`);
return `Issue ${number} is now ${state}`;
}
/**
* Update an issue fields from an id
* @param {string} owner
* @param {string} repo
* @param {string} number
* @param {string} fields
* @returns {string} response
*/
function updateIssue(owner, repo, number, fields) {
isGitFolder();
gh(`api -X PATCH /repos/${owner}/${repo}/issues/${number} ${fields}`);
return `Issue ${number} updated`;
}
module.exports = {
getUserLogin,
getThisRepoIssues,
getRepoIssues,
getIssue,
getAssignedIssuesByUser,
getAssignedIssuesByOrg,
openIssue,
updateIssueState,
updateIssue,
}