GetComments() public method

public GetComments ( NGitLab.Models.MergeRequest mergeRequest ) : IEnumerable
mergeRequest NGitLab.Models.MergeRequest
return IEnumerable
Example #1
0
        static void ForceSyncBuild(GitLabWrapper gitLabWrapper, MergeRequest mergeRequest, Project targetProject, MergeRequestHookClient hook) {
            var xmlComments = gitLabWrapper.GetComments(mergeRequest).Where(x => IsXml(x.Note));
            var options = xmlComments.Select(x => MergeRequestOptions.ConvertFromString(x.Note)).FirstOrDefault();
            if (options != null && options.ActionType == MergeRequestActionType.sync) {
                var action = (MergeRequestSyncAction)options.Action;
                if (action.PerformTesting) {
                    Log.Message("Check build status before force build.");
                    var commit = gitLabWrapper.GetMergeRequestCommits(mergeRequest).FirstOrDefault();
                    var build = commit != null ? gitLabWrapper.GetBuilds(mergeRequest, commit.Id).FirstOrDefault() : null;
                    var buildStatus = build?.Status ?? BuildStatus.undefined;
                    Log.Message($"Build status = {buildStatus}.");
                    if (buildStatus == BuildStatus.success)
                        ForceBuild(action.SyncTask);
                    return;
                }
                Log.Message("Build forces without checking tests status.");
                ForceBuild(action.SyncTask);
                return;
            }
            string task = FarmIntegrator.FindTask($"{mergeRequest.TargetBranch}@{targetProject.PathWithNamespace}");
            if (!string.IsNullOrEmpty(task)) {
                Log.Message($"Sync task {task} found by heuristic.");
                ForceBuild(task);
                return;
            }

            Log.Message("Merge request can`t be merged because merge request notes has no farm config.");
            Log.Message("");
        }
Example #2
0
        static int DoPatchWork(PatchOptions clo) {
            string localGitDir = clo.LocalFolder != null && Path.IsPathRooted(clo.LocalFolder) ? clo.LocalFolder : Path.Combine(Environment.CurrentDirectory, clo.LocalFolder ?? repoPath);

            string targetRepoPath = GetSimpleGitHttpPath(clo.Repo);

            if (string.IsNullOrEmpty(targetRepoPath)) {
                Log.Error($"Can`t parse repo path {clo.Repo}");
                return 1;
            }
            string sourceRepoPath = GetSimpleGitHttpPath(clo.SourceRepo);
            if (string.IsNullOrEmpty(sourceRepoPath)) {
                Log.Error($"Can`t parse source repo path {clo.SourceRepo}");
                return 1;
            }

            string username = clo.Login;
            string password = clo.Password;
            string gitlabauthtoken = clo.AuthToken;
            string targetBranchName = clo.Branch;
            string trackerPath = clo.Tracker;
            string gitServer = clo.Server;
            string sourceBranchName = clo.SourceBranch;
            string patchdir = clo.PatchDir ?? localGitDir;

            DXVcsWrapper vcsWrapper = new DXVcsWrapper(vcsServer, username, password);

            TrackBranch trackBranch = FindBranch(targetBranchName, trackerPath, vcsWrapper);
            if (trackBranch == null) {
                return 1;
            }

            string historyPath = GetVcsSyncHistory(vcsWrapper, trackBranch.HistoryPath);
            if (historyPath == null)
                return 1;
            SyncHistory history = SyncHistory.Deserialize(historyPath);
            if (history == null)
                return 1;

            SyncHistoryWrapper syncHistory = new SyncHistoryWrapper(history, vcsWrapper, trackBranch.HistoryPath, historyPath);
            var head = syncHistory.GetHistoryHead();
            if (head == null)
                return 1;

            GitLabWrapper gitLabWrapper = new GitLabWrapper(gitServer, gitlabauthtoken);

            Project targetProject = gitLabWrapper.FindProject(targetRepoPath);
            if (targetProject == null) {
                Log.Error($"Can`t find target project {targetRepoPath}.");
                return 1;
            }
            Log.Message($"Target project url: {targetProject.HttpUrl}");

            Branch targetBranch = gitLabWrapper.GetBranch(targetProject, targetBranchName);
            if (targetBranch == null) {
                Log.Error($"Can`t find targetBranch branch {targetBranchName}");
                return 1;
            }
            Log.Message($"Target branch name: {targetBranch.Name}");

            var sourceProject = gitLabWrapper.FindProjectFromAll(sourceRepoPath);
            if (sourceProject == null) {
                Log.Error($"Can`t find source project {sourceRepoPath}");
                return 1;
            }
            Log.Message($"Source project url: {sourceProject.HttpUrl}");

            var sourceBranch = gitLabWrapper.GetBranch(sourceProject, sourceBranchName);
            if (sourceBranch == null) {
                Log.Error($"Source branch {sourceBranchName} was not found.");
                return 1;
            }
            Log.Message($"Target branch name: {sourceBranch.Name}");

            MergeRequest mergeRequest = gitLabWrapper.GetMergeRequests(targetProject, 
                x => x.SourceBranch == sourceBranchName && x.TargetBranch == targetBranchName && x.SourceProjectId == sourceProject.Id).FirstOrDefault();
            if (mergeRequest == null) {
                Log.Error($"Can`t find merge request.");
                return 1;
            }
            Log.Message($"Merge request id: {mergeRequest.Id}.");
            Log.Message($"Merge request title: {mergeRequest.Title}.");

            var comments = gitLabWrapper.GetComments(mergeRequest);
            var mergeRequestSyncOptions = comments?.Where(x => IsXml(x.Note)).Where(x => {
                var mr = MergeRequestOptions.ConvertFromString(x.Note);
                return mr?.ActionType == MergeRequestActionType.sync;
            }).Select(x => (MergeRequestSyncAction)MergeRequestOptions.ConvertFromString(x.Note).Action).LastOrDefault();

            if (mergeRequest.Assignee?.Name != username || (!mergeRequestSyncOptions?.PerformTesting ?? false)) {
                Log.Error($"Merge request is not assigned to service user {username} or doesn`t require testing.");
                return 1;
            }

            GitWrapper gitWrapper = CreateGitWrapper(sourceRepoPath, localGitDir, sourceBranchName, username, password);
            if (gitWrapper == null) {
                Log.Error("Can`t create git wrapper.");
                return 1;
            }

            var changes = gitLabWrapper
                .GetMergeRequestChanges(mergeRequest)
                .Where(x => trackBranch.TrackItems.FirstOrDefault(track => CheckItemForChangeSet(x, track)) != null)
                .Select(x => new PatchItem() {
                    SyncAction = CalcSyncAction(x),
                    OldPath = x.OldPath,
                    NewPath = x.NewPath,
                    OldVcsPath = CalcVcsPath(trackBranch, x.OldPath),
                    NewVcsPath = CalcVcsPath(trackBranch, x.NewPath),
                }).ToList();

            var patch = new PatchInfo() { TimeStamp = DateTime.Now.Ticks, Items = changes };

            var patchPath = Path.Combine(patchdir, patchZip);
            using (Package zip = Package.Open(patchPath, FileMode.Create)) {
                SavePatchInfo(patchdir, patch);
                AddPart(zip, patchdir, "patch.info");
                foreach (var path in CalcFilesForPatch(patch)) {
                    AddPart(zip, localGitDir, path);
                }
            }

            Log.Message($"Patch.info generated at {patchPath}");
            return 0;
        }
