Skip to content

Commit

Permalink
Merge pull request #214 from samsmithnz/feature/newtasks21dec
Browse files Browse the repository at this point in the history
Added support for more tasks
  • Loading branch information
samsmithnz authored Dec 22, 2020
2 parents 0b79c35 + 704ad2b commit f629a11
Show file tree
Hide file tree
Showing 21 changed files with 867 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace AzurePipelinesToGitHubActionsConverter.Core.AzurePipelines
{
public class Canary
{
public int[] increments { get; set; }
public Deploy preDeploy { get; set; }
public Deploy deploy { get; set; }
public Deploy routeTraffic { get; set; }
public Deploy postRouteTraffic { get; set; }
public On on { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace AzurePipelinesToGitHubActionsConverter.Core.AzurePipelines
{
public class Deploy
{
public Pool pool { get; set; }
public Step[] steps { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace AzurePipelinesToGitHubActionsConverter.Core.AzurePipelines
{
public class On
{
public Deploy failure { get; set; }
public Deploy success { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace AzurePipelinesToGitHubActionsConverter.Core.AzurePipelines
{
public class Rolling
{
public int maxParallel { get; set; }
public Deploy preDeploy { get; set; }
public Deploy deploy { get; set; }
public Deploy routeTraffic { get; set; }
public Deploy postRouteTraffic { get; set; }
public On on { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace AzurePipelinesToGitHubActionsConverter.Core.AzurePipelines
{
public class RunOnce
{
{
public Deploy preDeploy { get; set; }
public Deploy deploy { get; set; }
public Deploy routeTraffic { get; set; }
public Deploy postRouteTraffic { get; set; }
public On on { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public string powershell
public string task { get; set; }
public string template { get; set; }
public string publish { get; set; }
public string download { get; set; }
public string artifact { get; set; }
public string patterns { get; set; }
public string displayName { get; set; }
public string name { get; set; }
public string condition { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ public class Strategy
public string maxParallel { get; set; }

public RunOnce runOnce { get; set; }
public Canary canary { get; set; }
public Rolling rolling { get; set; }
}
}
9 changes: 3 additions & 6 deletions src/AzurePipelinesToGitHubActionsConverter.Core/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,18 @@ private ConversionResponse ConvertAzurePipelineToGitHubActionV2(string yaml)
{
gitHubActions.messages.Add("TODO: Container conversion not yet done, we need help!: https://github.com/samsmithnz/AzurePipelinesToGitHubActionsConverter/issues/39");
}
//Strategy
string strategyYaml = json["strategy"]?.ToString();


//If we have stages, convert them into jobs first:
if (json["stages"] != null)
{
StagesProcessing sp = new StagesProcessing(_verbose);
gitHubActions.jobs = sp.ProcessStagesV2(json["stages"], strategyYaml);
gitHubActions.jobs = sp.ProcessStagesV2(json["stages"], json["strategy"]);
}
//If we don't have stages, but have jobs:
else if (json["stages"] == null && json["jobs"] != null)
{
JobProcessing jp = new JobProcessing(_verbose);
gitHubActions.jobs = jp.ProcessJobsV2(jp.ExtractAzurePipelinesJobsV2(json["jobs"], strategyYaml), gp.ExtractResourcesV2(resourcesYaml));
gitHubActions.jobs = jp.ProcessJobsV2(jp.ExtractAzurePipelinesJobsV2(json["jobs"], json["strategy"]), gp.ExtractResourcesV2(resourcesYaml));
_matrixVariableName = jp.MatrixVariableName;
}
//Otherwise, if we don't have stages or jobs, we just have steps, and need to load them into a new job
Expand All @@ -156,7 +153,7 @@ private ConversionResponse ConvertAzurePipelineToGitHubActionV2(string yaml)
//Steps
string stepsYaml = json["steps"]?.ToString();
JobProcessing jp = new JobProcessing(_verbose);
AzurePipelines.Job[] pipelineJobs = jp.ProcessJobFromPipelineRootV2(poolYaml, strategyYaml, stepsYaml);
AzurePipelines.Job[] pipelineJobs = jp.ProcessJobFromPipelineRootV2(poolYaml, json["strategy"], stepsYaml);
Resources resources = gp.ExtractResourcesV2(resourcesYaml);
gitHubActions.jobs = jp.ProcessJobsV2(pipelineJobs, resources);
_matrixVariableName = jp.MatrixVariableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class ConditionsProcessing

public static string TranslateConditions(string condition)
{
if (condition == null)
if (string.IsNullOrEmpty(condition) == true)
{
return null;
}
Expand Down Expand Up @@ -39,7 +39,7 @@ public static string TranslateConditions(string condition)
innerContentsProcessed += TranslateConditions(innerContent);
if (i != innerContents.Count - 1)
{
innerContentsProcessed += ",";
innerContentsProcessed += ", ";
}
}
contents = innerContentsProcessed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ public GeneralProcessing(bool verbose)

public string ProcessNameV2(string nameYaml)
{
if (nameYaml != null)
{
return nameYaml.Replace("name:", "").Replace(System.Environment.NewLine, "").Trim();
}
else
{
return null;
}
return nameYaml.Replace("name:", "").Replace(System.Environment.NewLine, "").Trim();
}

public string[] ProcessDependsOnV2(string dependsOnYaml)
Expand Down Expand Up @@ -79,8 +72,10 @@ public AzurePipelines.Environment ProcessEnvironmentV2(string environmentYaml)
JObject json = JsonSerialization.DeserializeStringToObject(environmentYaml);
if (json["name"] != null)
{
environment = new AzurePipelines.Environment();
environment.name = json["name"].ToString();
environment = new AzurePipelines.Environment
{
name = json["name"].ToString()
};
}
if (json["resourceName"] != null)
{
Expand Down Expand Up @@ -131,28 +126,6 @@ public Resources ExtractResourcesV2(string resourcesYaml)
return null;
}

public AzurePipelines.Strategy ProcessStrategyV2(string strategyYaml)
{
if (strategyYaml != null)
{
//try
//{
//Most often, the pool will be in this structure
AzurePipelines.Strategy strategy = YamlSerialization.DeserializeYaml<AzurePipelines.Strategy>(strategyYaml);
return strategy;
//}
//catch (Exception ex)
//{
// ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Strategy>(strategyYaml) swallowed an exception: " + ex.Message, _verbose);
//}
}
else
{
return null;
}
}


//process the build pool/agent
public string ProcessPool(Pool pool)
{
Expand Down Expand Up @@ -291,11 +264,18 @@ public GitHubActions.Strategy ProcessStrategy(AzurePipelines.Strategy strategy)
}
processedStrategy.max_parallel = strategy.maxParallel;
}
if (strategy.runOnce != null)
{
//TODO: There is currently no conversion path for other strategies
ConversionUtility.WriteLine("TODO: " + strategy.runOnce, _verbose);
}
//TODO: There is currently no conversion path for other strategies //if (strategy.runOnce != null)
//{
// ConversionUtility.WriteLine("TODO: " + strategy.runOnce, _verbose);
//}
//if (strategy.canary != null)
//{
// ConversionUtility.WriteLine("TODO: " + strategy.canary, _verbose);
//}
//if (strategy.rolling != null)
//{
// ConversionUtility.WriteLine("TODO: " + strategy.rolling, _verbose);
//}
return processedStrategy;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AzurePipelinesToGitHubActionsConverter.Core.PipelinesToActionsConversion
Expand Down Expand Up @@ -53,10 +52,43 @@ public GitHubActions.Job ProcessJob(AzurePipelines.Job job, AzurePipelines.Resou
//Initialize the array with no items
job.steps = new AzurePipelines.Step[0];
//Process the steps, adding the default checkout step
newJob.steps = sp.AddSupportingSteps(job.strategy?.runOnce?.deploy?.steps, false);
//TODO: There is currently no conversion path for templates
newJob.job_message += "Note: Azure DevOps strategy>runOnce>deploy does not have an equivalent in GitHub Actions yet";
newJob.steps = sp.AddSupportingSteps(job.strategy.runOnce.deploy.steps, false);
//TODO: There is currently no conversion path for runOnce strategy
newJob.job_message += "Note: Azure DevOps strategy>runOnce does not have an equivalent in GitHub Actions yet, and only the deploy steps are transferred to steps";
}
else if (newJob.steps == null && job.strategy?.canary?.deploy?.steps != null)
{
//Initialize the array with no items
job.steps = new AzurePipelines.Step[0];
//Process the steps, adding the default checkout step
newJob.steps = sp.AddSupportingSteps(job.strategy.canary.deploy.steps, false);
//TODO: There is currently no conversion path for runOnce strategy
newJob.job_message += "Note: Azure DevOps strategy>canary does not have an equivalent in GitHub Actions yet, and only the deploy steps are transferred to steps";
}
else if (newJob.steps == null && job.strategy?.rolling?.deploy?.steps != null)
{
//Initialize the array with no items
job.steps = new AzurePipelines.Step[0];
//Process the steps, adding the default checkout step
newJob.steps = sp.AddSupportingSteps(job.strategy.rolling.deploy.steps, false);
//TODO: There is currently no conversion path for runOnce strategy
newJob.job_message += "Note: Azure DevOps strategy>rolling does not have an equivalent in GitHub Actions yet, and only the deploy steps are transferred to steps";
}


//TODO: Add this pools in for other strategies
//if (newJob.runs_on == null && job.strategy?.runOnce?.deploy?.pool != null)
//{
// newJob.runs_on = generalProcessing.ProcessPool(job.strategy?.runOnce?.deploy?.pool);
//}
//else if (newJob.runs_on == null && job.strategy?.canary?.deploy?.pool != null)
//{
// newJob.runs_on = generalProcessing.ProcessPool(job.strategy?.canary?.deploy?.pool);
//}
//else if (newJob.runs_on == null && job.strategy?.rolling?.deploy?.pool != null)
//{
// newJob.runs_on = generalProcessing.ProcessPool(job.strategy?.rolling?.deploy?.pool);
//}
if (job.continueOnError == true)
{
newJob.continue_on_error = job.continueOnError;
Expand Down Expand Up @@ -96,9 +128,10 @@ public GitHubActions.Job ProcessJob(AzurePipelines.Job job, AzurePipelines.Resou
}
}

public AzurePipelines.Job[] ExtractAzurePipelinesJobsV2(JToken jobsJson, string strategyYaml)
public AzurePipelines.Job[] ExtractAzurePipelinesJobsV2(JToken jobsJson, JToken strategyJson)
{
GeneralProcessing gp = new GeneralProcessing(_verbose);
StrategyProcessing sp = new StrategyProcessing(_verbose);
AzurePipelines.Job[] jobs = new AzurePipelines.Job[jobsJson.Count()];
if (jobsJson != null)
{
Expand All @@ -116,21 +149,23 @@ public AzurePipelines.Job[] ExtractAzurePipelinesJobsV2(JToken jobsJson, string
{
job.pool = gp.ProcessPoolV2(jobJson["pool"].ToString());
}
//Strategy
if (jobJson["strategy"] != null)
{
job.strategy = gp.ProcessStrategyV2(jobJson["strategy"].ToString());
strategyJson = jobJson["strategy"];
}
else if (strategyYaml != null)
if (strategyJson != null)
{
job.strategy = gp.ProcessStrategyV2(strategyYaml);
job.strategy = sp.ProcessStrategyV2(strategyJson);
}

if (jobJson["dependsOn"] != null)
{
job.dependsOn = gp.ProcessDependsOnV2(jobJson["dependsOn"].ToString());
}
if (jobJson["condition"] != null)
{
job.condition = ConditionsProcessing.TranslateConditions(jobJson["condition"].ToString());
job.condition = jobJson["condition"].ToString();
}
if (jobJson["environment"] != null)
{
Expand Down Expand Up @@ -193,24 +228,21 @@ public AzurePipelines.Job[] ExtractAzurePipelinesJobsV2(JToken jobsJson, string
return jobs;
}

public AzurePipelines.Job[] ProcessJobFromPipelineRootV2(string poolYaml, string strategyYaml, string stepsYaml)
public AzurePipelines.Job[] ProcessJobFromPipelineRootV2(string poolYaml, JToken strategyJson, string stepsYaml)
{
//Pool
Pool pool = null;
if (poolYaml != null)
{
GeneralProcessing gp = new GeneralProcessing(_verbose);
pool = gp.ProcessPoolV2(poolYaml);
}
AzurePipelines.Strategy strategy = null;
try
{
//Most often, the pool will be in this structure
strategy = YamlSerialization.DeserializeYaml<AzurePipelines.Strategy>(strategyYaml);
}
catch (Exception ex)
{
ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Strategy>(strategyYaml) swallowed an exception: " + ex.Message, _verbose);
}

//Strategy
StrategyProcessing sp = new StrategyProcessing(_verbose);
AzurePipelines.Strategy strategy = sp.ProcessStrategyV2(strategyJson);

//Steps
AzurePipelines.Step[] steps = null;
if (stepsYaml != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public StagesProcessing(bool verbose)
_verbose = verbose;
}

public Dictionary<string, GitHubActions.Job> ProcessStagesV2(JToken stagesJson, string strategyYaml)
public Dictionary<string, GitHubActions.Job> ProcessStagesV2(JToken stagesJson, JToken strategyJson)
{
AzurePipelines.Job[] jobs = null;
List<AzurePipelines.Stage> stages = new List<AzurePipelines.Stage>();
Expand Down Expand Up @@ -40,7 +40,7 @@ public StagesProcessing(bool verbose)
if (stageJson["jobs"] != null)
{
JobProcessing jp = new JobProcessing(_verbose);
stage.jobs = jp.ExtractAzurePipelinesJobsV2(stageJson["jobs"], strategyYaml);
stage.jobs = jp.ExtractAzurePipelinesJobsV2(stageJson["jobs"], strategyJson);
}
if (stageJson["pool"] != null && stage.jobs != null)
{
Expand Down
Loading

0 comments on commit f629a11

Please sign in to comment.