Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for any valid repository declaration during publication #246

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/controllers/postPackages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @module postPackages
*/
const parseGithubURL = require("parse-github-url");

module.exports = {
docs: {
Expand Down Expand Up @@ -92,7 +93,7 @@ module.exports = {
}

// Check repository format validity.
if (params.repository === "" || typeof params.repository !== "string") {
if (params.repository === false) {
// repository format is invalid
const sso = new context.sso();

Expand All @@ -105,7 +106,8 @@ module.exports = {
// Currently though the repository is in `owner/repo` format,
// meanwhile needed functions expects just `repo`

const repo = params.repository.split("/")[1]?.toLowerCase();
const repo = parseGithubURL(params.repository)?.name.toLowerCase();
const ownerRepo = parseGithubURL(params.repository)?.repo;

if (repo === undefined) {
const sso = new context.sso();
Expand Down Expand Up @@ -133,7 +135,7 @@ module.exports = {
// has permissions to this package
const gitowner = await context.vcs.ownership(
user.content,
params.repository
ownerRepo
);

callStack.addCall("vcs.ownership", gitowner);
Expand All @@ -148,7 +150,7 @@ module.exports = {
// TODO: Stop hardcoding `git` as service
const newPack = await context.vcs.newPackageData(
user.content,
params.repository,
ownerRepo,
"git"
);

Expand Down Expand Up @@ -190,16 +192,15 @@ module.exports = {
// Now to check if this package is a bundled package (since they don't exist on the db)
const isBundled = context.bundled.isNameBundled(newPack.content.name);

callStack.addCall("bundled.isNameBundled", isBundled);

if (isBundled.ok && isBundled.content) {
const sso = new context.sso();

return sso
.notOk()
.addShort("package_exists")
.addCalls("auth.verifyAuth", user)
.addCalls("vcs.ownership", gitowner)
.addCalls("vcs.newPackageData", newPack)
.addCalls("bundled.isNameBundled", isBundled);
.assignCalls(callStack);
}

// Now with valid package data, we can insert them into the DB
Expand Down Expand Up @@ -247,7 +248,7 @@ module.exports = {
sso.featureDetection = {
user: user.content,
service: "git", // TODO stop hardcoding git
ownerRepo: params.repository,
ownerRepo: ownerRepo,
};

return sso.isOk().addContent(packageObjectFull);
Expand Down
29 changes: 19 additions & 10 deletions src/query_parameters/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* @function repo
* @desc Parses the 'repository' query parameter, returning it if valid, otherwise returning ''.
* @param {object} req - The `Request` object inherited from the Express endpoint.
* @returns {string} Returning the valid 'repository' query parameter, or '' if invalid.
* @returns {string} Returning the valid 'repository' query parameter, or false if invalid.
*/
const parseGithubURL = require("parse-github-url");

module.exports = {
schema: {
Expand All @@ -21,17 +22,25 @@ module.exports = {
const prov = req.query.repository;

if (prov === undefined) {
return "";
return false;
}

const re = /^[-a-zA-Z\d][-\w.]{0,213}\/[-a-zA-Z\d][-\w.]{0,213}$/;
const parsed = parseGithubURL(prov);

// Ensure req is in the format "owner/repo" and
// owner and repo observe the following rules:
// - less than or equal to 214 characters
// - only URL safe characters (letters, digits, dashes, underscores and/or dots)
// - cannot begin with a dot or an underscore
// - cannot contain a space.
return prov.match(re) !== null ? prov : "";
if (typeof parsed.owner !== "string" || typeof parsed.name !== "string") {
return false;
}

const re = /^[^._ ][^ ]{0,213}$/;
// Ensure both the name and owner:
// - less than or equal to 214 characters
// - cannot begin with a dot or an underscore
// - cannot contain a space

if (parsed.owner.match(re) === null || parsed.name.match(re) === null) {
return false;
}

return prov;
},
};
4 changes: 2 additions & 2 deletions tests/http/postPackages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("POST /api/packages Behaves as expected", () => {

const sso = await endpoint.logic(
{
repository: "",
repository: false,
auth: "valid-token",
},
localContext
Expand All @@ -63,7 +63,7 @@ describe("POST /api/packages Behaves as expected", () => {

const sso = await endpoint.logic(
{
repository: "bad-format",
repository: false,
auth: "valid-token",
},
localContext
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ describe("Verify Auth Query Returns", () => {

const repositoryCases = [
[{ query: { repository: "owner/repo" } }, "owner/repo"],
[{ query: {} }, ""],
[{ query: { repository: "InvalidRepo" } }, ""],
[{ query: {} }, false],
[{ query: { repository: "InvalidRepo" } }, false],
[{ query: { repository: "[email protected]:ndr-brt/pulsar-p5js" } }, "[email protected]:ndr-brt/pulsar-p5js"],
];

describe("Verify Repo Query Returns", () => {
Expand Down
Loading