|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 19 | + if (mod && mod.__esModule) return mod; |
| 20 | + var result = {}; |
| 21 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 22 | + __setModuleDefault(result, mod); |
| 23 | + return result; |
| 24 | +}; |
| 25 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 26 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 27 | +}; |
| 28 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 29 | +exports.getFileContent = exports.getChangedFiles = exports.getDiff = exports.getAssertion = exports.getSinglePR = exports.getPullRequests = exports.getRepo = exports.testConnection = exports.github = void 0; |
| 30 | +const dotenv_1 = __importDefault(require("dotenv")); |
| 31 | +const core = __importStar(require("@actions/core")); |
| 32 | +const rest_1 = require("@octokit/rest"); |
| 33 | +dotenv_1.default.config(); |
| 34 | +const token = core.getInput('GITHUB_TOKEN') || process.env.GITHUB_TOKEN; |
| 35 | +exports.github = new rest_1.Octokit({ |
| 36 | + auth: token, |
| 37 | +}); |
| 38 | +// We'll use this to test a connection to GitHub |
| 39 | +const testConnection = async () => { |
| 40 | + try { |
| 41 | + const { data } = await exports.github.repos.listForAuthenticatedUser(); |
| 42 | + console.log(`🚀 Connection to GH established`); |
| 43 | + return true; |
| 44 | + } |
| 45 | + catch (error) { |
| 46 | + console.error(error); |
| 47 | + return false; |
| 48 | + } |
| 49 | +}; |
| 50 | +exports.testConnection = testConnection; |
| 51 | +// If we can get a connection, we can get access to a repo, or we'll return null because |
| 52 | +const getRepo = async (owner, repo) => { |
| 53 | + try { |
| 54 | + const { data } = await exports.github.repos.get({ owner, repo }); |
| 55 | + console.log(`🐕 Fetched the repo`); |
| 56 | + return data; |
| 57 | + } |
| 58 | + catch (error) { |
| 59 | + console.error(error); |
| 60 | + return null; |
| 61 | + } |
| 62 | +}; |
| 63 | +exports.getRepo = getRepo; |
| 64 | +// If we can get a repo, we can get all the PRs associated with it |
| 65 | +const getPullRequests = async (owner, repo) => { |
| 66 | + try { |
| 67 | + const { data } = await exports.github.pulls.list({ owner, repo }); |
| 68 | + console.log(`🐕 Fetched all PRs`); |
| 69 | + return data; |
| 70 | + } |
| 71 | + catch (error) { |
| 72 | + console.error(error); |
| 73 | + return null; |
| 74 | + } |
| 75 | +}; |
| 76 | +exports.getPullRequests = getPullRequests; |
| 77 | +// We should be able to get a PR by its number |
| 78 | +const getSinglePR = async (owner, repo, prNumber) => { |
| 79 | + try { |
| 80 | + const { data } = await exports.github.pulls.get({ owner, repo, pull_number: prNumber }); |
| 81 | + console.log(`✅ Got PR #${prNumber}`); |
| 82 | + return data; |
| 83 | + } |
| 84 | + catch (error) { |
| 85 | + console.error(error); |
| 86 | + return null; |
| 87 | + } |
| 88 | +}; |
| 89 | +exports.getSinglePR = getSinglePR; |
| 90 | +// If we can get a PR, we can parse the description and isolate the assertion using the comments |
| 91 | +const getAssertion = async (description) => { |
| 92 | + // find everything in between <!-- DX:Assertion-start --> and <!-- DX:Assertion-end --> |
| 93 | + const regex = /<!-- DX:Assertion-start -->([\s\S]*?)<!-- DX:Assertion-end -->/g; |
| 94 | + const assertion = regex.exec(description); |
| 95 | + if (assertion) { |
| 96 | + console.log(`✅ Got assertion: ${assertion[1]}`); |
| 97 | + return assertion[1]; |
| 98 | + } |
| 99 | + return null; |
| 100 | +}; |
| 101 | +exports.getAssertion = getAssertion; |
| 102 | +// If we have a diff_url we can get the diff |
| 103 | +const getDiff = async (prNumber) => { |
| 104 | + const { data: diff } = await exports.github.pulls.get({ |
| 105 | + owner: 'hasura', |
| 106 | + repo: 'v3-docs', |
| 107 | + pull_number: prNumber, |
| 108 | + mediaType: { |
| 109 | + format: 'diff', |
| 110 | + }, |
| 111 | + }); |
| 112 | + // We'll have to convert the diff to a string, then we can return it |
| 113 | + const diffString = diff.toString(); |
| 114 | + console.log(`✅ Got diff for PR #${prNumber}`); |
| 115 | + return diffString; |
| 116 | +}; |
| 117 | +exports.getDiff = getDiff; |
| 118 | +// If we have the diff, we can determine which files were changed |
| 119 | +const getChangedFiles = (diff) => { |
| 120 | + const fileLines = diff.split('\n').filter((line) => line.startsWith('diff --git')); |
| 121 | + const changedFiles = fileLines |
| 122 | + .map((line) => { |
| 123 | + const paths = line.split(' ').slice(2); |
| 124 | + return paths.map((path) => path.replace('a/', '').replace('b/', '')); |
| 125 | + }) |
| 126 | + .flat(); |
| 127 | + console.log(`✅ Found ${changedFiles.length} affected files`); |
| 128 | + return [...new Set(changedFiles)]; |
| 129 | +}; |
| 130 | +exports.getChangedFiles = getChangedFiles; |
| 131 | +// We'll also need to get the whole file using the files changed from |
| 132 | +async function getFileContent(path) { |
| 133 | + let content = ''; |
| 134 | + // loop over the array of files |
| 135 | + for (let i = 0; i < path.length; i++) { |
| 136 | + // get the file content |
| 137 | + const { data } = await exports.github.repos.getContent({ |
| 138 | + owner: 'hasura', |
| 139 | + repo: 'v3-docs', |
| 140 | + path: path[i], |
| 141 | + }); |
| 142 | + // decode the file content |
| 143 | + const decodedContent = Buffer.from(data.content, 'base64').toString(); |
| 144 | + // add the decoded content to the content string |
| 145 | + content += decodedContent; |
| 146 | + } |
| 147 | + console.log(`✅ Got file(s) contents`); |
| 148 | + return content; |
| 149 | +} |
| 150 | +exports.getFileContent = getFileContent; |
0 commit comments