Cargowire.CIBridge.HookParsers.CodebaseHqHookParser.Parse C# (CSharp) Method

Parse() public method

Translates an http get/post from the Source control solution into a generic format for use with a build engine
public Parse ( NameValueCollection requestData ) : IEnumerable
requestData System.Collections.Specialized.NameValueCollection
return IEnumerable
        public IEnumerable<HookInfo> Parse(NameValueCollection requestData)
        {
            string jsonData = requestData["payload"];
            if (!string.IsNullOrEmpty(jsonData))
            {
                string json = jsonData;
                JObject o = JObject.Parse(json);
                User u = new User { Name = (string)((JObject)o["user"])["name"], Username = (string)((JObject)o["user"])["username"], Email = (string)((JObject)o["user"])["email"] };
                Repository r = new Repository { Name = (string)((JObject)o["repository"])["name"], Uri = new Uri((string)((JObject)o["repository"])["url"]) };
                List<Commit> commits = new List<Commit>();
                foreach(JObject commit in (JArray)o["commits"]){
                    commits.Add(new Commit { Author = u, Id = (string)commit["id"], DateTime = DateTime.Parse((string)commit["timestamp"]), Uri = new Uri((string)commit["url"]) });
                }
                r.Commits = commits;
                string branchName = (string)(JValue)o["ref"];
                string branchFullName = branchName;
                branchName = branchName.Substring(branchName.LastIndexOf("/") + 1);
                return new List<HookInfo>(new HookInfo[] { new HookInfo { Branch = new Branch { Name = branchName, FullName = branchFullName, IsMaster = (string.Compare(branchName, "master", true) == 0) }, User = u, Repository = r } });
            }
            return null;
        }

Usage Example

        public ActionResult Build(string name)
        {
            string values = Request.Form.ToString();
            try
            {
                Log(string.Concat("Form: ", Request.Form.ToString()));
                IEnumerable<HookInfo> hi;
                if (!string.IsNullOrEmpty(name))
                    hi = new List<HookInfo> { new HookInfo{ Repository = new Repository { Name = name }, User = null } };
                else
                {
                    var parser = new CodebaseHqHookParser();
                    hi = parser.Parse(Request.Form);
                }
                if (hi.Count() > 0)
                {
                    IBuildEngine engine = GetBuildEngine();
                    Log(string.Concat("Repository: ", hi.First().Repository.Name, "(", (hi.First().Branch ?? new Branch()).Name, ")"));
                    engine.ForceBuild(hi.First().Repository.Name, hi.First().Branch);
                }
                Log(Environment.NewLine);
            }
            catch (Exception ex)
            {
                Log(string.Concat("Error:", ex.Message));
                throw;
            }

            return (string.IsNullOrEmpty(name)) ? View() : (ActionResult)RedirectToAction("index");
        }
CodebaseHqHookParser