| 3 | // https://docs.github.com/en/rest/commits/commits#compare-two-commits |
| 4 | // Returns information comparing two commits for a given repository |
| 5 | export async function compareCommits(ownerAndRepo, before, after) { |
| 6 | return new Promise((resolve, reject) => { |
| 7 | const gitHubToken = process.env.GITHUB_ACCESS_TOKEN; |
| 8 | |
| 9 | if ( |
| 10 | before === '0000000000000000000000000000000000000000' || |
| 11 | after === '0000000000000000000000000000000000000000' |
| 12 | ) { |
| 13 | resolve({}); |
| 14 | } |
| 15 | |
| 16 | const options = { |
| 17 | host: 'api.github.com', |
| 18 | path: `/repos/${ownerAndRepo}/compare/${before}...${after}?per_page=16`, |
| 19 | method: 'GET', |
| 20 | headers: { |
| 21 | Accept: 'application/vnd.github.v3+json', |
| 22 | Authorization: `token ${gitHubToken}`, |
| 23 | 'User-Agent': 'PRX/CI (slack-message-handler)', |
| 24 | 'Content-Length': Buffer.byteLength(''), |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | const req = request(options, (res) => { |
| 29 | res.setEncoding('utf8'); |
| 30 | |
| 31 | let json = ''; |
| 32 | res.on('data', (chunk) => { |
| 33 | json += chunk; |
| 34 | }); |
| 35 | res.on('end', () => { |
| 36 | if (res.statusCode >= 200 && res.statusCode < 300) { |
| 37 | resolve(JSON.parse(json)); |
| 38 | } else { |
| 39 | reject(new Error(`GitHub request failed! ${res.statusCode}`)); |
| 40 | } |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | // Generic request error handling |
| 45 | req.on('error', (e) => reject(e)); |
| 46 | |
| 47 | req.write(''); |
| 48 | req.end(); |
| 49 | }); |
| 50 | } |