예제 #1
0
        public static async Task DoWork(FpCommandSettings settings, Parameters parameters, Jira jira)
        {
            var issueKey = JiraHelper.GetIssueKey(parameters.Get("Enter Jira ticket: "));
            var newLabel = parameters.Get("Label (1 - AlreadySolved, 2 - DuplicatedTicket, 3 - IndependentModules, 4 - IncorrectFinding, 5 - UnsolvableWithinCCProcess, 6 - AutoGeneratedCode): ");

            var duplicates = string.Empty;

            switch (newLabel)
            {
            case "1":
                newLabel = "AlreadySolved";
                break;

            case "2":
                newLabel   = "DuplicatedTicket";
                duplicates = parameters.Get("Enter duplicated ticket: ");
                JiraHelper.CheckUrl(duplicates);
                break;

            case "3":
                newLabel = "IndependentModules";
                break;

            case "4":
                newLabel = "IncorrectFinding";
                break;

            case "5":
                newLabel = "UnsolvableWithinCCProcess";
                break;

            case "6":
                newLabel = "AutoGeneratedCode";
                break;
            }

            Console.Write("Get ticket... ");
            var issue = await jira.Issues.GetIssueAsync(issueKey);

            Console.WriteLine("done");

            Console.Write("Move to FP... ");
            if (issue.Status.ToString().Equals("In progress", StringComparison.InvariantCultureIgnoreCase))
            {
                await issue.WorkflowTransitionAsync("To Do");
            }
            else if (issue.Status.ToString().Equals("Ready to refactor", StringComparison.InvariantCultureIgnoreCase))
            {
                await issue.WorkflowTransitionAsync("To Do");
            }
            else if (!issue.Status.ToString().Equals("To Do", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine($"Unknown status: {issue.Status}");
                return;
            }

            await issue.WorkflowTransitionAsync("False Positive");

            Console.WriteLine("done");

            Console.Write("Add label... ");
            issue.Labels.Add(newLabel);
            await issue.SaveChangesAsync();

            Console.WriteLine("done");

            if (newLabel == "DuplicatedTicket")
            {
                Console.Write("Leave comment... ");
                await issue.AddCommentAsync($"Duplicates {duplicates}");

                Console.WriteLine("done");
            }
        }
예제 #2
0
        public static async Task DoWork(PrCommandSettings settings, CommonSettings commonSettings, Parameters parameters, Jira jira)
        {
            var ticketUrl = parameters.Get("Enter Jira ticket: ");
            var issueKey  = JiraHelper.GetIssueKey(ticketUrl);

            var comment = GetComment(settings, parameters);

            var noteForReviewer = parameters.Get("Note for reviewer: ");

            if (settings.NotesAliases.ContainsKey(noteForReviewer))
            {
                noteForReviewer = settings.NotesAliases[noteForReviewer];
            }

            bool isOnlyPr = false;

            if (parameters.Any())
            {
                var str = parameters.Get(string.Empty);
                if (str == "--onlyPr")
                {
                    isOnlyPr = true;
                }
                else
                {
                    throw new CcException($"Unknown argument: {str}");
                }
            }

            Console.Write("Get ticket... ");
            var issue = await jira.Issues.GetIssueAsync(issueKey);

            var repoUrl = issue.GetScmUrl();

            if (string.IsNullOrWhiteSpace(repoUrl))
            {
                Console.WriteLine("Can't get repo name");
                return;
            }

            Console.WriteLine("done");

            var repoName = repoUrl.Split('/')[4];

            if (repoName.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
            {
                repoName = Path.GetFileNameWithoutExtension(repoName);
            }

            var repoSettings = SettingsHelper.GetRepoSettings(commonSettings, repoName);
            var branchPrefix = string.IsNullOrWhiteSpace(repoSettings?.BranchPrefix)
                ? "feature"
                : repoSettings.BranchPrefix;

            var mainBranch = SettingsHelper.GetMainBranch(repoSettings);

            var key        = issue.Key.ToString();
            var ticketType = GetTicketType(issue);

            if (!isOnlyPr)
            {
                var stagedFiles  = GitHelper.Exec("git diff --name-only --cached", repoSettings, commonSettings.ProjectsPath);
                var changedFiles = GitHelper.Exec("git diff --name-only", repoSettings, commonSettings.ProjectsPath);

                if (!stagedFiles.Any() && !changedFiles.Any())
                {
                    throw new CcException("No changed files found");
                }

                var filesForCheck = stagedFiles.Any() ? stagedFiles : changedFiles;

                if (filesForCheck.Any(f => f.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)))
                {
                    ConsoleHelper.WriteColor("You are going to commit changes in sln-file. Do you really want to do it? [y/N]: ", ConsoleColor.Yellow);
                    var answer = Console.ReadLine() ?? string.Empty;

                    if (!answer.Equals("y", StringComparison.OrdinalIgnoreCase))
                    {
                        return;
                    }
                }

                var currentBranch = GitHelper.GetCurrentBranch(repoSettings, commonSettings.ProjectsPath);

                if (currentBranch != mainBranch)
                {
                    ConsoleHelper.WriteColor(
                        $"Your current branch is {currentBranch}, but usually it should be {mainBranch}. Do you really want to create new branch from this one? [y/N]: ",
                        ConsoleColor.Yellow);
                    var answer = Console.ReadLine() ?? string.Empty;

                    if (!answer.Equals("y", StringComparison.OrdinalIgnoreCase))
                    {
                        return;
                    }
                }

                Console.Write("Set assignee... ");

                if (issue.Assignee == null)
                {
                    issue.Assignee = commonSettings.JiraUserName;
                    await issue.SaveChangesAsync();

                    Console.WriteLine("done");
                }
                else
                {
                    if (issue.Assignee != commonSettings.JiraUserName)
                    {
                        throw new CcException($"Ticket is already assigned to \"{issue.Assignee}\"!");
                    }
                    else
                    {
                        Console.WriteLine("already assigned");
                    }
                }

                Console.Write("Move ticket to \"In progress\"... ");
                if (issue.Status.ToString().Equals("To Do", StringComparison.InvariantCultureIgnoreCase))
                {
                    await issue.WorkflowTransitionAsync("Not False Positive");
                }

                if (issue.Status.ToString().Equals("Writing Test Code", StringComparison.InvariantCultureIgnoreCase))
                {
                    await issue.WorkflowTransitionAsync("Test coverage available");
                }

                if (issue.Status.ToString().Equals("Ready to refactor", StringComparison.InvariantCultureIgnoreCase))
                {
                    await issue.WorkflowTransitionAsync("Start progress");
                }

                Console.WriteLine("done");

                Console.Write("Push changes... ");
                var createBranchCommand = $"git checkout -b {branchPrefix}/{key}";
                var commitOption        = stagedFiles.Any() ? string.Empty : "a";
                var commitCommand       = $"git commit -{commitOption}m  \"{key}: Fixes {ticketType}\"";
                var pushCommand         = $"git push --set-upstream origin {branchPrefix}/{key}";
                GitHelper.Exec(
                    $"{createBranchCommand} && {commitCommand} && {pushCommand}",
                    repoSettings,
                    commonSettings.ProjectsPath);

                Console.WriteLine("done");

                Console.Write($"Checkout {mainBranch}... ");
                try
                {
                    GitHelper.Exec($"git checkout {mainBranch} && git pull", repoSettings, commonSettings.ProjectsPath);
                    Console.WriteLine("done");
                }
                catch (GitException)
                {
                    ConsoleHelper.WriteLineColor("failed", ConsoleColor.Yellow);
                }

                Console.Write("Waiting... ");
                Thread.Sleep(TimeSpan.FromSeconds(3));
                Console.WriteLine("done");
            }

            Console.Write("Create PR... ");
            var query    = @"query { repository(owner:""trilogy-group"", name:""" + repoName + @"""){ id }}";
            var repoData = await GithubHelper.Query(query, commonSettings.GithubToken);

            var repoId = repoData["repository"]["id"].Value <string>();

            var fullNote = string.IsNullOrWhiteSpace(noteForReviewer)
                ? string.Empty
                : $@"# Note for Reviewers\r\n- {noteForReviewer}\r\n\r\n";

            var(testCoverageMessage, coverageWarning) = GetTestCoverageMessage(issue);

            var mutation = @"mutation
                {
                  createPullRequest(input:{
                    title:""[" + key + "] " + issue.Summary.Replace("\"", "\\\"") + @""", 
                    baseRefName: """ + mainBranch + @""", 
                    body: ""# Links\r\nhttps://jira.devfactory.com/browse/" + key + @"\r\n\r\n# Changes\r\n- " + comment + @"\r\n\r\n" + fullNote + testCoverageMessage + @""", 
                    headRefName: """ + branchPrefix + "/" + key + @""", 
                    repositoryId: """ + repoId + @"""
                  }){
                    pullRequest{
                      id,
                      url
                    }
                  }
                }";

            var resultJson = await GithubHelper.Query(mutation, commonSettings.GithubToken);

            var jPullRequest = resultJson["createPullRequest"]["pullRequest"];
            var prUrl        = jPullRequest["url"].Value <string>();

            Console.WriteLine("done");

            if (coverageWarning)
            {
                Console.WriteLine("Please don't forget to fill test coverage manually");
            }

            if (settings.AssignPr)
            {
                Console.Write("Assign PR... ");

                var prId = jPullRequest["id"].Value <string>();

                var currentUserIdJson = await GithubHelper.Query("query {viewer {id}}", commonSettings.GithubToken);

                var userId = currentUserIdJson["viewer"]["id"].Value <string>();

                var assignPrMutation = @"mutation
                    {
                      addAssigneesToAssignable(input: {
                        assignableId: """ + prId + @""", 
                        assigneeIds: [""" + userId + @"""]}) { clientMutationId }}";

                await GithubHelper.Query(assignPrMutation, commonSettings.GithubToken);

                Console.WriteLine("done");
            }

            Console.Write("Write PR url to ticket... ");
            const string codeReviewUrl = "Code Review Ticket URL";

            if (issue.CustomFields.Any(f => f.Name == codeReviewUrl))
            {
                issue.CustomFields[codeReviewUrl].Values = new [] { prUrl };
            }
            else
            {
                issue.CustomFields.Add(codeReviewUrl, prUrl);
            }

            issue.SaveChanges();
            Console.WriteLine("done");

            Console.WriteLine("New PR url: " + prUrl);
        }
