Skip to content

Commit 263617b

Browse files
authored
Merge pull request #2142 from jmyersmsft/assumptionBasedFeedDetection
Calculate packaging prefixes based on assumptions about domain names
2 parents cd4dfae + abd1e54 commit 263617b

File tree

9 files changed

+119
-8
lines changed

9 files changed

+119
-8
lines changed

Tasks/Common/nuget-task-common/LocationHelpers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vstsWebApi from 'vso-node-api/WebApi';
44
import * as util from 'util';
55
import * as tl from 'vsts-task-lib/task';
66
import * as path from 'path';
7+
import * as url from 'url';
78

89
function getUriForAccessMapping(mapping: locationApi.AccessMapping): string {
910
let accessPoint = mapping.accessPoint;
@@ -135,4 +136,25 @@ export function getConnectionDataForArea(serviceUri: string, areaName: string, a
135136

136137
export function getNuGetConnectionData(serviceUri: string, accessToken: string): Q.Promise<locationApi.ConnectionData> {
137138
return getConnectionDataForArea(serviceUri, 'nuget', 'b3be7473-68ea-4a81-bfc7-9530baaa19ad', accessToken);
139+
}
140+
141+
/**
142+
* Make assumptions about VSTS domain names to generate URI prefixes for feeds in the current collection.
143+
* Returns a promise so as to provide a drop-in replacement for location-service-based lookup.
144+
*/
145+
export function assumeNuGetUriPrefixes(collectionUri: string): Q.Promise<string[]> {
146+
let prefixes = [collectionUri];
147+
148+
let collectionUrlObject = url.parse(collectionUri);
149+
if(collectionUrlObject.hostname.toUpperCase().endsWith(".VISUALSTUDIO.COM"))
150+
{
151+
let hostparts = collectionUrlObject.hostname.split('.');
152+
let packagingHostName = hostparts[0] + ".pkgs.visualstudio.com"
153+
collectionUrlObject.hostname = packagingHostName;
154+
// remove the host property so it doesn't override the hostname property for url.format
155+
delete collectionUrlObject.host;
156+
prefixes.push(url.format(collectionUrlObject));
157+
}
158+
159+
return Q(prefixes);
138160
}

Tasks/NuGetInstaller/nugetinstaller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ var accessToken = auth.getSystemAccessToken();
8181
let buildIdentityDisplayName: string = null;
8282
let buildIdentityAccount: string = null;
8383

84+
/*
85+
BUG: HTTP calls to access the location service currently do not work for customers behind proxies.
8486
locationHelpers.getNuGetConnectionData(serviceUri, accessToken)
8587
.then(connectionData => {
8688
buildIdentityDisplayName = locationHelpers.getIdentityDisplayName(connectionData.authorizedUser);
@@ -97,9 +99,19 @@ locationHelpers.getNuGetConnectionData(serviceUri, accessToken)
9799
}
98100
99101
throw err;
100-
})
102+
})*/
103+
locationHelpers.assumeNuGetUriPrefixes(serviceUri)
101104
.then(urlPrefixes => {
102-
tl.debug("discovered URL prefixes: " + urlPrefixes.join(';'))
105+
tl.debug(`discovered URL prefixes: ${urlPrefixes}`);
106+
107+
// Note to readers: This variable will be going away once we have a fix for the location service for
108+
// customers behind proxies
109+
let testPrefixes = tl.getVariable("NuGetTasks.ExtraUrlPrefixesForTesting");
110+
if (testPrefixes) {
111+
urlPrefixes = urlPrefixes.concat(testPrefixes.split(';'));
112+
tl.debug(`all URL prefixes: ${urlPrefixes}`)
113+
}
114+
103115
return new auth.NuGetAuthInfo(urlPrefixes, accessToken);
104116
})
105117
.then(authInfo => {

Tasks/NuGetInstaller/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"minimumAgentVersion": "1.83.0",
1515
"groups": [

Tasks/NuGetInstaller/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"minimumAgentVersion": "1.83.0",
1515
"groups": [

Tasks/NugetPublisher/nugetpublisher.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ var accessToken = auth.getSystemAccessToken();
7878
let buildIdentityDisplayName: string = null;
7979
let buildIdentityAccount: string = null;
8080

81+
/*
82+
BUG: HTTP calls to access the location service currently do not work for customers behind proxies.
8183
locationHelpers.getNuGetConnectionData(serviceUri, accessToken)
8284
.then(connectionData => {
8385
buildIdentityDisplayName = locationHelpers.getIdentityDisplayName(connectionData.authorizedUser);
@@ -94,9 +96,19 @@ locationHelpers.getNuGetConnectionData(serviceUri, accessToken)
9496
}
9597
9698
throw err;
97-
})
99+
})*/
100+
locationHelpers.assumeNuGetUriPrefixes(serviceUri)
98101
.then(urlPrefixes => {
99-
tl.debug("discovered URL prefixes: " + urlPrefixes.join(';'))
102+
tl.debug(`discovered URL prefixes: ${urlPrefixes}`);
103+
104+
// Note to readers: This variable will be going away once we have a fix for the location service for
105+
// customers behind proxies
106+
let testPrefixes = tl.getVariable("NuGetTasks.ExtraUrlPrefixesForTesting");
107+
if (testPrefixes) {
108+
urlPrefixes = urlPrefixes.concat(testPrefixes.split(';'));
109+
tl.debug(`all URL prefixes: ${urlPrefixes}`)
110+
}
111+
100112
return new auth.NuGetAuthInfo(urlPrefixes, accessToken);
101113
})
102114
.then(authInfo => {

Tasks/NugetPublisher/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"demands": [
1515
"Cmd"

Tasks/NugetPublisher/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 1
12+
"Patch": 2
1313
},
1414
"demands": [
1515
"Cmd"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/// <reference path="../../../definitions/mocha.d.ts"/>
2+
/// <reference path="../../../definitions/node.d.ts"/>
3+
4+
import assert = require('assert');
5+
import trm = require('../../lib/taskRunner');
6+
import path = require('path');
7+
import util = require('util');
8+
9+
// Paths aren't the same between compile time and run time. This will need some work
10+
let realrequire = require;
11+
function myrequire(module: string): any {
12+
return realrequire(path.join(__dirname, "../../../Tasks/NuGetInstaller/node_modules", module));
13+
}
14+
require = <typeof require>myrequire;
15+
16+
17+
import locationHelper = require('nuget-task-common/LocationHelpers');
18+
19+
describe("Common-NuGetTaskCommon Suite", () => {
20+
21+
before(done => {
22+
// init here
23+
done();
24+
});
25+
26+
after(function() {
27+
28+
});
29+
30+
it("assume pkg service from visualtudio.com", done => {
31+
locationHelper.assumeNuGetUriPrefixes("https://contoso.visualstudio.com/foo")
32+
.then(assumedPrefixes => {
33+
assert.strictEqual(assumedPrefixes.length, 2, "should have exactly two prefix results");
34+
assert(assumedPrefixes.indexOf("https://contoso.pkgs.visualstudio.com/foo") !== -1, "should contain a pkgs host");
35+
assert(assumedPrefixes.indexOf("https://contoso.visualstudio.com/foo") !== -1, "should contain the original string");
36+
done();
37+
})
38+
.fail(done);
39+
});
40+
41+
it("assume no pkg service from not visualtudio.com", done => {
42+
locationHelper.assumeNuGetUriPrefixes("https://contoso.example.com/foo")
43+
.then(assumedPrefixes => {
44+
assert.strictEqual(assumedPrefixes.length, 1, "should have exactly one prefix result")
45+
assert(assumedPrefixes.indexOf("https://contoso.example.com/foo") !== -1, "should contain the original string");
46+
done();
47+
})
48+
.fail(done)
49+
});
50+
51+
it("assume no pkg service from localhost", done => {
52+
locationHelper.assumeNuGetUriPrefixes("https://localhost/foo")
53+
.then(assumedPrefixes => {
54+
assert.strictEqual(assumedPrefixes.length, 1, "should have exactly one prefix result")
55+
assert(assumedPrefixes.indexOf("https://localhost/foo") !== -1, "should contain the original string");
56+
done();
57+
})
58+
.fail(done);
59+
});
60+
});

definitions/nuget-task-common.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ declare module 'nuget-task-common/LocationHelpers' {
8282
}
8383
export function getConnectionDataForArea(serviceUri: string, areaName: string, areaId: string, accessToken: string): Q.Promise<locationApi.ConnectionData>;
8484
export function getNuGetConnectionData(serviceUri: string, accessToken: string): Q.Promise<locationApi.ConnectionData>;
85+
/**
86+
* Make assumptions about VSTS domain names to generate URI prefixes for feeds in the current collection.
87+
* Returns a promise so as to provide a drop-in replacement for location-service-based lookup.
88+
*/
89+
export function assumeNuGetUriPrefixes(collectionUri: string): Q.Promise<string[]>;
8590

8691
}
8792
declare module 'nuget-task-common/NuGetToolRunner' {

0 commit comments

Comments
 (0)