Пример #1
0
        async internal static Task <bool> ApplyChangesToMergeRequest(
            ProjectKey projectKey, MergeRequest originalMergeRequest, ApplyMergeRequestChangesParameters parameters,
            string oldSpecialNote, string newSpecialNote, User currentUser, Shortcuts shortcuts)
        {
            if (String.IsNullOrEmpty(parameters.Title) ||
                String.IsNullOrEmpty(parameters.TargetBranch) ||
                parameters.AssigneeUserName == null ||
                newSpecialNote == null ||
                currentUser == null)
            {
                // this is unexpected due to UI restrictions, so don't implement detailed logging here
                MessageBox.Show("Invalid parameters for a merge request", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                Trace.TraceError("[MergeRequestEditHelper] Invalid parameters for a merge request");
                return(false);
            }

            string oldAssigneeUsername = originalMergeRequest.Assignee?.Username ?? String.Empty;
            User   assignee            = oldAssigneeUsername == parameters.AssigneeUserName
            ? originalMergeRequest.Assignee
            : await getUserAsync(parameters.AssigneeUserName, shortcuts);

            checkFoundAssignee(projectKey.HostName, parameters.AssigneeUserName, assignee);

            bool            result = false;
            MergeRequestKey mrk    = new MergeRequestKey(projectKey, originalMergeRequest.IId);

            if (oldSpecialNote != newSpecialNote)
            {
                result = await addComment(mrk, currentUser, newSpecialNote, shortcuts);
            }

            bool wasHighPriority = originalMergeRequest.Labels?
                                   .Contains(Common.Constants.Constants.HighPriorityLabel) ?? false;

            bool changed =
                oldAssigneeUsername != parameters.AssigneeUserName ||
                originalMergeRequest.Force_Remove_Source_Branch != parameters.DeleteSourceBranch ||
                originalMergeRequest.Squash != parameters.Squash ||
                originalMergeRequest.Target_Branch != parameters.TargetBranch ||
                originalMergeRequest.Title != parameters.Title ||
                originalMergeRequest.Description != parameters.Description ||
                wasHighPriority != parameters.IsHighPriority;

            if (!changed)
            {
                return(result);
            }

            string[] labels = null;
            if (wasHighPriority && !parameters.IsHighPriority)
            {
                labels = originalMergeRequest.Labels
                         .Where(label => label != Common.Constants.Constants.HighPriorityLabel).ToArray();
            }
            else if (!wasHighPriority && parameters.IsHighPriority)
            {
                labels = originalMergeRequest.Labels
                         .Concat(new string[] { Common.Constants.Constants.HighPriorityLabel }).ToArray();
            }

            int assigneeId = assignee?.Id ?? 0; // 0 means to unassign
            UpdateMergeRequestParameters updateMergeRequestParameters = new UpdateMergeRequestParameters(
                parameters.TargetBranch, parameters.Title, assigneeId, parameters.Description,
                null, parameters.DeleteSourceBranch, parameters.Squash, labels);

            try
            {
                MergeRequest mergeRequest = await shortcuts
                                            .GetMergeRequestEditor(mrk)
                                            .ModifyMergeRequest(updateMergeRequestParameters);
            }
            catch (MergeRequestEditorException ex)
            {
                reportErrorToUser(ex);
                return(result);
            }
            return(true);
        }
Пример #2
0
        async internal static Task <bool> ApplyChangesToMergeRequest(GitLabInstance gitLabInstance,
                                                                     IModificationListener modificationListener,
                                                                     ProjectKey projectKey, MergeRequest originalMergeRequest, ApplyMergeRequestChangesParameters parameters,
                                                                     string oldSpecialNote, string newSpecialNote, User currentUser)
        {
            if (String.IsNullOrEmpty(parameters.Title) ||
                String.IsNullOrEmpty(parameters.TargetBranch) ||
                parameters.AssigneeUserName == null ||
                newSpecialNote == null ||
                currentUser == null)
            {
                // this is unexpected due to UI restrictions, so don't implement detailed logging here
                MessageBox.Show("Invalid parameters for a merge request", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                Trace.TraceError("[MergeRequestEditHelper] Invalid parameters for a merge request");
                return(false);
            }

            string oldAssigneeUsername = originalMergeRequest.Assignee?.Username ?? String.Empty;
            User   assignee            = oldAssigneeUsername == parameters.AssigneeUserName
            ? originalMergeRequest.Assignee
            : await getUserAsync(gitLabInstance, parameters.AssigneeUserName);

            checkFoundAssignee(projectKey.HostName, parameters.AssigneeUserName, assignee);

            bool            result = false;
            MergeRequestKey mrk    = new MergeRequestKey(projectKey, originalMergeRequest.IId);

            if (oldSpecialNote != newSpecialNote)
            {
                result = await addComment(gitLabInstance, modificationListener, mrk, currentUser, newSpecialNote);
            }

            bool changed =
                oldAssigneeUsername != parameters.AssigneeUserName ||
                originalMergeRequest.Force_Remove_Source_Branch != parameters.DeleteSourceBranch ||
                originalMergeRequest.Squash != parameters.Squash ||
                originalMergeRequest.Target_Branch != parameters.TargetBranch ||
                originalMergeRequest.Title != parameters.Title ||
                originalMergeRequest.Description != parameters.Description;

            if (!changed)
            {
                return(result);
            }

            int assigneeId = assignee?.Id ?? 0; // 0 means to unassign
            UpdateMergeRequestParameters updateMergeRequestParameters = new UpdateMergeRequestParameters(
                parameters.TargetBranch, parameters.Title, assigneeId, parameters.Description,
                null, parameters.DeleteSourceBranch, parameters.Squash);

            try
            {
                MergeRequest mergeRequest = await Shortcuts
                                            .GetMergeRequestEditor(gitLabInstance, modificationListener, mrk)
                                            .ModifyMergeRequest(updateMergeRequestParameters);
            }
            catch (MergeRequestEditorException ex)
            {
                reportErrorToUser(ex);
                return(result);
            }
            return(true);
        }