Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to limit or exclude vcs file list whilst building payload #150

Merged
merged 1 commit into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jetbrains.buildServer.serverSide.SProject;
import jetbrains.buildServer.serverSide.SQueuedBuild;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.serverSide.TeamCityProperties;
import jetbrains.buildServer.vcs.SVcsModification;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -37,6 +38,9 @@
import webhook.teamcity.payload.variableresolver.VariableResolver;

public class WebHookPayloadContent {
private static final String MAX_CHANGE_FILE_LIST_SIZE = "maxChangeFileListSize";
private static final String WEBHOOK_MAX_CHANGE_FILE_LIST_SIZE = "webhook." + MAX_CHANGE_FILE_LIST_SIZE;

String buildStatus,
buildResult, buildResultPrevious, buildResultDelta,
notifyType,
Expand Down Expand Up @@ -81,6 +85,9 @@ public class WebHookPayloadContent {
List<String> buildTags;
ExtraParametersMap extraParameters;
private ExtraParametersMap teamcityProperties;
@Getter private int maxChangeFileListSize = 100;
@Getter private boolean maxChangeFileListCountExceeded = false;
@Getter private int changeFileListCount = 0;
private List<WebHooksChanges> changes = new ArrayList<>();
private WebHookResponsibility responsibilityInfo;
private String pinEventUsername;
Expand Down Expand Up @@ -142,7 +149,7 @@ public WebHookPayloadContent(VariableResolverFactory variableResolverFactory, SB
this.extraParameters = new ExtraParametersMap(extraParameters);
this.teamcityProperties = new ExtraParametersMap(teamcityProperties);
populateCommonContent(variableResolverFactory, server, sBuild, null, buildState, customTemplates);
populateMessageAndText(sBuild, buildState, customTemplates);
populateMessageAndText(sBuild, buildState);
populateArtifacts(sBuild);
if (username != null) {
this.pinEventUsername = username;
Expand Down Expand Up @@ -172,7 +179,7 @@ public WebHookPayloadContent(VariableResolverFactory variableResolverFactory, SB
this.extraParameters = new ExtraParametersMap(extraParameters);
this.teamcityProperties = new ExtraParametersMap(teamcityProperties);
populateCommonContent(variableResolverFactory, server, sRunningBuild, previousBuild, buildState, customTemplates);
populateMessageAndText(sRunningBuild, buildState, customTemplates);
populateMessageAndText(sRunningBuild, buildState);
populateArtifacts(sRunningBuild);
}

Expand Down Expand Up @@ -216,15 +223,15 @@ private void populateCommonContent(VariableResolverFactory variableResolverFacto
} catch (Exception e) {}

if (responsibilityHolder.getSBuildType() != null) {
SBuildType buildType = responsibilityHolder.getSBuildType();
setBuildRunners(buildType.getBuildRunners());
setBuildFullName(buildType.getFullName());
setBuildName(buildType.getName());
setBuildTypeId(TeamCityIdResolver.getBuildTypeId(buildType));
setBuildInternalTypeId(TeamCityIdResolver.getInternalBuildId(buildType));
setBuildExternalTypeId(TeamCityIdResolver.getExternalBuildId(buildType));
setBuildStatusUrl(getRootUrl() + "viewLog.html?buildTypeId=" + buildType.getBuildTypeId() + "&buildId=lastFinished");
setMessage("Build " + buildType.getFullName()
SBuildType sBuildType = responsibilityHolder.getSBuildType();
setBuildRunners(sBuildType.getBuildRunners());
setBuildFullName(sBuildType.getFullName());
setBuildName(sBuildType.getName());
setBuildTypeId(TeamCityIdResolver.getBuildTypeId(sBuildType));
setBuildInternalTypeId(TeamCityIdResolver.getInternalBuildId(sBuildType));
setBuildExternalTypeId(TeamCityIdResolver.getExternalBuildId(sBuildType));
setBuildStatusUrl(getRootUrl() + "viewLog.html?buildTypeId=" + sBuildType.getBuildTypeId() + "&buildId=lastFinished");
setMessage("Build " + sBuildType.getFullName()
+ " has changed responsibility from "
+ oldUser
+ " to "
Expand All @@ -233,7 +240,7 @@ private void populateCommonContent(VariableResolverFactory variableResolverFacto
+ getComment().trim()
+ "'"
);
setText(buildType.getFullName()
setText(sBuildType.getFullName()
+ " changed responsibility from "
+ oldUser
+ " to "
Expand Down Expand Up @@ -292,14 +299,14 @@ private void populateCommonContent(VariableResolverFactory variableResolverFacto
}

private void populateMessageAndText(SBuild sRunningBuild,
BuildStateEnum state, Map<String,String> templates) {
BuildStateEnum state) {
// Message is a long form message, for on webpages or in email.
setMessage("Build " + sRunningBuild.getBuildType().getFullName()
+ " has " + state.getDescriptionSuffix() + ". This is build number " + sRunningBuild.getBuildNumber()
+ ", has a status of \"" + this.buildResult + "\" and was triggered by " + sRunningBuild.getTriggeredBy().getAsString());

// Text is designed to be shorter, for use in Text messages and the like.
setText(sRunningBuild.getBuildType().getFullName().toString()
setText(sRunningBuild.getBuildType().getFullName()
+ " has " + state.getDescriptionSuffix() + ". Status: " + this.buildResult);
}

Expand Down Expand Up @@ -361,7 +368,12 @@ private void populateCommonContent(VariableResolverFactory variableResolverFacto
setTriggeredBy(sBuild.getTriggeredBy().getAsString());
setComment(WebHooksComment.build(sBuild.getBuildComment()));
setTags(sBuild.getTags());
setChanges(sBuild.getContainingChanges());
int fileChangeCount = 0;
for (SVcsModification mod : sBuild.getContainingChanges()) {
fileChangeCount += mod.getChangeCount();
}
this.changeFileListCount = fileChangeCount;
setChanges(sBuild.getContainingChanges(), includeVcsFileList());
try {
if (sBuild.getBranch() != null){
setBranch(sBuild.getBranch());
Expand All @@ -378,10 +390,46 @@ private void populateCommonContent(VariableResolverFactory variableResolverFacto
setRootUrl(StringUtils.stripTrailingSlash(server.getRootUrl()) + "/");
setBuildStatusUrl(getRootUrl() + "viewLog.html?buildTypeId=" + getBuildTypeId() + "&buildId=" + getBuildId());
setBuildStateDescription(buildState.getDescriptionSuffix());
setBuildStatusHtml(variableResolverFactory, buildState, templates.get(WebHookPayloadDefaultTemplates.HTML_BUILDSTATUS_TEMPLATE));
setBuildStatusHtml(variableResolverFactory, templates.get(WebHookPayloadDefaultTemplates.HTML_BUILDSTATUS_TEMPLATE));
setBuildIsPersonal(sBuild.isPersonal());
}

private boolean includeVcsFileList() {
// Firstly update the "maxChangeFileListSize" value from webhook config or build parameters.
if (extraParameters.containsKey(MAX_CHANGE_FILE_LIST_SIZE)) {
try {
this.maxChangeFileListSize = Integer.parseInt(extraParameters.get(MAX_CHANGE_FILE_LIST_SIZE));
} catch (NumberFormatException ex) {
Loggers.SERVER.info("WebHookPayloadContent : Unable to convert 'maxChangeFileListSize' value to a valid integer. Defaut value will be used '" + this.maxChangeFileListSize + "'");
}
} else if (teamcityProperties.containsKey(WEBHOOK_MAX_CHANGE_FILE_LIST_SIZE)) {
try {
this.maxChangeFileListSize = Integer.parseInt(teamcityProperties.get(WEBHOOK_MAX_CHANGE_FILE_LIST_SIZE));
} catch (NumberFormatException ex) {
Loggers.SERVER.info("WebHookPayloadContent : Unable to convert 'webhook.maxChangeFileListSize' value to a valid integer. Defaut value will be used '" + this.maxChangeFileListSize + "'");
}
} else if (Objects.nonNull(TeamCityProperties.getPropertyOrNull(WEBHOOK_MAX_CHANGE_FILE_LIST_SIZE))){
try {
this.maxChangeFileListSize = Integer.parseInt(TeamCityProperties.getProperty(WEBHOOK_MAX_CHANGE_FILE_LIST_SIZE));
} catch (NumberFormatException ex) {
Loggers.SERVER.info("WebHookPayloadContent : Unable to convert TeamCity global property 'webhook.maxChangeFileListSize' value to a valid integer. Defaut value will be used '" + this.maxChangeFileListSize + "'");
}
}

// If the value is negative, checking is disabled and maxChangeFileListSize is effectively unlimited.
if (this.maxChangeFileListSize < 0) {
this.maxChangeFileListCountExceeded = false;
return true;

// Or calculate that the count we found is less then the preferred one.
} else if (this.changeFileListCount > this.maxChangeFileListSize) {
this.maxChangeFileListCountExceeded = true;
return false;
}

return true;
}

public List<String> getBuildTags() {
return buildTags;
}
Expand All @@ -402,8 +450,8 @@ private void setComment(WebHooksComment webHooksComment) {
}
}

private void setChanges(List<SVcsModification> modifications){
this.changes = WebHooksChangeBuilder.build(modifications);
private void setChanges(List<SVcsModification> modifications, boolean includeVcsFileModifications){
this.changes = WebHooksChangeBuilder.build(modifications, includeVcsFileModifications);
}

public List<WebHooksChanges> getChanges(){
Expand Down Expand Up @@ -721,7 +769,7 @@ public void setBuildStatusHtml(String buildStatusHtml) {
}


private void setBuildStatusHtml(VariableResolverFactory variableResolverFactory, BuildStateEnum buildState, final String htmlStatusTemplate) {
private void setBuildStatusHtml(VariableResolverFactory variableResolverFactory, final String htmlStatusTemplate) {

VariableMessageBuilder builder = variableResolverFactory.createVariableMessageBuilder(
htmlStatusTemplate,
Expand Down Expand Up @@ -804,7 +852,7 @@ public String getResponsibilityUserNew() {
}

public Map<String, ExtraParametersMap> getAllParameters(){
Map<String, ExtraParametersMap> allParameters = new LinkedHashMap<String, ExtraParametersMap>();
Map<String, ExtraParametersMap> allParameters = new LinkedHashMap<>();

allParameters.put("teamcity", this.teamcityProperties);
allParameters.put("webhook", this.extraParameters);
Expand All @@ -819,9 +867,6 @@ public ExtraParametersMap getExtraParameters(VariableResolverFactory variableRes
VariableResolver resolver = variableResolverFactory.buildVariableResolver(new SimpleSerialiser(), this, getAllParameters());
ExtraParametersMap resolvedParametersMap = new ExtraParametersMap(extraParameters);

// ExtraParametersMap resolvedParametersMap = new ExtraParametersMap(this.teamcityProperties);
// resolvedParametersMap.putAll(extraParameters);

for (Entry<String,String> entry : extraParameters.getEntriesAsSet()){
builder = variableResolverFactory.createVariableMessageBuilder(entry.getValue(), resolver);
resolvedParametersMap.put(entry.getKey(), builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.Nullable;

import jetbrains.buildServer.vcs.SVcsModification;
import jetbrains.buildServer.vcs.VcsFileModification;
import jetbrains.buildServer.vcs.VcsRootInstance;
import org.jetbrains.annotations.Nullable;


public class WebHooksChange {

Expand All @@ -28,19 +28,24 @@ public class WebHooksChange {
return vcsRoot.getName();
}

public static WebHooksChange build(SVcsModification modification) {


public static WebHooksChange build(SVcsModification modification, boolean includeVcsFileModifications) {
WebHooksChange change = new WebHooksChange();
change.setComment(modification.getDescription());
change.setUsername(modification.getUserName());
change.setVcsRoot(tryGetVcsRootName(modification));
for (VcsFileModification fileModification: modification.getChanges()){
change.files.add(fileModification.getRelativeFileName());
if (includeVcsFileModifications) {
change.files = new ArrayList<>();
for (VcsFileModification fileModification: modification.getChanges()){
change.files.add(fileModification.getRelativeFileName());
}
}
return change;
}


private List<String> files = new ArrayList<>();
private List<String> files;
private String comment;
private String username;
private String vcsRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
public class WebHooksChangeBuilder{
private WebHooksChangeBuilder(){}

public static List<WebHooksChanges> build (List<SVcsModification> mods){
public static List<WebHooksChanges> build (List<SVcsModification> mods, boolean includeVcsFileModifications){
List<WebHooksChanges> changes = new ArrayList<>();

for (SVcsModification modification: mods){
changes.add(new WebHooksChanges(modification.getDisplayVersion(), WebHooksChange.build(modification)));
changes.add(new WebHooksChanges(modification.getDisplayVersion(), WebHooksChange.build(modification, includeVcsFileModifications)));
}
return changes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class WebHookPayloadJson extends WebHookPayloadGeneric implements WebHookPayload {

public static final String FORMAT_SHORT_NAME = "json";
public static final String FORMAT_SHORT_NAME = "json";
Integer rank = 100;
String charset = "UTF-8";

Expand All @@ -36,7 +36,7 @@ public String getFormatDescription() {
}

public String getFormatShortName() {
return FORMAT_SHORT_NAME;
return FORMAT_SHORT_NAME;
}

public String getFormatToolTipText() {
Expand All @@ -52,6 +52,7 @@ protected String getStatusAsString(WebHookPayloadContent content,WebHookTemplate
xstream.setMode(XStream.NO_REFERENCES);
xstream.registerConverter(new ExtraParametersMapToJsonConvertor());
xstream.registerConverter(new UserSingleValueConverter());
xstream.autodetectAnnotations(true);
xstream.alias("build", WebHookPayloadContent.class);
/* For some reason, the items are coming back as "@name" and "@value"
* so strip those out with a regex.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class WebHookPayloadXml extends WebHookPayloadGeneric {

public static final String FORMAT_SHORT_NAME = "xml";
public static final String FORMAT_SHORT_NAME = "xml";
private Integer rank = 100;

public WebHookPayloadXml(WebHookPayloadManager wpm, WebHookVariableResolverManager variableResolverManager) {
Expand All @@ -37,7 +37,7 @@ public String getFormatDescription() {
}

public String getFormatShortName() {
return FORMAT_SHORT_NAME;
return FORMAT_SHORT_NAME;
}

public String getFormatToolTipText() {
Expand All @@ -61,6 +61,7 @@ protected String getStatusAsString(WebHookPayloadContent content, WebHookTemplat
XStream xstream = new XStream();
xstream.setMode(XStream.NO_REFERENCES);
xstream.registerConverter(new ExtraParametersMapToXmlConvertor());
xstream.autodetectAnnotations(true);
xstream.alias("build", WebHookPayloadContent.class);
return xstream.toXML(content);
}
Expand Down
Loading