Example #3
0
        static void ProcessBuildHook(GitLabWrapper gitLabWrapper, string serviceUser, BuildHookClient hook, bool supportSendingMessages, string farmTaskName) {
            Log.Message($"Build hook title: {hook.BuildName}");
            Log.Message($"Build hook status: {hook.Status}");

            if (supportSendingMessages)
                SendMessage(serviceUser, hook.Json, farmTaskName);

            if (hook.Status == BuildStatus.success) {
                Project project = gitLabWrapper.GetProject(hook.ProjectId);
                if (project == null) {
                    Log.Message($"Can`t find project {hook.ProjectName}.");
                    return;
                }
                Log.Message($"Project: {project.PathWithNamespace}");
                var mergeRequest = CalcMergeRequest(gitLabWrapper, hook, project);
                if (mergeRequest == null) {
                    Log.Message("Can`t find merge request.");
                    return;
                }
                Log.Message($"Merge request: id = {mergeRequest.Id} title = {mergeRequest.Title}");
                Log.Message($"Merge request state = {mergeRequest.State}");
                if (mergeRequest.State == "opened" || mergeRequest.State == "reopened") {
                    var latestCommit = gitLabWrapper.GetMergeRequestCommits(mergeRequest).FirstOrDefault();
                    if (latestCommit == null) {
                        Log.Message("Wrong merge request found.");
                        return;
                    }
                    Log.Message($"Merge request latest commit sha = {latestCommit.Id}");
                    if (!latestCommit.Id.Equals(new Sha1(hook.Commit.Id))) {
                        Log.Message($"Additional commits has been added {hook.Commit.Id}");
                        return;
                    }

                    var xmlComments = gitLabWrapper.GetComments(mergeRequest).Where(x => IsXml(x.Note));
                    var options = xmlComments.Select(x => MergeRequestOptions.ConvertFromString(x.Note)).FirstOrDefault();
                    if (options?.ActionType == MergeRequestActionType.sync) {
                        Log.Message("Sync options found.");
                        var syncOptions = (MergeRequestSyncAction)options.Action;
                        Log.Message($"Sync options perform testing is {syncOptions.PerformTesting}");
                        Log.Message($"Sync options assign to service is {syncOptions.AssignToSyncService}");
                        Log.Message($"Sync options sync task is {syncOptions.SyncTask}");
                        Log.Message($"Sync options sync service is {syncOptions.SyncService}");
                        if (syncOptions.PerformTesting && syncOptions.AssignToSyncService) {
                            gitLabWrapper.UpdateMergeRequestAssignee(mergeRequest, syncOptions.SyncService);
                            ForceBuild(syncOptions.SyncTask);
                        }
                        return;
                    }
                    Log.Message("Sync options not found.");
                }
            }
        }
