From e4780cbb25f7fe608bfb91ce81a754064147f209 Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Wed, 27 Oct 2021 09:48:21 -0500 Subject: [PATCH 1/2] add version SHA to file name to make it easier for admins to version each extension --- common.js | 16 ++++++++++++++++ index.js | 9 ++++++++- to-clone/publish.js | 9 ++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 common.js diff --git a/common.js b/common.js new file mode 100644 index 0000000..dc28953 --- /dev/null +++ b/common.js @@ -0,0 +1,16 @@ +/* + * Extracts the version SHA from the manifest URL + * @param jsonManifest JSON object representing the manifest. + * expecting to have a 'url' at root level of format: + * "https://sourcegraph.com/-/static/extension/13212-sourcegraph-verilog.js?cf8s0z1vs41c--sourcegraph-verilog" + * @return string version + */ +function getExtensionVersion(jsonManifest) { + const url = new URL(jsonManifest['url']); + // .search.substr(1) to remove the '?' + const version = url.search.substr(1); + // now remove the extension ID (everything after --) + return version.substr(0, version.indexOf('--')); +} + +module.exports = { getExtensionVersion } diff --git a/index.js b/index.js index 6e1e195..9ef3c1b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const fs = require("fs"); const https = require("https"); +const common = require("./common"); (async function main() { const extensionIDs = getExtensionIDs(); @@ -144,15 +145,21 @@ function createBundlesDirectory(extensions) { // Clone customer instructions and publish script. fs.copyFileSync("./to-clone/instructions.md", `${bundlesPath}/README.md`); fs.copyFileSync("./to-clone/publish.js", `${bundlesPath}/publish.js`); + fs.copyFileSync("./common.js", `${bundlesPath}/common.js`); // Create extension directories (to be used by publish script). for (const { extensionID, manifest, bundle } of extensions) { const extensionFileName = extensionID.replace("/", "-"); + // get the version + // assumption: URL is in format: + // "url": "https://sourcegraph.com/-/static/extension/13212-sourcegraph-verilog.js?cf8s0z1vs41c--sourcegraph-verilog" + const version = common.getExtensionVersion(JSON.parse(manifest)); + fs.mkdirSync(`${bundlesPath}/${extensionFileName}`); fs.writeFileSync( - `${bundlesPath}/${extensionFileName}/${extensionFileName}.js`, + `${bundlesPath}/${extensionFileName}/${extensionFileName}.${version}.js`, bundle, "utf8" ); diff --git a/to-clone/publish.js b/to-clone/publish.js index 18172ab..89eedd6 100644 --- a/to-clone/publish.js +++ b/to-clone/publish.js @@ -2,6 +2,7 @@ const fs = require("fs"); const https = require("https"); const http = require("http"); const path = require("path"); +const common = require("./common"); const PUBLISHER = process.env.PUBLISHER; if (!PUBLISHER) { @@ -24,7 +25,7 @@ const sgurl = new URL(SRC_ENDPOINT); })(); function determineExtensionDirectories() { - const fixedFiles = ["package.json", "publish.js", "README.md"]; + const fixedFiles = ["package.json", "publish.js", "common.js", "README.md"]; const files = fs.readdirSync(__dirname); return files.filter((file) => !fixedFiles.includes(file)); @@ -56,13 +57,15 @@ async function publishExtension(extensionDirectory) { path.join(fullPathToExtensions, `package.json`), "utf-8" ); + const jsonManifest = JSON.parse(manifest); + const version = common.getExtensionVersion(jsonManifest); const bundle = await fs.promises.readFile( - path.join(fullPathToExtensions, `${extensionDirectory}.js`), + path.join(fullPathToExtensions, `${extensionDirectory}.${version}.js`), "utf-8" ); // Parse manifest to find extension name. - const { name } = JSON.parse(manifest); + const { name } = jsonManifest; const extensionID = `${PUBLISHER}/${name}`; // Make POST request to publish extension to registry. From 4a5287fcba5da830971bcde186f941fa0999eb5f Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Wed, 27 Oct 2021 10:03:43 -0500 Subject: [PATCH 2/2] add better handling of errors from Sourcegraph server --- to-clone/publish.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/to-clone/publish.js b/to-clone/publish.js index 89eedd6..8b0f5fb 100644 --- a/to-clone/publish.js +++ b/to-clone/publish.js @@ -101,7 +101,7 @@ async function publishExtension(extensionDirectory) { { hostname: sgurl.hostname, port: sgurl.port || undefined, - path: "/.api/graphql", + path: "/.api/graphql?PublishExtension", method: "POST", headers: { Authorization: `token ${SRC_ACCESS_TOKEN}`, @@ -124,15 +124,15 @@ async function publishExtension(extensionDirectory) { }); try { - if (typeof JSON.parse(result) === "string") { - // Could be an error message (e.g. "Private mode requires authentication") - console.log(result); + if (typeof result == 'object' && 'errors' in result && result['errors'].length > 0) { + console.log(`Unable to publish ${extensionID} from directory ${extensionDirectory}`); + result['errors'].forEach(err => console.log(err.message)); + } else { + console.log( + `Published extension ${extensionID} from directory: ${extensionDirectory}` + ); } - } catch { - // noop + } catch (e) { + console.log(e.stack); } - - console.log( - `Published extension ${extensionID} from directory: ${extensionDirectory}` - ); }