// TODO rename to Black Mamba?
 public void KillBuild(DroneBuild build, string reason)
 {
     Console.WriteLine($"Kill {reason} build {build.Repo} #{build.Number}");
     using (var web = new WebClient()) {
         var url = $"{_url}/api/repos/{build.Repo}/builds/{build.Number}?access_token={WebUtility.UrlEncode(_token)}";
         web.UploadString(url, "DELETE", "");
     }
 }
        public DroneQueue ReadQueue()
        {
            // FYI https://github.com/drone/drone/issues/2251

            var result = new DroneQueue();

            using (var web = new WebClient()) {
                Console.WriteLine("Reading Drone jobs");

                foreach (var b in ReadBuilds(web))
                {
                    Console.Write($"  {b.owner}/{b.name} #{b.number} ");

                    var createdAt = DateTimeOffset.FromUnixTimeSeconds((long)b.created_at);
                    if ((DateTime.UtcNow - createdAt).TotalHours > 6)
                    {
                        Console.WriteLine($"too old, created at {createdAt:u}");
                        // Don't count too old builds
                        // They may be abnormal queue items that are never picked up by an agent
                        continue;
                    }

                    var zombie = true;
                    var procs  = ReadBuildDetails(web, b).procs as IEnumerable <dynamic>;
                    if (procs != null)
                    {
                        var hasCancelledProc = procs.Any(p => p.state == "killed" || p.state == "failure" && p.error == "Cancelled");
                        if (hasCancelledProc)
                        {
                            Console.WriteLine("has killed or cancelled proc");
                            result.CancelledBuilds.Add(DroneBuild.FromApiObject(b));
                            continue;
                        }

                        foreach (var p in procs)
                        {
                            Console.Write(((string)p.state)[0]);
                            if (p.state == "running" || p.state == "pending")
                            {
                                result.ActiveJobCount++;
                                zombie = false;
                            }
                        }
                    }
                    else
                    {
                        Console.Write("no procs!");
                        zombie = false; // TODO
                    }

                    if (zombie)
                    {
                        Console.Write(" [zombie]");
                        result.ZombieBuilds.Add(DroneBuild.FromApiObject(b));
                    }

                    Console.WriteLine();
                }
            }

            return(result);
        }