@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
60 lines (59 loc) • 3.28 kB
JavaScript
import { __awaiter } from "tslib";
import { readFileSync } from 'fs';
import { resolve } from 'path';
import * as core from '@actions/core';
export function fileLoader({ octokit, owner, repo, }) {
const query = /* GraphQL */ `
query GetFile($repo: String!, $owner: String!, $expression: String!) {
repository(name: $repo, owner: $owner) {
object(expression: $expression) {
... on Blob {
isTruncated
oid
text
}
}
}
}
`;
return function loadFile(file) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
return __awaiter(this, void 0, void 0, function* () {
if (file.workspace) {
return readFileSync(resolve(file.workspace, file.path), 'utf8');
}
const result = yield octokit.graphql(query, {
repo,
owner,
expression: `${file.ref}:${file.path}`,
});
core.info(`Query ${file.ref}:${file.path} from ${owner}/${repo}`);
try {
if (((_b = (_a = result === null || result === void 0 ? void 0 : result.repository) === null || _a === void 0 ? void 0 : _a.object) === null || _b === void 0 ? void 0 : _b.oid) && ((_d = (_c = result === null || result === void 0 ? void 0 : result.repository) === null || _c === void 0 ? void 0 : _c.object) === null || _d === void 0 ? void 0 : _d.isTruncated)) {
const oid = (_f = (_e = result === null || result === void 0 ? void 0 : result.repository) === null || _e === void 0 ? void 0 : _e.object) === null || _f === void 0 ? void 0 : _f.oid;
const getBlobResponse = yield octokit.git.getBlob({
owner,
repo,
file_sha: oid,
});
if ((_g = getBlobResponse === null || getBlobResponse === void 0 ? void 0 : getBlobResponse.data) === null || _g === void 0 ? void 0 : _g.content) {
return Buffer.from((_h = getBlobResponse === null || getBlobResponse === void 0 ? void 0 : getBlobResponse.data) === null || _h === void 0 ? void 0 : _h.content, 'base64').toString('utf-8');
}
throw new Error('getBlobResponse.data.content is null');
}
if ((_k = (_j = result === null || result === void 0 ? void 0 : result.repository) === null || _j === void 0 ? void 0 : _j.object) === null || _k === void 0 ? void 0 : _k.text) {
if (((_m = (_l = result === null || result === void 0 ? void 0 : result.repository) === null || _l === void 0 ? void 0 : _l.object) === null || _m === void 0 ? void 0 : _m.isTruncated) === false) {
return result.repository.object.text;
}
throw new Error('result.repository.object.text is truncated and oid is null');
}
throw new Error('result.repository.object.text is null');
}
catch (error) {
console.log(result);
console.error(error);
throw new Error(`Failed to load '${file.path}' (ref: ${file.ref})`);
}
});
};
}