Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions YouTrack.Rest/Change.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;

namespace YouTrack.Rest
{
public class Change: IChange
{
public Change()
{
ChangedFields = new Dictionary<string, IChangedField>(StringComparer.InvariantCultureIgnoreCase);
}

public DateTime Updated { get; internal set; }

public string UpdaterName { get; internal set; }

public IDictionary<string, IChangedField> ChangedFields { get; private set; }
}
}
30 changes: 30 additions & 0 deletions YouTrack.Rest/ChangedField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using YouTrack.Rest.Deserialization;

namespace YouTrack.Rest
{
class ChangedField : IChangedField
{
public ChangedField(Field field)
{
Name = field.Name;

if (null != field.Values)
Values = field.Values.ConvertAll(v => v.ToString());

if (null != field.NewValues)
NewValues = field.NewValues.ConvertAll(v => v.ToString());

if (null != field.OldValues)
OldValues = field.OldValues.ConvertAll(v => v.ToString());
}

public string Name { get; private set; }

public IEnumerable<string> Values { get; private set; }

public IEnumerable<string> OldValues { get; private set; }

public IEnumerable<string> NewValues { get; private set; }
}
}
41 changes: 41 additions & 0 deletions YouTrack.Rest/Deserialization/Change.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;

namespace YouTrack.Rest.Deserialization
{
class Change : HasFieldsBase
{
public Change() : base("Change")
{
}

public IChange GetChange(IConnection connection)
{
Rest.Change change = new Rest.Change();

MapTo(change);

return change;
}

public void MapTo(Rest.Change change)
{
change.UpdaterName = GetString("updaterName", "");
change.Updated = GetDateTime("updated");

MapFields(change.ChangedFields);
}

private void MapFields(IDictionary<string, IChangedField> fields)
{
fields.Clear();

foreach (Field field in Fields)
{
if (!string.IsNullOrEmpty(field.Name))
{
fields[field.Name] = new ChangedField(field);
}
}
}
}
}
15 changes: 15 additions & 0 deletions YouTrack.Rest/Deserialization/ChangeCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Linq;

namespace YouTrack.Rest.Deserialization
{
class ChangeCollection
{
public List<Change> Changes { get; set; }

public IEnumerable<IChange> GetChanges(IConnection connection)
{
return Changes.Select(c => c.GetChange(connection)).ToArray();
}
}
}
10 changes: 8 additions & 2 deletions YouTrack.Rest/Deserialization/Field.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RestSharp.Deserializers;
using YouTrack.Rest.Exceptions;

