|
| 1 | +const GitHub = require('github-api'); |
| 2 | +const GitHubApi = require('github'); |
| 3 | +const Tag = require('./lib/tag'); |
| 4 | +const Release = require('./lib/release'); |
| 5 | +const Info = require('./lib/info'); |
| 6 | + |
| 7 | +class GitHubRelease { |
| 8 | + constructor(token, host, pathPrefix) { |
| 9 | + this.github = new GitHubApi({ |
| 10 | + host: host, |
| 11 | + pathPrefix: pathPrefix |
| 12 | + }); |
| 13 | + this.github.authenticate({ |
| 14 | + type: 'token', |
| 15 | + token: token |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + info(user, repo) { |
| 20 | + const tags = this.github.repos.getTags({ |
| 21 | + owner: user, |
| 22 | + repo: repo, |
| 23 | + per_page: 100 // TODO: support for pagination |
| 24 | + }); |
| 25 | + const releases = this.github.repos.getReleases({ |
| 26 | + owner: user, |
| 27 | + repo: repo, |
| 28 | + per_page: 100 // TODO: support for pagination |
| 29 | + }); |
| 30 | + return Promise.all([tags, releases]).then(resps => { |
| 31 | + const tagsResp = resps[0]; |
| 32 | + const releasesResp = resps[1]; |
| 33 | + const tags = tagsResp.data.map(obj => { |
| 34 | + return new Tag(obj.name, obj.commit.url); |
| 35 | + }); |
| 36 | + const releases = releasesResp.data.map(obj => { |
| 37 | + return new Release(obj.id, obj.tag_name, obj.name, obj.body, obj.draft, obj.prerelease, obj.created_at, obj.published_at); |
| 38 | + }); |
| 39 | + return new Info(tags, releases); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + release(user, repo, tag, name, description, target, draft, preRelease) { |
| 44 | + const options = { |
| 45 | + owner: user, |
| 46 | + repo: repo, |
| 47 | + tag_name: tag |
| 48 | + }; |
| 49 | + if (name !== undefined) { |
| 50 | + options.name = name; |
| 51 | + } |
| 52 | + if (target !== undefined) { |
| 53 | + options.target_commitish = target; |
| 54 | + } |
| 55 | + if (draft !== undefined) { |
| 56 | + options.draft = draft; |
| 57 | + } |
| 58 | + if (preRelease !== undefined) { |
| 59 | + options.prerelease = preRelease; |
| 60 | + } |
| 61 | + if (description !== undefined) { |
| 62 | + options.body = description; |
| 63 | + } |
| 64 | + return this.github.repos.createRelease(options); |
| 65 | + } |
| 66 | + |
| 67 | + edit(user, repo, tag, name, description, target, draft, preRelease) { |
| 68 | + return this.github.repos.getReleaseByTag({ |
| 69 | + owner: user, |
| 70 | + repo: repo, |
| 71 | + tag: tag |
| 72 | + }).catch(err => { |
| 73 | + if (err.code === 404) { |
| 74 | + throw new Error(`Error: release not found (user: ${user}, repo: ${repo}, tag: ${tag})`); |
| 75 | + } |
| 76 | + throw err; |
| 77 | + }).then(res => { |
| 78 | + const id = res.data.id; |
| 79 | + const options = { |
| 80 | + owner: user, |
| 81 | + repo: repo, |
| 82 | + id: id, |
| 83 | + tag_name: tag |
| 84 | + }; |
| 85 | + if (name !== undefined) { |
| 86 | + options.name = name; |
| 87 | + } |
| 88 | + if (target !== undefined) { |
| 89 | + options.target = target; |
| 90 | + } |
| 91 | + if (draft !== undefined) { |
| 92 | + options.draft = draft; |
| 93 | + } |
| 94 | + if (preRelease !== undefined) { |
| 95 | + options.prerelease = preRelease; |
| 96 | + } |
| 97 | + if (description !== undefined) { |
| 98 | + options.body = description; |
| 99 | + } |
| 100 | + return this.github.repos.editRelease(options); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + upload(user, repo, tag, name, label, file) { |
| 105 | + return this.github.repos.getReleaseByTag({ |
| 106 | + owner: user, |
| 107 | + repo: repo, |
| 108 | + tag: tag |
| 109 | + }).catch(err => { |
| 110 | + if (err.code === 404) { |
| 111 | + throw new Error(`Error: release not found (user: ${user}, repo: ${repo}, tag: ${tag})`); |
| 112 | + } |
| 113 | + throw err; |
| 114 | + }).then(res => { |
| 115 | + const id = res.data.id; |
| 116 | + const options = { |
| 117 | + owner: user, |
| 118 | + repo: repo, |
| 119 | + id: id, |
| 120 | + filePath: file, |
| 121 | + name: name |
| 122 | + }; |
| 123 | + if (label !== undefined) { |
| 124 | + options.label = label; |
| 125 | + } |
| 126 | + return this.github.repos.uploadAsset(options); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + destroy(user, repo, tag) { |
| 131 | + return this.github.repos.getReleaseByTag({ |
| 132 | + owner: user, |
| 133 | + repo: repo, |
| 134 | + tag: tag |
| 135 | + }).catch(err => { |
| 136 | + if (err.code === 404) { |
| 137 | + throw new Error(`Error: release not found (user: ${user}, repo: ${repo}, tag: ${tag})`); |
| 138 | + } |
| 139 | + throw err; |
| 140 | + }).then(res => { |
| 141 | + const id = res.data.id; |
| 142 | + const options = { |
| 143 | + owner: user, |
| 144 | + repo: repo, |
| 145 | + id: id |
| 146 | + }; |
| 147 | + return this.github.repos.deleteRelease(options); |
| 148 | + }); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +module.exports = GitHubRelease; |
0 commit comments