зеркало из
1
0
Форкнуть 0

Added GateSample and Release AttachmentSamples.

This commit is contained in:
Vikranth Thati 2018-12-11 15:16:05 +05:30
Родитель a9cfb16617
Коммит 58ff59f523
5 изменённых файлов: 496 добавлений и 9 удалений

Просмотреть файл

@ -175,6 +175,8 @@
<Compile Include="Graph\GroupsSample.cs" />
<Compile Include="Graph\UsersSample.cs" />
<Compile Include="Notification\*.cs" />
<Compile Include="Release\ReleaseAttachmentSample.cs" />
<Compile Include="Release\GatesSample.cs" />
<Compile Include="Release\ManualInterventionSample.cs" />
<Compile Include="Release\ReleasesSample.cs" />
<Compile Include="Security\AccessControlListsSample.cs" />

Просмотреть файл

@ -0,0 +1,179 @@
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts.Conditions;
using Newtonsoft.Json;
using WebApiRelease = Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Release;
namespace Microsoft.TeamServices.Samples.Client.Release
{
[ClientSample(ReleaseManagementApiConstants.ReleaseAreaName, ReleaseManagementApiConstants.ReleaseGatesResource)]
public class GatesSample : ClientSample
{
private const string releaseDefinitionName = "Fabrikam-web-with-Gates";
private int _newlyCreatedReleaseDefinitionId = 0;
[ClientSampleMethod]
public ReleaseGates IgnoreGate()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
string gateName = "Query Work Items";
ReleaseDefinitionGatesStep releaseDefinitionGatesStep = new ReleaseDefinitionGatesStep
{
Gates = new List<ReleaseDefinitionGate>
{
new ReleaseDefinitionGate
{
Tasks = new List<WorkflowTask>
{
new WorkflowTask
{
Enabled = true,
Name = gateName,
TaskId = new Guid("f1e4b0e6-017e-4819-8a48-ef19ae96e289"),
Version = "0.*",
Inputs = new Dictionary<string, string>
{
{ "queryId", "c871ca91-e30c-43a0-9306-97a2be93861e" },
{ "maxThreshold", "6" },
{ "minThreshold", "2" }
}
}
}
}
},
GatesOptions = new ReleaseDefinitionGatesOptions
{
IsEnabled = true,
MinimumSuccessDuration = 2,
SamplingInterval = 5,
StabilizationTime = 10,
Timeout = 60
},
Id = 0
};
ReleaseDefinition definition = new ReleaseDefinition()
{
Name = releaseDefinitionName,
Revision = 1,
Environments = new List<ReleaseDefinitionEnvironment>()
{
new ReleaseDefinitionEnvironment()
{
Name = "PROD",
Conditions = new List<Condition>()
{
new Condition()
{
ConditionType = ConditionType.Event,
Name = "ReleaseStarted"
}
},
DeployPhases = new List<DeployPhase>()
{
new RunOnServerDeployPhase()
{
Name = "Agentless phase",
Rank = 1,
WorkflowTasks = new List<WorkflowTask>()
{
new WorkflowTask()
{
Name = "Manual Intervention",
TaskId = new Guid("bcb64569-d51a-4af0-9c01-ea5d05b3b622"),
Version = "8.*",
Enabled = true
}
}
}
},
PreDeployApprovals = new ReleaseDefinitionApprovals()
{
Approvals = new List<ReleaseDefinitionApprovalStep>()
{
new ReleaseDefinitionApprovalStep()
{
IsAutomated = true,
Rank = 1,
}
}
},
PostDeployApprovals = new ReleaseDefinitionApprovals()
{
Approvals = new List<ReleaseDefinitionApprovalStep>()
{
new ReleaseDefinitionApprovalStep()
{
IsAutomated = true,
Rank = 1
}
}
},
RetentionPolicy = new EnvironmentRetentionPolicy()
{
DaysToKeep = 30,
ReleasesToKeep = 3,
RetainBuild = true
},
PreDeploymentGates = releaseDefinitionGatesStep
}
}
};
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// create a release definition
ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;
this._newlyCreatedReleaseDefinitionId = releaseDefinition.Id;
// create a release
WebApiRelease release = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);
Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Name);
// Wait till deployment moves to evaluation gates state
ClientSampleHelpers.Retry(
TimeSpan.FromMinutes(2),
TimeSpan.FromSeconds(5),
() =>
{
release = releaseClient.GetReleaseAsync(project: projectName, releaseId: release.Id).Result;
return release != null && release.Environments.First().DeploySteps.FirstOrDefault().OperationStatus == DeploymentOperationStatus.EvaluatingGates;
});
// Ignore the gate
GateUpdateMetadata gateUpdateMetadata = new GateUpdateMetadata
{
Comment = "Ignore gate",
GatesToIgnore = new List<string> { gateName }
};
int gateStepId = release.Environments.FirstOrDefault().DeploySteps.FirstOrDefault().PreDeploymentGates.Id;
ReleaseGates releaseGates = releaseClient.UpdateGatesAsync(gateUpdateMetadata: gateUpdateMetadata, project: projectName, gateStepId: gateStepId).Result;
Context.Log("{0} {1}", releaseGates.Id.ToString().PadLeft(6), releaseGates.Id);
return releaseGates;
}
[ClientSampleMethod]
public void DeleteReleaseDefinitionWithGates()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// delete release definition
releaseClient.DeleteReleaseDefinitionAsync(project: projectName, definitionId: this._newlyCreatedReleaseDefinitionId, forceDelete: true).SyncResult();
}
}
}

