2012-12-17 09:41:11 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-17 17:42:57 +01:00
|
|
|
using System.Globalization;
|
2012-12-17 09:41:11 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
2012-12-17 17:42:57 +01:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web.UI;
|
|
|
|
|
using RestSharp;
|
|
|
|
|
using uRelease.Models;
|
|
|
|
|
using YouTrackSharp.Infrastructure;
|
|
|
|
|
using Issue = uRelease.Models.Issue;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using Version = uRelease.Models.Version;
|
2012-12-17 09:41:11 +01:00
|
|
|
|
|
|
|
|
namespace uRelease.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class ApiController : Controller
|
|
|
|
|
{
|
2012-12-17 17:42:57 +01:00
|
|
|
private const string VersionBundleUrl = "admin/customfield/versionBundle/Umbraco v4 Versions";
|
|
|
|
|
private const string IssuesUrl = "issue/byproject/{0}?filter={1}&max=100";
|
2012-12-17 09:41:11 +01:00
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
private static readonly string ProjectId = ConfigurationManager.AppSettings["uReleaseProjectId"];
|
2012-12-17 09:41:11 +01:00
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
private static readonly string Login = ConfigurationManager.AppSettings["uReleaseUsername"];
|
|
|
|
|
private static readonly string Password = ConfigurationManager.AppSettings["uReleasePassword"];
|
2012-12-17 09:41:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
[OutputCache(Duration = 30, Location = OutputCacheLocation.ServerAndClient)]
|
|
|
|
|
public JsonResult Aggregate(string ids)
|
|
|
|
|
{
|
2012-12-17 17:42:57 +01:00
|
|
|
var idArray = new ArrayList();
|
2012-12-17 11:05:44 +01:00
|
|
|
idArray.AddRange(ids.Replace("all", "").TrimEnd(',').Split(','));
|
2012-12-17 09:41:11 +01:00
|
|
|
idArray.Remove("");
|
2012-12-17 17:42:57 +01:00
|
|
|
|
2012-12-17 09:41:11 +01:00
|
|
|
var gotBundle = GetVersionBundle();
|
|
|
|
|
|
|
|
|
|
// For each version in the bundle, go get the issues
|
|
|
|
|
var toReturn = new List<AggregateView>();
|
|
|
|
|
|
|
|
|
|
Version[] orderedVersions;
|
|
|
|
|
|
|
|
|
|
if (idArray.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//get version specific data
|
|
|
|
|
orderedVersions =
|
|
|
|
|
gotBundle.Data.Versions
|
|
|
|
|
.Where(x => idArray.Contains(x.Value))
|
|
|
|
|
.OrderBy(x => x.Value.AsFullVersion())
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//get all versions with projectID
|
|
|
|
|
orderedVersions =
|
|
|
|
|
gotBundle.Data.Versions
|
|
|
|
|
.OrderBy(x => x.Value.AsFullVersion())
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
2012-12-17 11:05:44 +01:00
|
|
|
|
2012-12-17 09:41:11 +01:00
|
|
|
|
|
|
|
|
//figure out which is the latest release
|
|
|
|
|
var latestRelease = orderedVersions.Where(x => x.Released).OrderByDescending(x => x.ReleaseDate).FirstOrDefault();
|
|
|
|
|
var inprogressRelease = orderedVersions.Where(x => !x.Released).OrderBy(x => x.ReleaseDate).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
// Just used to make sure we don't make repeated API requests for keys
|
|
|
|
|
var versionCache = new ConcurrentDictionary<string, RestResponse<IssuesWrapper>>();
|
|
|
|
|
|
|
|
|
|
foreach (var version in orderedVersions)
|
|
|
|
|
{
|
2012-12-17 17:42:57 +01:00
|
|
|
var item = new AggregateView
|
|
|
|
|
{
|
|
|
|
|
latestRelease = (latestRelease != null && version.Value == latestRelease.Value),
|
|
|
|
|
inProgressRelease = (inprogressRelease != null && version.Value == inprogressRelease.Value),
|
|
|
|
|
version = version.Value,
|
|
|
|
|
releaseDescription = version.Description ?? string.Empty,
|
|
|
|
|
released = version.Released,
|
2012-12-19 18:19:15 +01:00
|
|
|
releaseDate = version.ReleaseDate == 0 ? "" : new DateTime(1970, 1, 1).AddMilliseconds(version.ReleaseDate).ToString(CultureInfo.InvariantCulture)
|
2012-12-17 17:42:57 +01:00
|
|
|
};
|
2012-12-19 18:19:15 +01:00
|
|
|
|
2012-12-17 09:41:11 +01:00
|
|
|
// /rest/issue/byproject/{project}?{filter}
|
2012-12-19 15:05:50 +01:00
|
|
|
var issues = versionCache.GetOrAdd(version.Value, key => GetRestResponse<IssuesWrapper>(string.Format(IssuesUrl, ProjectId, "Due+in+version%3A+" + key)));
|
2012-12-17 09:41:11 +01:00
|
|
|
var issueView = new List<IssueView>();
|
|
|
|
|
var activityView = new List<ActivityView>();
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(
|
|
|
|
|
issues.Data.Issues,
|
|
|
|
|
issue =>
|
|
|
|
|
{
|
2012-12-17 17:42:57 +01:00
|
|
|
var view = new IssueView
|
2012-12-17 09:41:11 +01:00
|
|
|
{
|
|
|
|
|
id = issue.Id,
|
|
|
|
|
state = GetFieldFromIssue(issue, "State"),
|
|
|
|
|
title = GetFieldFromIssue(issue, "summary"),
|
|
|
|
|
type = GetFieldFromIssue(issue, "Type"),
|
|
|
|
|
breaking = (GetFieldFromIssue(issue, "Backwards compatible?") == "No")
|
|
|
|
|
};
|
|
|
|
|
issueView.Add(view);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var activitiesDateDesc = activityView.Where(x => x.changes.Any()).OrderByDescending(x => x.date);
|
|
|
|
|
var issueIdsFromActivities = activitiesDateDesc.Select(x => x.id).Distinct()
|
2012-12-17 11:05:44 +01:00
|
|
|
.Concat(issueView.Where(y => y != null && !activitiesDateDesc.Select(z => z.id).Contains(y.id)).Select(y => y.id)); // Add issues for which there is no activity
|
2012-12-17 09:41:11 +01:00
|
|
|
|
2012-12-17 11:05:44 +01:00
|
|
|
item.issues = issueIdsFromActivities.Select(x => issueView.Single(y => y != null && y.id == x)).OrderBy(x => x.id);
|
2012-12-17 09:41:11 +01:00
|
|
|
item.activities = activitiesDateDesc.Take(5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toReturn.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
return new JsonResult { Data = toReturn, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
|
2012-12-17 09:41:11 +01:00
|
|
|
}
|
2012-12-17 17:42:57 +01:00
|
|
|
|
2012-12-17 09:41:11 +01:00
|
|
|
private static string GetFieldFromIssue(Issue issue, string fieldName)
|
|
|
|
|
{
|
|
|
|
|
var findField = issue.Fields.FirstOrDefault(x => x.Name == fieldName);
|
|
|
|
|
return findField != null ? findField.Value : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JsonResult AllVersions()
|
|
|
|
|
{
|
|
|
|
|
var gotBundle = GetVersionBundle();
|
2012-12-17 17:42:57 +01:00
|
|
|
return new JsonResult { Data = gotBundle.Data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
|
2012-12-17 09:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RestResponse<VersionBundle> GetVersionBundle()
|
|
|
|
|
{
|
2012-12-19 15:05:50 +01:00
|
|
|
return GetRestResponse<VersionBundle>(VersionBundleUrl);
|
2012-12-17 09:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 15:05:50 +01:00
|
|
|
private static RestResponse<T> GetRestResponse<T>(string restUri)
|
2012-12-17 09:41:11 +01:00
|
|
|
where T : new()
|
|
|
|
|
{
|
|
|
|
|
var ctor = new DefaultUriConstructor("http", "issues.umbraco.org", 80, "");
|
|
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
var auth = new RestClient();
|
2012-12-17 09:41:11 +01:00
|
|
|
auth.RemoveHandler("application/json");
|
|
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
var req = new RestRequest(ctor.ConstructBaseUri("user/login"), Method.POST);
|
|
|
|
|
req.AddParameter("login", Login);
|
|
|
|
|
req.AddParameter("password", Password);
|
2012-12-17 09:41:11 +01:00
|
|
|
|
|
|
|
|
var resp = auth.Execute(req);
|
|
|
|
|
var cookie = resp.Cookies.ToArray();
|
|
|
|
|
|
2012-12-17 17:42:57 +01:00
|
|
|
var getVBundle = new RestRequest(ctor.ConstructBaseUri(restUri));
|
2012-12-17 09:41:11 +01:00
|
|
|
foreach (var restResponseCookie in cookie)
|
|
|
|
|
getVBundle.AddCookie(restResponseCookie.Name, restResponseCookie.Value);
|
2012-12-17 17:42:57 +01:00
|
|
|
|
2012-12-17 09:41:11 +01:00
|
|
|
var gotBundle = auth.Execute<T>(getVBundle);
|
|
|
|
|
return (RestResponse<T>) gotBundle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|