Skip to content

Commit

Permalink
fix: allow for permsRepo without repository (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Jul 20, 2020
1 parent 860b8ed commit f552d02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
14 changes: 11 additions & 3 deletions src/lib/packument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {json} from './json';
import parseGh = require('github-url-from-git');
let registryUrl = 'https://registry.npmjs.org';

export interface PackumentVersionWombat extends PackumentVersion {
permsRepo?: string;
}

// this only fetches public packuments.
// if we passed auth it could fetch any the publish user has access to.
export const packument = (name: string): Promise<Packument | false> => {
Expand Down Expand Up @@ -71,7 +75,9 @@ export const repoToGithub = (
return false;
};

export const findLatest = (doc: Packument): PackumentVersion | undefined => {
export const findLatest = (
doc: Packument
): PackumentVersionWombat | undefined => {
const latest = doc['dist-tags'] ? doc['dist-tags'].latest : false;
if (!latest) {
let newestVersion = '';
Expand All @@ -84,9 +90,11 @@ export const findLatest = (doc: Packument): PackumentVersion | undefined => {
const t = new Date(doc.time[version]).getTime();
if (t >= versionDate) newestVersion = version;
});
return doc.versions ? doc.versions[newestVersion] : undefined;
return doc.versions
? (doc.versions[newestVersion] as PackumentVersionWombat)
: undefined;
}
return doc.versions[latest];
return doc.versions[latest] as PackumentVersionWombat;
};

export const setRegistryUrl = (url: string) => {
Expand Down
19 changes: 13 additions & 6 deletions src/lib/write-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Packument} from '@npm/types';
import {Packument, PackumentVersion} from '@npm/types';
import {Request, Response} from 'express';
import * as request from 'request';

Expand All @@ -25,7 +25,12 @@ import {totpCode} from '../lib/totp-code';

import * as datastore from './datastore';
import {newVersions} from './new-versions';
import {findLatest, packument, repoToGithub} from './packument';
import {
findLatest,
packument,
repoToGithub,
PackumentVersionWombat,
} from './packument';
import {WombatServerError} from './wombat-server-error';

export interface WriteResponse {
Expand Down Expand Up @@ -90,7 +95,9 @@ export const writePackage = async (
// permissions
try {
doc = JSON.parse(drainedBody + '') as Packument;
latest = doc.versions[doc['dist-tags'].latest || ''];
latest = doc.versions[
doc['dist-tags'].latest || ''
] as PackumentVersionWombat;
// not all packages have a latest dist-tag
} catch (e) {
console.info('got ' + e + ' parsing publish');
Expand All @@ -111,15 +118,15 @@ export const writePackage = async (
return respondWithError(res, msg, 500);
}

if (!latest.repository) {
if (!latest.repository && !latest.permsRepo) {
console.info('missing repository in the latest version of ' + packageName);
const msg = `in order to publish the latest version must have a repository ${user.name} can access.`;
return respondWithError(res, msg, 400);
}

console.info('latest repo ', latest.repository);
console.info('latest repo ', latest.permsRepo ?? latest.repository);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const repo = repoToGithub((latest as any).permsRepo || latest.repository);
const repo = repoToGithub(latest.permsRepo ?? latest.repository);

// make sure publish user has permission to publish the package
// get the github repository from packument
Expand Down

0 comments on commit f552d02

Please sign in to comment.