From 90944bb3632912ceecd68571dfe8769deb47cba1 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Thu, 20 Jun 2024 22:20:50 -0700 Subject: [PATCH 1/7] Begin work to use Ref data --- src/vcs.js | 27 ++++++++++++++++++--------- src/vcs_providers/github.js | 22 ++++++++++++++++++---- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/vcs.js b/src/vcs.js index f33e0db9..14769621 100644 --- a/src/vcs.js +++ b/src/vcs.js @@ -6,6 +6,7 @@ * function. */ +const semver = require("semver"); const query = require("./query_parameters/index.js").logic; const utils = require("./utils.js"); const constructNewPackagePublishData = require("./models/constructNewPackagePublishData.js"); @@ -123,28 +124,36 @@ async function newPackageData(userObj, ownerRepo, service) { .build(); } - let pack = await provider.packageJSON(userObj, ownerRepo); + // We will get tags first so that we can utilize specific tags when asking + // for further information about a package, rather than use the default branch + // information. + // See: https://github.com/pulsar-edit/package-backend/issues/205 - if (!pack.ok) { + const tags = await provider.tags(userObj, ownerRepo); + + if (!tags.ok) { return new ServerStatus() .notOk() - .setContent(`Failed to get gh package for ${ownerRepo} - ${pack.short}`) - .setShort("Bad Package") + .setContent(`Failed to get gh tags for ${ownerRepo} - ${tags.short}`) + .setShort("Server Error") .build(); } - const tags = await provider.tags(userObj, ownerRepo); + // Sort the tags into descending order + tags.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); - if (!tags.ok) { + let pack = await provider.packageJSON(userObj, ownerRepo, tags[0]?.name); + + if (!pack.ok) { return new ServerStatus() .notOk() - .setContent(`Failed to get gh tags for ${ownerRepo} - ${tags.short}`) - .setShort("Server Error") + .setContent(`Failed to get gh package for ${ownerRepo} - ${pack.short}`) + .setShort("Bad Package") .build(); } // Now to get our Readme - const readme = await provider.readme(userObj, ownerRepo); + const readme = await provider.readme(userObj, ownerRepo, tags[0]?.name); if (!readme.ok) { return new ServerStatus() diff --git a/src/vcs_providers/github.js b/src/vcs_providers/github.js index 1f5a78f3..70cc95d0 100644 --- a/src/vcs_providers/github.js +++ b/src/vcs_providers/github.js @@ -172,12 +172,19 @@ class GitHub extends Git { * the specified users credentials. * @param {object} userObj - The Raw User Object after verification. * @param {string} ownerRepo - The `owner/repo` combo of the repository to get. + * @param {string} [ver] - A version string indicating the version of data we wish to collect. * @returns {object} A Server Status Object where content is the Markdown text of a readme. */ - async readme(userObj, ownerRepo) { + async readme(userObj, ownerRepo, ver) { try { + let reqString = `/repos/${ownerRepo}/readme`; + + if (ver) { + reqString += `?ref=${ver}`; + } + const readmeRaw = await this._webRequestAuth( - `/repos/${ownerRepo}/readme`, + reqString, userObj.token ); // Using just `/readme` will let GitHub attempt to get the repos prefferred readme file, @@ -289,13 +296,20 @@ class GitHub extends Git { * @desc Returns the JSON Parsed text of the `package.json` on a GitHub repo. * @param {object} userObj - The Full User Object as received after verification. * @param {string} ownerRepo - The String combo of `owner/repo` for the package + * @param {string} [ver] - A version string indicating the version of data we wish to collect. * @returns {object} A Server Status Object, which when successfully, whose `content` * Is the JSON parsed `package.json` of the repo specified. */ - async packageJSON(userObj, ownerRepo) { + async packageJSON(userObj, ownerRepo, ver) { try { + let reqString = `/repos/${ownerRepo}/contents/package.json`; + + if (ver) { + reqString += `?ref=${ver}`; + } + const raw = await this._webRequestAuth( - `/repos/${ownerRepo}/contents/package.json`, + reqString, userObj.token ); From 8fc143215cb3d54daead03da166772478a1348c2 Mon Sep 17 00:00:00 2001 From: confused_techie Date: Fri, 21 Jun 2024 10:47:26 -0700 Subject: [PATCH 2/7] Getting tags during version publication --- src/controllers/postPackagesPackageNameVersions.js | 4 ++++ src/vcs.js | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/controllers/postPackagesPackageNameVersions.js b/src/controllers/postPackagesPackageNameVersions.js index 9a15eaa1..3b710b81 100644 --- a/src/controllers/postPackagesPackageNameVersions.js +++ b/src/controllers/postPackagesPackageNameVersions.js @@ -37,6 +37,9 @@ module.exports = { packageName: (context, req) => { return context.query.packageName(req); }, + tag: (context, req) => { + return context.query.tag(req); + } }, async postReturnHTTP(req, res, context, obj) { // We use postReturnHTTP to ensure the user doesn't wait on these other actions @@ -145,6 +148,7 @@ module.exports = { let packMetadata = await context.vcs.newVersionData( user.content, ownerRepo, + params.tag, "git" ); diff --git a/src/vcs.js b/src/vcs.js index 14769621..3effb040 100644 --- a/src/vcs.js +++ b/src/vcs.js @@ -140,9 +140,9 @@ async function newPackageData(userObj, ownerRepo, service) { } // Sort the tags into descending order - tags.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); + tags.content.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); - let pack = await provider.packageJSON(userObj, ownerRepo, tags[0]?.name); + let pack = await provider.packageJSON(userObj, ownerRepo, tags.content[0]?.name); if (!pack.ok) { return new ServerStatus() @@ -153,7 +153,7 @@ async function newPackageData(userObj, ownerRepo, service) { } // Now to get our Readme - const readme = await provider.readme(userObj, ownerRepo, tags[0]?.name); + const readme = await provider.readme(userObj, ownerRepo, tags.content[0]?.name); if (!readme.ok) { return new ServerStatus() @@ -231,13 +231,15 @@ async function newPackageData(userObj, ownerRepo, service) { * @param {object} userObj - The Full User Object as returned by `auth.verifyAuth()` * @param {string} ownerRepo - The Owner Repo Combo of the package affected. * Such as `pulsar-edit/pulsar` + * @param {string} tagRef - The version number or ref where data should be sought + * from the remote resource. * @param {string} service - The service to use as expected to be returned * by `vcs.determineProvider()`. Currently should be hardcoded to "git" * @returns {SSO_VCS_newVersionData} A Server Status Object, which when `ok: true` * returns all data that would be needed to update a package on the DB, and * upload a new version. */ -async function newVersionData(userObj, ownerRepo, service) { +async function newVersionData(userObj, ownerRepo, tagRef, service) { // Originally when publishing a new version the responsibility to collect // all package data fell onto the package_handler itself // Including collecting readmes and tags, now this function should encapsulate @@ -253,7 +255,7 @@ async function newVersionData(userObj, ownerRepo, service) { provider = new GitHub(); } - let pack = await provider.packageJSON(userObj, ownerRepo); + let pack = await provider.packageJSON(userObj, ownerRepo, tagRef); if (!pack.ok) { return { @@ -266,7 +268,7 @@ async function newVersionData(userObj, ownerRepo, service) { // Now we will also need to get the packages data to update on the DB // during verison pushes. - let readme = await provider.readme(userObj, ownerRepo); + let readme = await provider.readme(userObj, ownerRepo, tagRef); if (!readme.ok) { return { From 9d7e0400a88cb7d33da4db419bf5e26e1c313609 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 23 Jun 2024 18:41:25 -0700 Subject: [PATCH 3/7] Utilize integration tests for testing new version code, remove impossible to maintain unit tests on VCS During this change-over in testing strategy, I was able to find bugs in webhook handling for versions and squash them, as well as found we skipped validating a repo existed during version publication. And finally found the documentation of the type of return was incorrect on post version endpoint --- .../postPackagesPackageNameVersions.js | 31 +- src/vcs.js | 13 + .../d-pulsar-package/initial-package.json | 11 + .../fixtures/d-pulsar-package/initial-tags.js | 10 + tests/full/fixtures/d-pulsar-package/match.js | 50 +++ .../fixtures/d-pulsar-package/package.json | 11 + .../full/fixtures/d-pulsar-package/readme.md | 3 + tests/full/fixtures/d-pulsar-package/tags.js | 18 + tests/full/publish-version.test.js | 393 ++++++++++++++++++ tests/full/publish.test.js | 181 +++++++- tests/vcs/vcs.vcs.test.js | 290 ------------- 11 files changed, 712 insertions(+), 299 deletions(-) create mode 100644 tests/full/fixtures/d-pulsar-package/initial-package.json create mode 100644 tests/full/fixtures/d-pulsar-package/initial-tags.js create mode 100644 tests/full/fixtures/d-pulsar-package/match.js create mode 100644 tests/full/fixtures/d-pulsar-package/package.json create mode 100644 tests/full/fixtures/d-pulsar-package/readme.md create mode 100644 tests/full/fixtures/d-pulsar-package/tags.js create mode 100644 tests/full/publish-version.test.js diff --git a/src/controllers/postPackagesPackageNameVersions.js b/src/controllers/postPackagesPackageNameVersions.js index 3b710b81..dff597c4 100644 --- a/src/controllers/postPackagesPackageNameVersions.js +++ b/src/controllers/postPackagesPackageNameVersions.js @@ -7,10 +7,7 @@ module.exports = { summary: "Creates a new package version.", responses: { 201: { - description: "The package details indicating success.", - content: { - "application/json": "$packageObjectFull", - }, + description: "An object with a key 'message' indicating what version has been published.", }, }, }, @@ -43,6 +40,24 @@ module.exports = { }, async postReturnHTTP(req, res, context, obj) { // We use postReturnHTTP to ensure the user doesn't wait on these other actions + + // Lets bail early in case these values don't exist. + // Such as the original request failing + + if (typeof obj?.webhook?.pack !== "string" || typeof obj?.webhook?.user !== "string") { + // This data isn't defined, and we cannot work with it + return; + } + + if ( + typeof obj?.featureDetection?.user !== "string" || + typeof obj?.featureDetection?.ownerRepo !== "string" || + typeof obj?.featureDetection?.service !== "string" + ) { + // This data isn't defined, and we cannot work with it + return; + } + await context.webhook.alertPublishVersion( obj.webhook.pack, obj.webhook.user @@ -157,7 +172,11 @@ module.exports = { if (!packMetadata.ok) { const sso = new context.sso(); - return sso.notOk().addContent(packMetadata).assignCalls(callStack); + return sso + .notOk() + .addContent(packMetadata) + .addMessage(packMetadata.content) // Trust the output of VCS as an error message + .assignCalls(callStack); } const newName = packMetadata.content.name; @@ -271,6 +290,6 @@ module.exports = { ownerRepo: ownerRepo, }; - return sso.isOk().addContent(addVer.content); + return sso.isOk().addContent(addVer).addMessage(addVer.content); }, }; diff --git a/src/vcs.js b/src/vcs.js index 3effb040..39eb9087 100644 --- a/src/vcs.js +++ b/src/vcs.js @@ -255,6 +255,19 @@ async function newVersionData(userObj, ownerRepo, tagRef, service) { provider = new GitHub(); } + let exists = await provider.exists(userObj, ownerRepo); + + if (!exists.ok) { + // Could be due to an error, or it doesn't exist at all. + // For now until we support custom error messages will do a catch all + // return. + return new ServerStatus() + .notOk() + .setContent(`Failed to get repo: ${ownerRepo} - ${exists.short}`) + .setShort("Bad Repo") + .build(); + } + let pack = await provider.packageJSON(userObj, ownerRepo, tagRef); if (!pack.ok) { diff --git a/tests/full/fixtures/d-pulsar-package/initial-package.json b/tests/full/fixtures/d-pulsar-package/initial-package.json new file mode 100644 index 00000000..51b468fc --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/initial-package.json @@ -0,0 +1,11 @@ +{ + "name": "d-pulsar-package", + "version": "1.0.0", + "description": "A new package for stuff", + "main": "./lib/main.js", + "engines": { + "atom": "*" + }, + "repository": "https://github.com/confused-Techie/d-pulsar-package", + "license": "MIT" +} diff --git a/tests/full/fixtures/d-pulsar-package/initial-tags.js b/tests/full/fixtures/d-pulsar-package/initial-tags.js new file mode 100644 index 00000000..94151dd0 --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/initial-tags.js @@ -0,0 +1,10 @@ +module.exports = [ + { + name: "v1.0.0", + tarball_url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/tarball/refs/tags/v1.0.0", + commit: { + sha: "09f", + url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/commits/09f" + } + } +]; diff --git a/tests/full/fixtures/d-pulsar-package/match.js b/tests/full/fixtures/d-pulsar-package/match.js new file mode 100644 index 00000000..88d9f44c --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/match.js @@ -0,0 +1,50 @@ +module.exports = { + name: "d-pulsar-package", + owner: "confused-Techie", + readme: expect.stringContaining("I'm a readme!"), + metadata: { + main: "./lib/main.js", + name: "d-pulsar-package", + engines: { atom: "*" }, + license: "MIT", + version: "2.0.0", + repository: "https://github.com/confused-Techie/d-pulsar-package", + description: "An old package for stuff" + }, + releases: { latest: "2.0.0" }, + versions: { + "2.0.0": { + dist: { + tarball: expect.stringContaining( + "/api/packages/d-pulsar-package/versions/2.0.0/tarball" + ), + }, + main: "./lib/main.js", + name: "d-pulsar-package", + license: "MIT", + version: "2.0.0", + repository: "https://github.com/confused-Techie/d-pulsar-package", + description: "An old package for stuff" + }, + "1.0.0": { + dist: { + tarball: expect.stringContaining( + "/api/packages/d-pulsar-package/versions/1.0.0/tarball" + ), + }, + main: "./lib/main.js", + name: "d-pulsar-package", + license: "MIT", + version: "1.0.0", + repository: "https://github.com/confused-Techie/d-pulsar-package", + description: "A new package for stuff" + } + }, + repository: { + url: "https://github.com/confused-Techie/d-pulsar-package", + type: "git" + }, + downloads: "0", + stargazers_count: "0", + badges: [{ title: "Made for Pulsar!", type: "success" }], +}; diff --git a/tests/full/fixtures/d-pulsar-package/package.json b/tests/full/fixtures/d-pulsar-package/package.json new file mode 100644 index 00000000..ca87585d --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/package.json @@ -0,0 +1,11 @@ +{ + "name": "d-pulsar-package", + "version": "2.0.0", + "description": "An old package for stuff", + "main": "./lib/main.js", + "engines": { + "atom": "*" + }, + "repository": "https://github.com/confused-Techie/d-pulsar-package", + "license": "MIT" +} diff --git a/tests/full/fixtures/d-pulsar-package/readme.md b/tests/full/fixtures/d-pulsar-package/readme.md new file mode 100644 index 00000000..30508f91 --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/readme.md @@ -0,0 +1,3 @@ +# d-pulsar-package + +I'm a readme! diff --git a/tests/full/fixtures/d-pulsar-package/tags.js b/tests/full/fixtures/d-pulsar-package/tags.js new file mode 100644 index 00000000..8b439ac9 --- /dev/null +++ b/tests/full/fixtures/d-pulsar-package/tags.js @@ -0,0 +1,18 @@ +module.exports = [ + { + name: "v2.0.0", + tarball_url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/tarball/refs/tags/v2.0.0", + commit: { + sha: "09f", + url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/commits/09f" + } + }, + { + name: "v1.0.0", + tarball_url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/tarball/refs/tags/v1.0.0", + commit: { + sha: "09f", + url: "https://api.github.com/repos/confused-Techie/d-pulsar-package/commits/09f" + } + } +]; diff --git a/tests/full/publish-version.test.js b/tests/full/publish-version.test.js new file mode 100644 index 00000000..b80d4707 --- /dev/null +++ b/tests/full/publish-version.test.js @@ -0,0 +1,393 @@ +const fs = require("fs"); +const { Buffer } = require("node:buffer"); +const supertest = require("supertest"); +const nock = require("nock"); +const app = require("../../src/setupEndpoints.js"); +const database = require("../../src/database/_export.js"); + +function getFileEncoded(loc) { + const file = fs.readFileSync(loc, { encoding: "utf8" }); + + const buf = Buffer.from(file).toString("base64"); + + return buf; +} + +describe("publish package versions", () => { + beforeAll(async () => { + // Create our test user + let addUser = await database.insertNewUser( + "full-publish-version-test-user", + "full-publish-version-test-user-node-id", + "https://roadtonowhere.com" + ); + expect(addUser.ok).toBe(true); + + nock.disableNetConnect(); + nock.enableNetConnect("127.0.0.1"); + + // Add our test package + // ==================== + // Ensure the ownership tests passes for our test user + nock("https://api.github.com").get("/user").reply(200, { + node_id: "full-publish-version-test-user-node-id" + }); + + // Lets publish the initial version of our package to test additional versions with + + // Ensure user has ownership of our repo + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-version-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true + }, + role_name: "Admin" + } + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package") + .reply(200, { + full_name: "confused-Techie/d-pulsar-package" + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/readme?ref=v1.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/readme.md" + ), + encoding: "base64" + }); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/tags") + .reply(200, require("./fixtures/d-pulsar-package/initial-tags.js")); + + // Ensure we can get the 'package.json' + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/package.json?ref=v1.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/initial-package.json" + ), + encoding: "base64" + }); + + // Lets fail on any feature detection + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/snippets") + .reply(404); + + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/grammars") + .reply(404); + + const res = await supertest(app) + .post("/api/packages") + .query({ repository: "confused-Techie/d-pulsar-package" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(201); + expect(res.body.name).toBe("d-pulsar-package"); + // Notice we don't remove the package yet, until the end all of tests + + }); + + afterAll(async () => { + // Lets delete our test package versions + await database.removePackageByName("d-pulsar-package", true); + + nock.cleanAll(); + nock.enableNetConnect(); + }); + + beforeEach(() => { + // Ensure the ownership tests passes for our test user + nock("https://api.github.com").get("/user").reply(200, { + node_id: "full-publish-version-test-user-node-id" + }); + }); + + afterEach(() => { + // Remove any interceptors setup for this test + nock.cleanAll(); + }); + + test("when the request to verify if the repo exists fails", async () => { + // Ensure user has ownership of our repo + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-version-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true + }, + role_name: "Admin" + } + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package") + .reply(500); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/readme?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/readme.md" + ), + encoding: "base64" + }); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/tags") + .reply(200, require("./fixtures/d-pulsar-package/tags.js")); + + // Ensure we can get the 'package.json' + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/package.json?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/package.json" + ), + encoding: "base64" + }); + + // Lets fail on any feature detection + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/snippets") + .reply(404); + + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/grammars") + .reply(404); + + const res = await supertest(app) + .post("/api/packages/d-pulsar-package/versions") + .query({ tag: "v2.0.0" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Bad Repo: Failed to get repo: confused-Techie/d-pulsar-package - Server Error" + ); + }); + + test("when it fails to get the 'readme'", async () => { + // Ensure user has ownership of our repo + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-version-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true + }, + role_name: "Admin" + } + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package") + .reply(200, { + full_name: "confused-Techie/d-pulsar-package" + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/readme?ref=v2.0.0") + .reply(500); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/tags") + .reply(200, require("./fixtures/d-pulsar-package/tags.js")); + + // Ensure we can get the 'package.json' + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/package.json?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/package.json" + ), + encoding: "base64" + }); + + // Lets fail on any feature detection + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/snippets") + .reply(404); + + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/grammars") + .reply(404); + + const res = await supertest(app) + .post("/api/packages/d-pulsar-package/versions") + .query({ tag: "v2.0.0" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Bad Repo: Failed to get GitHub ReadMe confused-Techie/d-pulsar-package - Server Error - 500" + ); + }); + + test("when it fails to get the 'tags'", async () => { + // Ensure user has ownership of our repo + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-version-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true + }, + role_name: "Admin" + } + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package") + .reply(200, { + full_name: "confused-Techie/d-pulsar-package" + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/readme?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/readme.md" + ), + encoding: "base64" + }); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/tags") + .reply(500); + + // Ensure we can get the 'package.json' + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/package.json?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/package.json" + ), + encoding: "base64" + }); + + // Lets fail on any feature detection + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/snippets") + .reply(404); + + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/grammars") + .reply(404); + + const res = await supertest(app) + .post("/api/packages/d-pulsar-package/versions") + .query({ tag: "v2.0.0" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Server Error: Failed to get GitHub Tags for confused-Techie/d-pulsar-package - Server Error - 500" + ); + }); + + test("successfully publishes new version", async () => { + // Ensure user has ownership of our repo + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-version-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true + }, + role_name: "Admin" + } + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package") + .reply(200, { + full_name: "confused-Techie/d-pulsar-package" + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/readme?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/readme.md" + ), + encoding: "base64" + }); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/tags") + .reply(200, require("./fixtures/d-pulsar-package/tags.js")); + + // Ensure we can get the 'package.json' + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/package.json?ref=v2.0.0") + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/d-pulsar-package/package.json" + ), + encoding: "base64" + }); + + // Lets fail on any feature detection + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/snippets") + .reply(404); + + nock("https://api.github.com") + .get("/repos/confused-Techie/d-pulsar-package/contents/grammars") + .reply(404); + + const res = await supertest(app) + .post("/api/packages/d-pulsar-package/versions") + .query({ tag: "v2.0.0" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(201); + expect(res.body.content).toBe("Successfully added new version: d-pulsar-package@2.0.0"); + + const resPack = await supertest(app) + .get("/api/packages/d-pulsar-package"); + + expect(resPack).toHaveHTTPCode(200); + expect(resPack.body).toMatchObject(require("./fixtures/d-pulsar-package/match.js")); + }); +}); diff --git a/tests/full/publish.test.js b/tests/full/publish.test.js index 1824e191..134e7c32 100644 --- a/tests/full/publish.test.js +++ b/tests/full/publish.test.js @@ -13,7 +13,7 @@ function getFileEncoded(loc) { return buf; } -describe("can publish packages", () => { +describe("publish packages", () => { beforeAll(async () => { // Create our test user let addUser = await database.insertNewUser( @@ -69,7 +69,7 @@ describe("can publish packages", () => { // Ensure we can get the readme nock("https://api.github.com") - .get("/repos/confused-Techie/a-pulsar-package/readme") + .get("/repos/confused-Techie/a-pulsar-package/readme?ref=v1.0.0") .reply(200, { content: getFileEncoded( "./tests/full/fixtures/a-pulsar-package/readme.md" @@ -84,7 +84,7 @@ describe("can publish packages", () => { // Ensure we can get the `package.json` nock("https://api.github.com") - .get("/repos/confused-Techie/a-pulsar-package/contents/package.json") + .get("/repos/confused-Techie/a-pulsar-package/contents/package.json?ref=v1.0.0") .reply(200, { content: getFileEncoded( "./tests/full/fixtures/a-pulsar-package/package.json" @@ -116,6 +116,177 @@ describe("can publish packages", () => { await database.removePackageByName("a-pulsar-package", true); }); + test("when the request for the 'package.json' fails", async () => { + // Ensure the user has ownership of the repo + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true, + }, + role_name: "Admin", + }, + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package") + .reply(200, { + full_name: "confused-Techie/b-pulsar-package", + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/readme") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/b-pulsar-package/readme.md" + ), + encoding: "base64", + }); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/tags") + .reply(200, require("./fixtures/b-pulsar-package/tags.js")); + + // But we fail to deliver the `package.json` + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/contents/package.json") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(500); + + const res = await supertest(app) + .post("/api/packages") + .query({ repository: "confused-Techie/b-pulsar-package" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Bad Package: Failed to get gh package for confused-Techie/b-pulsar-package - Server Error" + ); + }); + + test("when the request for the 'tags' fails", async () => { + // Ensure the user has ownership of the repo + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true, + }, + role_name: "Admin", + }, + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package") + .reply(200, { + full_name: "confused-Techie/b-pulsar-package", + }); + + // Ensure we can get the readme + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/readme") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/b-pulsar-package/readme.md" + ), + encoding: "base64", + }); + + // But we fail on the tags request + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/tags") + .reply(500); + + // Ensure we can get the `package.json` + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/contents/package.json") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/b-pulsar-package/package.json" + ), + encoding: "base64", + }); + const res = await supertest(app) + .post("/api/packages") + .query({ repository: "confused-Techie/b-pulsar-package" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Server Error: Failed to get gh tags for confused-Techie/b-pulsar-package - Server Error" + ); + }); + + test("when the request for the 'readme' fails", async () => { + // Ensure the user has ownership of the repo + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/collaborators?page=1") + .reply(200, [ + { + node_id: "full-publish-test-user-node-id", + permissions: { + admin: true, + maintain: true, + push: true, + }, + role_name: "Admin", + }, + ]); + + // Ensure we can report that the package exists + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package") + .reply(200, { + full_name: "confused-Techie/b-pulsar-package", + }); + + // But then we fail on the 'readme' request + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/readme") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(500); + + // Ensure we can get the tags + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/tags") + .reply(200, require("./fixtures/b-pulsar-package/tags.js")); + + // Ensure we can get the `package.json` + nock("https://api.github.com") + .get("/repos/confused-Techie/b-pulsar-package/contents/package.json") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` + .reply(200, { + content: getFileEncoded( + "./tests/full/fixtures/b-pulsar-package/package.json" + ), + encoding: "base64", + }); + const res = await supertest(app) + .post("/api/packages") + .query({ repository: "confused-Techie/b-pulsar-package" }) + .set({ Authorization: "any-token-will-do" }); + + expect(res).toHaveHTTPCode(500); + expect(res.body.message).toBe( + "Server Error: From Bad Repo: Failed to get gh readme for confused-Techie/b-pulsar-package - Server Error" + ); + }); + test("when multiple tags are available", async () => { // Ensure user has ownership of repo nock("https://api.github.com/") @@ -142,6 +313,7 @@ describe("can publish packages", () => { // Ensure we can get the readme nock("https://api.github.com") .get("/repos/confused-Techie/b-pulsar-package/readme") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` .reply(200, { content: getFileEncoded( "./tests/full/fixtures/b-pulsar-package/readme.md" @@ -157,6 +329,7 @@ describe("can publish packages", () => { // Ensure we can get the `package.json` nock("https://api.github.com") .get("/repos/confused-Techie/b-pulsar-package/contents/package.json") + .query(true) // Match on any query tags, to support both `v1.0.0` and `v2.0.0` .reply(200, { content: getFileEncoded( "./tests/full/fixtures/b-pulsar-package/package.json" @@ -214,6 +387,7 @@ describe("can publish packages", () => { // Ensure we can get the readme nock("https://api.github.com") .get("/repos/confused-Techie/c-pulsar-package/readme") + .query(true) // Match on any query tags .reply(200, { content: getFileEncoded( "./tests/full/fixtures/c-pulsar-package/readme.md" @@ -229,6 +403,7 @@ describe("can publish packages", () => { // Ensure we can get the `package.json` nock("https://api.github.com") .get("/repos/confused-Techie/c-pulsar-package/contents/package.json") + .query(true) // Match on any query tags .reply(200, { content: getFileEncoded( "./tests/full/fixtures/c-pulsar-package/package.json" diff --git a/tests/vcs/vcs.vcs.test.js b/tests/vcs/vcs.vcs.test.js index d0bbb473..b6befe2f 100644 --- a/tests/vcs/vcs.vcs.test.js +++ b/tests/vcs/vcs.vcs.test.js @@ -30,296 +30,6 @@ describe("Does NewPackageData Return as expected", () => { expect(res.short).toBe("Bad Repo"); expect(res.content).toBe(`Failed to get repo: ${ownerRepo} - Bad Repo`); }); - - test("Package Error on Bad WebRequest", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack1.good_exists = new httpMock.HTTP(`/repos/${ownerRepo}`) - .ok(true) - .body({ full_name: ownerRepo }) - .parse(); - - http_cache.pack1.bad_package_json = new httpMock.HTTP( - `/repos/${ownerRepo}/contents/package.json` - ) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack1.good_exists, - http_cache.pack1.bad_package_json, - ]); - - const res = await vcs.newPackageData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Bad Package"); - expect(res.content).toBe( - `Failed to get gh package for ${ownerRepo} - Server Error` - ); - }); - - test("Tags Error Response on Bad WebRequest", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack1.good_package_json = new httpMock.HTTP( - `/repos/${ownerRepo}/contents/package.json` - ) - .ok(true) - .body( - httpMock.base64( - '{ "name": "pulsar", "version": "v1.101.0-beta", "repository": "https://github.com/pulsar-edit/pulsar" }' - ) - ) - .parse(); - - http_cache.pack1.bad_tags = new httpMock.HTTP(`/repos/${ownerRepo}/tags`) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack1.good_exists, - http_cache.pack1.good_package_json, - http_cache.pack1.bad_tags, - ]); - - const res = await vcs.newPackageData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Server Error"); - expect(res.content).toBe( - `Failed to get gh tags for ${ownerRepo} - Server Error` - ); - }); - - test("Readme Error on Bad Web Request", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack1.good_tags = new httpMock.HTTP(`/repos/${ownerRepo}/tags`) - .ok(true) - .status(200) - .body([ - { - name: "v1.101.0-beta", - tarball_url: - "https://api.github.com/repos/pulsar-edit/pulsar/tarball/refs/tags/v1.101.0-beta", - commit: { - sha: "dca05a3fccdc7d202e4ce00a5a2d3edef50a640f", - }, - }, - ]) - .parse(); - - http_cache.pack1.bad_readme = new httpMock.HTTP( - `/repos/${ownerRepo}/readme` - ) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack1.good_exists, - http_cache.pack1.good_package_json, - http_cache.pack1.good_tags, - http_cache.pack1.bad_readme, - ]); - - const res = await vcs.newPackageData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Bad Repo"); - expect(res.content).toBe( - `Failed to get gh readme for ${ownerRepo} - Server Error` - ); - }); - - test("Returns Valid New Package Data with successful Requests", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack1.good_readme = new httpMock.HTTP( - `/repos/${ownerRepo}/readme` - ) - .ok(true) - .status(200) - .body(httpMock.base64("This is a readme")) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack1.good_exists, - http_cache.pack1.good_package_json, - http_cache.pack1.good_tags, - http_cache.pack1.good_readme, - ]); - - const res = await vcs.newPackageData(userDataGeneric, ownerRepo, "git"); - expect(res.ok).toBe(true); - expect(res.content.name).toBe("pulsar"); - expect(res.content.creation_method).toBe("User Made Package"); - expect(res.content.readme).toBe("This is a readme"); - expect(res.content.metadata.name).toBe("pulsar"); - expect(res.content.metadata.version).toBe("v1.101.0-beta"); - expect(res.content.metadata.repository).toBe( - "https://github.com/pulsar-edit/pulsar" - ); - expect(res.content.repository.type).toBe("git"); - expect(res.content.repository.url).toBe( - "https://github.com/pulsar-edit/pulsar" - ); - expect(res.content.versions["1.101.0-beta"]).toBeDefined(); - expect(res.content.versions["1.101.0-beta"].name).toBe("pulsar"); - expect(res.content.versions["1.101.0-beta"].version).toBe("v1.101.0-beta"); - expect(res.content.versions["1.101.0-beta"].repository).toBe( - "https://github.com/pulsar-edit/pulsar" - ); - expect(res.content.versions["1.101.0-beta"].dist.tarball).toBe( - "https://api.github.com/repos/pulsar-edit/pulsar/tarball/refs/tags/v1.101.0-beta" - ); - expect(res.content.versions["1.101.0-beta"].dist.sha).toBe( - "dca05a3fccdc7d202e4ce00a5a2d3edef50a640f" - ); - expect(res.content.releases.latest).toBe("1.101.0-beta"); - }); -}); - -describe("Does newVersionData Return as Expected", () => { - test("Package Error on Bad WebRequest", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack2.bad_pack = new httpMock.HTTP( - `/repos/${ownerRepo}/contents/package.json` - ) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([http_cache.pack2.bad_pack]); - - const res = await vcs.newVersionData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Bad Package"); - expect(res.content).toBe( - `Failed to get GitHub Package ${ownerRepo} - Server Error - 500` - ); - }); - - test("Readme Error on Bad Web Request", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack2.good_pack = new httpMock.HTTP( - `/repos/${ownerRepo}/contents/package.json` - ) - .ok(true) - .status(200) - .body( - httpMock.base64( - '{ "name": "pulsar", "version": "v1.101.0-beta", "repository": "https://github.com/pulsar-edit/pulsar" }' - ) - ) - .parse(); - - http_cache.pack2.bad_readme = new httpMock.HTTP( - `/repos/${ownerRepo}/readme` - ) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack2.good_pack, - http_cache.pack2.bad_readme, - ]); - - const res = await vcs.newVersionData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Bad Repo"); - expect(res.content).toBe( - `Failed to get GitHub ReadMe ${ownerRepo} - Server Error - 500` - ); - }); - - test("Tags Error on Bad Web Request", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack2.good_readme = new httpMock.HTTP( - `/repos/${ownerRepo}/readme` - ) - .ok(true) - .status(200) - .body(httpMock.base64("This is a readme")) - .parse(); - - http_cache.pack2.bad_tags = new httpMock.HTTP(`/repos/${ownerRepo}/tags`) - .ok(false) - .short("Failed Request") - .status(500) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack2.good_pack, - http_cache.pack2.good_readme, - http_cache.pack2.bad_tags, - ]); - - const res = await vcs.newVersionData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(false); - expect(res.short).toBe("Server Error"); - expect(res.content).toBe( - `Failed to get GitHub Tags for ${ownerRepo} - Server Error - 500` - ); - }); - - test("Returns Valid New Version Data with successful Requests", async () => { - const ownerRepo = "confused-Techie/pulsar-backend"; - - http_cache.pack2.good_tags = new httpMock.HTTP(`/repos/${ownerRepo}/tags`) - .ok(true) - .status(200) - .body([ - { - name: "v1.101.0-beta", - tarball_url: - "https://api.github.com/repos/pulsar-edit/pulsar/tarball/refs/tags/v1.101.0-beta", - commit: { - sha: "dca05a3fccdc7d202e4ce00a5a2d3edef50a640f", - }, - }, - ]) - .parse(); - - const tmpMock = httpMock.webRequestMock([ - http_cache.pack2.good_pack, - http_cache.pack2.good_readme, - http_cache.pack2.good_tags, - ]); - - const res = await vcs.newVersionData(userDataGeneric, ownerRepo, "git"); - - expect(res.ok).toBe(true); - expect(res.content.name).toBe("pulsar"); - expect(res.content.repository.type).toBe("git"); - expect(res.content.repository.url).toBe( - "https://github.com/pulsar-edit/pulsar" - ); - expect(res.content.readme).toBe("This is a readme"); - expect(res.content.metadata.name).toBe("pulsar"); - expect(res.content.metadata.version).toBe("v1.101.0-beta"); - expect(res.content.metadata.tarball_url).toBe( - "https://api.github.com/repos/pulsar-edit/pulsar/tarball/refs/tags/v1.101.0-beta" - ); - expect(res.content.metadata.sha).toBe( - "dca05a3fccdc7d202e4ce00a5a2d3edef50a640f" - ); - }); }); describe("Ownership Returns as Expected", () => { From d9c252268e49b1d9add3cb3c5e383ccd291681ba Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Tue, 25 Jun 2024 22:42:36 -0700 Subject: [PATCH 4/7] Enable latest `ref` support for featureDetection --- src/vcs_providers/github.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/vcs_providers/github.js b/src/vcs_providers/github.js index 70cc95d0..84fd7487 100644 --- a/src/vcs_providers/github.js +++ b/src/vcs_providers/github.js @@ -5,6 +5,7 @@ const Git = require("./git.js"); const CSON = require("cson"); +const semver = require("semver"); /** * @class GitHub @@ -426,10 +427,16 @@ class GitHub extends Git { */ async featureDetection(userObj, ownerRepo) { // First lets declare the functions we will rely on within this - const providesSnippets = async () => { + const providesSnippets = async (ref) => { try { + let reqString = `/repos/${ownerRepo}/contents/snippets`; + + if (ref) { + reqString += `?ref=${ref}`; + } + const raw = await this._webRequestAuth( - `/repos/${ownerRepo}/contents/snippets`, + reqString, userObj.token ); @@ -449,10 +456,16 @@ class GitHub extends Git { } }; - const getGrammars = async () => { + const getGrammars = async (ref) => { try { + let reqString = `/repos/${ownerRepo}/contents/grammars`; + + if (ref) { + reqString += `?ref=${ref}`; + } + const raw = await this._webRequestAuth( - `/repos/${ownerRepo}/contents/grammars`, + ref, userObj.token ); @@ -470,8 +483,14 @@ class GitHub extends Git { let supportedLanguages = []; for (let i = 0; i < raw.content.body.length; i++) { + let innerReqString = `/repos/${ownerRepo}/contents/grammars/${res.body[i].name}`; + + if (ref) { + innerReqString += `?ref=${ref}`; + } + const rawInner = this._webRequestAuth( - `/repos/${ownerRepo}/contents/grammars/${res.body[i].name}`, + innerReqString, userObj.token ); @@ -518,8 +537,12 @@ class GitHub extends Git { // Now with our utility functions here defined, lets call them and build // our featureObject - let grammars = await getGrammars(); - let snippets = await providesSnippets(); + let tags = await tags(userObj, ownerRepo); + // Sort the tags into descending order + tags.content.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); + + const grammars = await getGrammars(tags.content[0]?.name); + const snippets = await providesSnippets(tags.content[0]?.name); let featureObject = {}; From 94c2cbc8563e7dd1f2648667f4ee009d9fe0a705 Mon Sep 17 00:00:00 2001 From: confused_techie Date: Wed, 26 Jun 2024 08:00:23 -0700 Subject: [PATCH 5/7] Properly call `this.tags` --- src/vcs_providers/github.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcs_providers/github.js b/src/vcs_providers/github.js index 84fd7487..d3aa92a5 100644 --- a/src/vcs_providers/github.js +++ b/src/vcs_providers/github.js @@ -537,7 +537,7 @@ class GitHub extends Git { // Now with our utility functions here defined, lets call them and build // our featureObject - let tags = await tags(userObj, ownerRepo); + let tags = await this.tags(userObj, ownerRepo); // Sort the tags into descending order tags.content.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); From fcf4d497ae1f7058c8b4e119497b31115e18dfbd Mon Sep 17 00:00:00 2001 From: confused_techie Date: Wed, 26 Jun 2024 08:18:57 -0700 Subject: [PATCH 6/7] Get tests to wait for next tick After some complexity added to featureDetection, seems that now `nock` is being cleaned up right after the API returns, not giving a chance for featureDetection logic to use those endpoints to function as it runs afterwards. Attempting a simple method of waiting, otherwise may need more featureDetection specific logic --- src/vcs_providers/github.js | 2 ++ tests/full/publish-version.test.js | 2 ++ tests/full/publish.test.js | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/vcs_providers/github.js b/src/vcs_providers/github.js index d3aa92a5..ae68ed48 100644 --- a/src/vcs_providers/github.js +++ b/src/vcs_providers/github.js @@ -538,6 +538,8 @@ class GitHub extends Git { // Now with our utility functions here defined, lets call them and build // our featureObject let tags = await this.tags(userObj, ownerRepo); + console.log("tags value"); + console.log(tags); // Sort the tags into descending order tags.content.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); diff --git a/tests/full/publish-version.test.js b/tests/full/publish-version.test.js index b80d4707..1fc6fb6a 100644 --- a/tests/full/publish-version.test.js +++ b/tests/full/publish-version.test.js @@ -103,6 +103,8 @@ describe("publish package versions", () => { }); afterAll(async () => { + await new Promise(process.nextTick); + // Lets delete our test package versions await database.removePackageByName("d-pulsar-package", true); diff --git a/tests/full/publish.test.js b/tests/full/publish.test.js index 134e7c32..8309a34c 100644 --- a/tests/full/publish.test.js +++ b/tests/full/publish.test.js @@ -28,6 +28,8 @@ describe("publish packages", () => { }); afterAll(() => { + await new Promise(process.nextTick); + nock.cleanAll(); nock.enableNetConnect(); }); From 3aa6c7e98266d06d37e657d2351ad3b4466a099d Mon Sep 17 00:00:00 2001 From: confused_techie Date: Wed, 26 Jun 2024 08:25:34 -0700 Subject: [PATCH 7/7] Add safety to avoid featureDetection failure --- src/vcs_providers/github.js | 14 ++++++++++++-- tests/full/publish-version.test.js | 4 +--- tests/full/publish.test.js | 2 -- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/vcs_providers/github.js b/src/vcs_providers/github.js index ae68ed48..3a083b46 100644 --- a/src/vcs_providers/github.js +++ b/src/vcs_providers/github.js @@ -538,8 +538,18 @@ class GitHub extends Git { // Now with our utility functions here defined, lets call them and build // our featureObject let tags = await this.tags(userObj, ownerRepo); - console.log("tags value"); - console.log(tags); + + if (!tags.ok) { + // We failed to get the tags data for featureDetection. + // This would be rather difficult to occur in real life, but this safety check + // becomes necessary in testing, as the mocked endpoints won't stay alive + // after the HTTP request returns. + return { + ok: false, + content: tags + }; + } + // Sort the tags into descending order tags.content.sort((a, b) => { return semver.rcompare(a.name, b.name)} ); diff --git a/tests/full/publish-version.test.js b/tests/full/publish-version.test.js index 1fc6fb6a..dd1428e6 100644 --- a/tests/full/publish-version.test.js +++ b/tests/full/publish-version.test.js @@ -102,9 +102,7 @@ describe("publish package versions", () => { }); - afterAll(async () => { - await new Promise(process.nextTick); - + afterAll(async () => { // Lets delete our test package versions await database.removePackageByName("d-pulsar-package", true); diff --git a/tests/full/publish.test.js b/tests/full/publish.test.js index 8309a34c..134e7c32 100644 --- a/tests/full/publish.test.js +++ b/tests/full/publish.test.js @@ -28,8 +28,6 @@ describe("publish packages", () => { }); afterAll(() => { - await new Promise(process.nextTick); - nock.cleanAll(); nock.enableNetConnect(); });