use of jenkins.model.Jenkins in project promoted-builds-plugin by jenkinsci.
the class PromotedBuildParameterDefinition method getRuns.
/**
* Gets a list of promoted builds for the project.
* @param base Base item for the relative addressing
* @return List of {@link AbstractBuild}s, which have been promoted.
* May return an empty list if {@link Jenkins} instance is not ready
* @since 2.22
*/
@Nonnull
public List<Run<?, ?>> getRuns(@CheckForNull Item base) {
final List<Run<?, ?>> runs = new ArrayList<Run<?, ?>>();
final Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null) {
return runs;
}
// JENKINS-25011: also look for jobs in folders.
final AbstractProject<?, ?> job = ItemPathResolver.getByPath(projectName, base, AbstractProject.class);
if (job == null) {
return runs;
}
PromotedProjectAction promotedProjectAction = job.getAction(PromotedProjectAction.class);
if (promotedProjectAction == null) {
return runs;
}
for (Run<?, ?> run : job.getBuilds()) {
List<PromotedBuildAction> actions = run.getActions(PromotedBuildAction.class);
for (PromotedBuildAction buildAction : actions) {
if (buildAction.contains(promotionProcessName)) {
runs.add(run);
break;
}
}
}
return runs;
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class BlueOceanConfigStatePreloader method getStateJson.
/**
* {@inheritDoc}
*/
@Override
public String getStateJson() {
StringWriter writer = new StringWriter();
Jenkins jenkins = Jenkins.get();
VersionNumber versionNumber = Jenkins.getVersion();
String version = versionNumber != null ? versionNumber.toString() : Jenkins.VERSION;
AuthorizationStrategy authorizationStrategy = jenkins.getAuthorizationStrategy();
boolean allowAnonymousRead = true;
if (authorizationStrategy instanceof FullControlOnceLoggedInAuthorizationStrategy) {
allowAnonymousRead = ((FullControlOnceLoggedInAuthorizationStrategy) authorizationStrategy).isAllowAnonymousRead();
}
String jwtTokenEndpointHostUrl = Jenkins.get().getRootUrl();
JwtTokenServiceEndpoint jwtTokenServiceEndpoint = JwtTokenServiceEndpoint.first();
if (jwtTokenServiceEndpoint != null) {
jwtTokenEndpointHostUrl = jwtTokenServiceEndpoint.getHostUrl();
}
addFeatures(new JSONBuilder(writer).object().key("version").value(getBlueOceanPluginVersion()).key("jenkinsConfig").object().key("analytics").value(Analytics.isAnalyticsEnabled()).key("version").value(version).key("security").object().key("enabled").value(jenkins.isUseSecurity()).key("loginUrl").value(jenkins.getSecurityRealm() == SecurityRealm.NO_AUTHENTICATION ? null : jenkins.getSecurityRealm().getLoginUrl()).key("authorizationStrategy").object().key("allowAnonymousRead").value(allowAnonymousRead).endObject().key("enableJWT").value(BlueOceanConfigProperties.BLUEOCEAN_FEATURE_JWT_AUTHENTICATION).key("jwtServiceHostUrl").value(jwtTokenEndpointHostUrl).endObject().endObject()).endObject();
return writer.toString();
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class PipelineBranchRunStatePreloader method getFetchData.
@Override
protected FetchData getFetchData(@NonNull BlueUrlTokenizer blueUrl) {
//
if (!blueUrl.hasPart(BlueUrlTokenizer.UrlPart.BRANCH) || !blueUrl.hasPart(BlueUrlTokenizer.UrlPart.PIPELINE_RUN_DETAIL_ID)) {
// Not interested in it
return null;
}
Jenkins jenkins = Jenkins.get();
String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
String branchName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.BRANCH);
String runId = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE_RUN_DETAIL_ID);
Item pipelineJobItem = jenkins.getItemByFullName(pipelineFullName);
if (pipelineJobItem instanceof MultiBranchProject) {
try {
MultiBranchProject pipelineMBP = (MultiBranchProject) pipelineJobItem;
Job pipelineBranchJob = pipelineMBP.getItem(branchName);
if (pipelineBranchJob != null) {
Run run = pipelineBranchJob.getBuild(runId);
if (run != null) {
BlueRun blueRun = BlueRunFactory.getRun(run, BluePipelineFactory.resolve(pipelineBranchJob));
if (blueRun != null) {
try {
return new FetchData(blueRun.getLink().getHref(), Export.toJson(blueRun));
} catch (IOException e) {
LOGGER.log(Level.FINE, String.format("Unable to preload run for pipeline '%s'. Run serialization error.", run.getUrl()), e);
return null;
}
} else {
LOGGER.log(Level.FINE, String.format("Unable to find run %s on branch named %s on pipeline named '%s'.", runId, branchName, pipelineFullName));
return null;
}
}
} else {
LOGGER.log(Level.FINE, String.format("Unable to find branch named %s on pipeline named '%s'.", branchName, pipelineFullName));
return null;
}
} catch (Exception e) {
LOGGER.log(Level.FINE, String.format("Unable to find run from pipeline named '%s'.", pipelineFullName), e);
return null;
}
} else {
LOGGER.log(Level.FINE, String.format("Unable to find pipeline named '%s'.", pipelineFullName));
return null;
}
// Don't preload any data on the page.
return null;
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class PipelineNodeTest method sequentialParallelStagesWithPost.
@Test
@Issue("JENKINS-49779")
public void sequentialParallelStagesWithPost() throws Exception {
WorkflowJob p = createWorkflowJobWithJenkinsfile(getClass(), "sequentialParallelWithPost.jenkinsfile");
Slave s = j.createOnlineSlave();
s.setNumExecutors(2);
// Run until completed
j.buildAndAssertSuccess(p);
List<Map> nodes = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/1/nodes/", List.class);
assertEquals(9, nodes.size());
Optional<Map> thirdSeqStage = nodes.stream().filter(map -> map.get("displayName").equals("third-sequential-stage")).findFirst();
assertTrue(thirdSeqStage.isPresent());
List<Map> steps = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/1/nodes/" + thirdSeqStage.get().get("id") + "/steps/", List.class);
assertEquals(2, steps.size());
assertEquals("echo 'dummy text third-sequential-stage'", steps.get(0).get("displayDescription"));
assertEquals("echo 'dummy text post multiple-stages'", steps.get(1).get("displayDescription"));
}
use of jenkins.model.Jenkins in project blueocean-plugin by jenkinsci.
the class UserImplPermissionTest method setup.
@Before
public void setup() throws IOException {
testOrganization = new TestOrganization("org", "orgDisplayName");
user = mock(User.class);
when(user.getId()).thenReturn("some_user");
authentication = new Authentication() {
public String getName() {
return "some_user";
}
public GrantedAuthority[] getAuthorities() {
return null;
}
public Object getCredentials() {
return null;
}
public Object getDetails() {
return null;
}
public Object getPrincipal() {
return null;
}
public boolean isAuthenticated() {
return false;
}
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
}
};
jenkins = mock(Jenkins.class);
when(jenkins.getACL()).thenReturn(new ACL() {
public boolean hasPermission(Authentication a, Permission permission) {
return false;
}
});
mockStatic(Jenkins.class);
when(Jenkins.getAuthentication()).thenReturn(authentication);
when(Jenkins.get()).thenReturn(jenkins);
try {
// After Jenkins 2.77 hasPermission is no longer in Node.class and is not final so we need to mock it
// prior to it is called as being final and mocking it will fail for the same reason.
// TODO remove after core base line is >= 2.77
Node.class.getDeclaredMethod("hasPermission", Permission.class);
} catch (NoSuchMethodException e) {
when(jenkins.hasPermission(Mockito.any())).thenAnswer(new Answer<Boolean>() {
public Boolean answer(InvocationOnMock invocation) {
Permission permission = invocation.getArgument(0);
Jenkins j = (Jenkins) invocation.getMock();
ACL acl = j.getACL();
try {
return acl.hasPermission(permission);
} catch (NullPointerException x) {
throw new AssumptionViolatedException("TODO cannot be made to work prior to Spring Security update", x);
}
}
});
}
mockStatic(User.class);
when(User.get("some_user", false, Collections.EMPTY_MAP)).thenReturn(user);
}
Aggregations