public GitHubActions.Trigger ProcessSchedulesV2(string schedulesYaml)
        {
            Schedule[] schedules = null;
            if (schedulesYaml != null)
            {
                try
                {
                    schedules = GenericObjectSerialization.DeserializeYaml <Schedule[]>(schedulesYaml);
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<Schedule[]>(schedulesYaml) swallowed an exception: " + ex.Message, _verbose);
                }
            }

            //Convert the pieces to GitHub
            string[] schedule = ProcessSchedules(schedules);

            //Build the return results
            if (schedule != null)
            {
                return(new GitHubActions.Trigger
                {
                    schedule = schedule
                });
            }
            else
            {
                return(null);
            }
        }
        public AzurePipelines.Job[] ProcessJobFromPipelineRootV2(string poolYaml, string strategyYaml, string stepsYaml)
        {
            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 = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Strategy>(strategyYaml);
            }
            catch (Exception ex)
            {
                ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Strategy>(strategyYaml) swallowed an exception: " + ex.Message, _verbose);
            }
            AzurePipelines.Step[] steps = null;
            if (stepsYaml != null)
            {
                try
                {
                    steps = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Step[]>(stepsYaml);
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Step[]>(stepsYaml) swallowed an exception: " + ex.Message, _verbose);
                }
            }

            AzurePipelines.Job job = new AzurePipelines.Job
            {
                pool     = pool,
                strategy = strategy,
                steps    = steps
            };
            //Don't add the build name unless there is content
            if (job.pool != null || job.strategy != null || steps != null)
            {
                AzurePipelines.Job[] jobs = new AzurePipelines.Job[1];
                job.job = "build";
                jobs[0] = job;
                return(jobs);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 3
0
        /// <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
            });
        }
