public static string GetAssetOid(string jiraId, string assetTypeStr) { var config = (JiraConnectionConfiguration)ConfigurationManager.GetSection("jiraAttachments"); var metaconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/meta.v1/"); var dataconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/rest-1.v1/") .WithVersionOneUsernameAndPassword(config.V1Connection.Username, config.V1Connection.Password); MetaModel metaModel = new MetaModel(metaconnector); Services services = new Services(metaModel, dataconnector); var assetType = metaModel.GetAssetType(assetTypeStr); var query = new Query(assetType); var jiraIdAttribute = assetType.GetAttributeDefinition(GetV1IdCustomFieldName(assetTypeStr)); query.Selection.Add(jiraIdAttribute); var jiraIdTerm = new FilterTerm(jiraIdAttribute); jiraIdTerm.Equal(jiraId); query.Filter = jiraIdTerm; var result = services.Retrieve(query); if (result.Assets.Count == 0) { return String.Empty; } return result.Assets[0].Oid.ToString(); }
public static string GetV1IdCustomFieldName(string internalAssetTypeName) { var config = (JiraConnectionConfiguration)ConfigurationManager.GetSection("jiraAttachments"); if (!String.IsNullOrEmpty(config.V1Connection.CustomField)) { string customFieldName = String.Empty; var metaconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/meta.v1/"); var dataconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/rest-1.v1/") .WithVersionOneUsernameAndPassword(config.V1Connection.Username, config.V1Connection.Password); MetaModel metaApi = new MetaModel(metaconnector); Services dataApi = new Services(metaApi,dataconnector); IAssetType assetType = metaApi.GetAssetType("AttributeDefinition"); Query query = new Query(assetType); IAttributeDefinition nameAttribute = assetType.GetAttributeDefinition("Name"); query.Selection.Add(nameAttribute); IAttributeDefinition isCustomAttribute = assetType.GetAttributeDefinition("IsCustom"); query.Selection.Add(isCustomAttribute); IAttributeDefinition assetNameAttribute = assetType.GetAttributeDefinition("Asset.Name"); query.Selection.Add(assetNameAttribute); FilterTerm assetName = new FilterTerm(assetNameAttribute); assetName.Equal(internalAssetTypeName); FilterTerm isCustom = new FilterTerm(isCustomAttribute); isCustom.Equal("true"); query.Filter = new AndFilterTerm(assetName, isCustom); QueryResult result = dataApi.Retrieve(query); foreach (Asset asset in result.Assets) { string attributeValue = asset.GetAttribute(nameAttribute).Value.ToString(); if (attributeValue.StartsWith(config.V1Connection.CustomField)) { customFieldName = attributeValue; break; } } return customFieldName; } return null; }
static void Main(string[] args) { var dataConnector = new VersionOneAPIConnector(BASE_URL + "/rest-1.v1/").WithOAuth2(SecretsFile, CredsFile); var metaConnector = new VersionOneAPIConnector(BASE_URL + "/meta.v1/"); var metaModel = new MetaModel(metaConnector); var services = new Services(metaModel, dataConnector); var scopeType = metaModel.GetAssetType("Member"); var nameAttr = scopeType.GetAttributeDefinition("Name"); var descAttr = scopeType.GetAttributeDefinition("Nickname"); var worksItemsNameAttr = scopeType.GetAttributeDefinition("OwnedWorkitems.Name"); var query = new Query(scopeType); var whereAdmin = new FilterTerm(descAttr); whereAdmin.Equal("admin"); var whereNotTheAdmin = new FilterTerm(nameAttr); whereNotTheAdmin.NotEqual("theAdmin"); var andFilter = new AndFilterTerm(whereAdmin, whereNotTheAdmin); query.Filter = andFilter; query.Selection.AddRange(new[] { nameAttr, descAttr, worksItemsNameAttr }); var result = services.Retrieve(query); foreach (var asset in result.Assets) { Console.WriteLine("Name: " + asset.GetAttribute(nameAttr).Value); Console.WriteLine("Description: " + asset.GetAttribute(descAttr).Value); var workItems = asset.GetAttribute(worksItemsNameAttr).ValuesList; Console.WriteLine("Workitems count: " + workItems.Count); foreach (var workitem in workItems) { Console.WriteLine("Workitem: " + workitem); } } Console.ReadLine(); }
//Verifies the connection to the V1 source instance. private static void VerifyV1TargetConnection() { try { _targetMetaConnector = new V1APIConnector(_config.V1TargetConnection.Url + "/meta.v1/"); if (_config.V1TargetConnection.AuthenticationType == "standard") { _targetDataConnector = new V1APIConnector(_config.V1TargetConnection.Url + "/rest-1.v1/", _config.V1TargetConnection.Username, _config.V1TargetConnection.Password, false); _targetImageConnector = new V1APIConnector(_config.V1TargetConnection.Url + "/attachment.img/", _config.V1TargetConnection.Username, _config.V1TargetConnection.Password, false); } else if (_config.V1TargetConnection.AuthenticationType == "windows") { _logger.Info("Connecting with user {0}.", System.Security.Principal.WindowsIdentity.GetCurrent().Name); _targetDataConnector = new V1APIConnector(_config.V1TargetConnection.Url + "/rest-1.v1/", null, null, true); _targetImageConnector = new V1APIConnector(_config.V1TargetConnection.Url + "/attachment.img/", null, null, true); } else if (_config.V1TargetConnection.AuthenticationType == "oauth") { throw new Exception("OAuth authentication is not supported -- yet."); } else { throw new Exception("Unable to determine the V1TargetConnection authentication type in the config file. Value used must be standard|windows|oauth."); } _targetMetaAPI = new MetaModel(_targetMetaConnector); _targetDataAPI = new Services(_targetMetaAPI, _targetDataConnector); IAssetType assetType = _targetMetaAPI.GetAssetType("Member"); Query query = new Query(assetType); IAttributeDefinition nameAttribute = assetType.GetAttributeDefinition("Username"); query.Selection.Add(nameAttribute); FilterTerm idFilter = new FilterTerm(nameAttribute); idFilter.Equal(_config.V1TargetConnection.Username); query.Filter = idFilter; QueryResult result = _targetDataAPI.Retrieve(query); if (result.TotalAvaliable > 0) { _logger.Info("-> Connection to V1 target instance \"{0}\" verified.", _config.V1TargetConnection.Url); _logger.Info("-> V1 target instance version: {0}.", _targetMetaAPI.Version.ToString()); MigrationStats.WriteStat(_sqlConn, "Target API Version", _targetMetaAPI.Version.ToString()); } else { throw new Exception(String.Format("Unable to validate connection to {0} with username {1}. You may not have permission to access this instance.", _config.V1TargetConnection.Url, _config.V1TargetConnection.Username)); } } catch (Exception ex) { _logger.Error("-> Unable to connect to V1 source instance \"{0}\".", _config.V1TargetConnection.Url); throw ex; } }