Просмотреть файл

@ -12,7 +12,7 @@ using WebApiRelease = Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.R
namespace Microsoft.TeamServices.Samples.Client.Release
{
//[ClientSample(ReleaseManagementApiConstants.ReleaseAreaName, ReleaseManagementApiConstants.ReleasesResource)]
[ClientSample(ReleaseManagementApiConstants.ReleaseAreaName, ReleaseManagementApiConstants.ReleasesResource)]
public class ManualInterventionSample : ClientSample
{
private const string releaseDefinitionName = "Fabrikam-web-with-MI";

Просмотреть файл

@ -0,0 +1,184 @@
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts.Conditions;
using Newtonsoft.Json;
using WebApiRelease = Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Release;
namespace Microsoft.TeamServices.Samples.Client.Release
{
[ClientSample(ReleaseManagementApiConstants.ReleaseAreaName, ReleaseManagementApiConstants.ReleaseAttachmentsResource)]
public class ReleaseAttachmentSample : ClientSample
{
private const string releaseDefinitionName = "Fabrikam-web-with-ReleaseAttachment";
private int _newlyCreatedReleaseDefinitionId = 0;
private int _newlyCreatedRelease = 0;
private const string taskName = "PowerShell Script";
[ClientSampleMethod]
public List<ReleaseTaskAttachment> GetReleaseAttachment()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
ReleaseDefinition definition = new ReleaseDefinition()
{
Name = releaseDefinitionName,
Revision = 1,
Environments = new List<ReleaseDefinitionEnvironment>()
{
new ReleaseDefinitionEnvironment()
{
Name = "PROD",
Conditions = new List<Condition>()
{
new Condition()
{
ConditionType = ConditionType.Event,
Name = "ReleaseStarted"
}
},
DeployPhases = new List<DeployPhase>()
{
new AgentBasedDeployPhase()
{
Name = "Run on agent",
Rank = 1,
DeploymentInput = new AgentDeploymentInput()
{
QueueId = 1
},
WorkflowTasks = new List<WorkflowTask>
{
new WorkflowTask
{
Name = taskName,
Enabled = true,
TimeoutInMinutes = 0,
Inputs = new Dictionary<string, string> {
{ "targetType", "inline" },
{ "script", "New-Item -Path 'newfile.txt' -ItemType File\n\nWrite-Host \"##vso[task.addattachment type=myattachmenttype;name=myattachmentname;]$(SYSTEM.DEFAULTWORKINGDIRECTORY)\\newfile.txt\"" }
},
TaskId = new Guid("e213ff0f-5d5c-4791-802d-52ea3e7be1f1"),
Version = "2.*",
DefinitionType = "task",
Condition = "succeeded()",
}
}
}
},
PreDeployApprovals = new ReleaseDefinitionApprovals()
{
Approvals = new List<ReleaseDefinitionApprovalStep>()
{
new ReleaseDefinitionApprovalStep()
{
IsAutomated = true,
Rank = 1
}
}
},
PostDeployApprovals = new ReleaseDefinitionApprovals()
{
Approvals = new List<ReleaseDefinitionApprovalStep>()
{
new ReleaseDefinitionApprovalStep()
{
IsAutomated = true,
Rank = 1
}
}
},
RetentionPolicy = new EnvironmentRetentionPolicy()
{
DaysToKeep = 30,
ReleasesToKeep = 3,
RetainBuild = true
}
}
}
};
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// create a release definition
ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;
this._newlyCreatedReleaseDefinitionId = releaseDefinition.Id;
Context.Log("{0} {1} {2}", releaseDefinition.Id.ToString().PadLeft(6), releaseDefinition.Name, projectName);
// create a release
WebApiRelease release = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);
Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Name);
_newlyCreatedRelease = release.Id;
// Wait till deployment completed
ClientSampleHelpers.Retry(
TimeSpan.FromMinutes(2),
TimeSpan.FromSeconds(5),
() =>
{
release = releaseClient.GetReleaseAsync(project: projectName, releaseId: release.Id).Result;
return release != null && release.Environments.First().Status == EnvironmentStatus.Succeeded;
});
// Get release task attachments
ReleaseEnvironment environment = release.Environments.FirstOrDefault();
DeploymentAttempt deployStep = environment.DeploySteps.First();
Guid planId = deployStep.ReleaseDeployPhases.First().RunPlanId.Value;
List<ReleaseTaskAttachment> releaseTaskAttachment = releaseClient.GetReleaseTaskAttachmentsAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, type: "myattachmenttype").Result;
Context.Log("{0} {1}", releaseTaskAttachment.First().Name.PadLeft(6), releaseTaskAttachment.First().Type);
return releaseTaskAttachment;
}
[ClientSampleMethod]
public System.IO.Stream GetReleaseTaskAttachmentContent()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: this._newlyCreatedRelease).Result;
// Get release task attachments
ReleaseEnvironment environment = release.Environments.FirstOrDefault();
DeploymentAttempt deployStep = environment.DeploySteps.First();
Guid planId = deployStep.ReleaseDeployPhases.First().RunPlanId.Value;
List<ReleaseTaskAttachment> releaseTaskAttachment = releaseClient.GetReleaseTaskAttachmentsAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, type: "myattachmenttype").Result;
ReleaseTaskAttachment firstReleaseTaskAttachment = releaseTaskAttachment.First();
Guid timelineId = firstReleaseTaskAttachment.TimelineId;
Guid recordId = firstReleaseTaskAttachment.RecordId;
string attachmentType = firstReleaseTaskAttachment.Type;
string attachmentName = firstReleaseTaskAttachment.Name;
System.IO.Stream attachmentData = releaseClient.GetReleaseTaskAttachmentContentAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, timelineId: timelineId, recordId: recordId, type: attachmentType, name: attachmentName).Result;
Context.Log("{0} {1}", attachmentName.PadLeft(6), attachmentType);
return attachmentData;
}
[ClientSampleMethod]
public void DeleteReleaseDefinitionWithReleaseAttachments()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// delete release definition
releaseClient.DeleteReleaseDefinitionAsync(project: projectName, definitionId: this._newlyCreatedReleaseDefinitionId, forceDelete: true).SyncResult();
}
}
}

