Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit 4bf1908

Browse files
author
TED Vortex (Teodor Eugen Dutulescu)
authored
Merge pull request #48 from superleap/gils43-update-labels-method
feat(github): added update label/s method and examples, changed long …
2 parents c7c88dd + 94e60c1 commit 4bf1908

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"lines-around-comment": [2, { "beforeBlockComment": true, "allowObjectStart": true }],
102102
"max-depth": 2,
103103
"max-len": [2, 120],
104-
"max-lines": [2, 500],
104+
"max-lines": [2, 1500],
105105
"max-nested-callbacks": 2,
106106
"max-params": [2, 5],
107107
"max-statements": [2, { "max": 15 }],

examples/updateLabel.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let config = require('./config/config');
2+
let labels = require('./config/labels');
3+
let {user, repo, token, options} = config.github;
4+
let githubIssuesLabelSync = new (require('./../lib/LabelSync'))(options, user, repo, token);
5+
6+
let label = labels[0];
7+
label.color = '000';
8+
9+
githubIssuesLabelSync.updateLabel(label).then((response) => {
10+
/**
11+
* @example
12+
* [ { name: 'TEST GH Review: accepted',
13+
* color: '000',
14+
* status: 'success' } ]
15+
*/
16+
console.log(response);
17+
}).catch((error) => {
18+
/**
19+
* @example
20+
* { code: 401,
21+
* status: 'Unauthorized',
22+
* message: '{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}' }
23+
* @example
24+
* { code: 404,
25+
* status: 'Not Found',
26+
* message: '{"message":"Not Found","documentation_url":"https://developer.github.com/v3/issues/labels/#update-a-label"}' }
27+
*/
28+
console.log(error.toJSON());
29+
});

examples/updateLabels.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let config = require('./config/config');
2+
let labels = require('./config/labels');
3+
let {user, repo, token, options} = config.github;
4+
let githubIssuesLabelSync = new (require('./../lib/LabelSync'))(options, user, repo, token);
5+
6+
labels.map((label) => {
7+
label.color = '000';
8+
return label;
9+
});
10+
11+
githubIssuesLabelSync.updateLabels(labels).then((response) => {
12+
/**
13+
* @example
14+
* [ { url: 'https://api.github.com/repos/superleap/github-issues-label-sync/labels/Semver:%20premajor',
15+
* name: 'Semver: premajor',
16+
* color: 'e99695' },
17+
* ...
18+
* { url: 'https://api.github.com/repos/superleap/github-issues-label-sync/labels/Type:%20feature-request',
19+
* name: 'Type: feature-request',
20+
* color: 'c7def8' } ]
21+
*/
22+
console.log(response);
23+
}).catch((error) => {
24+
/**
25+
* @example
26+
* { code: 401,
27+
* status: 'Unauthorized',
28+
* message: '{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}' }
29+
* @example
30+
* { code: 404,
31+
* status: 'Not Found',
32+
* message: '{"message":"Not Found","documentation_url":"https://developer.github.com/v3/issues/labels/#update-a-label"}' }
33+
*/
34+
console.log(error.toJSON());
35+
});

src/LabelSync.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default class GithubIssuesLabelSync {
9595
this._labels = [];
9696
this._deletedLabels = [];
9797
this._createdLabels = [];
98+
this._updatedLabels = [];
9899

99100
this.authenticate();
100101
}
@@ -163,6 +164,14 @@ export default class GithubIssuesLabelSync {
163164
return this._createdLabels;
164165
}
165166

167+
/**
168+
* Get github API repository updated labels
169+
* @type {Array<GithubIssuesLabelSync.Label>}
170+
*/
171+
get updatedLabels() {
172+
return this._updatedLabels;
173+
}
174+
166175
/**
167176
* Set github API Client options
168177
* @type {Object} Options we instantiate the github api package with
@@ -232,6 +241,14 @@ export default class GithubIssuesLabelSync {
232241
this._createdLabels.push(label);
233242
}
234243

244+
/**
245+
* Push github API repository updated label
246+
* @type {GithubIssuesLabelSync.Label}
247+
*/
248+
set updatedLabel(label) {
249+
this._updatedLabels.push(label);
250+
}
251+
235252
/**
236253
* We only do this once
237254
* @access private
@@ -400,6 +417,58 @@ export default class GithubIssuesLabelSync {
400417
});
401418
}
402419

420+
/**
421+
* Update github API repository label on remote
422+
* @example
423+
* githubIssuesLabelSync.updateLabel(label).then((response) => {
424+
* console.log(response);
425+
* }).catch((error) => {
426+
* console.log(error.toJSON());
427+
* });
428+
* @async
429+
* @param {GithubIssuesLabelSync.Label} label - The label we want to update
430+
* @param {String} label.name - The label's name
431+
* @param {String} label.color - The label's colour
432+
* @param {String} label.status - Promise status of operation
433+
* @return {Promise<GithubIssuesLabelSync.createdLabels, Error>} Updated label
434+
*/
435+
updateLabel(label) {
436+
return new Promise((resolve, reject) => {
437+
return this.github.issues.updateLabel({
438+
"user": this.user,
439+
"repo": this.repo,
440+
"name": label.name,
441+
"color": label.color
442+
}, (error) => {
443+
if (error) {
444+
reject(error);
445+
} else {
446+
label.status = 'success';
447+
this.updatedLabel = label;
448+
resolve(this.updatedLabels);
449+
}
450+
});
451+
});
452+
}
453+
454+
/**
455+
* Update github API repository labels on remote
456+
* @example
457+
* githubIssuesLabelSync.updateLabels(labels).then((response) => {
458+
* console.log(response);
459+
* }).catch((error) => {
460+
* console.log(error.toJSON());
461+
* });
462+
* @async
463+
* @param {Array<GithubIssuesLabelSync.Label>} labels - The labels we want to update
464+
* @return {Promise<GithubIssuesLabelSync.createdLabels, Error>} Array of updated labels
465+
*/
466+
updateLabels(labels) {
467+
return Promise.all(labels).map((label) => {
468+
return this.updateLabel(label);
469+
});
470+
}
471+
403472
/**
404473
* Delete all github API repository labels on remote
405474
* @example

0 commit comments

Comments
 (0)