|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +class GithubHelper { |
| 4 | + /** |
| 5 | + * Set authentication repository config |
| 6 | + * @param {Object} repo - personal access token |
| 7 | + */ |
| 8 | + constructor(repo) { |
| 9 | + this.repo = repo; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * make and return promise, using github api |
| 14 | + * @param {Function} api - api function |
| 15 | + * @returns {Promise} - response data, or error |
| 16 | + */ |
| 17 | + request(api) { |
| 18 | + return api() |
| 19 | + .then(response => { |
| 20 | + if (response.status !== 200) { |
| 21 | + throw new Error(this.pretty(response)); |
| 22 | + } |
| 23 | + |
| 24 | + return response; |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * console log on error |
| 30 | + * @param {Exception} error - error while Promise |
| 31 | + * @param {string} [description] - additional description on error |
| 32 | + */ |
| 33 | + consoleError(error, description) { |
| 34 | + if (error && error.message) { |
| 35 | + console.error(error.message); |
| 36 | + } |
| 37 | + |
| 38 | + if (description) { |
| 39 | + console.error(description); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get all tags in github |
| 45 | + * then set COMPARE and BASE tag by argments |
| 46 | + * BASE tag omits when COMPARE tag is initial tag |
| 47 | + * @returns {Promise} - compare tag and base tag |
| 48 | + */ |
| 49 | + getTags() { |
| 50 | + return this.request(this.repo.listTags.bind(this.repo)) |
| 51 | + .then(response => response.data) |
| 52 | + .catch(err => this.consoleError(err, 'Could not get tags from github')); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * tag range needed to collect commits |
| 57 | + * @typedef {Object} Range - comparing ranges from `base` to `compare` |
| 58 | + * @property {string} compare - tag to deploy |
| 59 | + * @property {stirng} [base] - prior release tag |
| 60 | + */ |
| 61 | + /** |
| 62 | + * Get tag names to compare |
| 63 | + * |
| 64 | + * @param {Array} tags - tags, latest tag comes first |
| 65 | + * @param {string} argvTag - tag passed as a argument at bash |
| 66 | + * @returns {Range} - tags to compare |
| 67 | + */ |
| 68 | + getTagRange(tags, argvTag) { |
| 69 | + const range = argvTag ? this.getTagsWithTagName(tags, argvTag) : this.getLatestTwoTags(tags); |
| 70 | + |
| 71 | + this.releasingTag = range.compare.name; |
| 72 | + console.log(`\n>>>> tag: ${this.releasingTag}`); |
| 73 | + |
| 74 | + return range; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * User set target tag by `--tag={tag}` option at bash |
| 79 | + * Find target tag from tag list, |
| 80 | + * @param {Array} tags - tags, latest tag comes first |
| 81 | + * @param {string} findingTag - tag name want to find, come from bash |
| 82 | + * @returns {Range} - tags to compare |
| 83 | + */ |
| 84 | + getTagsWithTagName(tags, findingTag) { |
| 85 | + let compare = null; |
| 86 | + let base = null; |
| 87 | + |
| 88 | + const {length} = tags; |
| 89 | + const index = tags.findIndex(tag => tag.name === findingTag); |
| 90 | + compare = tags[index]; |
| 91 | + if (index < length - 1) { |
| 92 | + base = tags[index + 1]; |
| 93 | + } |
| 94 | + |
| 95 | + if (!compare) { |
| 96 | + throw new Error(`Could not find ${findingTag} in GitHub tag list`); |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + compare, |
| 101 | + base |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get latest two tags |
| 107 | + * @param {Array} tags - tags in Github |
| 108 | + * @returns {Range} - tags to compare |
| 109 | + */ |
| 110 | + getLatestTwoTags(tags) { |
| 111 | + const [compare] = tags; |
| 112 | + const base = tags[1] || null; |
| 113 | + |
| 114 | + if (!compare) { |
| 115 | + throw new Error('Could not find latest tag. No tags in GitHub'); |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + compare, |
| 120 | + base |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get commit logs between tags |
| 126 | + * @param {string} since - tag name registered former |
| 127 | + * @param {string} until - tag name registered latter |
| 128 | + * @returns {Promise} - get commit logs between tags |
| 129 | + */ |
| 130 | + getCommitLogsBetweenTags(since, until) { |
| 131 | + return this.request(this.repo.compareBranches.bind(this.repo, since, until)) |
| 132 | + .then(response => response.data.commits) |
| 133 | + .catch(this.consoleError); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get commit by tag name |
| 138 | + * @param {string} tag - tag name |
| 139 | + * @returns {Promise} - get commit on tagging |
| 140 | + */ |
| 141 | + getCommitByTag(tag) { |
| 142 | + return this.request(this.repo.getSingleCommit.bind(this.repo, tag)) |
| 143 | + .then(response => response.data) |
| 144 | + .catch(err => this.consoleError( |
| 145 | + err, `Could not get commit of ${tag}. Please check tag is registered on GitHub.` |
| 146 | + )); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Get commit by sha |
| 151 | + * @param {string} sha - sha code |
| 152 | + * @returns {Promise} - get commit by sha |
| 153 | + */ |
| 154 | + getCommitBySHA(sha) { |
| 155 | + return this.request(this.repo.getCommit.bind(this.repo, sha)) |
| 156 | + .then(response => response.data) |
| 157 | + .catch(err => this.consoleError(err, `Could not get commit by ${sha.substr(0, 7)}`)); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get commit list |
| 162 | + * @param {Object} options - list commit options |
| 163 | + * @returns {Promise} get commit logs |
| 164 | + */ |
| 165 | + getCommits(options) { |
| 166 | + return this.request(this.repo.listCommits.bind(this.repo, options)) |
| 167 | + .then(response => response.data) |
| 168 | + .catch(err => this.consoleError(err, 'Could not get commits')); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Post release note |
| 173 | + * @param {string} releaseNote - generated release note |
| 174 | + */ |
| 175 | + publishReleaseNote(releaseNote) { |
| 176 | + const options = { |
| 177 | + 'tag_name': this.releasingTag, |
| 178 | + name: this.releasingTag, |
| 179 | + body: releaseNote |
| 180 | + }; |
| 181 | + |
| 182 | + return this.request(this.repo.createRelease.bind(this.repo, options)) |
| 183 | + .then(() => { |
| 184 | + console.log('Posted release notes to GitHub') |
| 185 | + }) |
| 186 | + .catch(err => this.consoleError(err, 'Could not post release notes to GitHub')); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Print http request and response data on console |
| 191 | + * @param {JSON} response - http response |
| 192 | + */ |
| 193 | + pretty(response) { |
| 194 | + console.log('****************************************************************'); |
| 195 | + console.log(`* method: ${response.config.method}`); |
| 196 | + console.log(`* url: ${response.config.url}`); |
| 197 | + console.log(`* status: ${response.status} ${response.statusText}`); |
| 198 | + console.log('* ------------------------------------------------------------- *'); |
| 199 | + console.log('* data: '); |
| 200 | + console.log(response.data); |
| 201 | + console.log('****************************************************************'); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +module.exports = GithubHelper; |
0 commit comments