Esempio n. 4
0
        public void GitHubActionYamlToGenericObjectTest()
        {
            //Arrange
            string yaml = @"
on:
  schedule:
  - cron: ""0 0 * * *""
";

            //Act
            object yamlObject = GenericObjectSerialization.DeserializeYaml <object>(yaml);

            //Assert
            Assert.AreNotEqual(null, yamlObject);
        }
 public Resources ExtractResourcesV2(string resourcesYaml)
 {
     if (resourcesYaml != null)
     {
         try
         {
             Resources resources = GenericObjectSerialization.DeserializeYaml <Resources>(resourcesYaml);
             return(resources);
         }
         catch (Exception ex)
         {
             ConversionUtility.WriteLine($"DeserializeYaml<Resources>(resourcesYaml) swallowed an exception: " + ex.Message, _verbose);
         }
     }
     return(null);
 }
 public AzurePipelines.Strategy ProcessStrategyV2(string strategyYaml)
 {
     if (strategyYaml != null)
     {
         try
         {
             //Most often, the pool will be in this structure
             AzurePipelines.Strategy strategy = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Strategy>(strategyYaml);
             return(strategy);
         }
         catch (Exception ex)
         {
             ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Strategy>(strategyYaml) swallowed an exception: " + ex.Message, _verbose);
         }
     }
     return(null);
 }
        public string[] ProcessDependsOnV2(string dependsOnYaml)
        {
            string[] dependsOn = null;
            if (dependsOnYaml != null)
            {
                try
                {
                    string simpleDependsOn = GenericObjectSerialization.DeserializeYaml <string>(dependsOnYaml);
                    dependsOn    = new string[1];
                    dependsOn[0] = simpleDependsOn;
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<string>(dependsOnYaml) swallowed an exception: " + ex.Message, _verbose);
                    dependsOn = GenericObjectSerialization.DeserializeYaml <string[]>(dependsOnYaml);
                }
            }

            //Build the return results
            return(dependsOn);
        }
        public GitHubActions.Trigger ProcessTriggerV2(string triggerYaml)
        {
            AzurePipelines.Trigger trigger = null;
            if (triggerYaml != null)
            {
                try
                {
                    string[] simpleTrigger = GenericObjectSerialization.DeserializeYaml <string[]>(triggerYaml);
                    trigger = new AzurePipelines.Trigger
                    {
                        branches = new IncludeExclude
                        {
                            include = simpleTrigger
                        }
                    };
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<string[]>(triggerYaml) swallowed an exception: " + ex.Message, _verbose);
                    trigger = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Trigger>(triggerYaml);
                }
            }

            //Convert the pieces to GitHub
            GitHubActions.Trigger push = ProcessComplexTrigger(trigger);

            //Build the return results
            if (push != null)
            {
                return(new GitHubActions.Trigger
                {
                    push = push?.push
                });
            }
            else
            {
                return(null);
            }
        }
        public AzurePipelines.Job[] ExtractAzurePipelinesJobsV2(JToken jobsJson, string strategyYaml)
        {
            GeneralProcessing gp = new GeneralProcessing(_verbose);

            AzurePipelines.Job[] jobs = new AzurePipelines.Job[jobsJson.Count()];
            if (jobsJson != null)
            {
                int i = 0;
                foreach (JToken jobJson in jobsJson)
                {
                    AzurePipelines.Job job = new AzurePipelines.Job
                    {
                        job         = jobJson["job"]?.ToString(),
                        deployment  = jobJson["deployment"]?.ToString(),
                        displayName = jobJson["displayName"]?.ToString(),
                        template    = jobJson["template"]?.ToString()
                    };
                    if (jobJson["pool"] != null)
                    {
                        job.pool = gp.ProcessPoolV2(jobJson["pool"].ToString());
                    }
                    if (jobJson["strategy"] != null)
                    {
                        job.strategy = gp.ProcessStrategyV2(jobJson["strategy"].ToString());
                    }
                    else if (strategyYaml != null)
                    {
                        job.strategy = gp.ProcessStrategyV2(strategyYaml);
                    }
                    if (jobJson["dependsOn"] != null)
                    {
                        job.dependsOn = gp.ProcessDependsOnV2(jobJson["dependsOn"].ToString());
                    }
                    if (jobJson["condition"] != null)
                    {
                        job.condition = ConditionsProcessing.TranslateConditions(jobJson["condition"].ToString());
                    }
                    if (jobJson["environment"] != null)
                    {
                        job.environment = gp.ProcessEnvironmentV2(jobJson["environment"].ToString());
                    }
                    if (jobJson["timeoutInMinutes"] != null)
                    {
                        int.TryParse(jobJson["timeoutInMinutes"].ToString(), out int timeOut);
                        if (timeOut > 0)
                        {
                            job.timeoutInMinutes = timeOut;
                        }
                    }
                    if (jobJson["continueOnError"] != null)
                    {
                        bool.TryParse(jobJson["continueOnError"].ToString(), out bool continueOnError);
                        job.continueOnError = continueOnError;
                    }
                    if (jobJson["variables"] != null)
                    {
                        VariablesProcessing vp = new VariablesProcessing(_verbose);
                        job.variables = vp.ProcessParametersAndVariablesV2(null, jobJson["variables"].ToString());
                    }
                    if (jobJson["steps"] != null)
                    {
                        try
                        {
                            job.steps = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Step[]>(jobJson["steps"].ToString());
                        }
                        catch (Exception ex)
                        {
                            ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Step[]>(jobJson[\"steps\"].ToString() swallowed an exception: " + ex.Message, _verbose);
                        }
                    }
                    jobs[i] = job;
                    i++;
                }
            }

            return(jobs);
        }
        public Dictionary <string, string> ProcessParametersAndVariablesV2(string parametersYaml, string variablesYaml)
        {
            List <Parameter> parameters = null;

            if (parametersYaml != null)
            {
                try
                {
                    Dictionary <string, string> simpleParameters = GenericObjectSerialization.DeserializeYaml <Dictionary <string, string> >(parametersYaml);
                    parameters = new List <Parameter>();
                    foreach (KeyValuePair <string, string> item in simpleParameters)
                    {
                        parameters.Add(new Parameter
                        {
                            name     = item.Key,
                            @default = item.Value
                        });
                    }
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<Dictionary<string, string>>(parametersYaml) swallowed an exception: " + ex.Message, _verbose);
                    parameters = GenericObjectSerialization.DeserializeYaml <List <Parameter> >(parametersYaml);
                }
            }

            List <Variable> variables = null;

            if (variablesYaml != null)
            {
                try
                {
                    Dictionary <string, string> simpleVariables = GenericObjectSerialization.DeserializeYaml <Dictionary <string, string> >(variablesYaml);
                    variables = new List <Variable>();
                    foreach (KeyValuePair <string, string> item in simpleVariables)
                    {
                        variables.Add(new Variable
                        {
                            name  = item.Key,
                            value = item.Value
                        });
                    }
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<Dictionary<string, string>>(variablesYaml) swallowed an exception: " + ex.Message, _verbose);
                    variables = GenericObjectSerialization.DeserializeYaml <List <Variable> >(variablesYaml);
                }
            }

            Dictionary <string, string> env = new Dictionary <string, string>();
            Dictionary <string, string> processedParameters = ProcessComplexParametersV2(parameters);
            Dictionary <string, string> processedVariables  = ProcessComplexVariablesV2(variables);

            foreach (KeyValuePair <string, string> item in processedParameters)
            {
                if (env.ContainsKey(item.Key) == false)
                {
                    env.Add(item.Key, item.Value);
                }
            }
            foreach (KeyValuePair <string, string> item in processedVariables)
            {
                if (env.ContainsKey(item.Key) == false)
                {
                    env.Add(item.Key, item.Value);
                }
            }

            if (env.Count > 0)
            {
                return(env);
            }
            else
            {
                return(null);
            }
        }
        public AzurePipelines.Environment ProcessEnvironmentV2(string environmentYaml)
        {
            AzurePipelines.Environment environment = null;
            if (environmentYaml != null)
            {
                try
                {
                    environment = GenericObjectSerialization.DeserializeYaml <AzurePipelines.Environment>(environmentYaml);
                }
                catch (Exception ex1)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<AzurePipelines.Environment>(environmentYaml) swallowed an exception: " + ex1.Message, _verbose);

                    try
                    {
                        //when the environment is just a simple string, e.g.  //environment: environmentName.resourceName
                        string simpleEnvironment = GenericObjectSerialization.DeserializeYaml <string>(environmentYaml);
                        environment = new AzurePipelines.Environment
                        {
                            name = simpleEnvironment
                        };
                    }
                    catch (Exception ex2)
                    {
                        JObject json = JSONSerialization.DeserializeStringToObject(environmentYaml);
                        if (json["tags"].Type.ToString() == "String")
                        {
                            string name = null;
                            if (json["name"] != null)
                            {
                                name = json["name"].ToString();
                            }
                            string resourceName = null;
                            if (json["resourceName"] != null)
                            {
                                name = json["resourceName"].ToString();
                            }
                            string resourceId = null;
                            if (json["resourceId"] != null)
                            {
                                name = json["resourceId"].ToString();
                            }
                            string resourceType = null;
                            if (json["resourceType"] != null)
                            {
                                name = json["resourceType"].ToString();
                            }
                            environment = new AzurePipelines.Environment
                            {
                                name         = name,
                                resourceName = resourceName,
                                resourceId   = resourceId,
                                resourceType = resourceType
                            };
                            //Move the single string demands to an array
                            environment.tags    = new string[1];
                            environment.tags[0] = json["tags"].ToString();
                        }
                        else
                        {
                            ConversionUtility.WriteLine($"Manual deserialization with demands string swallowed an exception: " + ex2.Message, _verbose);
                        }
                    }
                }
            }

            return(environment);
        }
        public Pool ProcessPoolV2(string poolYaml)
        {
            Pool pool = null;

            if (poolYaml != null)
            {
                try
                {
                    //Most often, the pool will be in this structure
                    pool = GenericObjectSerialization.DeserializeYaml <Pool>(poolYaml);
                }
                catch (Exception ex)
                {
                    ConversionUtility.WriteLine($"DeserializeYaml<Pool>(poolYaml) swallowed an exception: " + ex.Message, _verbose);
                    //If it's a simple pool string, and has no json in it, assign it to the name
                    if (poolYaml.IndexOf("{") < 0)
                    {
                        pool = new Pool
                        {
                            name = poolYaml
                        };
                    }
                    else
                    {
                        //otherwise, demands is probably a string, instead of string[], let's fix it
                        JObject json = JSONSerialization.DeserializeStringToObject(poolYaml);
                        if (json["demands"].Type.ToString() == "String")
                        {
                            string name = null;
                            if (json["name"] != null)
                            {
                                name = json["name"].ToString();
                            }
                            string vmImage = null;
                            if (json["vmImage"] != null)
                            {
                                vmImage = json["vmImage"].ToString();
                            }
                            string demands = null;
                            if (json["demands"] != null)
                            {
                                demands = json["demands"].ToString();
                            }
                            pool = new Pool
                            {
                                name    = name,
                                vmImage = vmImage
                            };
                            //Move the single string demands to an array
                            pool.demands    = new string[1];
                            pool.demands[0] = demands;
                        }
                        else
                        {
                            ConversionUtility.WriteLine($"Manual deserialization with demands string swallowed an exception: " + ex.Message, _verbose);
                        }
                    }
                }
            }
            return(pool);
        }
