private void ImportUsers(StreamWriter streamWriter, SpiraSoapService.SoapServiceClient spiraClient, APIClient testRailApi, int userId) { //Get the users from the TestRail API JArray users = (JArray)testRailApi.SendGet("get_users"); if (users != null) { foreach (JObject user in users) { //Extract the user data int testRailId = user["id"].Value <int>(); string userName = user["email"].Value <string>(); string fullName = user["name"].Value <string>(); string firstName = userName; string lastName = userName; if (fullName.Contains(' ')) { firstName = fullName.Substring(0, fullName.IndexOf(' ')); lastName = fullName.Substring(fullName.IndexOf(' ') + 1); } else { firstName = fullName; lastName = fullName; } string emailAddress = userName; bool isActive = user["is_active"].Value <bool>(); //See if we're importing users or mapping to a single user if (Properties.Settings.Default.Users) { //Default to observer role for all imports, for security reasons RemoteUser remoteUser = new RemoteUser(); remoteUser.FirstName = firstName; remoteUser.LastName = lastName; remoteUser.UserName = userName; remoteUser.EmailAddress = emailAddress; remoteUser.Active = isActive; remoteUser.Approved = true; remoteUser.Admin = false; userId = spiraClient.User_Create(remoteUser, Properties.Settings.Default.NewUserPassword, "What was my default email address?", emailAddress, PROJECT_ROLE_ID).UserId.Value; } //Add the mapping to the hashtable for use later on this.usersMapping.Add(testRailId, userId); } } }
/// <summary> /// This method is responsible for actually importing the data /// </summary> /// <param name="stateInfo">State information handle</param> /// <remarks>This runs in background thread to avoid freezing the progress form</remarks> public void ImportData(object stateInfo) { //First open up the textfile that we will log information to (used for debugging purposes) string debugFile = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\Spira_TestRail_Import.log"; StreamWriter streamWriter = File.CreateText(debugFile); try { streamWriter.WriteLine("Starting import at: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); //Create the mapping hashtables this.testCaseMapping = new Dictionary <int, int>(); this.testSuiteMapping = new Dictionary <int, int>(); this.testSuiteSectionMapping = new Dictionary <int, int>(); this.testStepMapping = new Dictionary <int, int>(); this.usersMapping = new Dictionary <int, int>(); this.testRunStepMapping = new Dictionary <int, int>(); this.testRunStepToTestStepMapping = new Dictionary <int, int>(); this.testSetMapping = new Dictionary <int, int>(); this.testSetTestCaseMapping = new Dictionary <int, int>(); //Connect to Spira streamWriter.WriteLine("Connecting to Spira..."); SpiraSoapService.SoapServiceClient spiraClient = new SpiraSoapService.SoapServiceClient(); //Set the end-point and allow cookies string url = Properties.Settings.Default.SpiraUrl + ImportForm.IMPORT_WEB_SERVICES_URL; spiraClient.Endpoint.Address = new EndpointAddress(url); BasicHttpBinding httpBinding = (BasicHttpBinding)spiraClient.Endpoint.Binding; ConfigureBinding(httpBinding, spiraClient.Endpoint.Address.Uri); bool success = spiraClient.Connection_Authenticate2(Properties.Settings.Default.SpiraUserName, Properties.Settings.Default.SpiraPassword, "TestRailImporter"); if (!success) { string message = "Failed to authenticate with Spira using login: '******' so terminating import!"; streamWriter.WriteLine(message); streamWriter.Close(); //Display the exception message this.progressForm.ProgressForm_OnError(message); return; } //Connect to TestRail APIClient testRailApi = new APIClient(Properties.Settings.Default.TestRailUrl); testRailApi.User = Properties.Settings.Default.TestRailUserName; testRailApi.Password = Properties.Settings.Default.TestRailPassword; //1) Create a new project ImportProject(streamWriter, spiraClient, testRailApi); //2) Get the users and import - if we don't want to import user, map all TestRail users to single SpiraId int userId = -1; string newPassword = Properties.Settings.Default.NewUserPassword; if (!Properties.Settings.Default.Users) { RemoteUser remoteUser = new RemoteUser(); remoteUser.FirstName = "Test"; remoteUser.LastName = "Rail"; remoteUser.UserName = "******"; remoteUser.EmailAddress = "*****@*****.**"; remoteUser.Active = true; remoteUser.Approved = true; remoteUser.Admin = false; userId = spiraClient.User_Create(remoteUser, newPassword, "What was my default email address?", remoteUser.EmailAddress, PROJECT_ROLE_ID).UserId.Value; } ImportUsers(streamWriter, spiraClient, testRailApi, userId); //**** Show that we've imported users **** if (Properties.Settings.Default.Users) { streamWriter.WriteLine("Users Imported"); this.progressForm.ProgressForm_OnProgressUpdate(1); } //3) Get the releases and import if (Properties.Settings.Default.Releases) { ImportReleases(streamWriter, spiraClient, testRailApi); } //**** Show that we've imported releases **** streamWriter.WriteLine("Releases Imported"); this.progressForm.ProgressForm_OnProgressUpdate(2); //3-4) Import the test suites and test cases if (Properties.Settings.Default.TestCases) { ImportTestSuites(streamWriter, spiraClient, testRailApi); //**** Show that we've imported test cases and test steps **** streamWriter.WriteLine("Test Suites Imported"); this.progressForm.ProgressForm_OnProgressUpdate(3); ImportTestCases(streamWriter, spiraClient, testRailApi); //**** Show that we've imported test cases and test steps **** streamWriter.WriteLine("Test Cases and Custom Steps Imported"); this.progressForm.ProgressForm_OnProgressUpdate(4); } //5) Import the test plans (check for case import) if (Properties.Settings.Default.TestCases) { ImportTestPlans(streamWriter, spiraClient, testRailApi); //**** Show that we've imported test cases and test steps **** streamWriter.WriteLine("Test Plans Imported"); this.progressForm.ProgressForm_OnProgressUpdate(5); } //6) Import the test runs (have to have case as well!) if (Properties.Settings.Default.TestCases && Properties.Settings.Default.TestRuns) { ImportTestRuns(streamWriter, spiraClient, testRailApi); //**** Show that we've imported test cases and test steps **** streamWriter.WriteLine("Test Runs Imported"); this.progressForm.ProgressForm_OnProgressUpdate(6); } //**** Mark the form as finished **** streamWriter.WriteLine("Import completed at: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); this.progressForm.ProgressForm_OnFinish(); //Close the debugging file streamWriter.Close(); } catch (Exception exception) { //Log the error streamWriter.WriteLine("*ERROR* Occurred during Import: '" + exception.Message + "' at " + exception.Source + " (" + exception.StackTrace + ")"); streamWriter.Close(); //Display the exception message this.progressForm.ProgressForm_OnError(exception); } }