public class Constants
{
public class DevOps
{
public class Build
{
public const string QUEUE_STATUS = "enabled";
public const string YAML_FILE_PATH = "Data/azure-pipelines.yml";
public const string AGENT_POOL_NAME = "Hosted Ubuntu 1604";
public const string JOB_AUTHORIZATION_SCOPE = "projectCollection";
public const string REPOSITORY_TYPE = "TfsGit";
public const string CREATE_BUILD_DEF_URI = "https://dev.azure.com/{0}/{1}/_apis/build/definitions?api-version=5.0";
}
}
}
public class Repository
{
public string id { get; set; }
public string name { get; set; }
public string url { get; set; }
public DevOpsProject project { get; set; }
public int size { get; set; }
public string remoteUrl { get; set; }
public string sshUrl { get; set; }
public bool isFork { get; set; }
public _Links _links { get; set; }
public string defaultBranch { get; set; }
public string type { get; set; }
}
private void CreateIntegrationPipelineWithForkRepository(string orgnizationName, string sourceProjectName, string sourceProjectId, Repository forkRepository)
{
System.Action action = delegate
{
//********************************* Create Build with CICD ******************///
var continuousIntegration = new List<BuildTrigger>() { new BuildTrigger() };
var referenceYamlPath = new
{
yamlFilename = Constants.DevOps.Build.YAML_FILE_PATH,// = "Data/azure-pipelines.yml",
type = 2
};
var queueWithAgentPool = new
{
name = Constants.DevOps.Build.AGENT_POOL_NAME,
pool = new
{
name = Constants.DevOps.Build.AGENT_POOL_NAME,
isHosted = true
}
};
//Set repository type to: 'TfsGit'
forkRepository.type = Constants.DevOps.Build.REPOSITORY_TYPE;// = "TfsGit";
var requestBuild = new
{
triggers = continuousIntegration,
jobAuthorizationScope = Constants.DevOps.Build.JOB_AUTHORIZATION_SCOPE,
jobTimeoutInMinutes = 60,
jobCancelTimeoutInMinutes = 5,
process = referenceYamlPath,
repository = forkRepository, // new repository
quality = "definition",
queue = queueWithAgentPool,
name = string.Format("{0}-{1}", forkRepository.name, "ci"),
path = "\\",
type = "build",
queueStatus = Constants.DevOps.Build.QUEUE_STATUS,//= "enabled",
revision = 1,
project = new { id = sourceProjectId } // source project id
};
//********************************* Create Build with CICD ******************///
var uri = string.Format(Constants.DevOps.Build.CREATE_BUILD_DEF_URI, orgnizationName,sourceProjectName);
this.Post(uri, HttpMethod.Post, requestBuild);
};
_exceptionHandlingPolicy.Execute(action);
}
private string Post<T>(uri, HttpMethod.Post, T request)
{
if (request != null)
{
data = JsonConvert.SerializeObject(request);
}
var personalaccesstoken = "azure-devOps-PAT-token";
var authorization = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken)));
var result = _httpClient.Send(uri, method, data, authorization);
}