use of jetbrains.buildServer.users.SUser in project spring-bot by finos.
the class SymphonyNotificator method sendNotification.
private void sendNotification(String project, String build, String statusText, String statusColor, Set<SUser> users, Build bt) {
MessagesApi messages;
String jsonString;
String template;
Config config = c.getConfig();
try {
String details = bt.getLogMessages(0, Integer.MAX_VALUE).stream().filter(m -> !m.contains("errorreport")).filter(m -> m.contains("error")).map(a -> HtmlUtils.htmlEscape(a)).reduce("", (a, b) -> a + "<br/>" + b);
String url = sBuildServer.getRootUrl() + "/project.html?projectId=" + URIUtil.encodeQuery(bt.getProjectId());
BuildData bd = new BuildData(project, build, statusText, statusColor, url, details);
EntityJson json = new EntityJson();
json.put("teamcity", bd);
jsonString = c.getObjectMapper().writeValueAsString(json);
log.warn("JSON: \n" + jsonString);
} catch (Exception e1) {
log.error("Couldn't format string ", e1);
return;
}
try {
messages = c.getAPI(MessagesApi.class);
} catch (Exception e) {
log.error("Couldn't aquire symphony API ", e);
return;
}
if (StringUtils.hasText(config.getTemplate())) {
template = config.getTemplate();
log.warn("Using custom symphony template");
} else {
template = asString(rl.getResource("classpath:/template.ftl"));
log.warn("Using built-in symphony template");
}
for (SUser sUser : users) {
String streamId = sUser.getPropertyValue(symphonyStreamId);
log.warn("Sending notification to Symphony on " + streamId);
if (StringUtils.hasText(streamId)) {
try {
messages.v4StreamSidMessageCreatePost(null, streamId, template, jsonString, null, null, null, null);
} catch (Exception e) {
log.error("Couldn't send message to symphony ", e);
}
}
}
}
use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.
the class BuildType method createNewBuildTypeFromPosted.
@NotNull
public BuildTypeOrTemplate createNewBuildTypeFromPosted(@NotNull final ServiceLocator serviceLocator) {
SProject project;
SubmitedParameters submittedParams = mySubmitted.get();
if (submittedParams.project == null) {
if (submittedParams.projectId == null) {
throw new BadRequestException("Build type creation request should contain project node.");
}
// noinspection ConstantConditions
project = serviceLocator.findSingletonService(ProjectManager.class).findProjectByExternalId(submittedParams.projectId);
if (project == null) {
throw new BadRequestException("Cannot find project with id '" + submittedParams.projectId + "'.");
}
} else {
// noinspection ConstantConditions
project = submittedParams.project.getProjectFromPosted(serviceLocator.findSingletonService(ProjectFinder.class));
}
if (StringUtil.isEmpty(submittedParams.name)) {
throw new BadRequestException("When creating a build type, non empty name should be provided.");
}
final BuildTypeOrTemplate resultingBuildType = createEmptyBuildTypeOrTemplate(serviceLocator, project, submittedParams.name);
try {
fillBuildTypeOrTemplate(new BuildTypeOrTemplatePatcher() {
@NotNull
public BuildTypeOrTemplate getBuildTypeOrTemplate() {
return resultingBuildType;
}
}, serviceLocator);
} catch (Exception e) {
// error on filling the build type, should not preserve the created empty build type
AuthorityHolder authorityHolder = myBeanContext.getSingletonService(SecurityContext.class).getAuthorityHolder();
resultingBuildType.remove((SUser) authorityHolder.getAssociatedUser(), resultingBuildType.isBuildType() ? "Removing broken build configuration" : "Removing broken template");
throw e;
}
return resultingBuildType;
}
use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.
the class Build method getFieldValue.
@Nullable
public static String getFieldValue(@NotNull final BuildPromotion buildPromotion, @Nullable final String field, @NotNull final BeanContext beanContext) {
final Build build = new Build(buildPromotion, Fields.ALL, beanContext);
if ("number".equals(field)) {
// supporting number here in addition to BuildRequest as this method is used from other requests classes as well
return build.getNumber();
} else if ("status".equals(field)) {
return build.getStatus();
} else if ("statusText".equals(field)) {
return build.getStatusText();
} else if ("id".equals(field)) {
return String.valueOf(build.getId());
} else if ("state".equals(field)) {
return build.getState().toString();
} else if ("failedToStart".equals(field)) {
return String.valueOf(build.isFailedToStart());
} else if ("startEstimateDate".equals(field)) {
return build.getStartEstimate();
} else if (FINISH_ESTIMATE.equals(field)) {
return build.getFinishEstimate();
} else if ("percentageComplete".equals(field)) {
return String.valueOf(build.getPercentageComplete());
} else if ("personal".equals(field)) {
return String.valueOf(build.isPersonal());
} else if ("usedByOtherBuilds".equals(field)) {
return String.valueOf(build.isUsedByOtherBuilds());
} else if ("queuedDate".equals(field)) {
return build.getQueuedDate();
} else if ("startDate".equals(field)) {
return build.getStartDate();
} else if ("finishDate".equals(field)) {
return build.getFinishDate();
} else if ("buildTypeId".equals(field)) {
return build.getBuildTypeId();
} else if ("buildTypeInternalId".equals(field)) {
return buildPromotion.getBuildTypeId();
} else if ("branchName".equals(field)) {
return build.getBranchName();
} else if ("branch".equals(field)) {
Branch branch = buildPromotion.getBranch();
return branch == null ? "" : branch.getName();
} else if ("defaultBranch".equals(field)) {
return String.valueOf(build.getDefaultBranch());
} else if ("unspecifiedBranch".equals(field)) {
return String.valueOf(build.getUnspecifiedBranch());
} else if (PROMOTION_ID.equals(field) || "promotionId".equals(field)) {
// Experimental support only
return (String.valueOf(build.getPromotionId()));
} else if ("modificationId".equals(field)) {
// Experimental support only
return String.valueOf(buildPromotion.getLastModificationId());
} else if ("chainModificationId".equals(field)) {
// Experimental support only
return String.valueOf(((BuildPromotionEx) buildPromotion).getChainModificationId());
} else if ("commentText".equals(field)) {
// Experimental support only
final Comment comment = build.getComment();
return comment == null ? null : comment.text;
} else if ("collectChangesError".equals(field)) {
// Experimental support only
return ((BuildPromotionEx) buildPromotion).getCollectChangesError();
} else if ("changesCollectingInProgress".equals(field)) {
// Experimental support only
return String.valueOf(((BuildPromotionEx) buildPromotion).isChangesCollectingInProgress());
} else if ("changeCollectingNeeded".equals(field)) {
// Experimental support only
return String.valueOf(((BuildPromotionEx) buildPromotion).isChangeCollectingNeeded());
} else if ("revision".equals(field)) {
// Experimental support only
final List<BuildRevision> revisions = buildPromotion.getRevisions();
return revisions.size() != 1 ? String.valueOf(revisions.get(0).getRevision()) : null;
} else if ("settingsHash".equals(field)) {
// Experimental support only to get settings digest
return new String(Hex.encodeHex(((BuildPromotionEx) buildPromotion).getSettingsDigest(false)));
} else if ("currentSettingsHash".equals(field)) {
// Experimental support only to get settings digest
return new String(Hex.encodeHex(((BuildPromotionEx) buildPromotion).getBuildSettings().getDigest()));
}
final SBuild associatedBuild = buildPromotion.getAssociatedBuild();
final SQueuedBuild queuedBuild = buildPromotion.getQueuedBuild();
if ("triggeredBy.username".equals(field)) {
// Experimental support only
if (associatedBuild != null) {
final SUser user = associatedBuild.getTriggeredBy().getUser();
return user == null ? null : user.getUsername();
}
if (queuedBuild != null) {
final SUser user = queuedBuild.getTriggeredBy().getUser();
return user == null ? null : user.getUsername();
}
return null;
} else if ("triggeredBy.raw".equals(field)) {
// Experimental support only
if (associatedBuild != null) {
return associatedBuild.getTriggeredBy().getRawTriggeredBy();
}
if (queuedBuild != null) {
return queuedBuild.getTriggeredBy().getRawTriggeredBy();
}
return null;
}
throw new NotFoundException("Field '" + field + "' is not supported. Supported are: number, status, statusText, id, startDate, finishDate, buildTypeId, branchName.");
}
use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.
the class ProjectFinderTest method testUserSelectedDimension.
@Test
public void testUserSelectedDimension() throws Exception {
myFixture.getServerSettings().setPerProjectPermissionsEnabled(true);
ProjectEx root = myProjectManager.getRootProject();
final SProject project10 = createProject("p10", "project 10");
final SProject project20 = createProject("p20", "project 20");
final SProject project10_10 = project10.createProject("p10_10", "p10 child1");
final SProject project10_20 = project10.createProject("p10_20", "xxx");
final SProject project10_10_10 = project10_10.createProject("p10_10_10", "xxx");
final SProject project10_10_20 = project10_10.createProject("p10_10_20", "p10_10 child2");
final SProject project10_10_30 = project10_10.createProject("p10_10_30", "p10_10 child3");
final SProject project30 = createProject(project10.getProjectId(), "project 30");
final SProject project40 = createProject("p40", "project 40");
final SUser user2 = createUser("user2");
user2.addRole(RoleScope.projectScope(project10.getProjectId()), getProjectViewerRole());
// default sorting is hierarchy-based + name-based within the same level
check("selectedByUser:(username:user2)", project10, project10_10, project10_10_20, project10_10_30, project10_10_10, project10_20);
check("selectedByUser:(user:(username:user2),mode:selected_and_unknown)", project10, project10_10, project10_10_20, project10_10_30, project10_10_10, project10_20);
check("selectedByUser:(user:(username:user2),mode:all_with_order)", root, project10, project10_10, project10_10_20, project10_10_30, project10_10_10, project10_20);
check("selectedByUser:(user:(username:user2),mode:selected)");
final SUser user1 = createUser("user1");
user1.addRole(RoleScope.projectScope(project10.getProjectId()), getProjectViewerRole());
user1.addRole(RoleScope.projectScope(project20.getProjectId()), getProjectViewerRole());
user1.addRole(RoleScope.projectScope(project30.getProjectId()), getProjectViewerRole());
user1.setVisibleProjects(Arrays.asList(project10.getProjectId(), project10_10_20.getProjectId(), project10_10_10.getProjectId(), project40.getProjectId(), project30.getProjectId()));
user1.setProjectsOrder(Arrays.asList(project10.getProjectId(), project10_10_20.getProjectId(), project10_10_10.getProjectId(), project40.getProjectId(), project30.getProjectId()));
check("selectedByUser:(username:user1)", project10, project10_10_20, project10_10_10, project30);
check("selectedByUser:(user:(username:user1))", project10, project10_10_20, project10_10_10, project30);
check("selectedByUser:(user:(username:user1),mode:selected_and_unknown)", project10, project10_10_20, project10_10_10, project30);
check("selectedByUser:(user:(username:user1),mode:all_with_order)", root, project10, project10_10, project10_10_20, project10_10_10, project10_10_30, project10_20, project30, project20);
check("selectedByUser:(user:(username:user1),mode:selected)", project10, project10_10_20, project10_10_10, project30);
check("selectedByUser:(username:user1),project:(id:_Root)", project10, project30);
check("selectedByUser:(user:(username:user1),mode:selected_and_unknown),project:(id:_Root)", project10, project30);
check("selectedByUser:(user:(username:user1),mode:all_with_order),project:(id:_Root)", project10, project30, project20);
check("selectedByUser:(user:(username:user1),mode:selected),project:(id:_Root)", project10, project30);
check("selectedByUser:(username:user1),project:(id:p10)");
check("selectedByUser:(user:(username:user1),mode:selected_and_unknown),project:(id:p10)");
check("selectedByUser:(user:(username:user1),mode:all_with_order),project:(id:p10)", project10_10, project10_20);
check("selectedByUser:(user:(username:user1),mode:selected),project:(id:p10)");
user1.setVisibleProjects(Arrays.asList(project30.getProjectId(), project10_10_20.getProjectId(), project10_10_10.getProjectId()));
user1.setProjectsOrder(Arrays.asList(project30.getProjectId(), project10_10_20.getProjectId(), project10_10_10.getProjectId()));
check("selectedByUser:(username:user1)", project30, project10_10_20, project10_10_10);
check("selectedByUser:(username:user1),project:(id:_Root)", project30);
checkExceptionOnItemsSearch(LocatorProcessException.class, "selectedByUser:(username:user2,mode:selected)");
checkExceptionOnItemsSearch(BadRequestException.class, "selectedByUser:(user:(username:user2),mode:aaa)");
checkExceptionOnItemsSearch(LocatorProcessException.class, "selectedByUser:(user:(username:user2),mode:aaa,ccc:ddd)");
checkExceptionOnItemsSearch(LocatorProcessException.class, "selectedByUser:(user:(username:user2,aaa:bbb))");
// add checks after ProjectEx.setOwnProjectsOrder / setOwnBuildTypesOrder
}
use of jetbrains.buildServer.users.SUser in project teamcity-rest by JetBrains.
the class UserFinderTest method testCurrentUserClash.
@Test
public void testCurrentUserClash() throws Throwable {
final SUser user1 = createUser("current");
final SUser user2 = createUser("user1");
{
checkExceptionOnItemSearch(NotFoundException.class, "current");
}
SecurityContextImpl securityContext = myFixture.getSecurityContext();
securityContext.runAs(user1, new SecurityContextEx.RunAsAction() {
@Override
public void run() throws Throwable {
SUser result = myUserFinder.getItem("current");
assertNotNull(result);
assertEquals(user1.getId(), result.getId());
}
});
securityContext.runAs(user2, new SecurityContextEx.RunAsAction() {
@Override
public void run() throws Throwable {
SUser result = myUserFinder.getItem("current");
assertNotNull(result);
assertEquals(user2.getId(), result.getId());
}
});
securityContext.runAsSystem(new SecurityContextEx.RunAsAction() {
@Override
public void run() throws Throwable {
checkExceptionOnItemSearch(NotFoundException.class, "current");
}
});
}
Aggregations