Use null namepsace when no namespace for package

This commit is contained in:
Lane Seppala
2022-06-16 16:29:00 -06:00
parent 9fcd421723
commit 111eaef29a
4 changed files with 47 additions and 10 deletions
+12 -3
View File
@@ -100,15 +100,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseGoModGraph = exports.parseGoList = void 0; exports.parseGoModGraph = exports.parseGoList = exports.parseGoPackage = void 0;
const path_1 = __importDefault(__nccwpck_require__(1017)); const path_1 = __importDefault(__nccwpck_require__(1017));
const packageurl_js_1 = __nccwpck_require__(8915); const packageurl_js_1 = __nccwpck_require__(8915);
function parseGoPackage(pkg) { function parseGoPackage(pkg) {
const [qualifiedPackage, version] = pkg.split('@'); const [qualifiedPackage, version] = pkg.split('@');
const namespace = encodeURIComponent(path_1.default.dirname(qualifiedPackage)); let namespace = null;
const name = path_1.default.basename(qualifiedPackage); let name;
if (qualifiedPackage.indexOf('/') !== -1) {
// need to URL-safe encode slashes in the namespace
namespace = encodeURIComponent(path_1.default.dirname(qualifiedPackage));
name = path_1.default.basename(qualifiedPackage);
}
else {
name = qualifiedPackage;
}
return new packageurl_js_1.PackageURL('golang', namespace, name, version !== null && version !== void 0 ? version : null, null, null); return new packageurl_js_1.PackageURL('golang', namespace, name, version !== null && version !== void 0 ? version : null, null, null);
} }
exports.parseGoPackage = parseGoPackage;
/** /**
* parseGoList parses a list of Go packages (one per line) matching the format * parseGoList parses a list of Go packages (one per line) matching the format
* "${GO_PACKAGE}@v{VERSION}" into Package URLs. This expects the output of 'go * "${GO_PACKAGE}@v{VERSION}" into Package URLs. This expects the output of 'go
+1 -1
View File
File diff suppressed because one or more lines are too long
+24 -3
View File
@@ -1,4 +1,4 @@
import { parseGoList, parseGoModGraph } from './parse' import { parseGoPackage, parseGoList, parseGoModGraph } from './parse'
const GO_DEPENDENCIES = `go.opentelemetry.io/otel/exporters/otlp/otlptrace/[email protected] const GO_DEPENDENCIES = `go.opentelemetry.io/otel/exporters/otlp/otlptrace/[email protected]
golang.org/x/[email protected] golang.org/x/[email protected]
@@ -6,7 +6,28 @@ golang.org/x/[email protected]
golang.org/x/[email protected] golang.org/x/[email protected]
golang.org/x/[email protected]` golang.org/x/[email protected]`
describe('test dependenciesProcessorFunc', () => { describe('parseGoPackage', () => {
it('parses a package with a namespace', () => {
expect(parseGoPackage('foo/[email protected]').toString()).toEqual(
'pkg:golang/foo/[email protected]'
)
})
// this test should pass, but packageurl-js is double URL-safe encoding the
// '%' to %25. It won't, however, URL-encode the '/' that is represented by
// %2F.
it.skip('parses a package with a namespace with slashes', () => {
expect(parseGoPackage('foo/boo/[email protected]').toString()).toEqual(
'pkg:golang/foo%2Fboo/[email protected]'
)
})
it('parses a package without a namespace', () => {
expect(parseGoPackage('[email protected]').toString()).toEqual(
'pkg:golang/[email protected]'
)
})
})
describe('parseGoList', () => {
test('parses output of go list command into dependencies', () => { test('parses output of go list command into dependencies', () => {
const dependencies = parseGoList(GO_DEPENDENCIES) const dependencies = parseGoList(GO_DEPENDENCIES)
@@ -49,7 +70,7 @@ describe('parseGoModGraph', () => {
expect(assocList[0][0]).toEqual({ expect(assocList[0][0]).toEqual({
type: 'golang', type: 'golang',
name: 'go-example', name: 'go-example',
namespace: '.', // we expect the namespace for the root package to process to dot namespace: null,
version: null, version: null,
qualifiers: null, qualifiers: null,
subpath: null subpath: null
+10 -3
View File
@@ -1,10 +1,17 @@
import path from 'path' import path from 'path'
import { PackageURL } from 'packageurl-js' import { PackageURL } from 'packageurl-js'
function parseGoPackage (pkg: string): PackageURL { export function parseGoPackage (pkg: string): PackageURL {
const [qualifiedPackage, version] = pkg.split('@') const [qualifiedPackage, version] = pkg.split('@')
const namespace = encodeURIComponent(path.dirname(qualifiedPackage)) let namespace: string | null = null
const name = path.basename(qualifiedPackage) let name: string
if (qualifiedPackage.indexOf('/') !== -1) {
// need to URL-safe encode slashes in the namespace
namespace = encodeURIComponent(path.dirname(qualifiedPackage))
name = path.basename(qualifiedPackage)
} else {
name = qualifiedPackage
}
return new PackageURL('golang', namespace, name, version ?? null, null, null) return new PackageURL('golang', namespace, name, version ?? null, null, null)
} }