Example #1
0
        string IRestService.DoItNow(GitHubPushWebhook pushInfo)
        {
            WebOperationContext ctx = WebOperationContext.Current;

            if (!(Utility.IsAllowed(HttpContext.Current)))
            {
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
                return("Source IP not in ACL");
            }


            string confJSON = HostingEnvironment.MapPath("~/App_Data/Configuration.json");
            Repos  config;

            try
            {
                string json = File.ReadAllText(confJSON);
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Repos));

                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
                config = (Repos)ser.ReadObject(stream);
                stream.Close();
            }
            catch (FileNotFoundException exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                return("Configuration.json not found, no commands are run.");
            }
            catch (SerializationException exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                return(String.Format("The following error occurred trying to deserialize JSON in Configuration.json:\n{0}", exc.Message));
            }


            List <string> commands = new List <string>();
            IEnumerable <RepositoryConfig> repositories = new List <RepositoryConfig>();

            try
            {
                // get list of repos by matching key
                // and ref if present
                repositories = config.repos.Where(repo => (string)pushInfo.repository[repo.repo_key] == repo.repo_value &&
                                                  (string.IsNullOrEmpty(repo.ref_) ? true : pushInfo.ref_ == repo.ref_)).ToList();
            }
            catch (Exception exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return(String.Format("The following error occurred trying to match repository information with commands defined via Configuration.json:\n{0}", exc.Message));
            }

            if (repositories.Count() == 0)
            {
                return(String.Format("No actions defined for repo with full name: {0}", pushInfo.repository.full_name));
            }
            foreach (RepositoryConfig repository in repositories)
            {
                List <Commands> list = repository.commands.ToList <Commands>();
                list.Sort();
                commands.AddRange(list.Select(command => command.command));
            }
            PowerShell shell = PowerShell.Create();


            foreach (string command in commands)
            {
                shell.AddScript(command);
            }
            shell.BeginInvoke();
            return(String.Format("Following actions were executed for repo with full name: {0}:\n\t{1}",
                                 pushInfo.repository.full_name,
                                 String.Join("\n\t", commands)));
        }
        string IRestService.DoItNow(GitHubPushWebhook pushInfo)
        {
            WebOperationContext ctx = WebOperationContext.Current;
            if(!(Utility.IsAllowed(HttpContext.Current)))
            {
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
                return "Source IP not in ACL";
            }

            string confJSON = HostingEnvironment.MapPath("~/App_Data/Configuration.json");
            Repos config;
            try
            {
                string json = File.ReadAllText(confJSON);
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Repos));

                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
                config = (Repos)ser.ReadObject(stream);
                stream.Close();
            }
            catch (FileNotFoundException exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                return "Configuration.json not found, no commands are run.";
            }
            catch (SerializationException exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                return String.Format("The following error occurred trying to deserialize JSON in Configuration.json:\n{0}",exc.Message);
            }

            List<string> commands = new List<string>();
            IEnumerable<RepositoryConfig> repositories = new List<RepositoryConfig>();

            try
            {
                repositories = config.repos.Where(repo => (string)pushInfo.repository[repo.repo_key] == repo.repo_value).ToList();
            }
            catch (Exception exc)
            {
                Utility.LogException(exc);
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return String.Format("The following error occurred trying to match repository information with commands defined via Configuration.json:\n{0}", exc.Message);
            }

            if (repositories.Count() == 0)
            {
                return String.Format("No actions defined for repo with full name: {0}", pushInfo.repository.full_name);
            }
            foreach (RepositoryConfig repository in repositories)
            {
                List<Commands> list = repository.commands.ToList<Commands>();
                list.Sort();
                commands.AddRange(list.Select(command => command.command));
            }
            PowerShell shell = PowerShell.Create();

            foreach (string command in commands)
            {
                shell.AddScript(command);
            }
            shell.BeginInvoke();
            return String.Format("Following actions were executed for repo with full name: {0}:\n\t{1}",
                                 pushInfo.repository.full_name,
                                 String.Join("\n\t",commands));
        }