in sonar-plugin-server/src/main/java/jetbrains/buildserver/sonarplugin/buildfeatures/BranchesAndPullRequestsBuildStartContextProcessor.java [38:88]
public void updateParameters(BuildStartContext context) {
if (context.getBuild().getBuildFeaturesOfType(BranchesAndPullRequestsBuildFeature.BUILD_FEATURE_TYPE).isEmpty()
|| context.getSharedParameters().containsKey(SQS_SYSENV)) {
// Currently only GitHub is supported => if feature defined, it is for GitHub
// If sysenv already defined (outside this processor), skip process.
return;
}
if (!isTeamCityMinimalVersion(getServerVersionInfo())) {
context.getBuild().getBuildLog().message(
String.format("Build feature '%s' requires TeamCity 2019.2 or above", BranchesAndPullRequestsBuildFeature.BUILD_FEATURE_NAME),
Status.ERROR, MessageAttrs.attrs());
return;
}
String branchIsDefault = context.getSharedParameters().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 = context.getSharedParameters().get("teamcity.pullRequest.number");
if (StringUtil.isEmpty(prNumber)) {
// Branch
type = "branch";
final String vcsBranch = context.getSharedParameters().get("vcsroot.branch");
json.addProperty("sonar.branch.name", context.getSharedParameters().get("teamcity.build.branch"));
json.addProperty("sonar.branch.target", vcsBranch.substring(vcsBranch.indexOf("refs/heads/") + 11));
} else {
// Pull Request
type = "pull-request";
String repo = context.getSharedParameters().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", context.getSharedParameters().get("teamcity.pullRequest.title"));
json.addProperty("sonar.pullrequest.base", context.getSharedParameters().get("teamcity.pullRequest.target.branch"));
json.addProperty("sonar.pullrequest.provider", "github");
json.addProperty("sonar.pullrequest.github.repository", repo);
}
final String jsonString = json.toString();
context.getBuild().getBuildLog().message(String.format("SonarQube plugin detects %s, '%s' set with '%s'", type, SQS_SYSENV, jsonString),
Status.NORMAL, MessageAttrs.attrs());
context.addSharedParameter(SQS_SYSENV, jsonString);
}