Example #4
0
        static int DoProcessTestResultsWork(ProcessTestsOptions clo) {
            Log.Message("process test results.");
            string targetRepoPath = GetSimpleGitHttpPath(clo.Repo);

            if (string.IsNullOrEmpty(targetRepoPath)) {
                Log.Error($"Can`t parse repo path {clo.Repo}");
                return 1;
            }

            string sourceRepoPath = GetSimpleGitHttpPath(clo.SourceRepo);
            if (string.IsNullOrEmpty(sourceRepoPath)) {
                Log.Error($"Can`t parse source repo path {clo.SourceRepo}");
                return 1;
            }

            string gitlabauthtoken = clo.AuthToken;
            string targetBranchName = clo.Branch;
            string gitServer = clo.Server;
            string sourceBranchName = clo.SourceBranch;

            GitLabWrapper gitLabWrapper = new GitLabWrapper(gitServer, gitlabauthtoken);

            Project targetProject = gitLabWrapper.FindProject(targetRepoPath);
            if (targetProject == null) {
                Log.Error($"Can`t find target project {targetRepoPath}.");
                return 1;
            }
            Branch targetBranch = gitLabWrapper.GetBranch(targetProject, targetBranchName);
            if (targetBranch == null) {
                Log.Error($"Can`t find targetBranch branch {targetBranchName}");
                return 1;
            }

            var sourceProject = gitLabWrapper.FindProjectFromAll(sourceRepoPath);
            if (sourceProject == null) {
                Log.Error($"Can`t find source project {sourceRepoPath}");
                return 1;
            }

            var sourceBranch = gitLabWrapper.GetBranch(sourceProject, sourceBranchName);
            if (sourceBranch == null) {
                Log.Error($"Source branch {sourceBranchName} was not found.");
                return 1;
            }

            MergeRequest mergeRequest = gitLabWrapper.GetMergeRequests(targetProject, x => x.SourceBranch == sourceBranchName && x.TargetBranch == targetBranchName).FirstOrDefault();
            if (mergeRequest == null) {
                Log.Error($"Can`t find merge request.");
                return 1;
            }

            var comments = gitLabWrapper.GetComments(mergeRequest);
            var mergeRequestSyncOptions = comments?.Where(x => IsXml(x.Note)).Where(x => {
                var mr = MergeRequestOptions.ConvertFromString(x.Note);
                return mr?.ActionType == MergeRequestActionType.sync;
            }).Select(x => (MergeRequestSyncAction)MergeRequestOptions.ConvertFromString(x.Note).Action).LastOrDefault();

            if (mergeRequestSyncOptions == null) {
                Log.Message("Merge request sync options not found. Nothing to do.");
                return 0;
            }

            if (!mergeRequestSyncOptions.PerformTesting) {
                Log.Message("Testing is disabled in config.");
                return 0;
            }

            Log.Message("Testing is enabled in config.");
            var commit = gitLabWrapper.GetMergeRequestCommits(mergeRequest).FirstOrDefault();
            if (commit == null) {
                Log.Message("Merge request has no commits.");
                return 0;
            }

            var mergeRequestBuild = gitLabWrapper.GetBuilds(mergeRequest, commit.Id).FirstOrDefault();
            if (mergeRequestBuild == null) {
                Log.Message("Merge request has no build.");
                return 0;
            }

            Log.Message($"Build has {mergeRequestBuild.Status} status.");

            if (mergeRequestBuild.Status == BuildStatus.success) {
                if (mergeRequestSyncOptions.AssignToSyncService) {
                    gitLabWrapper.UpdateMergeRequestAssignee(mergeRequest, mergeRequestSyncOptions.SyncTask);
                    Log.Message($"Success tests. Assigning to sync service {mergeRequestSyncOptions.SyncTask}");
                    return 0;
                }
                else {
                    gitLabWrapper.UpdateMergeRequestAssignee(mergeRequest, mergeRequest.Author.Username);
                    Log.Message($"Success tests. Assigning to back to author {mergeRequest.Author.Username}");
                    return 0;
                }
            }
            if (mergeRequestBuild.Status == BuildStatus.failed) {
                Log.Message($"Failed tests.");
                return 0;
            }
            Log.Message("Nothing happens.");
            return 0;
        }