PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Atlassian.Jira/CustomFieldCollection.cs

https://bitbucket.org/yyo/atlassian.net-sdk-v2.0
C# | 71 lines | 49 code | 8 blank | 14 comment | 1 complexity | ee8073af7c26ef68f30275222f5bbeac MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using Atlassian.Jira.Remote;
  7. namespace Atlassian.Jira
  8. {
  9. /// <summary>
  10. /// Collection of custom fields
  11. /// </summary>
  12. public class CustomFieldCollection : ReadOnlyCollection<CustomField>, IRemoteIssueFieldProvider
  13. {
  14. private readonly Jira _jira;
  15. private readonly string _projectKey;
  16. internal CustomFieldCollection(Jira jira, string projectKey)
  17. : this(jira, projectKey, new List<CustomField>())
  18. {
  19. }
  20. internal CustomFieldCollection(Jira jira, string projectKey, IList<CustomField> list)
  21. : base(list)
  22. {
  23. _jira = jira;
  24. _projectKey = projectKey;
  25. }
  26. /// <summary>
  27. /// Add a custom field by name
  28. /// </summary>
  29. /// <param name="fieldName">The name of the custom field as defined in JIRA</param>
  30. /// <param name="fieldValues">The values of the field</param>
  31. public void Add(string fieldName, string[] fieldValues)
  32. {
  33. var fieldId = GetIdForFieldName(fieldName);
  34. this.Items.Add(new CustomField(fieldId, fieldName, _jira) { Values = fieldValues });
  35. }
  36. /// <summary>
  37. /// Gets a custom field by name
  38. /// </summary>
  39. /// <param name="fieldName">Name of the custom field as defined in JIRA</param>
  40. /// <returns>CustomField instance if the field has been set on the issue, null otherwise</returns>
  41. public CustomField this[string fieldName]
  42. {
  43. get
  44. {
  45. var fieldId = GetIdForFieldName(fieldName);
  46. return this.Items.FirstOrDefault(f => f.Id == fieldId);
  47. }
  48. }
  49. private string GetIdForFieldName(string fieldName)
  50. {
  51. // workaround for bug JRA-6857: GetCustomFields() is for admins only
  52. return _jira.GetFieldsForEdit(_projectKey).First(f => f.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase)).Id;
  53. }
  54. RemoteFieldValue[] IRemoteIssueFieldProvider.GetRemoteFields()
  55. {
  56. return this.Items.Select(f => new RemoteFieldValue()
  57. {
  58. id = f.Id,
  59. values = f.Values
  60. }).ToArray();
  61. }
  62. }
  63. }