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 void TestFixtureSetUp()
		{
			var metaConnector = new VersionOneAPIConnector(V1Url + MetaUrl).WithVersionOneUsernameAndPassword(Username, Password);
			var serviceConnector = new VersionOneAPIConnector(V1Url + DataUrl).WithVersionOneUsernameAndPassword(Username, Password);
			MetaModel = new MetaModel(metaConnector);
			Services = new Services(MetaModel, serviceConnector);

			var doc = new XmlDocument();
			doc.LoadXml(string.Format(@"<Settings> 
                            <APIVersion></APIVersion>
                            <ApplicationUrl>{0}</ApplicationUrl>
                            <Username>{1}</Username>
                            <Password>{2}</Password> 
                            <IntegratedAuth>false</IntegratedAuth>
                            <ProxySettings disabled='1'>
                                 <Uri>http://proxyhost:3128</Uri>
                                    <UserName>username</UserName>
                                    <Password>password</Password> 
                                    <Domain>domain</Domain>
                                </ProxySettings>
                            </Settings>", V1Url, Username, Password));

			var loggerMock = MockRepository.Stub<ILogger>();
			V1Processor = new VersionOneProcessor(doc["Settings"], loggerMock);
			V1Processor.AddProperty(Workitem.NumberProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Entity.NameProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Entity.DescriptionProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Workitem.PriorityProperty, VersionOneProcessor.PrimaryWorkitemType, true);
			V1Processor.AddProperty(Entity.StatusProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Workitem.EstimateProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Entity.AssetTypeProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(PrimaryWorkitem.ParentNameProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(PrimaryWorkitem.TeamNameProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(PrimaryWorkitem.SprintNameProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(PrimaryWorkitem.OrderProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(BaseEntity.ReferenceProperty, VersionOneProcessor.PrimaryWorkitemType, false);
			V1Processor.AddProperty(Entity.NameProperty, VersionOneProcessor.StoryType, false);
			V1Processor.AddProperty(Entity.NameProperty, VersionOneProcessor.FeatureGroupType, false);
			V1Processor.AddProperty(BaseEntity.ReferenceProperty, VersionOneProcessor.FeatureGroupType, false);
			V1Processor.AddProperty(VersionOneProcessor.OwnersAttribute, VersionOneProcessor.FeatureGroupType, false);
			V1Processor.AddProperty(FieldInfo.NameProperty, VersionOneProcessor.AttributeDefinitionType, false);
			V1Processor.AddProperty(FieldInfo.AssetTypeProperty, VersionOneProcessor.AttributeDefinitionType, false);
			V1Processor.AddProperty(FieldInfo.AttributeTypeProperty, VersionOneProcessor.AttributeDefinitionType, false);
			V1Processor.AddProperty(FieldInfo.IsReadOnlyProperty, VersionOneProcessor.AttributeDefinitionType, false);
			V1Processor.AddProperty(FieldInfo.IsRequiredProperty, VersionOneProcessor.AttributeDefinitionType, false);
			V1Processor.AddProperty(Entity.NameProperty, VersionOneProcessor.LinkType, false);
			V1Processor.AddProperty(Link.UrlProperty, VersionOneProcessor.LinkType, false);
			V1Processor.AddProperty(Link.OnMenuProperty, VersionOneProcessor.LinkType, false);
			V1Processor.AddProperty(Entity.NameProperty, VersionOneProcessor.MemberType, false);
			V1Processor.AddProperty(Member.EmailProperty, VersionOneProcessor.MemberType, false);
			V1Processor.AddProperty(Member.DefaultRoleNameProperty, VersionOneProcessor.MemberType, false);
			V1Processor.AddProperty(Workitem.OwnersProperty, VersionOneProcessor.StoryType, false);
			V1Processor.AddProperty(Workitem.OwnersProperty, VersionOneProcessor.DefectType, false);
			V1Processor.AddProperty(Workitem.OwnersProperty, VersionOneProcessor.PrimaryWorkitemType, false);

			Assert.IsTrue(V1Processor.ValidateConnection(), "Connection is not valid");
		}
        private IAPIConnector MakeConnector(string path, ProxyProvider proxy = null)
        {
            var connector = new VersionOneAPIConnector(path, proxyProvider: proxy);

            connector.WithVersionOneUsernameAndPassword(_credentials.V1UserName, _credentials.V1Password);
            if (_useWindowsIntegratedAuth)
                connector.WithWindowsIntegratedAuthentication();

            return connector;
        }
        private IAPIConnector MakeConnector(string path, ProxyProvider proxy = null)
        {
            var connector = new VersionOneAPIConnector(path, proxyProvider: proxy);

            connector.WithVersionOneUsernameAndPassword(_credentials.V1UserName, _credentials.V1Password);
            if (_useWindowsIntegratedAuth)
            {
                connector.WithWindowsIntegratedAuthentication();
            }

            return(connector);
        }
        private IAPIConnector PrepareConnector(string url)
        {
            var connector = new VersionOneAPIConnector(url, proxyProvider: _proxyProvider)
                            .WithVersionOneUsernameAndPassword(_username, _password);

            if (_integratedAuth)
            {
                connector.WithWindowsIntegratedAuthentication();
            }

            return(PopulateHeaders(connector));
        }
        private IAPIConnector GetConnector(string path, bool anonymous)
        {
            var connector = new VersionOneAPIConnector(ApplicationUrl + path, proxyProvider: Proxy);

            if (anonymous)
                return connector;

            connector.WithVersionOneUsernameAndPassword(_username, _password);
            if (_integratedAuth)
                connector.WithWindowsIntegratedAuthentication();

            return connector;

        }
        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;
        }
示例#8
0
        private IAPIConnector GetConnector(string path, bool anonymous)
        {
            var connector = new VersionOneAPIConnector(ApplicationUrl + path, proxyProvider: Proxy);

            if (anonymous)
            {
                return(connector);
            }

            connector.WithVersionOneUsernameAndPassword(_username, _password);
            if (_integratedAuth)
            {
                connector.WithWindowsIntegratedAuthentication();
            }

            return(connector);
        }
		private IAPIConnector MakeConnector(string path, ProxyProvider proxy = null)
		{
			var connector = new VersionOneAPIConnector(path, proxyProvider: proxy);

			if (_storage != null)
				return connector.WithOAuth2(_storage);

			if (_useWindowsIntegratedAuth)
				return connector.WithWindowsIntegratedAuthentication();

			if (!string.IsNullOrWhiteSpace(_credentials.V1UserName))
				return connector
					.WithVersionOneUsernameAndPassword(_credentials.V1UserName, _credentials.V1Password);

			throw new Exception("It was not possible to create a connector with the current authentication configuration.");

		}
示例#10
0
        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();
        }
示例#11
0
        //Verifies the connection to the V1 source instance.
        private static void VerifyV1SourceConnection()
        {
            try
            {
                _sourceMetaConnector = new V1APIConnector(_config.V1SourceConnection.Url + "/meta.v1/");

                if (_config.V1SourceConnection.AuthenticationType == "standard")
                {
                    _sourceDataConnector = new V1APIConnector(_config.V1SourceConnection.Url + "/rest-1.v1/", _config.V1SourceConnection.Username, _config.V1SourceConnection.Password, false);
                    _sourceImageConnector = new V1APIConnector(_config.V1SourceConnection.Url + "/attachment.img/", _config.V1SourceConnection.Username, _config.V1SourceConnection.Password, false);
                }
                else if (_config.V1SourceConnection.AuthenticationType == "windows")
                {
                    _logger.Info("Connecting with user {0}.", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                    _sourceDataConnector = new V1APIConnector(_config.V1SourceConnection.Url + "/rest-1.v1/", null, null, true);
                    _sourceImageConnector = new V1APIConnector(_config.V1SourceConnection.Url + "/attachment.img/", null, null, true);
                    VersionOneAPIConnector v1connec = new VersionOneAPIConnector(_config.V1SourceConnection.Url);
                }
                else if (_config.V1SourceConnection.AuthenticationType == "oauth")
                {
                    throw new Exception("OAuth authentication is not supported -- yet.");
                }
                else
                {
                    throw new Exception("Unable to determine the V1SourceConnection authentication type in the config file. Value used must be standard|windows|oauth.");
                }

                _sourceMetaAPI = new MetaModel(_sourceMetaConnector);
                _sourceDataAPI = new Services(_sourceMetaAPI, _sourceDataConnector);

                IAssetType assetType = _sourceMetaAPI.GetAssetType("Member");
                Query query = new Query(assetType);
                IAttributeDefinition nameAttribute = assetType.GetAttributeDefinition("Username");
                query.Selection.Add(nameAttribute);
                FilterTerm idFilter = new FilterTerm(nameAttribute);
                idFilter.Equal(_config.V1SourceConnection.Username);
                query.Filter = idFilter;
                QueryResult result = _sourceDataAPI.Retrieve(query);
                if (result.TotalAvaliable > 0)
                {
                    _logger.Info("-> Connection to V1 source instance \"{0}\" verified.", _config.V1SourceConnection.Url);
                    _logger.Debug("-> V1 source instance version: {0}.", _sourceMetaAPI.Version.ToString());
                    MigrationStats.WriteStat(_sqlConn, "Source API Version", _sourceMetaAPI.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.V1SourceConnection.Url, _config.V1SourceConnection.Username));
                }
            }
            catch (Exception ex)
            {
                _logger.Error("-> Unable to connect to V1 source instance \"{0}\".", _config.V1SourceConnection.Url);
                throw ex;
            }
        }
		private VersionOneAPIConnector PopulateHeaders(VersionOneAPIConnector connector)
		{
            IDictionary<string, string> dict = connector.CustomHttpHeaders;
            foreach(KeyValuePair<string, string> pair in _customHttpHeaders) {
                dict.Add(pair.Key, pair.Value);
            }
            return connector;
        }
		private VersionOneAPIConnector PrepareConnector(string url)
		{
			var connector = new VersionOneAPIConnector(url, proxyProvider: _proxyProvider);

			if (_integratedAuth)
				connector.WithWindowsIntegratedAuthentication();
			else
				connector.WithVersionOneUsernameAndPassword(_username, _password);

            return PopulateHeaders(connector);
        }
		private VersionOneAPIConnector GetConnector(string path, bool anonymous)
		{
			var connector = new VersionOneAPIConnector(ApplicationUrl + path, proxyProvider: Proxy);

			if (anonymous)
				return connector;

			if (_integratedAuth)
				return connector.WithWindowsIntegratedAuthentication();

			if (!string.IsNullOrWhiteSpace(_username))
				return connector.WithVersionOneUsernameAndPassword(_username, _password);

			throw new Exception("It was not possible to create a connector with the current authentication configuration.");
		}
        public static bool UploadAttachment(string filename, string jiraId, string rawfile)
        {
            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);
            var attachmentconnector =
                                new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/attachment.img/")
                    .WithVersionOneUsernameAndPassword(config.V1Connection.Username, config.V1Connection.Password);

            MetaModel metaModel = new MetaModel(metaconnector);
            Services services = new Services(metaModel, dataconnector);
            Attachments attachments = new Attachments(attachmentconnector);

            string mimeType = MimeType.Resolve(filename);

            string assetoid = GetAssetOid(jiraId, "PrimaryWorkitem");
            if (String.IsNullOrEmpty(assetoid))
            {
                assetoid = GetAssetOid(jiraId, "Task");
            }
            if (String.IsNullOrEmpty(assetoid)) return false;

            Oid attachmentContext = Oid.FromToken(assetoid, metaModel);

            IAssetType attachmentType = metaModel.GetAssetType("Attachment");
            IAttributeDefinition attachmentNameDef = attachmentType.GetAttributeDefinition("Name");
            IAttributeDefinition attachmentContentDef = attachmentType.GetAttributeDefinition("Content");
            IAttributeDefinition attachmentContentTypeDef = attachmentType.GetAttributeDefinition("ContentType");
            IAttributeDefinition attachmentFileNameDef = attachmentType.GetAttributeDefinition("Filename");

            Asset newAttachment = services.New(attachmentType, attachmentContext);
            newAttachment.SetAttributeValue(attachmentNameDef, "Imported from Jira");
            newAttachment.SetAttributeValue(attachmentContentDef, string.Empty);
            newAttachment.SetAttributeValue(attachmentContentTypeDef, mimeType);
            newAttachment.SetAttributeValue(attachmentFileNameDef, rawfile);
            services.Save(newAttachment);

            //Setup and attach the payload
            string attachmentKey = newAttachment.Oid.Key.ToString();
            int buffersize = 4096;

            using (FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                using (Stream output = attachments.GetWriteStream(attachmentKey))
                {
                    byte[] buffer = new byte[buffersize];
                    for (; ; )
                    {
                        int read = input.Read(buffer, 0, buffersize);
                        if (read == 0)
                            break;
                        output.Write(buffer, 0, read);
                    }
                }
            }
            attachments.SetWriteStream(attachmentKey, mimeType);
            return true;
        }