bjira 0.0.3 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sprint.js CHANGED
@@ -1,9 +1,6 @@
1
- import inquirer from 'inquirer';
2
- import Table from 'cli-table3';
3
-
1
+ import Ask from './ask.js';
4
2
  import Command from './command.js';
5
3
  import Jira from './jira.js';
6
- import ErrorHandler from './errorhandler.js';
7
4
  import Project from './project.js';
8
5
 
9
6
  class Sprint extends Command {
@@ -22,18 +19,14 @@ class Sprint extends Command {
22
19
  return;
23
20
  }
24
21
 
25
- try {
26
- await jira.spin('Adding the issue to the sprint...',
27
- jira.apiAgileRequest(`/sprint/${sprintId}/issue`, {
28
- method: 'POST',
29
- followAllRedirects: true,
30
- body: {
31
- issues: [id]
32
- }
33
- }));
34
- } catch (e) {
35
- ErrorHandler.showError(jira, e);
36
- }
22
+ await jira.spin('Adding the issue to the sprint...',
23
+ jira.apiAgileRequest(`/sprint/${sprintId}/issue`, {
24
+ method: 'POST',
25
+ followAllRedirects: true,
26
+ body: {
27
+ issues: [id]
28
+ }
29
+ }));
37
30
  });
38
31
 
39
32
  sprintCmd.command('remove')
@@ -42,18 +35,14 @@ class Sprint extends Command {
42
35
  .action(async id => {
43
36
  const jira = new Jira(program);
44
37
 
45
- try {
46
- await jira.spin('Adding the issue to the sprint...',
47
- jira.apiAgileRequest("/backlog/issue", {
48
- method: 'POST',
49
- followAllRedirects: true,
50
- body: {
51
- issues: [id]
52
- }
53
- }));
54
- } catch (e) {
55
- ErrorHandler.showError(jira, e);
56
- }
38
+ await jira.spin('Adding the issue to the sprint...',
39
+ jira.apiAgileRequest("/backlog/issue", {
40
+ method: 'POST',
41
+ followAllRedirects: true,
42
+ body: {
43
+ issues: [id]
44
+ }
45
+ }));
57
46
  });
58
47
  }
59
48
 
@@ -63,71 +52,23 @@ class Sprint extends Command {
63
52
  jira.api.getAllBoards(undefined, undefined, undefined, undefined,
64
53
  project.key));
65
54
 
66
- const boardNames = [];
67
- const boardIds = [];
68
-
69
- boardList.values.forEach(board => {
70
- boardNames.push(board.name);
71
- boardIds.push(board.id);
72
- });
73
-
74
- const boardQuestion = [{
75
- type: 'list',
76
- name: 'board',
77
- message: 'Board:',
78
- choices: boardNames,
79
- filter: name => {
80
- const pos = boardNames.indexOf(name);
81
- return {
82
- pos,
83
- name,
84
- id: boardIds[pos]
85
- };
86
- }
87
- }];
88
-
89
- const boardAnswer = await inquirer.prompt(boardQuestion);
55
+ const boardPos = await Ask.askList('Board:', boardList.values.map(board => board.name));
90
56
 
91
57
  const sprintList = await jira.spin('Retrieving sprints...',
92
- jira.api.getAllSprints(boardAnswer.board.id));
93
-
94
- const sprintNames = [];
95
- const sprintIds = [];
58
+ jira.api.getAllSprints(boardList.values[boardPos].id));
96
59
 
97
- sprintList.values.forEach(sprint => {
98
- if (sprint.state === 'active' || sprint.state === 'future') {
99
- sprintNames.push(sprint.name);
100
- sprintIds.push(sprint.id);
101
- }
102
- });
60
+ const sprints = sprintList.values.filter(sprint => sprint.state === 'active' || sprint.state === 'future');
103
61
 
104
- if (sprintNames.length === 0) {
62
+ if (sprints.length === 0) {
105
63
  return 0;
106
64
  }
107
65
 
108
- let sprintId = sprintIds[0];
109
-
110
- if (sprintNames.length > 1) {
111
- const sprintQuestion = [{
112
- type: 'list',
113
- name: 'sprint',
114
- message: 'Board:',
115
- choices: sprintNames,
116
- filter: name => {
117
- const pos = sprintNames.indexOf(name);
118
- return {
119
- pos,
120
- name,
121
- id: sprintIds[pos]
122
- };
123
- }
124
- }];
125
-
126
- const sprintAnswer = await inquirer.prompt(sprintQuestion);
127
- sprintId = sprintAnswer.sprint.id;
66
+ if (sprints.length > 1) {
67
+ const sprintPos = await Ask.askList('Sprint:', sprints.map(sprint => sprint.name));
68
+ return sprints[sprintPos].id;
128
69
  }
129
70
 
130
- return sprintId;
71
+ return sprints[0].id;
131
72
  }
132
73
  };
