-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
50 lines (45 loc) · 1.38 KB
/
index.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
'use strict'
const Octokat = require('octokat')
var octo
module.exports = function (repoName, description, flags, token) {
flags = flags || {}
if (typeof repoName !== 'string') {
throw new TypeError('Expected a string')
}
if (repoName.split('/').length !== 2) {
throw new Error('Not a repository name in form \'user/repo\'')
}
if (flags.enterprise && typeof flags.enterprise === 'boolean') {
flags.enterprise = process.env.GITHUB_ENDPOINT
}
return Promise.resolve().then(() => {
octo = new Octokat({
token: token || (flags.enterprise) ? null : process.env.GH_DESCRIPTION_TOKEN,
rootURL: flags.enterprise
})
return repoName
}).then((repoName) => {
repoName = repoName.split('/')
return octo.repos(repoName[0], repoName[1]).fetch()
}).then(function (result) {
if (description) {
return octo.repos(result.owner.login, result.name).update({
'description': description,
'name': result.name
}).then((result) => {
return { method: 'patch', description: result.description }
}).catch((err) => {
console.log(err)
throw ('Unable to set description', err)
})
} else {
return { method: 'get', description: result.description }
}
}).catch(function (err) {
if (err.status === 404) {
return err
} else {
throw ('Could not get GitHub user', err)
}
})
}