Esempio n. 13
0
        //TODO: Add more task types
        public GitHubActions.Step ProcessStep(AzurePipelines.Step step)
        {
            GitHubActions.Step gitHubStep = null;
            if (step.task != null)
            {
                step = CleanStepInputs(step);
                //TODO: Should we be handling versions seperately?
                switch (step.task)
                {
                case "Ant@1":
                    gitHubStep = CreateAntStep(step);
                    break;

                case "ArchiveFiles@2":
                    gitHubStep = CreateArchiveFilesStep(step);
                    break;

                case "AzureAppServiceManage@0":
                    gitHubStep = CreateAzureAppServiceManageStep(step);
                    break;

                case "AzureResourceGroupDeployment@2":
                    gitHubStep = CreateAzureManageResourcesStep(step);
                    break;

                case "AzureFunctionAppContainer@1":
                case "AzureRmWebAppDeployment@3":
                case "AzureWebAppContainer@1":
                case "AzureRmWebAppDeployment@4":
                    gitHubStep = CreateAzureWebAppDeploymentStep(step);
                    break;

                case "CmdLine@2":
                    gitHubStep = CreateScriptStep("cmd", step);
                    break;

                case "CopyFiles@2":
                    gitHubStep = CreateCopyFilesStep(step);
                    break;

                case "Docker@1":
                case "Docker@2":
                    gitHubStep = CreateDockerStep(step);
                    break;

                case "DotNetCoreCLI@2":
                    gitHubStep = CreateDotNetCommandStep(step);
                    break;

                case "DownloadBuildArtifacts@0":
                    gitHubStep = CreateDownloadBuildArtifacts(step);
                    break;

                case "Gradle@2":
                    gitHubStep = CreateGradleStep(step);
                    break;

                case "Maven@3":
                    gitHubStep = CreateMavenStep(step);
                    break;

                case "NodeTool@0":
                    gitHubStep = CreateNodeToolStep(step);
                    break;

                case "NuGetCommand@2":
                    gitHubStep = CreateNuGetCommandStep(step);
                    break;

                case "NuGetToolInstaller@1":
                    gitHubStep = CreateNuGetToolInstallerStep();
                    break;

                case "PowerShell@2":
                    gitHubStep = CreateScriptStep("powershell", step);
                    break;

                case "PublishPipelineArtifact@0":
                case "PublishBuildArtifacts@1":
                    gitHubStep = CreatePublishBuildArtifactsStep(step);
                    break;

                case "PythonScript@0":
                    gitHubStep = CreatePythonStep(step);
                    break;

                case "SqlAzureDacpacDeployment@1":
                    gitHubStep = CreateSQLAzureDacPacDeployStep(step);
                    break;

                case "UseDotNet@2":
                    gitHubStep = CreateUseDotNetStep(step);
                    break;

                case "UsePythonVersion@0":
                    gitHubStep = CreateUsePythonStep(step);
                    break;

                case "UseRubyVersion@0":
                    gitHubStep = CreateUseRubyStep(step);
                    break;

                case "VSBuild@1":
                    gitHubStep = CreateMSBuildStep(step);
                    break;

                case "VSTest@2":
                    gitHubStep = CreateFunctionalTestingStep(step);
                    break;

                case "XamarinAndroid@1":
                    gitHubStep = CreateXamarinAndroidStep(step);
                    break;

                case "XamariniOS@2":
                    gitHubStep = CreateXamariniOSStep(step);
                    break;

                default:
                    gitHubStep = CreateScriptStep("powershell", step);
                    string        newYaml      = GenericObjectSerialization.SerializeYaml <AzurePipelines.Step>(step);
                    string[]      newYamlSplit = newYaml.Split(Environment.NewLine);
                    StringBuilder yamlBuilder  = new StringBuilder();
                    for (int i = 0; i < newYamlSplit.Length; i++)
                    {
                        string line = newYamlSplit[i];
                        if (line.Trim().Length > 0)
                        {
                            yamlBuilder.Append("#");
                            yamlBuilder.Append(line);
                        }
                    }
                    gitHubStep.step_message = "Note: This step does not have a conversion path yet: " + step.task;
                    gitHubStep.run          = "Write-Host " + gitHubStep.step_message + " " + yamlBuilder.ToString();
                    break;
                }
            }
            else if (step.script != null)
            {
                gitHubStep = new GitHubActions.Step
                {
                    run  = step.script,
                    with = step.inputs
                };
            }
            else if (step.pwsh != null)
            {
                gitHubStep = CreateScriptStep("pwsh", step);
            }
            else if (step.powershell != null)
            {
                gitHubStep = CreateScriptStep("powershell", step);
            }
            else if (step.bash != null)
            {
                gitHubStep = CreateScriptStep("bash", step);
            }
            else if (step.publish != null)
            {
                //The shortcut to the build publish step
                //https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#publish
                gitHubStep = CreatePublishBuildArtifactsStep(step);
            }

            if (gitHubStep != null)
            {
                //Add in generic name and conditions
                if (step.displayName != null)
                {
                    gitHubStep.name = step.displayName;
                }
                if (step.condition != null)
                {
                    gitHubStep._if = ConditionsProcessing.TranslateConditions(step.condition);
                }
                //Double check the with. Sometimes we start to add a property, but for various reasons, we don't use it, and have to null out the with so it doesn't display an empty node in the final yaml
                if (gitHubStep.with != null)
                {
                    if (gitHubStep.with.Count >= 0)
                    {
                        //Look to see if there is non-null data in the collection
                        bool foundData = false;
                        foreach (KeyValuePair <string, string> item in gitHubStep.with)
                        {
                            //If data was found, break out of the loop, we don't need to look anymore
                            if (item.Value != null)
                            {
                                foundData = true;
                                break;
                            }
                        }
                        //If no data was found, null out the with property
                        if (foundData == false)
                        {
                            gitHubStep.with = null;
                        }
                    }
                }
            }
            return(gitHubStep);
        }