UNPKG

3.64 kBJavaScriptView Raw
1/*
2 Copyright 2019 Google LLC
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 https://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * Responds to any HTTP request that can provide a "message" field in the body.
19 *
20 * @param {!Object} req Cloud Function request context.
21 * @param {!Object} res Cloud Function response context.
22 *
23 * gcloud beta functions deploy lints --trigger-http --stage-bucket lintresults --memory 2048
24 */
25
26exports.lints = function lintResults(req, res) {
27 const util = require("util");
28
29 var customer,
30 org,
31 api,
32 revision,
33 authorization = req.get("Authorization"),
34 formatter;
35
36 if (req.method === "GET") {
37 customer = req.query.customer;
38 org = req.query.org;
39 api = req.query.api;
40 revision = req.query.revision;
41 formatter = req.query.formatter;
42 } else if (req.method === "POST") {
43 customer = req.body.customer;
44 org = req.body.organization;
45 api = req.body.api;
46 revision = req.body.revision;
47 formatter = req.body.formatter;
48 }
49
50 //interpret the accepts header if the formatter was not explicitly supplied
51 if (!formatter) {
52 if (req.accepts("json")) {
53 formatter = "json.js";
54 } else if (req.accepts("xml")) {
55 formatter = "jslint-xml.js";
56 } else if (req.accepts("text")) {
57 formatter = "unix.js";
58 } else {
59 formatter = "html.js";
60 }
61 }
62
63 if (!org && customer) {
64 //populate from the customer if supplied
65
66 var str = '';
67
68 var options = {
69 host: 'nucleus-api-test.apigee.com',
70 path: '/sfdc/account_orgs'
71 };
72
73 var callback = function(response) {
74 response.on('data', function (chunk) {
75 str += chunk;
76 });
77
78 response.on('end', function () {
79 console.log(str);
80 //str contains the payload from the account_orgs - parse to get the orgs to lint
81 res.send(str); // SEND ACTUAL RESPONSE HERE
82 });
83 }
84
85 var req = http.request(options, callback);
86 req.end();
87
88 }else{
89
90 }
91
92 if (!org || !api || !revision || !req.get("Authorization")) {
93 // This is an error case, as "message" is required.
94 res
95 .status(400)
96 .send(
97 "org(" +
98 org +
99 "), api(" +
100 api +
101 "), and revision(" +
102 revision +
103 ") are required parameters and authorization header is required"
104 );
105 } else {
106 // Everything is okay.
107
108 const configuration = {
109 source: {
110 type: "ManagementServer",
111 org,
112 api,
113 revision,
114 authorization
115 },
116 output: function(report) {
117 if (req.query.debug) {
118 console.log(report);
119 }
120 },
121 apiUpload: {
122 destPath: "https://csdata-test.apigee.net/v1/lintresults",
123 authorization,
124 organization: org
125 },
126 formatter
127 };
128
129 var bl = require("./lib/package/bundleLinter.js");
130 bl.lint(configuration, function(result, err) {
131 if (err) {
132 console.log(util.inspect(err, { showHidden: false, depth: 3 }));
133 res.status(err.status).send(err.message);
134 } else {
135 res.status(200).send(result.apiUploadResponse);
136 }
137 });
138 }
139};