use of hudson.model.Item in project blueocean-plugin by jenkinsci.
the class GithubPipelineCreateRequestTest method shouldFindUserStoreCredential.
@Test
public void shouldFindUserStoreCredential() throws IOException {
// add username password credential to user's credential store in user domain and in USER scope
User user = login();
CredentialsStore store = null;
for (CredentialsStore s : CredentialsProvider.lookupStores(user)) {
if (s.hasPermission(CredentialsProvider.CREATE) && s.hasPermission(CredentialsProvider.UPDATE)) {
store = s;
break;
}
}
assertNotNull(store);
store.addDomain(new Domain("github-domain", "GitHub Domain to store personal access token", Collections.<DomainSpecification>singletonList(new BlueOceanDomainSpecification())));
Domain domain = store.getDomainByName("github-domain");
StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "GitHub Access Token", user.getId(), "12345");
store.addCredentials(domain, credential);
// create another credentials with same id in system store with different description
for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.get())) {
s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System GitHub Access Token", user.getId(), "12345"));
}
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "demo");
AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
mp.addProperty(prop);
// lookup for created credential id in system store, it should resolve to previously created user store credential
StandardCredentials c = Connector.lookupScanCredentials((Item) mp, "https://api.github.com", credential.getId());
assertEquals("GitHub Access Token", c.getDescription());
assertNotNull(c);
assertTrue(c instanceof StandardUsernamePasswordCredentials);
StandardUsernamePasswordCredentials usernamePasswordCredentials = (StandardUsernamePasswordCredentials) c;
assertEquals(credential.getId(), usernamePasswordCredentials.getId());
assertEquals(credential.getPassword().getPlainText(), usernamePasswordCredentials.getPassword().getPlainText());
assertEquals(credential.getUsername(), usernamePasswordCredentials.getUsername());
// check the domain
Domain d = CredentialsUtils.findDomain(credential.getId(), user);
assertNotNull(d);
assertTrue(d.test(new BlueOceanDomainRequirement()));
// now remove this property
mp.getProperties().remove(prop);
// it must resolve to system credential
c = Connector.lookupScanCredentials((Item) mp, null, credential.getId());
assertEquals("System GitHub Access Token", c.getDescription());
}
use of hudson.model.Item in project blueocean-plugin by jenkinsci.
the class BlueRunChangesetPreloader method getPipeline.
private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null) {
return null;
}
String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
try {
Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
} catch (Exception e) {
LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
return null;
}
}
use of hudson.model.Item in project blueocean-plugin by jenkinsci.
the class FavoriteListStatePreloader method getStateJson.
@CheckForNull
@Override
public String getStateJson() {
User jenkinsUser = User.current();
if (jenkinsUser == null) {
return null;
}
FavoriteUserProperty fup = jenkinsUser.getProperty(FavoriteUserProperty.class);
if (fup == null) {
return null;
}
Set<String> favorites = fup.getAllFavorites();
if (favorites == null) {
return null;
}
final Jenkins jenkins = Jenkins.get();
final List<FavoritPreload> favoritPreloads = favorites.stream().map(name -> {
final Item item = jenkins.getItemByFullName(name);
if (item instanceof Job) {
final Job<?, ?> job = (Job<?, ?>) item;
if (job.getAction(PrimaryInstanceMetadataAction.class) != null) {
return new FavoritPreload(name, true);
}
}
return new FavoritPreload(name, false);
}).collect(Collectors.toList());
return JSONArray.fromObject(favoritPreloads).toString();
}
use of hudson.model.Item in project blueocean-plugin by jenkinsci.
the class BlueOceanUrlMapperImpl method getUrl.
@Override
public String getUrl(@Nonnull ModelObject modelObject) {
BlueOrganization organization = getOrganization(modelObject);
if (organization == null) {
// no organization, best we can do is to land user on landing page
return getLandingPagePath();
}
String organizationName = organization.getName();
String baseUrl = getOrgPrefix(organizationName);
if (modelObject instanceof ModifiableTopLevelItemGroup) {
return baseUrl;
} else if (modelObject instanceof Job) {
BluePipeline blueResource = getJobResource(modelObject);
if (blueResource != null) {
return getPipelineUrl(baseUrl, blueResource);
}
} else if (modelObject instanceof Run) {
Run run = (Run) modelObject;
Job job = run.getParent();
BluePipeline blueResource = getJobResource(job);
if (blueResource != null) {
// encoded and re-encode to do the full monty. Nasty :)
return baseUrl + "/" + encodeURIComponent(blueResource.getFullName()) + "/detail/" + encodeURIComponent(decodeURIComponent(job.getName())) + "/" + encodeURIComponent(run.getId()) + "/";
}
} else if (modelObject instanceof Item) {
Resource bluePipeline = BluePipelineFactory.resolve((Item) modelObject);
if (bluePipeline instanceof BlueMultiBranchPipeline) {
return getPipelineUrl(baseUrl, (BluePipeline) bluePipeline);
}
}
return null;
}
use of hudson.model.Item in project blueocean-plugin by jenkinsci.
the class PipelineActivityStatePreloader method getPipeline.
private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
if (addPipelineRuns(blueUrl)) {
Jenkins jenkins = Jenkins.get();
String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);
try {
Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
} catch (Exception e) {
LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
return null;
}
}
return null;
}
Aggregations