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);
            }
        }
Пример #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
            });
        }
Пример #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);
        }