private AuthenticationHeaderValue ResolveAuthenticationHeader(VstsEnvironmentVariables env) { if (env == null) { throw new ArgumentNullException("env"); } if (this.credentials != null) { return(new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", this.credentials.UserName, this.credentials.Password))))); } string accessToken = env.System.AccessToken; if (String.IsNullOrEmpty(accessToken)) { throw new InvalidOperationException("Failed to resolve System.AccessToken as it was blank in the VSTS variables provided to the test"); } return(new AuthenticationHeaderValue("Bearer", accessToken)); }
/// <summary> /// Creates a new <see cref="VstsEnvironmentVariables"/> instance from the given <paramref name="testProperties"/> /// </summary> /// <param name="testProperties"> /// The test properties from which we want to extract the TFS Test Run settings. Must not be null. /// This should typically be <see cref="TestContext.Properties"/> /// </param> /// <returns> /// A new <see cref="VstsEnvironmentVariables"/> instance generated from the given <paramref name="testProperties"/>. /// Guaranteed not to be null. /// </returns> public static VstsEnvironmentVariables Create(IDictionary testProperties) { if (testProperties == null) { throw new ArgumentNullException("testProperties"); } Dictionary <string, object> properties = testProperties.Keys.OfType <string>() .Where(k => WellKnownPrefixes.Any(k.StartsWith)) .ToDictionary(k => k, k => testProperties[k]); VstsEnvironmentVariables runProperties = new VstsEnvironmentVariables { Agent = new VstsAgentVariables(), Build = new VstsBuildVariables(), Release = new VstsReleaseVariables(), Tf = new VstsTeamFoundationVariables(), Common = new VstsCommonVariables(), System = new VstsSystemVariables(), IsValid = false }; if (properties.Count == 0) { return(runProperties); } PropertyHelper.SetVariables(runProperties.Agent, PropertyHelper.SelectPropertiesAndTrimName(properties, AgentPrefix)); PropertyHelper.SetVariables(runProperties.Build, PropertyHelper.SelectPropertiesAndTrimName(properties, BuildPrefix)); PropertyHelper.SetVariables(runProperties.Release, PropertyHelper.SelectPropertiesAndTrimName(properties, ReleasePrefix)); PropertyHelper.SetVariables(runProperties.Tf, PropertyHelper.SelectPropertiesAndTrimName(properties, TeamFoundationPrefix)); PropertyHelper.SetVariables(runProperties.Common, PropertyHelper.SelectPropertiesAndTrimName(properties, CommonPrefix)); PropertyHelper.SetVariables(runProperties.System, PropertyHelper.SelectPropertiesAndTrimName(properties, SystemPrefix)); runProperties.IsValid = true; return(runProperties); }
private static async Task <IList <TestConfig> > GetTestConfigurationsPageAsync(VstsEnvironmentVariables env, int pageIndex, int pageSize, HttpClient client) { if (env == null) { throw new ArgumentNullException("env"); } if (client == null) { throw new ArgumentNullException("client"); } if (pageIndex < 0) { throw new ArgumentOutOfRangeException("pageIndex"); } if (pageSize < 1) { throw new ArgumentOutOfRangeException("pageSize"); } UriBuilder uriBuilder = new UriBuilder(env.System.TeamFoundationCollectionUri); uriBuilder.Path += String.Format("/{0}/_apis/test/configurations", env.System.TeamProject); uriBuilder.Query = String.Format("includeAllProperties={0}&api-version={1}&$skip={2}&$top={3}", true, "3.0-preview.1", pageIndex * pageSize, pageSize); string jsonResponse; using (HttpResponseMessage response = await client.GetAsync(uriBuilder.Uri)) { jsonResponse = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); } dynamic result = JsonConvert.DeserializeObject(jsonResponse); JArray configurations = result.value; return(configurations.ToObject <List <TestConfig> >()); }
/// <inheritdoc /> public async Task <IDictionary <string, string> > GetTestConfigurationPropertiesAsync(VstsEnvironmentVariables env, string configurationName) { if (env == null) { throw new ArgumentNullException("env"); } if (string.IsNullOrEmpty(configurationName)) { throw new ArgumentException("Must not be null or empty", "configurationName"); } HttpClient client = new HttpClient { DefaultRequestHeaders = { Authorization = this.ResolveAuthenticationHeader(env) } }; int pageIndex = -1; IList <TestConfig> configurationProperties; TestConfig configuration; do { configurationProperties = await GetTestConfigurationsPageAsync(env, ++pageIndex, this.pageSize, client); if (configurationProperties == null) { return(null); } configuration = configurationProperties.FirstOrDefault(tc => StringComparer.InvariantCultureIgnoreCase.Equals(tc.Name, configurationName)); }while (configurationProperties.Count >= this.pageSize && configuration == null); return(configuration == null ? null : (configuration.Values ?? Enumerable.Empty <TestConfigValue>()).ToDictionary(x => x.Name, x => x.Value)); }