Use null namepsace when no namespace for package

This commit is contained in:
Lane Seppala
2022-06-16 16:14:02 -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 };
};
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 packageurl_js_1 = __nccwpck_require__(8915);
function parseGoPackage(pkg) {
const [qualifiedPackage, version] = pkg.split('@');
const namespace = encodeURIComponent(path_1.default.dirname(qualifiedPackage));
const name = path_1.default.basename(qualifiedPackage);
let namespace = null;
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);
}
exports.parseGoPackage = parseGoPackage;
/**
* 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
+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/otlptracehttp@v1.7.0
golang.org/x/sys@v0.0.0-20220317061510-51cd9980dadf
@@ -6,7 +6,28 @@ golang.org/x/text@v0.3.7
golang.org/x/text@v0.3.7
golang.org/x/text@v0.3.7`
describe('test dependenciesProcessorFunc', () => {
describe('parseGoPackage', () => {
it('parses a package with a namespace', () => {
expect(parseGoPackage('foo/bar@0.1.2').toString()).toEqual(
'pkg:golang/foo/bar@0.1.2'
)
})
// 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/bar@0.1.2').toString()).toEqual(
'pkg:golang/foo%2Fboo/bar@0.1.2'
)
})
it('parses a package without a namespace', () => {
expect(parseGoPackage('foo.io@0.1.2').toString()).toEqual(
'pkg:golang/foo.io@0.1.2'
)
})
})
describe('parseGoList', () => {
test('parses output of go list command into dependencies', () => {
const dependencies = parseGoList(GO_DEPENDENCIES)
@@ -49,7 +70,7 @@ describe('parseGoModGraph', () => {
expect(assocList[0][0]).toEqual({
type: 'golang',
name: 'go-example',
namespace: '.', // we expect the namespace for the root package to process to dot
namespace: null,
version: null,
qualifiers: null,
subpath: null
+10 -3
View File
@@ -1,10 +1,17 @@
import path from 'path'
import { PackageURL } from 'packageurl-js'
function parseGoPackage (pkg: string): PackageURL {
export function parseGoPackage (pkg: string): PackageURL {
const [qualifiedPackage, version] = pkg.split('@')
const namespace = encodeURIComponent(path.dirname(qualifiedPackage))
const name = path.basename(qualifiedPackage)
let namespace: string | null = null
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)
}