//
        // Creates the patch file containing the files we want to review
        //
        private static string CreateReviewPatch(string[] reviewContent, string rootPath, Logging logger)
        {
            // Now we need to generate a patch file for all the files we do want to include
            StringBuilder filesToDiff = new StringBuilder("diff ");

            foreach (string thisFile in reviewContent)
            {
                filesToDiff.Append('"').Append(thisFile).Append("\" ");
            }

            // Let people know what we're running
            logger.Log("Generating patch");
            logger.Log(" * svn {0}", filesToDiff.ToString());

            // Generate the diff result
            Process.Output diffOutput = Process.Start(rootPath, "svn", filesToDiff.ToString());
            if (string.IsNullOrWhiteSpace(diffOutput.StdErr) == false)
            {
                MessageBox.Show("Unable to generate review patch file\n\n" + diffOutput.StdErr, "Error when raising review", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            // Pipe out the diff to the root directory
            string patchFileName = rootPath + "\\rbi_patch_" + Guid.NewGuid() + ValidPatchExtensions[0];

            File.WriteAllText(patchFileName, diffOutput.StdOut);

            // Return the path file
            return(patchFileName);
        }
Exemplo n.º 2
0
        //
        // Returns a list of review groups from the given server
        //
        public static ReviewGroup[] GetReviewGroups(string workingDirectory, string server, string username, string password, Logging logger)
        {
            // Build up the command
            string commandOptions = string.Format(@"api-get --server {0} /groups/", server);
            string rbtPath        = RB_Tools.Shared.Targets.Reviewboard.Path();

            // Update process
            logger.Log("Getting review groups");
            logger.Log("* Calling {0} {1}", rbtPath, commandOptions);

            // Run the process
            Process.Output output = Process.Start(workingDirectory, rbtPath, commandOptions);

            // Throw to return the error
            if (string.IsNullOrWhiteSpace(output.StdErr) == false)
            {
                logger.Log("Error encountered when getting review groups\n\n{0}", output.StdErr);
                throw new ApplicationException(output.StdErr);
            }

            // Get the list we'll add out review groups to
            var returnedGroups = new[] { new { display_name = "", name = "" } }.ToList();

            // Parse the results of the operation
            JObject        parsedJson = JObject.Parse(output.StdOut);
            IList <JToken> results    = parsedJson["groups"].Children().ToList();

            // Pull out our groups and put them in our list
            foreach (JToken result in results)
            {
                // Add it to the list
                var anonObject   = returnedGroups.ElementAt(0);
                var searchResult = JsonConvert.DeserializeAnonymousType(result.ToString(), anonObject);

                returnedGroups.Add(searchResult);
                logger.Log(" * Found group - {0} : {1}", searchResult.display_name, searchResult.name);
            }

            // Remove the dummy entry
            returnedGroups.RemoveAt(0);

            // Build up the list to return
            ReviewGroup[] groups = new ReviewGroup[returnedGroups.Count];
            for (int i = 0; i < groups.Length; ++i)
            {
                var thisGroup = returnedGroups.ElementAt(i);
                groups[i] = new ReviewGroup(thisGroup.display_name, thisGroup.name);
            }

            // Return the list of groups
            logger.Log(" * Found {0} groups", groups.Length);
            return(groups);
        }
Exemplo n.º 3
0
        //
        // Runs an API request
        //
        public static RequestApiResult RequestApi(Simple credentials, string apiRequest, string workingDirectory)
        {
            // Build up the RBT request
            string rbtPath = Path();
            string rbtArgs = string.Format(@"api-get --username {0} --password {1} --server {2} {3}", credentials.User, credentials.Password, Names.Url[(int)Names.Type.Reviewboard], apiRequest);

            Process.Output output = Process.Start(workingDirectory, rbtPath, rbtArgs);
            if (string.IsNullOrEmpty(output.StdErr) == false)
            {
                return(new RequestApiResult(Result.Error, null, output.StdErr));
            }

            // Parse the data
            try
            {
                JObject parsedOutput = JObject.Parse(output.StdOut);

                // Good or bad?
                string result = (string)parsedOutput["stat"];
                if (result.Equals("fail", StringComparison.InvariantCultureIgnoreCase) == true)
                {
                    // This was a bad response, so pull out the message
                    string errorCode     = (string)parsedOutput["err"]["code"];
                    string errorMessage  = (string)parsedOutput["err"]["msg"];
                    string returnMessage = string.Format("Error Code: {0}\n{1}", errorCode, errorMessage);

                    // Do we have a valid code?
                    int  resultCode  = int.Parse(errorCode);
                    bool knownResult = Enum.IsDefined(typeof(Result), resultCode);
                    if (knownResult == true)
                    {
                        return(new RequestApiResult((Result)resultCode, null, returnMessage));
                    }

                    // Return a general error
                    return(new RequestApiResult(Result.Error, null, returnMessage));
                }
                else
                {
                    // We had a valid response so return it
                    return(new RequestApiResult(Result.Success, parsedOutput, string.Empty));
                }
            }
            catch
            {
                return(new RequestApiResult(Result.Error, null, @"Unable to parse the Reviewboard API result"));
            }
        }
Exemplo n.º 4
0
        //
        // Raises a new review
        //
        public static ReviewRequestResult RequestReview(string workingDirectory, string server, string username, string password, Review.Review.Properties reviewProperties, Logging logger)
        {
            // We may not need to generate a review
            if (reviewProperties.ReviewLevel != RB_Tools.Shared.Review.Properties.Level.FullReview)
            {
                // No review needed so exit
                logger.Log("Ignoring review request as a full review was not requested");
                return(new ReviewRequestResult(string.Empty, string.Empty, reviewProperties));
            }

            // Read out the description and testing into a temp file
            string descriptionFile = GetFileWithContents(reviewProperties.Description, reviewProperties.Summary);
            string testingFile     = GetFileWithContents(reviewProperties.Testing, null);

            // Build up the review command
            string commandProperties = string.Format("--server {0} --summary \"{1}\" ", server, reviewProperties.Summary);

            if (string.IsNullOrWhiteSpace(descriptionFile) == false)
            {
                commandProperties += string.Format("--description-file \"{0}\" ", descriptionFile);
            }
            if (string.IsNullOrWhiteSpace(testingFile) == false)
            {
                commandProperties += string.Format("--testing-done-file \"{0}\" ", testingFile);
            }
            if (string.IsNullOrWhiteSpace(reviewProperties.JiraId) == false)
            {
                string formattedJiraIds = BuildJiraLinks(reviewProperties.JiraId);
                commandProperties += string.Format("--bugs-closed \"{0}\" ", formattedJiraIds);
            }

            // Options
            commandProperties += string.Format("--svn-show-copies-as-adds={0} ", reviewProperties.CopiesAsAdds == true ? "y":"n");

            // Build up the review groups
            if (reviewProperties.Groups.Count != 0)
            {
                string reviewGroups = reviewProperties.Groups[0];
                for (int i = 1; i < reviewProperties.Groups.Count; ++i)
                {
                    reviewGroups += "," + reviewProperties.Groups[i];
                }

                // Add the command
                commandProperties += string.Format("--target-groups \"{0}\" ", reviewGroups);
            }

            // Get the name of the branch we're on
            string branchUrl = Svn.GetBranch(workingDirectory);

            if (string.IsNullOrWhiteSpace(branchUrl) == false)
            {
                commandProperties += string.Format("--branch \"{0}\" ", branchUrl);
            }

            // Update an existing review?
            if (string.IsNullOrWhiteSpace(reviewProperties.ReviewId) == false)
            {
                commandProperties += string.Format("--review-request-id \"{0}\" ", reviewProperties.ReviewId);
            }

            // Pass through the patch file we generated or used
            commandProperties += string.Format("--diff-filename \"{0}\" ", reviewProperties.Contents.Patch);

            // Build up the command
            string commandOptions = string.Format(@"post {0}", commandProperties);
            string rbtPath        = RB_Tools.Shared.Targets.Reviewboard.Path();

            logger.Log("Requesting review");
            logger.Log("* Calling {0} {1}", rbtPath, commandOptions);

            // Run the process
            Process.Output output = Process.Start(workingDirectory, rbtPath, commandOptions);

            // Throw to return the error
            if (string.IsNullOrWhiteSpace(output.StdErr) == false)
            {
                logger.Log("Error raised when requesting review - {0}", output.StdErr);
                throw new ApplicationException(output.StdErr);
            }

            // Break up the results
            string[] outputLines = output.StdOut.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            // Pull out the link to the review (use the diff)
            string reviewUrl = string.Empty;

            if (outputLines.Length >= 3)
            {
                reviewUrl = outputLines[2];
            }

            // Review URL needs to reference the reviewboard server
            string reviewError = null;

            if (reviewUrl.ToLower().Contains(server.ToLower()) == false)
            {
                reviewError = output.StdOut;
            }

            // Lose the temp files if we have them
            CleanUpTemporaryFiles(descriptionFile, testingFile, logger);

            // Return the results
            return(new ReviewRequestResult(reviewUrl, reviewError, reviewProperties));
        }