Repository: FHIR/sushi (ts)

Issue:
Resolution of "current" IG dependencies broken
Changes in the the CI Build infrastructure have caused the resolution of "current" IGs to fail.  For example, if your package.json declares the following dependencies:
```json
  "dependencies": {
     "hl7.fhir.r4.core": "4.0.1",
     "hl7.fhir.uv.vhdir":"current"
  },
```
Then running SUSHI will now issue these errors:
```
error: Failed to load hl7.fhir.uv.vhdir#current
error: 404 - <html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>

error: Failed to add hl7.fhir.uv.vhdir:current to ImplementationGuide instance.  Could not determine its canonical URL from the FHIR cache.
```

More on the changes that caused this:
https://chat.fhir.org/#narrow/stream/179165-committers/topic/Build.20Problem/near/187594182

Pull request:
Fix resolution of "current" IG dependencies
Changes in the CI Build infrastructure broke the way we were finding the "current" version of IG dependencies.  This commit fixes the approach as described on Zulip here: https://chat.fhir.org/#narrow/stream/179165-committers/topic/Build.20Problem/near/187610137

Fixes #201

Patch:
diff --git a/src/fhirdefs/load.ts b/src/fhirdefs/load.ts
index 39870ac..7bb82f2 100644
--- a/src/fhirdefs/load.ts
+++ b/src/fhirdefs/load.ts
@@ -36,18 +36,26 @@ export async function loadDependency(
     } else if (version === 'current') {
       // Current packages need to be loaded using build.fhir.org
       const baseUrl = 'http://build.fhir.org/ig';
-      const res = await rp.get({ uri: `${baseUrl}/qas.json`, json: true });
+      const res: { 'package-id': string; date: string; repo: string }[] = await rp.get({
+        uri: `${baseUrl}/qas.json`,
+        json: true
+      });
       // Find matching packages and sort by date to get the most recent
       let newestPackage;
       if (res && res.length > 0) {
-        const matchingPackages = res.filter((p: any) => p['package-id'] === packageName);
-        newestPackage = matchingPackages.sort((p1: any, p2: any) => {
+        const matchingPackages = res.filter(p => p['package-id'] === packageName);
+        newestPackage = matchingPackages.sort((p1, p2) => {
           return Date.parse(p2['date']) - Date.parse(p1['date']);
         })[0];
       }
-      if (newestPackage) {
-        // Current packages are stored at build.fhir.org
-        packageUrl = `${baseUrl}/${newestPackage.repo}/package.tgz`;
+      if (newestPackage && newestPackage.repo) {
+        // Find the package based on the first two parts of the package's 'repo' property.  E.g.,
+        //   "repo": "HL7/US-Core-R4/branches/test-branch-tweak/qa.json"
+        // means to find the package at:
+        //    http://build.fhir.org/ig/HL7/US-Core-R4/package.tgz
+        // See: https://chat.fhir.org/#narrow/stream/179165-committers/topic/Build.20Problem/near/187610137
+        const [org, repo] = newestPackage.repo.split('/');
+        packageUrl = `${baseUrl}/${org}/${repo}/package.tgz`;
       } else {
         throw new CurrentPackageLoadError(fullPackageName);
       }


Test patch:
diff --git a/test/fhirdefs/load.test.ts b/test/fhirdefs/load.test.ts
index 56cbcdd..ac98ebf 100644
--- a/test/fhirdefs/load.test.ts
+++ b/test/fhirdefs/load.test.ts
@@ -68,30 +68,30 @@ describe('#loadDependency()', () => {
       if (options.uri === 'http://build.fhir.org/ig/qas.json') {
         return [
           {
-            url: 'http://hl7.org/fhir/hspc/ImplementationGuide/hspc',
-            name: 'HSPC Implementation Guide',
-            'package-id': 'hl7.fhir.hspc',
-            'ig-ver': '1.0',
-            date: 'Thu, 11 Oct, 2018 11:00:14 -0600',
-            errs: 34,
-            warnings: 10,
-            hints: 0,
-            version: '3.0.1',
-            tool: '3.4.0-13844',
-            repo: 'nrdavis1/HSPCFHIRtest/bran'
+            url: 'http://hl7.org/fhir/us/core/ImplementationGuide/hl7.fhir.us.core.r4-4.0.0',
+            name: 'USCoreR4',
+            'package-id': 'hl7.fhir.us.core.r4',
+            'ig-ver': '4.0.0',
+            date: 'Sat, 18 May, 2019 01:48:14 +0000',
+            errs: 538,
+            warnings: 34,
+            hints: 202,
+            version: '4.0.0',
+            tool: '4.1.0 (3)',
+            repo: 'HL7Imposter/US-Core-R4/branches/oldbranch/qa.json'
           },
           {
-            url: 'http://hl7.org/fhir/hspc/ImplementationGuide/hspc',
-            name: 'HSPC Implementation Guide',
-            'package-id': 'hl7.fhir.hspc',
-            'ig-ver': '1.0',
-            date: 'Tue, 05 Mar, 2019 12:02:14 -0700',
-            errs: 26980,
-            warnings: 10,
-            hints: 0,
-            version: '3.0.1',
-            tool: '3.4.0-13844',
-            repo: 'nrdavis1/HSPCFHIRtest/branches'
+            url: 'http://hl7.org/fhir/us/core/ImplementationGuide/hl7.fhir.us.core.r4-4.0.0',
+            name: 'USCoreR4',
+            'package-id': 'hl7.fhir.us.core.r4',
+            'ig-ver': '4.0.0',
+            date: 'Mon, 20 Jan, 2020 19:36:43 +0000',
+            errs: 1496,
+            warnings: 36,
+            hints: 228,
+            version: '4.0.0',
+            tool: '4.1.0 (3)',
+            repo: 'HL7/US-Core-R4/branches/newbranch/qa.json'
           }
         ];
       } else {
@@ -138,8 +138,8 @@ describe('#loadDependency()', () => {
   });
 
   it('should try to load the latest package from build.fhir.org when a current package version is loaded', async () => {
-    await expect(loadDependency('hl7.fhir.hspc', 'current', defs, 'foo')).rejects.toThrow(
-      'The package hl7.fhir.hspc#current could not be loaded locally or from the FHIR package registry'
+    await expect(loadDependency('hl7.fhir.us.core.r4', 'current', defs, 'foo')).rejects.toThrow(
+      'The package hl7.fhir.us.core.r4#current could not be loaded locally or from the FHIR package registry'
     );
     expect(requestSpy.mock.calls[0]).toEqual([
       {
@@ -148,10 +148,10 @@ describe('#loadDependency()', () => {
       }
     ]);
     expect(requestSpy.mock.calls[1][0].uri).toBe(
-      'http://build.fhir.org/ig/nrdavis1/HSPCFHIRtest/branches/package.tgz'
+      'http://build.fhir.org/ig/HL7/US-Core-R4/package.tgz'
     );
-    expect(ensureDirSpy.mock.calls[0]).toEqual([path.join('foo', 'hl7.fhir.hspc#current')]);
-    expect(tarSpy.mock.calls[0][0].cwd).toBe(path.join('foo', 'hl7.fhir.hspc#current'));
+    expect(ensureDirSpy.mock.calls[0]).toEqual([path.join('foo', 'hl7.fhir.us.core.r4#current')]);
+    expect(tarSpy.mock.calls[0][0].cwd).toBe(path.join('foo', 'hl7.fhir.us.core.r4#current'));
   });
 
   it('should throw CurrentPackageLoadError when a current package is not listed', async () => {