/// <summary> /// Convert a single Azure DevOps Pipeline task to a GitHub Actions task /// </summary> /// <param name="input">Yaml to convert</param> /// <returns>Converion object, with original yaml, processed yaml, and comments on the conversion</returns> public ConversionResponse ConvertAzurePipelineTaskToGitHubActionTask(string input) { string yaml = ""; string processedInput = ConversionUtility.StepsPreProcessing(input); GitHubActions.Step gitHubActionStep = new GitHubActions.Step(); //Process the YAML for the individual job AzurePipelines.Job azurePipelinesJob = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Job>(processedInput); if (azurePipelinesJob != null && azurePipelinesJob.steps != null && azurePipelinesJob.steps.Length > 0) { //As we needed to create an entire (but minimal) pipelines job, we need to now extract the step for processing StepsProcessing stepsProcessing = new StepsProcessing(); gitHubActionStep = stepsProcessing.ProcessStep(azurePipelinesJob.steps[0]); //Find all variables in this text block, we need this for a bit later VariablesProcessing vp = new VariablesProcessing(_verbose); List <string> variableList = vp.SearchForVariables(processedInput); //Create the GitHub YAML and apply some adjustments if (gitHubActionStep != null) { //add the step into a github job so it renders correctly GitHubActions.Job gitHubJob = new GitHubActions.Job { steps = new GitHubActions.Step[1] //create an array of size 1 }; //Load the step into the single item array gitHubJob.steps[0] = gitHubActionStep; //Finally, we can serialize the job back to yaml yaml = GitHubActionsSerialization.SerializeJob(gitHubJob, variableList); } } //Load failed tasks and comments for processing List <string> allComments = new List <string>(); if (gitHubActionStep != null) { allComments.Add(gitHubActionStep.step_message); } //Return the final conversion result, with the original (pipeline) yaml, processed (actions) yaml, and any comments return(new ConversionResponse { pipelinesYaml = input, actionsYaml = yaml, comments = allComments }); }
//process the steps private GitHubActions.Step[] ProcessSteps(AzurePipelines.Step[] steps, bool addCheckoutStep = true) { StepsProcessing stepsProcessing = new StepsProcessing(); GitHubActions.Step[] newSteps = null; if (steps != null) { //Start by scanning all of the steps, to see if we need to insert additional tasks int stepAdjustment = 0; bool addJavaSetupStep = false; bool addGradleSetupStep = false; bool addAzureLoginStep = false; bool addMSSetupStep = false; string javaVersion = null; //If the code needs a Checkout step, add it first if (addCheckoutStep == true) { stepAdjustment++; // we are inserting a step and need to start moving steps 1 place into the array } //Loop through the steps to see if we need other tasks inserted in for specific circumstances foreach (AzurePipelines.Step step in steps) { if (step.task != null) { switch (step.task) { //If we have an Java based step, we will need to add a Java setup step case "Ant@1": case "Maven@3": if (addJavaSetupStep == false) { addJavaSetupStep = true; stepAdjustment++; javaVersion = stepsProcessing.GetStepInput(step, "jdkVersionOption"); } break; //Needs a the Java step and an additional Gradle step case "Gradle@2": if (addJavaSetupStep == false) { addJavaSetupStep = true; stepAdjustment++; //Create the java step, as it doesn't exist javaVersion = "1.8"; } if (addGradleSetupStep == false) { addGradleSetupStep = true; stepAdjustment++; } break; //If we have an Azure step, we will need to add a Azure login step case "AzureAppServiceManage@0": case "AzureResourceGroupDeployment@2": case "AzureRmWebAppDeployment@3": if (addAzureLoginStep == false) { addAzureLoginStep = true; stepAdjustment++; } break; case "VSBuild@1": if (addMSSetupStep == false) { addMSSetupStep = true; stepAdjustment++; } break; } } } //Re-size the newSteps array with adjustments as needed newSteps = new GitHubActions.Step[steps.Length + stepAdjustment]; int adjustmentsUsed = 0; //Add the steps array if (addCheckoutStep == true) { //Add the check out step to get the code newSteps[adjustmentsUsed] = stepsProcessing.CreateCheckoutStep(); adjustmentsUsed++; } if (addJavaSetupStep == true) { //Add the JavaSetup step to the code if (javaVersion != null) { newSteps[adjustmentsUsed] = stepsProcessing.CreateSetupJavaStep(javaVersion); adjustmentsUsed++; } } if (addGradleSetupStep == true) { //Add the Gradle setup step to the code newSteps[adjustmentsUsed] = stepsProcessing.CreateSetupGradleStep(); adjustmentsUsed++; } if (addAzureLoginStep == true) { //Add the Azure login step to the code newSteps[adjustmentsUsed] = stepsProcessing.CreateAzureLoginStep(); adjustmentsUsed++; } if (addMSSetupStep == true) { //Add the Azure login step to the code newSteps[adjustmentsUsed] = stepsProcessing.CreateMSBuildSetupStep(); //adjustmentsUsed++; } //Translate the other steps for (int i = stepAdjustment; i < steps.Length + stepAdjustment; i++) { newSteps[i] = stepsProcessing.ProcessStep(steps[i - stepAdjustment]); } } return(newSteps); }