public void fixRunBuildParameters()

in sonar-plugin-server/src/main/java/jetbrains/buildserver/sonarplugin/buildfeatures/BranchesAndPullRequestsParametersPreprocessor.java [25:74]


    public void fixRunBuildParameters(SRunningBuild build, Map<String, String> runParameters, Map<String, String> buildParams) {

        if (build.getBuildFeaturesOfType(BranchesAndPullRequestsBuildFeature.BUILD_FEATURE_TYPE).isEmpty() || buildParams.containsKey(SQS_SYSENV)) {
            // Currently only GitHub is supported => if feature defined, it is for GitHub
            // ParametersPreprocessor is called for each steps (or could be defined manually). So if sysenv already defined, skip process.
            return;
        }

        if (!isTeamCityMinimalVersion(getServerVersionInfo())) {
            build.getBuildLog().message(
                    String.format("Build feature '%s' requiers TeamCity 2019.2 or above", BranchesAndPullRequestsBuildFeature.BUILD_FEATURE_NAME),
                    Status.ERROR, MessageAttrs.attrs());
            return;
        }

        String branchIsDefault = buildParams.get("teamcity.build.branch.is_default");
        if (StringUtil.isEmpty(branchIsDefault) || Boolean.TRUE.equals(Boolean.valueOf(branchIsDefault))) {
            // No information or default branch, nothing to provide
            return;
        }

        final String type;
        final JsonObject json = new JsonObject();
        final String prNumber = buildParams.get("teamcity.pullRequest.number");
        if (StringUtil.isEmpty(prNumber)) {
            // Branch
            type = "branch";
            final String vcsBranch = buildParams.get("vcsroot.branch");
            json.addProperty("sonar.branch.name", buildParams.get("teamcity.build.branch"));
            json.addProperty("sonar.branch.target", vcsBranch.substring(vcsBranch.indexOf("refs/heads/") + 11));
        } else {
            // Pull Request
            type = "pull-request";
            String repo = buildParams.get("vcsroot.url");
            repo = repo.replaceFirst("^git@.*:", "");
            repo = repo.replaceFirst("^https?://[^/]*/", "");
            repo = repo.replaceFirst("\\.git$", "");

            json.addProperty("sonar.pullrequest.key", prNumber);
            json.addProperty("sonar.pullrequest.branch", buildParams.get("teamcity.pullRequest.title"));
            json.addProperty("sonar.pullrequest.base", buildParams.get("teamcity.pullRequest.target.branch"));
            json.addProperty("sonar.pullrequest.provider", "github");
            json.addProperty("sonar.pullrequest.github.repository", repo);
        }

        final String jsonString = json.toString();
        build.getBuildLog().message(String.format("SonarQube plugin detects %s, '%s' set with '%s'", type, SQS_SYSENV, jsonString), Status.NORMAL,
                MessageAttrs.attrs());
        buildParams.put(SQS_SYSENV, jsonString);
    }