133
74
 
package/src/table.js ADDED
@@ -0,0 +1,162 @@
1
+ import color from 'chalk';
2
+
3
+ class Table {
4
+ constructor(options) {
5
+ this._columns = [];
6
+ this._rows = 0;
7
+
8
+ options.head?.forEach(head => {
9
+ this._columns.push(this._createColumn(head, 0));
10
+ });
11
+ }
12
+
13
+ addRow(row) {
14
+ while (this._columns.length < row.length) {
15
+ this._columns.push(this._createColumn(undefined, this._rows));
16
+ }
17
+
18
+ row.forEach((field, pos) => {
19
+ if (typeof field !== "object") field = {
20
+ text: field
21
+ };
22
+ field.text = ("" + field.text).split("\n");
23
+ this._columns[pos].rows.push(field);
24
+ });
25
+
26
+ for (let i = row.length; i < this._columns.length; ++i) this._columns[i].rows.push({});
27
+
28
+ ++this._rows;
29
+ }
30
+
31
+ addRows(rows) {
32
+ rows.forEach(row => this.addRow(row));
33
+ }
34
+
35
+ toString() {
36
+ if (!this._columns.length) {
37
+ return "";
38
+ }
39
+
40
+ this._columns.forEach(column => this._computeColumnWidth(column));
41
+
42
+ const totalWidth = this._columns.reduce((x, column) => x + column.width, this._columns.length - 1);
43
+ if (totalWidth > process.stdout.columns) {
44
+ this._resizeWidthOf(totalWidth - process.stdout.columns + (this._columns.length - 1));
45
+ }
46
+
47
+ const lines = [];
48
+
49
+ if (this._columns.find(column => !!column.head)) {
50
+ lines.push(this._columns.map(column => (color.red(this._toWidth(column.head || "", column.width)))).join(" "));
51
+ }
52
+
53
+ for (let row = 0; row < this._rows; ++row) {
54
+ const rowHeight = Math.max(...this._columns.map(column => this._computeRowHeight(column, row)));
55
+
56
+ for (let i = 0; i < rowHeight; ++i) {
57
+ lines.push(this._columns.map(column => this._colorize(column.rows[row], this._toWidth(this._computeLine(column.rows[row], i), column.width))).join(" "));
58
+ }
59
+ }
60
+
61
+ return lines.join("\n");
62
+ }
63
+
64
+ _createColumn(head, rows) {
65
+ return {
66
+ head,
67
+ rows: new Array(rows),
68
+ width: 0
69
+ }
70
+ }
71
+
72
+ _computeColumnWidth(column) {
73
+ column.width = Math.max(...column.rows.map(row =>
74
+ Math.max(...row.text.map(line => line.length))
75
+ ));
76
+
77
+ if (column.head) column.width = Math.max(column.width, column.head.length);
78
+ }
79
+
80
+ _computeRowHeight(column, row) {
81
+ return column.rows[row].text.length;
82
+ }
83
+
84
+ _toWidth(str, width) {
85
+ str = str || "";
86
+
87
+ let strLength = str.length;
88
+ if (strLength > width) {
89
+ return this._truncate(str, width - 1) + "…";
90
+ }
91
+
92
+ for (let strLength = str.length; strLength < width; ++strLength) str += " ";
93
+ return str;
94
+ }
95
+
96
+ _computeLine(row, subRow) {
97
+ return row.text.length < subRow ? "" : row.text[subRow];
98
+ }
99
+
100
+ _resizeWidthOf(size) {
101
+ for (; size > 0; --size) {
102
+ this._resizeWidthOfOne();
103
+ }
104
+
105
+ this._columns.forEach(column => {
106
+ column.rows.forEach((row, pos) => {
107
+ let lines = [];
108
+ row.text.forEach(line => {
109
+ lines = lines.concat(this._maybeSplitRow(line, column.width));
110
+ });
111
+ column.rows[pos].text = lines;
112
+ });
113
+ });
114
+ }
115
+
116
+ _maybeSplitRow(row, width) {
117
+ if (row.length < width) return [row];
118
+
119
+ const rows = [];
120
+ let currentRow = "";
121
+ for (let part of row.split(" ")) {
122
+ if (currentRow.length === 0) {
123
+ currentRow = part;
124
+ } else if ((currentRow.length + part.length + 1) < width) {
125
+ currentRow += " " + part;
126
+ } else {
127
+ rows.push(currentRow);
128
+ currentRow = part;
129
+ }
130
+ }
131
+
132
+ if (currentRow.length !== 0) {
133
+ rows.push(currentRow);
134
+ }
135
+
136
+ return rows;
137
+ }
138
+
139
+ _resizeWidthOfOne() {
140
+ const max = Math.max(...this._columns.map(column => column.width));
141
+ for (let column of this._columns) {
142
+ if (column.width === max) {
143
+ --column.width;
144
+ break;
145
+ }
146
+ }
147
+ }
148
+
149
+ _truncate(str, width) {
150
+ return str.slice(0, width);
151
+ }
152
+
153
+ _colorize(row, text) {
154
+ if (!("color" in row)) {
155
+ return text;
156
+ }
157
+
158
+ return color[row.color].apply(null, [text]);
159
+ }
160
+ };
161
+
162
+ export default Table;
package/.babelrc DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "presets": [
3
- ["@babel/env", {
4
- "targets": {
5
- "node": "current"
6
- }
7
- }]
8
- ],
9
- "plugins": [
10
- "@babel/plugin-proposal-class-properties",
11
- "@babel/plugin-proposal-object-rest-spread"
12
- ]
13
- }
package/dist/command.js DELETED
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- class Command {
9
- addOptions(program) {}
10
-
11
- }
12
-
13
- ;
14
- var _default = Command;
15
- exports.default = _default;
package/dist/comment.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _execa = _interopRequireDefault(require("execa"));
9
-
10
- var _inquirer = _interopRequireDefault(require("inquirer"));
11
-
12
- var _fs = _interopRequireDefault(require("fs"));
13
-
14
- var _temp = _interopRequireDefault(require("temp"));
15
-
16
- var _command = _interopRequireDefault(require("./command.js"));
17
-
18
- var _jira = _interopRequireDefault(require("./jira.js"));
19
-
20
- var _errorhandler = _interopRequireDefault(require("./errorhandler.js"));
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- class Comment extends _command.default {
25
- addOptions(program) {
26
- const commentCmd = program.command('comment').description('Add a comment to an issue').argument('<id>', 'The issue ID').action(async id => {
27
- _temp.default.track();
28
-
29
- let file;
30
-
31
- try {
32
- file = _temp.default.openSync('jira');
33
- } catch (e) {
34
- console.log("Failed to open a temporary file");
35
- return;
36
- }
37
-
38
- const code = await new Promise(resolve => {
39
- const subprocess = (0, _execa.default)(process.env.EDITOR, [file.path], {
40
- detached: true,
41
- stdio: 'inherit'
42
- });
43
- subprocess.on('error', () => resolve(-1));
44
- subprocess.on('close', resolve);
45
- });
46
-
47
- if (code !== 0) {
48
- console.log("Failed to run the app");
49
- return;
50
- }
51
-
52
- let comment = _fs.default.readFileSync(file.path);
53
-
54
- comment = comment.toString().trim();
55
-
56
- if (comment === "") {
57
- console.log("No comment message");
58
- return;
59
- }
60
-
61
- const jira = new _jira.default(program);
62
- const data = await jira.spin('Adding the comment...', jira.api.addComment(id, comment));
63
- });
64
- }
65
-
66
- }
67
-
68
- ;
69
- var _default = Comment;
70
- exports.default = _default;
package/dist/create.js DELETED
@@ -1,127 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _chalk = _interopRequireDefault(require("chalk"));
9
-
10
- var _inquirer = _interopRequireDefault(require("inquirer"));
11
-
12
- var _cliTable = _interopRequireDefault(require("cli-table3"));
13
-
14
- var _command = _interopRequireDefault(require("./command.js"));
15
-
16
- var _jira = _interopRequireDefault(require("./jira.js"));
17
-
18
- var _issue = _interopRequireDefault(require("./issue.js"));
19
-
20
- var _project = _interopRequireDefault(require("./project.js"));
21
-
22
- var _user = _interopRequireDefault(require("./user.js"));
23
-
24
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
- class Create extends _command.default {
27
- addOptions(program) {
28
- const cmd = program.command('create').description('Create a new issue').action(async () => {
29
- const jira = new _jira.default(program);
30
- const project = await _project.default.pickProject(jira);
31
- const issueQuestions = [{
32
- type: 'list',
33
- name: 'issueType',
34
- message: 'Issue type:',
35
- choices: project.issueTypes,
36
- filter: name => project.issueTypes.find(obj => obj.name === name)
37
- }, {
38
- type: 'input',
39
- name: 'summary',
40
- message: 'Summary:',
41
- default: 'New Issue'
42
- }, {
43
- type: 'input',
44
- name: 'description',
45
- message: 'Description:'
46
- }, {
47
- type: 'confirm',
48
- name: 'assign',
49
- message: 'Do you want to assign it?'
50
- }]; // Ask for the issue name and type
51
-
52
- const issueAnswers = await _inquirer.default.prompt(issueQuestions); // Create the issue object
53
-
54
- const newIssue = {
55
- fields: {
56
- project: {
57
- key: project.key
58
- },
59
- summary: issueAnswers.summary,
60
- issuetype: {
61
- id: issueAnswers.issueType.id
62
- }
63
- }
64
- };
65
-
66
- if (issueAnswers.description) {
67
- newIssue.fields.description = issueAnswers.description;
68
- }
69
-
70
- if (issueAnswers.assign) {
71
- const userList = await _user.default.pickUser(jira);
72
- const userNames = [];
73
- const userIds = [];
74
- userList.forEach(user => {
75
- if (user.active) {
76
- userNames.push(user.displayName);
77
- userIds.push(user.accountId);
78
- }
79
- });
80
- const assigneeQuestion = [{
81
- type: 'list',
82
- name: 'assignee',
83
- message: 'Assignee:',
84
- choices: userNames,
85
- filter: name => {
86
- const pos = userNames.indexOf(name);
87
- return {
88
- pos,
89
- name,
90
- id: userIds[pos]
91
- };
92
- }
93
- }];
94
- const assigneeAnswer = await _inquirer.default.prompt(assigneeQuestion);
95
- newIssue.fields.assignee = {
96
- accountId: assigneeAnswer.assignee.id
97
- };
98
- }
99
-
100
- if (issueAnswers.issueType.name === 'Task') {
101
- const parentIssueQuestion = [{
102
- type: 'input',
103
- name: 'issueParentName',
104
- message: 'Please provide the epic:'
105
- }];
106
- const parentIssueAnswer = await _inquirer.default.prompt(parentIssueQuestion);
107
-
108
- if (parentIssueAnswer.issueParentName !== '') {
109
- newIssue.fields.parent = {
110
- key: parentIssueAnswer.issueParentName
111
- };
112
- }
113
- }
114
-
115
- const issue = await jira.spin('Creating the issue...', jira.api.addNewIssue(newIssue));
116
- console.log('');
117
- console.log('New issue: ' + _chalk.default.bold.red(issue.key));
118
- console.log(_chalk.default.blue(_issue.default.url(jira, issue.key)));
119
- console.log('');
120
- });
121
- }
122
-
123
- }
124
-
125
- ;
126
- var _default = Create;
127
- exports.default = _default;
@@ -1,37 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _chalk = _interopRequireDefault(require("chalk"));
9
-
10
- var _cliTable = _interopRequireDefault(require("cli-table3"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- class ErrorHandler {
15
- static showError(jira, e) {
16
- const table = new _cliTable.default({
17
- chars: jira.tableChars,
18
- head: ['Errors']
19
- });
20
- e.error.errorMessages.forEach(error => table.push([_chalk.default.blue(error)]));
21
- console.log(table.toString());
22
- }
23
-
24
- static showWarningMessages(jira, messages) {
25
- const table = new _cliTable.default({
26
- chars: jira.tableChars,
27
- head: ['Warnings']
28
- });
29
- messages.forEach(warning => table.push([_chalk.default.blue(warning)]));
30
- console.log(table.toString());
31
- }
32
-
33
- }
34
-
35
- ;
36
- var _default = ErrorHandler;
37
- exports.default = _default;