namespace YouTrack.Rest.Deserialization
{
class Field
{
public string Name { get; set; }
public List<Value_> Values { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public List<Value_> Values { get; set; }

// issue field value changes
public List<NewValue> NewValues { get; set; }
public List<OldValue> OldValues { get; set; }

public string GetValue()
{
Expand Down
76 changes: 76 additions & 0 deletions YouTrack.Rest/Deserialization/HasFieldsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using YouTrack.Rest.Exceptions;

namespace YouTrack.Rest.Deserialization
{
abstract class HasFieldsBase
{
private readonly string type;

public string Id { get; set; }

public List<Field> Fields { get; set; }

protected HasFieldsBase(string type)
{
this.type = type;
}

protected bool HasFieldFor(string name)
{
return Fields.Any(GetCompareNamesPredicate(name));
}

protected bool HasSingleFieldFor(string name)
{
return Fields.Count(GetCompareNamesPredicate(name)) == 1;
}

protected Field GetSingleFieldFor(string name)
{
return Fields.Single(GetCompareNamesPredicate(name));
}

private Func<Field, bool> GetCompareNamesPredicate(string name)
{
return f => f.Name.ToUpper() == name.ToUpper();
}

protected int GetInt32(string name)
{
if (HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetInt32();
}

throw new IssueDeserializationException(String.Format("{0} '{1}' has zero or multiple integer values for field '{2}'.", type, Id, name));
}

protected DateTime GetDateTime(string name)
{
if (HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetDateTime();
}

throw new IssueDeserializationException(String.Format("{0} '{1}' has zero or multiple datetime values for field '{2}'.", type, Id, name));
}

protected string GetString(string name, string defaultValue = null)
{
if (!HasFieldFor(name) && defaultValue != null)
{
return defaultValue;
}

if (HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetValue();
}

throw new IssueDeserializationException(String.Format("{0} '{1}' has zero or multiple string values for field '{2}'.", type, Id, name));
}
}
}
67 changes: 6 additions & 61 deletions YouTrack.Rest/Deserialization/Issue.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using YouTrack.Rest.Exceptions;

namespace YouTrack.Rest.Deserialization
{
//Has to have name Issue for RestSharp deserialization to work properly.
class Issue
class Issue : HasFieldsBase
{
public string Id { get; set; }
public List<Field> Fields { get; set; }
public List<Comment> Comments { get; set; }
public List<Comment> Comments { get; set; }

public Issue() : base("Issue")
{
}

public virtual IIssue GetIssue(IConnection connection)
{
Expand All @@ -21,61 +21,6 @@ public virtual IIssue GetIssue(IConnection connection)
return issue;
}

private int GetInt32(string name)
{
if (HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetInt32();
}

throw new IssueDeserializationException(String.Format("Issue '{0}' has zero or multiple integer values for field '{1}'.", Id, name));
}

private DateTime GetDateTime(string name)
{
if(HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetDateTime();
}

throw new IssueDeserializationException(String.Format("Issue '{0}' has zero or multiple datetime values for field '{1}'.", Id, name));
}

private string GetString(string name, string defaultValue = null)
{
if(!HasFieldFor(name) && defaultValue != null)
{
return defaultValue;
}

if (HasSingleFieldFor(name))
{
return GetSingleFieldFor(name).GetValue();
}

throw new IssueDeserializationException(String.Format("Issue '{0}' has zero or multiple string values for field '{1}'.", Id, name));
}

private bool HasFieldFor(string name)
{
return Fields.Any(GetCompareNamesPredicate(name));
}

private bool HasSingleFieldFor(string name)
{
return Fields.Count(GetCompareNamesPredicate(name)) == 1;
}

private Field GetSingleFieldFor(string name)
{
return Fields.Single(GetCompareNamesPredicate(name));
}

private Func<Field, bool> GetCompareNamesPredicate(string name)
{
return f => f.Name.ToUpper() == name.ToUpper();
}

public void MapTo(Rest.Issue issue, IConnection connection)
{
issue.CommentsCount = GetInt32("commentsCount");
Expand Down
5 changes: 4 additions & 1 deletion YouTrack.Rest/Deserialization/Value_.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace YouTrack.Rest.Deserialization
{
{
internal class NewValue : Value_ { }
internal class OldValue : Value_ { }

internal class Value_
{
public string Value { get; set; }
Expand Down
13 changes: 13 additions & 0 deletions YouTrack.Rest/IChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;

namespace YouTrack.Rest
{
public interface IChange
{
DateTime Updated { get; }
string UpdaterName { get; }

IDictionary<string, IChangedField> ChangedFields { get; }
}
}
12 changes: 12 additions & 0 deletions YouTrack.Rest/IChangedField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace YouTrack.Rest
{
public interface IChangedField
{
string Name { get; }
IEnumerable<string> Values { get; }
IEnumerable<string> OldValues { get; }
IEnumerable<string> NewValues { get; }
}
}
3 changes: 2 additions & 1 deletion YouTrack.Rest/IIssueActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IIssueActions
void SetSubsystem(string subsystem, string group = null);
void SetType(string type, string group = null);
void ApplyCommand(string command, string group = null);
void ApplyCommands(params string[] commands);
void ApplyCommands(params string[] commands);
IEnumerable<IChange> ChangeHistory { get; }
}
}
20 changes: 18 additions & 2 deletions YouTrack.Rest/IssueActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ public virtual void ApplyCommands(params string[] commands)
ApplyCommandsToAnIssueRequest request = new ApplyCommandsToAnIssueRequest(Id, commands);

Connection.Post(request);
}

}

private IEnumerable<IChange> changes;

public IEnumerable<IChange> ChangeHistory
{
get { return changes ?? (changes = GetChanges()); }
}

private IEnumerable<IChange> GetChanges()
{
GetChangeHistoryOfAnIssueRequest request = new GetChangeHistoryOfAnIssueRequest(Id);

ChangeCollection changeCollection = Connection.Get<ChangeCollection>(request);

return changeCollection.GetChanges(Connection);
}

public void SetSubsystem(string subsystem, string group = null)
{
ApplyCommand(Commands.SetSubsystem(subsystem), group);
Expand Down
8 changes: 7 additions & 1 deletion YouTrack.Rest/Repositories/IIssueRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System.Collections.Generic;

namespace YouTrack.Rest.Repositories
{
public interface IIssueRepository
{
IIssue CreateIssue(string project, string summary, string description, string group = null);
IIssue GetIssue(string issueId);
IIssue GetIssue(string issueId);
IEnumerable<IIssue> Search(string query);
IEnumerable<IIssue> Search(string query, params string[] withFields);
IEnumerable<IIssue> Search(string query, int maximumNumberOfRecordsToReturn, int startFrom);
IEnumerable<IIssue> Search(string query, string[] withFields, int maximumNumberOfRecordsToReturn, int startFrom);
void DeleteIssue(string issueId);
bool IssueExists(string issueId);
}
Expand Down
Loading