1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
11 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13 | function verb(n) { return function (v) { return step([n, v]); }; }
|
14 | function step(op) {
|
15 | if (f) throw new TypeError("Generator is already executing.");
|
16 | while (_) try {
|
17 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18 | if (y = 0, t) op = [op[0] & 2, t.value];
|
19 | switch (op[0]) {
|
20 | case 0: case 1: t = op; break;
|
21 | case 4: _.label++; return { value: op[1], done: false };
|
22 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
23 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24 | default:
|
25 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29 | if (t[2]) _.ops.pop();
|
30 | _.trys.pop(); continue;
|
31 | }
|
32 | op = body.call(thisArg, _);
|
33 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35 | }
|
36 | };
|
37 | Object.defineProperty(exports, "__esModule", { value: true });
|
38 | var getGithubConfig = require("probot-config");
|
39 | var graphql_1 = require("graphql");
|
40 | var types_1 = require("./types");
|
41 | var diff_1 = require("./diff");
|
42 | var check = require("./check-runs");
|
43 | function fileLoader(_a) {
|
44 | var context = _a.context, owner = _a.owner, repo = _a.repo;
|
45 | var query = "\n query GetFile($repo: String!, $owner: String!, $expression: String!) {\n repository(name: $repo, owner: $owner) {\n object(expression: $expression) {\n ... on Blob {\n text\n }\n }\n }\n }\n ";
|
46 | return function loadFile(file) {
|
47 | return __awaiter(this, void 0, void 0, function () {
|
48 | var result;
|
49 | return __generator(this, function (_a) {
|
50 | switch (_a.label) {
|
51 | case 0: return [4 , context.github.graphql(query, {
|
52 | repo: repo,
|
53 | owner: owner,
|
54 | expression: file.ref + ":" + file.path,
|
55 | })];
|
56 | case 1:
|
57 | result = _a.sent();
|
58 | if (result.data) {
|
59 | return [2 , result.data.repository.object.text];
|
60 | }
|
61 | return [2 , result.repository.object.text];
|
62 | }
|
63 | });
|
64 | });
|
65 | };
|
66 | }
|
67 | function handleProbot(app) {
|
68 | var _this = this;
|
69 | app.on('check_run', function (context) { return __awaiter(_this, void 0, void 0, function () {
|
70 | var ref, action, _a, owner, repo;
|
71 | return __generator(this, function (_b) {
|
72 | switch (_b.label) {
|
73 | case 0:
|
74 | ref = context.payload.check_run.head_sha;
|
75 | action = context.payload.action;
|
76 | _a = context.repo(), owner = _a.owner, repo = _a.repo;
|
77 | return [4 , handleAction({ context: context, owner: owner, repo: repo, ref: ref, action: action })];
|
78 | case 1:
|
79 | _b.sent();
|
80 | return [2 ];
|
81 | }
|
82 | });
|
83 | }); });
|
84 | app.on('check_suite', function (context) { return __awaiter(_this, void 0, void 0, function () {
|
85 | var ref, action, _a, owner, repo;
|
86 | return __generator(this, function (_b) {
|
87 | switch (_b.label) {
|
88 | case 0:
|
89 | ref = context.payload.check_suite.head_sha;
|
90 | action = context.payload.action;
|
91 | _a = context.repo(), owner = _a.owner, repo = _a.repo;
|
92 | return [4 , handleAction({ context: context, owner: owner, repo: repo, ref: ref, action: action })];
|
93 | case 1:
|
94 | _b.sent();
|
95 | return [2 ];
|
96 | }
|
97 | });
|
98 | }); });
|
99 | }
|
100 | exports.default = handleProbot;
|
101 | function handleAction(_a) {
|
102 | var context = _a.context, owner = _a.owner, repo = _a.repo, ref = _a.ref, action = _a.action;
|
103 | return __awaiter(this, void 0, void 0, function () {
|
104 | function loadConfig() {
|
105 | return __awaiter(this, void 0, void 0, function () {
|
106 | var identifier, yamlConfig, ymlConfig, pkg, _a, _b;
|
107 | return __generator(this, function (_c) {
|
108 | switch (_c.label) {
|
109 | case 0:
|
110 | identifier = 'graphql-inspector';
|
111 | return [4 , getGithubConfig(context, identifier + '.yml')];
|
112 | case 1:
|
113 | yamlConfig = _c.sent();
|
114 | return [4 , getGithubConfig(context, identifier + '.yml')];
|
115 | case 2:
|
116 | ymlConfig = _c.sent();
|
117 | if (!(yamlConfig || ymlConfig)) return [3 , 3];
|
118 | return [2 , yamlConfig || ymlConfig];
|
119 | case 3:
|
120 | _b = (_a = JSON).parse;
|
121 | return [4 , loadFile({
|
122 | ref: ref,
|
123 | path: 'package.json',
|
124 | })];
|
125 | case 4:
|
126 | pkg = _b.apply(_a, [_c.sent()]);
|
127 | if (pkg[identifier]) {
|
128 | return [2 , pkg[identifier]];
|
129 | }
|
130 | _c.label = 5;
|
131 | case 5: return [2 ];
|
132 | }
|
133 | });
|
134 | });
|
135 | }
|
136 | var id, loadFile, url, config, oldPointer, newPointer, schemas, _b, _c, _d, actions, results, conclusion, annotations, issueInfo, _e, title, summary;
|
137 | return __generator(this, function (_f) {
|
138 | switch (_f.label) {
|
139 | case 0:
|
140 | if (['requested', 'rerequested', 'gh-action'].indexOf(action) === -1) {
|
141 | return [2 ];
|
142 | }
|
143 | id = owner + "/" + repo + "#" + ref;
|
144 | context.log.info("GraphQL Inspector started (action: " + action + ") - " + id);
|
145 | loadFile = fileLoader({
|
146 | context: context,
|
147 | owner: owner,
|
148 | repo: repo,
|
149 | });
|
150 | return [4 , check.start({
|
151 | context: context,
|
152 | owner: owner,
|
153 | repo: repo,
|
154 | sha: ref,
|
155 | })];
|
156 | case 1:
|
157 | url = _f.sent();
|
158 | return [4 , loadConfig()];
|
159 | case 2:
|
160 | config = _f.sent();
|
161 | if (!!config) return [3 , 4];
|
162 | context.log.error("No config file - " + id);
|
163 | return [4 , check.complete({
|
164 | url: url,
|
165 | context: context,
|
166 | conclusion: types_1.CheckConclusion.Failure,
|
167 | })];
|
168 | case 3:
|
169 | _f.sent();
|
170 | return [2 ];
|
171 | case 4:
|
172 | oldPointer = config.schema;
|
173 | newPointer = {
|
174 | path: oldPointer.path,
|
175 | ref: ref,
|
176 | };
|
177 | _b = {};
|
178 | _c = graphql_1.buildSchema;
|
179 | return [4 , loadFile(oldPointer)];
|
180 | case 5:
|
181 | _b.old = _c.apply(void 0, [_f.sent()]);
|
182 | _d = graphql_1.buildSchema;
|
183 | return [4 , loadFile(newPointer)];
|
184 | case 6:
|
185 | schemas = (_b.new = _d.apply(void 0, [_f.sent()]),
|
186 | _b);
|
187 | context.log.info("Both schemas built - " + id);
|
188 | actions = [];
|
189 | if (config.diff) {
|
190 | actions.push(diff_1.diff({
|
191 | path: config.schema.path,
|
192 | schemas: schemas,
|
193 | }));
|
194 | }
|
195 | return [4 , Promise.all(actions)];
|
196 | case 7:
|
197 | results = _f.sent();
|
198 | context.log.info("Actions (" + actions.length + ") are ready - " + id);
|
199 | conclusion = results.some(function (action) { return action.conclusion === types_1.CheckConclusion.Failure; })
|
200 | ? types_1.CheckConclusion.Failure
|
201 | : types_1.CheckConclusion.Success;
|
202 | annotations = results.reduce(function (annotations, action) {
|
203 | if (action.annotations) {
|
204 | return annotations.concat(action.annotations);
|
205 | }
|
206 | return annotations;
|
207 | }, []);
|
208 | issueInfo = "Found " + annotations.length + " issue" + (annotations.length > 1 ? 's' : '');
|
209 | _e = conclusion === types_1.CheckConclusion.Failure
|
210 | ? {
|
211 | title: "Something is wrong with your schema",
|
212 | summary: issueInfo,
|
213 | }
|
214 | : {
|
215 | title: 'Everything looks good',
|
216 | summary: issueInfo,
|
217 | }, title = _e.title, summary = _e.summary;
|
218 | context.log.info("Sending annotations (" + annotations.length + ")");
|
219 | return [4 , check.annotations({
|
220 | url: url,
|
221 | context: context,
|
222 | title: title,
|
223 | summary: summary,
|
224 | annotations: annotations,
|
225 | })];
|
226 | case 8:
|
227 | _f.sent();
|
228 | context.log.info("Finishing check (" + conclusion + ")");
|
229 | return [4 , check.complete({
|
230 | url: url,
|
231 | context: context,
|
232 | conclusion: conclusion,
|
233 | })];
|
234 | case 9:
|
235 | _f.sent();
|
236 | context.log.info("Completed");
|
237 | return [2 ];
|
238 | }
|
239 | });
|
240 | });
|
241 | }
|
242 | exports.handleAction = handleAction;
|
243 |
|
\ | No newline at end of file |