Пример #1
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;
        }