예제 #3
0
        public static async Task DoWork(NewCommandSettings settings, CommonSettings commonSettings, Parameters parameters, Jira jira)
        {
            if (parameters.Any())
            {
                throw new CcException("Command \"new\" doesn't support parameters yet");
            }

            var    shortTicketType = parameters.Get("Enter ticket type: ");
            var    longTicketType  = GetTicketType(shortTicketType);
            string brpQualifier    = string.Empty;

            if (longTicketType == "BRP Issues")
            {
                brpQualifier = parameters.Get("Please specify BRP type [m = Magic Strings; f = Formatting]: ");
                brpQualifier = " - " + GetBrpQualifier(brpQualifier);
            }

            var fileParts = ReadFileParts(longTicketType);

            var sourceTicketUrl = parameters.Get("Enter source Jira ticket: ");
            var ticketKey       = JiraHelper.GetIssueKey(sourceTicketUrl);

            var strLinkTickets = parameters.Get("Link this ticket to the new one? [Y/n]: ");
            var linkTickets    = string.IsNullOrWhiteSpace(strLinkTickets) ||
                                 strLinkTickets.Equals("y", StringComparison.OrdinalIgnoreCase);

            Console.Write("Get ticket... ");
            var issue = await jira.Issues.GetIssueAsync(ticketKey);

            Console.WriteLine("done");

            var scmUrl   = issue.GetScmUrl();
            var repoName = scmUrl.Split('/')[4];

            if (repoName.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
            {
                repoName = Path.GetFileNameWithoutExtension(repoName);
            }

            var repoSettings  = SettingsHelper.GetRepoSettings(commonSettings, repoName);
            var mainBranch    = SettingsHelper.GetMainBranch(repoSettings);
            var currentBranch = GitHelper.GetCurrentBranch(repoSettings, commonSettings.ProjectsPath);

            if (currentBranch != mainBranch)
            {
                ConsoleHelper.WriteColor(
                    $"Your current branch is {currentBranch}, but usually it should be {mainBranch}. Do you really want to create new ticket from this branch? [y/N]: ",
                    ConsoleColor.Yellow);
                var answer = Console.ReadLine() ?? string.Empty;
                if (!answer.Equals("y", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
            }

            Console.Write("Create new issue... ");
            var newIssue = jira.CreateIssue("CC");

            newIssue.Summary  = longTicketType + brpQualifier + " :: " + GetFileName(fileParts.First().FileName, commonSettings.ProjectsPath, repoSettings.ActualFolderName);
            newIssue.Type     = longTicketType;
            newIssue.Assignee = commonSettings.JiraUserName;
            newIssue.Priority = issue.Priority;
            newIssue.Labels.AddRange(issue.Labels);

            const string formattingLabel = "Formatting";

            if (brpQualifier == " - Formatting" && !issue.Labels.Contains(formattingLabel))
            {
                newIssue.Labels.Add(formattingLabel);
            }

            var scmBranch = GetScmBranchField(currentBranch, repoSettings, commonSettings);

            newIssue.CustomFields.Add("scm_branch", scmBranch);

            foreach (var field in settings.FieldsToCopy.Where(f => f != "scm_branch"))
            {
                CopyCustomField(issue, newIssue, field);
            }

            newIssue.Description = GetDescription(mainBranch, fileParts, commonSettings.ProjectsPath, repoSettings);
            await newIssue.SaveChangesAsync();

            Console.WriteLine("done");

            if (linkTickets)
            {
                Console.Write("Add link... ");
                await newIssue.LinkToIssueAsync(issue.Key.ToString(), "Relates");

                Console.WriteLine("done");
            }

            Console.WriteLine($"New issue: https://jira.devfactory.com/browse/{newIssue.Key}");
        }