1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.githubIssueToClubhouseStory = exports.clubhouseStoryToGithubIssue = exports.loadConfig = exports.saveConfig = undefined;
|
7 |
|
8 | var _regenerator = require('babel-runtime/regenerator');
|
9 |
|
10 | var _regenerator2 = _interopRequireDefault(_regenerator);
|
11 |
|
12 | var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
13 |
|
14 | var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
15 |
|
16 | var clubhouseStoryToGithubIssue = exports.clubhouseStoryToGithubIssue = function () {
|
17 | var _ref = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee(clubhouseStoryURL, githubRepoURL) {
|
18 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
19 |
|
20 | var _parseClubhouseStoryU, storyId, _parseGithubRepoURL, owner, repo, clubhouseUsers, clubhouseUsersById, story, unsavedIssue, unsavedIssueComments, issue;
|
21 |
|
22 | return _regenerator2.default.wrap(function _callee$(_context) {
|
23 | while (1) {
|
24 | switch (_context.prev = _context.next) {
|
25 | case 0:
|
26 | _assertOption('githubToken', options);
|
27 | _assertOption('clubhouseToken', options);
|
28 |
|
29 | _parseClubhouseStoryU = (0, _urlParse.parseClubhouseStoryURL)(clubhouseStoryURL), storyId = _parseClubhouseStoryU.storyId;
|
30 | _parseGithubRepoURL = (0, _urlParse.parseGithubRepoURL)(githubRepoURL), owner = _parseGithubRepoURL.owner, repo = _parseGithubRepoURL.repo;
|
31 | _context.next = 6;
|
32 | return (0, _clubhouse.listUsers)(options.clubhouseToken);
|
33 |
|
34 | case 6:
|
35 | clubhouseUsers = _context.sent;
|
36 | clubhouseUsersById = clubhouseUsers.reduce(function (acc, user) {
|
37 | acc[user.id] = user;
|
38 | return acc;
|
39 | });
|
40 | _context.next = 10;
|
41 | return (0, _clubhouse.getStory)(options.clubhouseToken, storyId);
|
42 |
|
43 | case 10:
|
44 | story = _context.sent;
|
45 | unsavedIssue = _storyToIssue(clubhouseStoryURL, story);
|
46 | unsavedIssueComments = _presentClubhouseComments(story.comments, clubhouseUsersById);
|
47 | _context.next = 15;
|
48 | return (0, _gitHub.createIssue)(options.githubToken, owner, repo, unsavedIssue);
|
49 |
|
50 | case 15:
|
51 | issue = _context.sent;
|
52 | _context.next = 18;
|
53 | return _bluebird2.default.each(unsavedIssueComments, function (comment) {
|
54 | return (0, _gitHub.createIssueComment)(options.githubToken, owner, repo, issue.number, comment);
|
55 | });
|
56 |
|
57 | case 18:
|
58 | return _context.abrupt('return', issue);
|
59 |
|
60 | case 19:
|
61 | case 'end':
|
62 | return _context.stop();
|
63 | }
|
64 | }
|
65 | }, _callee, this);
|
66 | }));
|
67 |
|
68 | return function clubhouseStoryToGithubIssue(_x, _x2) {
|
69 | return _ref.apply(this, arguments);
|
70 | };
|
71 | }();
|
72 |
|
73 | var githubIssueToClubhouseStory = exports.githubIssueToClubhouseStory = function () {
|
74 | var _ref2 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee2(githubIssueURL, clubhouseProject) {
|
75 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
76 |
|
77 | var users, authorId, projects, project, projectId, _parseGithubIssueURL, owner, repo, issueNumber, issue, issueComments, unsavedStory, story;
|
78 |
|
79 | return _regenerator2.default.wrap(function _callee2$(_context2) {
|
80 | while (1) {
|
81 | switch (_context2.prev = _context2.next) {
|
82 | case 0:
|
83 | _assertOption('githubToken', options);
|
84 | _assertOption('clubhouseToken', options);
|
85 |
|
86 | _context2.next = 4;
|
87 | return (0, _clubhouse.listUsers)(options.clubhouseToken);
|
88 |
|
89 | case 4:
|
90 | users = _context2.sent;
|
91 | authorId = users[0].id;
|
92 | _context2.next = 8;
|
93 | return (0, _clubhouse.listProjects)(options.clubhouseToken);
|
94 |
|
95 | case 8:
|
96 | projects = _context2.sent;
|
97 | project = projects.find(function (project) {
|
98 | return project.name === clubhouseProject;
|
99 | });
|
100 |
|
101 | if (project) {
|
102 | _context2.next = 12;
|
103 | break;
|
104 | }
|
105 |
|
106 | throw new Error('The \'' + clubhouseProject + '\' project wasn\'t found in your Clubhouse');
|
107 |
|
108 | case 12:
|
109 | projectId = project.id;
|
110 | _parseGithubIssueURL = (0, _urlParse.parseGithubIssueURL)(githubIssueURL), owner = _parseGithubIssueURL.owner, repo = _parseGithubIssueURL.repo, issueNumber = _parseGithubIssueURL.issueNumber;
|
111 | _context2.next = 16;
|
112 | return (0, _gitHub.getIssue)(options.githubToken, owner, repo, issueNumber);
|
113 |
|
114 | case 16:
|
115 | issue = _context2.sent;
|
116 | _context2.next = 19;
|
117 | return (0, _gitHub.getCommentsForIssue)(options.githubToken, owner, repo, issueNumber);
|
118 |
|
119 | case 19:
|
120 | issueComments = _context2.sent;
|
121 | unsavedStory = _issueToStory(authorId, projectId, issue, issueComments);
|
122 | story = (0, _clubhouse.createStory)(options.clubhouseToken, unsavedStory);
|
123 | return _context2.abrupt('return', story);
|
124 |
|
125 | case 23:
|
126 | case 'end':
|
127 | return _context2.stop();
|
128 | }
|
129 | }
|
130 | }, _callee2, this);
|
131 | }));
|
132 |
|
133 | return function githubIssueToClubhouseStory(_x4, _x5) {
|
134 | return _ref2.apply(this, arguments);
|
135 | };
|
136 | }();
|
137 |
|
138 | var _config = require('./util/config');
|
139 |
|
140 | Object.defineProperty(exports, 'saveConfig', {
|
141 | enumerable: true,
|
142 | get: function get() {
|
143 | return _config.saveConfig;
|
144 | }
|
145 | });
|
146 | Object.defineProperty(exports, 'loadConfig', {
|
147 | enumerable: true,
|
148 | get: function get() {
|
149 | return _config.loadConfig;
|
150 | }
|
151 | });
|
152 |
|
153 | var _bluebird = require('bluebird');
|
154 |
|
155 | var _bluebird2 = _interopRequireDefault(_bluebird);
|
156 |
|
157 | var _gitHub = require('./fetchers/gitHub');
|
158 |
|
159 | var _clubhouse = require('./fetchers/clubhouse');
|
160 |
|
161 | var _urlParse = require('./util/urlParse');
|
162 |
|
163 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
164 |
|
165 | function _assertOption(name, options) {
|
166 | if (!options[name]) {
|
167 | throw new Error(name + ' option must be provided');
|
168 | }
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 | function _issueToStory(authorId, projectId, issue, issueComments) {
|
174 | return {
|
175 | project_id: projectId,
|
176 | name: issue.title,
|
177 | description: issue.body,
|
178 | comments: _presentGithubComments(authorId, issueComments),
|
179 | created_at: issue.created_at,
|
180 | updated_at: issue.updated_at,
|
181 | external_id: issue.url
|
182 | };
|
183 | }
|
184 |
|
185 | function _presentGithubComments(authorId, issueComments) {
|
186 | return issueComments.map(function (issueComment) {
|
187 | return {
|
188 | author_id: authorId,
|
189 | text: '**[Comment from GitHub user @' + issueComment.user.login + ':]** ' + issueComment.body,
|
190 | created_at: issueComment.created_at,
|
191 | updated_at: issueComment.updated_at,
|
192 | external_id: issueComment.url
|
193 | };
|
194 | });
|
195 | }
|
196 |
|
197 | function _storyToIssue(clubhouseStoryURL, story) {
|
198 | var renderedTasks = story.tasks.map(function (task) {
|
199 | return '- [' + (task.complete ? 'x' : ' ') + '] ' + task.description;
|
200 | }).join('\n');
|
201 | var renderedTasksSection = renderedTasks.length > 0 ? '\n### Tasks\n\n' + renderedTasks : '';
|
202 | var originalStory = 'From [ch' + story.id + '](' + clubhouseStoryURL + ')';
|
203 |
|
204 | return {
|
205 | title: story.name,
|
206 | body: originalStory + '\n\n' + story.description + renderedTasksSection
|
207 | };
|
208 | }
|
209 |
|
210 | function _presentClubhouseComments(comments, clubhouseUsersById) {
|
211 | return comments.map(function (comment) {
|
212 | var user = clubhouseUsersById[comment.author_id] || { username: comment.author_id };
|
213 | return {
|
214 | body: '**[Comment from Clubhouse user @' + user.username + ':]** ' + comment.text
|
215 | };
|
216 | });
|
217 | } |
\ | No newline at end of file |