Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Improve checking of Team Foundation Server server name formats #42

Merged
merged 2 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/contexts/gitcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"use strict";

import { Utils } from "../helpers/utils";
import { RepoUtils } from "../helpers/repoutils";

var pgc = require("parse-git-config");
var gri = require("git-repo-info");
Expand Down Expand Up @@ -44,23 +45,21 @@ export class GitContext {
this._gitCurrentBranch = this._gitRepoInfo.branch;
this._gitCurrentRef = "refs/heads/" + this._gitCurrentBranch;

if (this._gitOriginalRemoteUrl.toLowerCase().indexOf("/_git/") >= 0) {
//All Team Services and TFS Git remote urls contain /_git/
if (RepoUtils.IsTeamFoundationGitRepo(this._gitOriginalRemoteUrl)) {
let purl = url.parse(this._gitOriginalRemoteUrl);
if (purl != null) {
let splitHostName = purl.hostname.split(".");
if (splitHostName.length > 2) {
this._isTeamServicesUrl = splitHostName[1] === "visualstudio" && splitHostName[2] === "com";
if (this._isTeamServicesUrl === true) {
let splitHref = purl.href.split("@");
if (splitHref.length === 2) { //RemoteUrl is SSH
//For Team Services, default to https:// as the protocol
this._gitRemoteUrl = "https://" + purl.hostname + purl.pathname;
this._isSsh = true;
} else {
this._gitRemoteUrl = this._gitOriginalRemoteUrl;
}
if (RepoUtils.IsTeamFoundationServicesRepo(this._gitOriginalRemoteUrl)) {
this._isTeamServicesUrl = true;
let splitHref = purl.href.split("@");
if (splitHref.length === 2) { //RemoteUrl is SSH
//For Team Services, default to https:// as the protocol
this._gitRemoteUrl = "https://" + purl.hostname + purl.pathname;
this._isSsh = true;
} else {
this._gitRemoteUrl = this._gitOriginalRemoteUrl;
}
} else if (splitHostName.length === 1) {
} else if (RepoUtils.IsTeamFoundationServerRepo(this._gitOriginalRemoteUrl)) {
this._isTeamFoundationServer = true;
this._gitRemoteUrl = this._gitOriginalRemoteUrl;
if (purl.protocol.toLowerCase() === "ssh:") {
Expand Down
31 changes: 31 additions & 0 deletions src/helpers/repoutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
"use strict";

export class RepoUtils {
//Checks to see if url contains /_git/ signifying a Team Foundation Git repository
public static IsTeamFoundationGitRepo(url: string): boolean {
if (url && url.toLowerCase().indexOf("/_git/") >= 0) {
return true;
}
return false;
}

//Checks to ensure it's a Team Foundation Git repo, then ensures it's hosted on visualstudio.com
public static IsTeamFoundationServicesRepo(url: string): boolean {
if (RepoUtils.IsTeamFoundationGitRepo(url) && url.toLowerCase().indexOf(".visualstudio.com") >= 0) {
return true;
}
return false;
}

//Checks to ensure it's a Team Foundation repo, then ensures it's *not* on visualstudio.com
public static IsTeamFoundationServerRepo(url: string): boolean {
if (RepoUtils.IsTeamFoundationGitRepo(url) && !RepoUtils.IsTeamFoundationServicesRepo(url)) {
return true;
}
return false;
}
}
7 changes: 4 additions & 3 deletions src/info/repositoryinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"use strict";

import { Logger } from "../helpers/logger";
import { RepoUtils } from "../helpers/repoutils";

var url = require("url");

Expand Down Expand Up @@ -53,13 +54,13 @@ export class RepositoryInfo {
this._protocol = purl.protocol;
this._query = purl.query;

if (repositoryUrl.toLowerCase().indexOf("/_git/") >= 0) {
if (this._host.toLowerCase().indexOf(".visualstudio.com") >= 0) {
if (RepoUtils.IsTeamFoundationGitRepo(repositoryUrl)) {
if (RepoUtils.IsTeamFoundationServicesRepo(repositoryUrl)) {
let splitHost = this._host.split(".");
this._account = splitHost[0];
this._isTeamServicesUrl = true;
Logger.LogDebug("_isTeamServicesUrl: true");
} else {
} else if (RepoUtils.IsTeamFoundationServerRepo(repositoryUrl)) {
this._account = purl.host;
this._isTeamFoundationServer = true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/team-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export class TeamExtension {
//Go get the details about the repository
let repositoryClient: QTeamServicesApi = new QTeamServicesApi(this._gitContext.RemoteUrl, [CredentialManager.GetCredentialHandler()]);
Logger.LogInfo("Getting repository information (vsts/info) with repositoryClient");
Logger.LogDebug("RemoteUrl = " + this._gitContext.RemoteUrl);
repositoryClient.getVstsInfo().then((repoInfo) => {
Logger.LogInfo("Retrieved repository info with repositoryClient");
Logger.LogObject(repoInfo);
Expand All @@ -375,6 +376,7 @@ export class TeamExtension {
let connectionUrl: string = (this._serverContext.RepoInfo.IsTeamServices === true ? this._serverContext.RepoInfo.AccountUrl : this._serverContext.RepoInfo.CollectionUrl);
let accountClient: QTeamServicesApi = new QTeamServicesApi(connectionUrl, [CredentialManager.GetCredentialHandler()]);
Logger.LogInfo("Getting connectionData with accountClient");
Logger.LogDebug("connectionUrl = " + connectionUrl);
accountClient.connect().then((settings) => {
Logger.LogInfo("Retrieved connectionData with accountClient");
this.resetErrorStatus();
Expand Down
126 changes: 126 additions & 0 deletions test/helpers/repoutils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
"use strict";

import { RepoUtils } from "../../src/helpers/repoutils";

var chai = require("chai");
/* tslint:disable:no-unused-variable */
var expect = chai.expect;
/* tslint:enable:no-unused-variable */
var assert = chai.assert;
chai.should();

describe("RepoUtils", function() {

beforeEach(function() {
//
});

it("should ensure valid Team Foundation Server Git urls", function() {
let url : string;
//Server names with ports are valid
url = "http://pioneer-new-dt:8080/tfs/DevDiv_Projects2/_git/JavaALM";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://minint-i0lvs2o:8080/tfs/DefaultCollection/_git/GitProject";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://java-tfs2015:8081/tfs/DefaultCollection/_git/GitJava";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://sources2010/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://sources2010/tfs/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
//Multi-part server names are valid
url = "http://java-tfs01.3redis.local/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://java-tfs01.loseit.local:8080/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://stdtfs.amways.local/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
//IP addresses would be valid
url = "http://192.168.0.1/sources2010/tfs/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
url = "http://192.168.0.1:8084/sources2010/tfs/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
//SSH urls would be valid
url = "ssh://sources2010:22/tfs/DefaultCollection/_git/GitAgile";
assert.isTrue(RepoUtils.IsTeamFoundationServerRepo(url));
});

it("should ensure Team Services urls are not valid Team Foundation Server Git urls", function() {
let url : string;
url = "https://mseng.visualstudio.com/VSOnline/_git/Java.VSCode.CredentialStore";
assert.isFalse(RepoUtils.IsTeamFoundationServerRepo(url));
url = "https://mseng.visualstudio.com/DefaultCollection/VSOnline/_git/Java.IntelliJ";
assert.isFalse(RepoUtils.IsTeamFoundationServerRepo(url));
url = "ssh://mseng@mseng.visualstudio.com:22/DefaultCollection/VSOnline/_git/Java.IntelliJ/";
assert.isFalse(RepoUtils.IsTeamFoundationServerRepo(url));
});

it("should ensure valid Team Services Git urls", function() {
let url : string;
url = "https://mseng.visualstudio.com/VSOnline/_git/Java.VSCode.CredentialStore";
assert.isTrue(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "https://mseng.visualstudio.com/DefaultCollection/VSOnline/_git/Java.IntelliJ";
assert.isTrue(RepoUtils.IsTeamFoundationServicesRepo(url));
//SSH urls would be valid
url = "ssh://mseng@mseng.visualstudio.com:22/DefaultCollection/VSOnline/_git/Java.IntelliJ/";
assert.isTrue(RepoUtils.IsTeamFoundationServicesRepo(url));
});

it("should ensure Team Server urls are not valid Team Services Git urls", function() {
let url : string;

url = "http://pioneer-new-dt:8080/tfs/DevDiv_Projects2/_git/JavaALM";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://minint-i0lvs2o:8080/tfs/DefaultCollection/_git/GitProject";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://java-tfs2015:8081/tfs/DefaultCollection/_git/GitJava";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://sources2010/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://sources2010/tfs/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://java-tfs01.3redis.local/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://java-tfs01.loseit.local:8080/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://stdtfs.amways.local/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://192.168.0.1/sources2010/tfs/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "http://192.168.0.1:8084/sources2010/tfs/DefaultCollection/_git/repo";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "ssh://sources2010:22/tfs/DefaultCollection/_git/GitAgile";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
});

it("should ensure valid Team Foundation Git urls", function() {
let url : string;
url = "http://sources2010/tfs/DefaultCollection/_git/repo";
assert.isTrue(RepoUtils.IsTeamFoundationGitRepo(url));
url = "https://account.visualstudio.com/DefaultCollection/VSOnline/_git/Java.IntelliJ";
assert.isTrue(RepoUtils.IsTeamFoundationServicesRepo(url));

//The following url has no "/_git/" in the path
url = "https://account.visualstudio.com/DefaultCollection/VSOnline/Java.IntelliJ";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
});

it("should detect invalid Team Foundation Git urls", function() {
let url : string;

//The following urls have no "/_git/" in the path
url = "https://account.visualstudio.com/DefaultCollection/VSOnline/Java.IntelliJ";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "git@github.com:Microsoft/Git-Credential-Manager-for-Mac-and-Linux.git";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "https://github.com/Microsoft/vsts-vscode.git";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
url = "https://account.visualstudio.com/DefaultCollection/VSOnline/Java.IntelliJ";
assert.isFalse(RepoUtils.IsTeamFoundationServicesRepo(url));
});

});