@@ -5,23 +5,21 @@ Object.defineProperty(exports, "__esModule", {
55} ) ;
66
77var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
8- /**
9- * Module dependencies.
10- */
118
129var _lodash = require ( 'lodash' ) ;
1310
1411var _github = require ( 'github' ) ;
1512
1613var _github2 = _interopRequireDefault ( _github ) ;
1714
18- var _bluebird = require ( 'bluebird' ) ;
19-
20- var _bluebird2 = _interopRequireDefault ( _bluebird ) ;
21-
2215function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
2316
24- function _asyncToGenerator ( fn ) { return function ( ) { var gen = fn . apply ( this , arguments ) ; return new _bluebird2 . default ( function ( resolve , reject ) { function step ( key , arg ) { try { var info = gen [ key ] ( arg ) ; var value = info . value ; } catch ( error ) { reject ( error ) ; return ; } if ( info . done ) { resolve ( value ) ; } else { return _bluebird2 . default . resolve ( value ) . then ( function ( value ) { step ( "next" , value ) ; } , function ( err ) { step ( "throw" , err ) ; } ) ; } } return step ( "next" ) ; } ) ; } ; }
17+ function _asyncToGenerator ( fn ) { return function ( ) { var gen = fn . apply ( this , arguments ) ; return new Promise ( function ( resolve , reject ) { function step ( key , arg ) { try { var info = gen [ key ] ( arg ) ; var value = info . value ; } catch ( error ) { reject ( error ) ; return ; } if ( info . done ) { resolve ( value ) ; } else { return Promise . resolve ( value ) . then ( function ( value ) { step ( "next" , value ) ; } , function ( err ) { step ( "throw" , err ) ; } ) ; } } return step ( "next" ) ; } ) ; } ; }
18+
19+ function _objectWithoutProperties ( obj , keys ) { var target = { } ; for ( var i in obj ) { if ( keys . indexOf ( i ) >= 0 ) continue ; if ( ! Object . prototype . hasOwnProperty . call ( obj , i ) ) continue ; target [ i ] = obj [ i ] ; } return target ; }
20+ /**
21+ * Module dependencies.
22+ */
2523
2624/**
2725 * GitHub configuration.
@@ -35,6 +33,20 @@ const config = {
3533 version : '3.0.0'
3634} ;
3735
36+ /**
37+ * Get repository owner and name and include a validation.
38+ */
39+
40+ const getRepositoryOptions = repository => {
41+ const [ owner , repo ] = repository . split ( '/' ) ;
42+
43+ if ( ! owner || ! repo ) {
44+ throw new Error ( 'Malformed repository option' ) ;
45+ }
46+
47+ return { owner, repo } ;
48+ } ;
49+
3850/**
3951 * Export `Client`.
4052 */
@@ -45,36 +57,30 @@ class Client {
4557 * Constructor.
4658 */
4759
48- constructor ( { owner, repo, options } ) {
49- this . github = new _github2 . default ( _extends ( { } , config , options ) ) ;
50- this . owner = owner ;
51- this . repo = repo ;
60+ constructor ( _ref ) {
61+ let { token } = _ref ,
62+ options = _objectWithoutProperties ( _ref , [ 'token' ] ) ;
5263
53- _bluebird2 . default . promisifyAll ( this . github . issues ) ;
54- _bluebird2 . default . promisifyAll ( this . github ) ;
55- }
56-
57- /**
58- * Authenticate.
59- */
64+ this . github = new _github2 . default ( _extends ( { } , config , options ) ) ;
6065
61- authenticate ( token ) {
6266 this . github . authenticate ( { token, type : 'oauth' } ) ;
6367 }
6468
6569 /**
6670 * Create a new label with given `name` and `color`.
6771 */
6872
69- createLabel ( name , color ) {
73+ createLabel ( repository , name , color ) {
7074 var _this = this ;
7175
7276 return _asyncToGenerator ( function * ( ) {
73- return yield _this . github . issues . createLabelAsync ( {
77+ const { owner, repo } = getRepositoryOptions ( repository ) ;
78+
79+ return yield _this . github . issues . createLabel ( {
7480 color,
7581 name,
76- owner : _this . owner ,
77- repo : _this . repo
82+ owner,
83+ repo
7884 } ) ;
7985 } ) ( ) ;
8086 }
@@ -83,40 +89,42 @@ class Client {
8389 * Create or update a label with given `name`.
8490 */
8591
86- createOrUpdateLabel ( name , color ) {
92+ createOrUpdateLabel ( repository , name , color ) {
8793 var _this2 = this ;
8894
8995 return _asyncToGenerator ( function * ( ) {
9096 let label = false ;
9197
9298 try {
93- label = yield _this2 . getLabel ( name ) ;
99+ label = yield _this2 . getLabel ( repository , name ) ;
94100 } catch ( err ) {
95101 if ( ! ( 0 , _lodash . has ) ( err , 'code' ) || ( 0 , _lodash . get ) ( err , 'code' ) !== 404 ) {
96102 throw err ;
97103 }
98104 }
99105
100106 if ( label ) {
101- return yield _this2 . updateLabel ( name , color ) ;
107+ return yield _this2 . updateLabel ( repository , name , color ) ;
102108 }
103109
104- return yield _this2 . createLabel ( name , color ) ;
110+ return yield _this2 . createLabel ( repository , name , color ) ;
105111 } ) ( ) ;
106112 }
107113
108114 /**
109115 * Delete label by given `name`.
110116 */
111117
112- deleteLabel ( name ) {
118+ deleteLabel ( repository , name ) {
113119 var _this3 = this ;
114120
115121 return _asyncToGenerator ( function * ( ) {
116- return yield _this3 . github . issues . deleteLabelAsync ( {
122+ const { owner, repo } = getRepositoryOptions ( repository ) ;
123+
124+ return yield _this3 . github . issues . deleteLabel ( {
117125 name,
118- owner : _this3 . owner ,
119- repo : _this3 . repo
126+ owner,
127+ repo
120128 } ) ;
121129 } ) ( ) ;
122130 }
@@ -125,13 +133,15 @@ class Client {
125133 * Get all repo labels.
126134 */
127135
128- getLabels ( ) {
136+ getLabels ( repository ) {
129137 var _this4 = this ;
130138
131139 return _asyncToGenerator ( function * ( ) {
132- const result = yield _this4 . github . issues . getLabelsAsync ( {
133- owner : _this4 . owner ,
134- repo : _this4 . repo
140+ const { owner, repo } = getRepositoryOptions ( repository ) ;
141+
142+ const result = yield _this4 . github . issues . getLabels ( {
143+ owner,
144+ repo
135145 } ) ;
136146
137147 return result . data ;
@@ -142,14 +152,16 @@ class Client {
142152 * Get label by given `name`.
143153 */
144154
145- getLabel ( name ) {
155+ getLabel ( repository , name ) {
146156 var _this5 = this ;
147157
148158 return _asyncToGenerator ( function * ( ) {
149- return yield _this5 . github . issues . getLabelAsync ( {
159+ const { owner, repo } = getRepositoryOptions ( repository ) ;
160+
161+ return yield _this5 . github . issues . getLabel ( {
150162 name,
151- owner : _this5 . owner ,
152- repo : _this5 . repo
163+ owner,
164+ repo
153165 } ) ;
154166 } ) ( ) ;
155167 }
@@ -158,16 +170,18 @@ class Client {
158170 * Update an existing label with given `color`.
159171 */
160172
161- updateLabel ( name , color ) {
173+ updateLabel ( repository , name , color ) {
162174 var _this6 = this ;
163175
164176 return _asyncToGenerator ( function * ( ) {
165- return yield _this6 . github . issues . updateLabelAsync ( {
177+ const { owner, repo } = getRepositoryOptions ( repository ) ;
178+
179+ return yield _this6 . github . issues . updateLabel ( {
166180 color,
167181 name,
168182 oldname : name ,
169- owner : _this6 . owner ,
170- repo : _this6 . repo
183+ owner,
184+ repo
171185 } ) ;
172186 } ) ( ) ;
173187 }
@@ -176,28 +190,29 @@ class Client {
176190 * Set labels.
177191 */
178192
179- setLabels ( labels ) {
193+ setLabels ( repository , labels ) {
180194 var _this7 = this ;
181195
182196 return _asyncToGenerator ( function * ( ) {
183- const current = yield _this7 . getLabels ( ) ;
197+ const current = yield _this7 . getLabels ( repository ) ;
184198
185199 // Delete current labels that are not included in the wanted labels.
186200 const deprecated = ( 0 , _lodash . differenceBy ) ( current , labels , 'name' ) ;
187201
188- for ( const _ref of deprecated ) {
189- const { name } = _ref ;
202+ for ( const _ref2 of deprecated ) {
203+ const { name } = _ref2 ;
190204
191- yield _this7 . deleteLabel ( name ) ;
205+ yield _this7 . deleteLabel ( repository , name ) ;
192206 }
193207
194208 // Create or update wanted labels.
195- for ( const _ref2 of labels ) {
196- const { color, name } = _ref2 ;
209+ for ( const _ref3 of labels ) {
210+ const { color, name } = _ref3 ;
197211
198- yield _this7 . createOrUpdateLabel ( name , color ) ;
212+ yield _this7 . createOrUpdateLabel ( repository , name , color ) ;
199213 }
200214 } ) ( ) ;
201215 }
216+
202217}
203218exports . default = Client ;
0 commit comments