doc-only-change.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const args = require('minimist')(process.argv.slice(2));
  2. const { Octokit } = require('@octokit/rest');
  3. const octokit = new Octokit();
  4. async function checkIfDocOnlyChange () {
  5. if (args.prNumber || args.prBranch || args.prURL) {
  6. try {
  7. let pullRequestNumber = args.prNumber;
  8. if (!pullRequestNumber || isNaN(pullRequestNumber)) {
  9. if (args.prURL) {
  10. // CircleCI doesn't provide the PR number for branch builds, but it does provide the PR URL
  11. const pullRequestParts = args.prURL.split('/');
  12. pullRequestNumber = pullRequestParts[pullRequestParts.length - 1];
  13. } else if (args.prBranch) {
  14. // AppVeyor doesn't provide a PR number for branch builds - figure it out from the branch
  15. const prsForBranch = await octokit.pulls.list({
  16. owner: 'electron',
  17. repo: 'electron',
  18. state: 'open',
  19. head: `electron:${args.prBranch}`
  20. });
  21. if (prsForBranch.data.length === 1) {
  22. pullRequestNumber = prsForBranch.data[0].number;
  23. } else {
  24. // If there are 0 PRs or more than one PR on a branch, just assume that this is more than a doc change
  25. process.exit(1);
  26. }
  27. }
  28. }
  29. const filesChanged = await octokit.paginate(octokit.pulls.listFiles.endpoint.merge({
  30. owner: 'electron',
  31. repo: 'electron',
  32. pull_number: pullRequestNumber,
  33. per_page: 100
  34. }));
  35. console.log('Changed Files:', filesChanged.map(fileInfo => fileInfo.filename));
  36. const nonDocChange = filesChanged.find((fileInfo) => {
  37. const fileDirs = fileInfo.filename.split('/');
  38. if (fileDirs[0] !== 'docs') {
  39. return true;
  40. }
  41. });
  42. if (nonDocChange || filesChanged.length === 0) {
  43. process.exit(1);
  44. } else {
  45. process.exit(0);
  46. }
  47. } catch (ex) {
  48. console.error('Error getting list of files changed: ', ex);
  49. process.exit(-1);
  50. }
  51. } else {
  52. console.error(`Check if only the docs were changed for a commit.
  53. Usage: doc-only-change.js --prNumber=PR_NUMBER || --prBranch=PR_BRANCH || --prURL=PR_URL`);
  54. process.exit(-1);
  55. }
  56. }
  57. checkIfDocOnlyChange();