* Fetch PR information * @param {string} prUrl - PR url
(prUrl)
| 63 | * @param {string} prUrl - PR url |
| 64 | */ |
| 65 | function getPRInfo(prUrl) { |
| 66 | const options = { |
| 67 | ...parseURL(prUrl), |
| 68 | headers: { |
| 69 | 'User-Agent': 'Node.js https client', |
| 70 | }, |
| 71 | }; |
| 72 | return new Promise((resolve, reject) => { |
| 73 | https |
| 74 | .get(options, res => { |
| 75 | const {statusCode} = res; |
| 76 | const contentType = res.headers['content-type']; |
| 77 | |
| 78 | let error; |
| 79 | if (statusCode !== 200) { |
| 80 | error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`); |
| 81 | } else if (!/^application\/json/.test(contentType)) { |
| 82 | error = new Error( |
| 83 | 'Invalid content-type.\n' + |
| 84 | `Expected application/json but received ${contentType}`, |
| 85 | ); |
| 86 | } |
| 87 | if (error) { |
| 88 | console.error(error.message); |
| 89 | // Consume response data to free up memory |
| 90 | res.resume(); |
| 91 | return reject(error); |
| 92 | } |
| 93 | |
| 94 | res.setEncoding('utf8'); |
| 95 | let rawData = ''; |
| 96 | res.on('data', chunk => { |
| 97 | rawData += chunk; |
| 98 | }); |
| 99 | res.on('end', () => { |
| 100 | try { |
| 101 | const parsedData = JSON.parse(rawData); |
| 102 | resolve(parsedData); |
| 103 | } catch (e) { |
| 104 | reject(e); |
| 105 | } |
| 106 | }); |
| 107 | }) |
| 108 | .on('error', e => { |
| 109 | reject(e); |
| 110 | }); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Run `git` command with the arguments |