Просмотреть файл

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Net.Http;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts;
@ -20,6 +21,7 @@ namespace Microsoft.TeamServices.Samples.Client.Release
{
private const string releaseDefinitionName = "Fabrikam-web";
private int newlyCreatedReleaseDefinitionId = 0;
private int completedReleaseId = 0;
[ClientSampleMethod]
public ReleaseDefinition CreateReleaseDefinition()
@ -44,7 +46,7 @@ namespace Microsoft.TeamServices.Samples.Client.Release
Rank = 1,
DeploymentInput = new AgentDeploymentInput()
{
// QueueId = 1
QueueId = 1
}
}
},
@ -254,6 +256,35 @@ namespace Microsoft.TeamServices.Samples.Client.Release
return release;
}
[ClientSampleMethod]
public WebApiRelease UpdateRelease()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
List<WebApiRelease> releases = releaseClient.GetReleasesAsync(project: projectName).Result;
int releaseId = releases.FirstOrDefault().Id;
WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: releaseId).Result;
IDictionary<string, ConfigurationVariableValue> varaibles = release.Variables;
varaibles.Add(
new KeyValuePair<string, ConfigurationVariableValue>("System.Debug", new ConfigurationVariableValue
{ AllowOverride = true, IsSecret = false, Value = "false" }
)
);
// Update the release
WebApiRelease updatedRelease = releaseClient.UpdateReleaseAsync(release: release, project: projectName, releaseId: releaseId).Result;
Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), updatedRelease.Name);
return release;
}
[ClientSampleMethod]
public IEnumerable<WebApiRelease> ListAllReleases()
{
@ -294,7 +325,7 @@ namespace Microsoft.TeamServices.Samples.Client.Release
}
[ClientSampleMethod]
public WebApiRelease StartDeployment()
public ReleaseEnvironment StartDeployment()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
@ -315,7 +346,7 @@ namespace Microsoft.TeamServices.Samples.Client.Release
ReleaseEnvironment releaseEnvironment = releaseClient.UpdateReleaseEnvironmentAsync(releaseEnvironmentUpdateMetadata, projectName, release.Id, releaseEnvironmentId).Result;
Context.Log("{0} {1}", releaseEnvironment.Id.ToString().PadLeft(6), releaseEnvironment.Name);
return release;
return releaseEnvironment;
}
[ClientSampleMethod]
@ -560,7 +591,98 @@ namespace Microsoft.TeamServices.Samples.Client.Release
return deployments;
}
[ClientSampleMethod]
public System.IO.Stream GetLogsOfReleaseTask()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// Get Release Defintion
ReleaseDefinition releaseDefinition = releaseClient.GetReleaseDefinitionAsync(project: projectName, definitionId: newlyCreatedReleaseDefinitionId).Result;
// Get all completed environment releases.
// environmentStatusFilter values Undefined = 0, NotStarted = 1, InProgress = 2, Succeeded = 4, Canceled = 8, Rejected = 16, Qeued = 32, Scheduled = 64, PartiallySucceeded = 128
List<WebApiRelease> releases = new List<WebApiRelease>();
ClientSampleHelpers.Retry(
TimeSpan.FromMinutes(2),
TimeSpan.FromSeconds(5),
() =>
{
releases = releaseClient.GetReleasesAsync(project: projectName, definitionId: releaseDefinition.Id, definitionEnvironmentId: releaseDefinition.Environments.First().Id, expand: ReleaseExpands.Environments, environmentStatusFilter: 4).Result;
return releases.Count > 0;
});
// Get first release first environment first deployment first task log
WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: releases.First().Id).Result;
this.completedReleaseId = release.Id;
ReleaseEnvironment releaseEnvironment = release.Environments.First();
ReleaseDeployPhase deployPhase = releaseEnvironment.DeploySteps.First().ReleaseDeployPhases.FirstOrDefault();
ReleaseTask task = deployPhase.DeploymentJobs.First().Tasks.First();
System.IO.Stream taskLog = releaseClient.GetTaskLogAsync(project: projectName, releaseId: release.Id, environmentId: releaseEnvironment.Id, releaseDeployPhaseId: deployPhase.Id, taskId: task.Id).Result;
Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Id);
return taskLog;
}
[ClientSampleMethod]
public System.IO.Stream GetLogsOfRelease()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
System.IO.Stream taskLogs = releaseClient.GetLogsAsync(project: projectName, releaseId: completedReleaseId).Result;
Context.Log("{0} {1}", completedReleaseId.ToString().PadLeft(6), completedReleaseId);
return taskLogs;
}
[ClientSampleMethod]
public string GetReleaseRevisionOfRelease()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// Get release
WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: completedReleaseId).Result;
// Get release revision
System.IO.Stream releaseRevision = releaseClient.GetReleaseRevisionAsync(project: projectName, releaseId: release.Id, definitionSnapshotRevision: release.DefinitionSnapshotRevision).Result;
Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Id);
StreamReader reader = new StreamReader(releaseRevision);
string releaseData = reader.ReadToEnd();
return releaseData;
}
[ClientSampleMethod]
public ReleaseDefinitionSummary GetReleaseDefinitionSummary()
{
string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
// Get a release client instance
VssConnection connection = Context.Connection;
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// Get release definition summary
ReleaseDefinitionSummary releaseDefinitionSummary = releaseClient.GetReleaseDefinitionSummaryAsync(project: projectName, definitionId: newlyCreatedReleaseDefinitionId, releaseCount: 2).Result;
Context.Log("{0} {1}", newlyCreatedReleaseDefinitionId.ToString().PadLeft(6), newlyCreatedReleaseDefinitionId);
return releaseDefinitionSummary;
}
[ClientSampleMethod]
public void GetReleaseDefinitionAccessControlLists()
{
@ -587,18 +709,18 @@ namespace Microsoft.TeamServices.Samples.Client.Release
ReleaseHttpClient releaseClient = connection.GetClient<ReleaseHttpClient>();
// delete release definition
releaseClient.DeleteReleaseDefinitionAsync(project: projectName, definitionId: newlyCreatedReleaseDefinitionId).SyncResult();
releaseClient.DeleteReleaseDefinitionAsync(project: projectName, definitionId: newlyCreatedReleaseDefinitionId, forceDelete: true).SyncResult();
}
public static WebApiRelease CreateRelease(ReleaseHttpClient releaseClient, int releaseDefinitionId, string projectName)
{
//BuildVersion instanceReference = new BuildVersion { Id = "2" };
//ArtifactMetadata artifact = new ArtifactMetadata { Alias = "Fabrikam.CI", InstanceReference = instanceReference };
BuildVersion instanceReference = new BuildVersion { Id = "2" };
ArtifactMetadata artifact = new ArtifactMetadata { Alias = "Fabrikam.CI", InstanceReference = instanceReference };
ReleaseStartMetadata releaseStartMetaData = new ReleaseStartMetadata();
releaseStartMetaData.DefinitionId = releaseDefinitionId;
releaseStartMetaData.Description = "Creating Sample release";
//releaseStartMetaData.Artifacts = new[] { artifact };
releaseStartMetaData.Artifacts = new[] { artifact };
// Create a release
WebApiRelease release =
releaseClient.CreateReleaseAsync(project: projectName, releaseStartMetadata: releaseStartMetaData).Result;