Search in sources :

Example 1 with GET

use of org.kohsuke.stapler.verb.GET in project blueocean-plugin by jenkinsci.

the class GithubScm method getRepository.

/**
 * @param jobName the job name
 * @param apiUrl github api url
 * @return GHRepository used by the job
 */
@GET
@WebMethod(name = "repository")
@TreeResponse
public GithubRepository getRepository(@QueryParameter String jobName, @QueryParameter String apiUrl) {
    Item item = Jenkins.get().getItem(jobName);
    if (item == null) {
        throw new ServiceException.NotFoundException(String.format("Job %s not found", jobName));
    }
    GitHubSCMSource gitHubSCMSource = ((GitHubSCMSource) ((WorkflowMultiBranchProject) item).getSCMSource("blueocean"));
    if (gitHubSCMSource == null) {
        throw new ServiceException.NotFoundException(String.format("GitHubSCMSource for Job %s not found", jobName));
    }
    String repoOwner = gitHubSCMSource.getRepoOwner();
    String repoName = gitHubSCMSource.getRepository();
    StandardUsernamePasswordCredentials credential = getCredential();
    String accessToken = credential.getPassword().getPlainText();
    try {
        String url = String.format("%s/repos/%s/%s", apiUrl, repoOwner, repoName);
        GHRepository ghRepository = HttpRequest.get(url).withAuthorizationToken(accessToken).to(GHRepository.class);
        return new GithubRepository(ghRepository, credential, this);
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) Item(hudson.model.Item) GHRepository(org.kohsuke.github.GHRepository) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) IOException(java.io.IOException) WebMethod(org.kohsuke.stapler.WebMethod) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 2 with GET

use of org.kohsuke.stapler.verb.GET in project blueocean-plugin by jenkinsci.

the class PipelineMetadataService method doAgentMetadata.

/**
 * Function to return all {@link DeclarativeAgent}s present in the system when accessed through the REST API
 */
@GET
@TreeResponse
public ExportedDescribableModel[] doAgentMetadata() {
    List<ExportedDescribableModel> models = new ArrayList<>();
    for (DeclarativeAgentDescriptor d : DeclarativeAgentDescriptor.all()) {
        try {
            DescribableModel<? extends DeclarativeAgent> model = new DescribableModel<>(d.clazz);
            String symbol = symbolForObject(d);
            if ("label".equals(symbol)) {
                // Label has 2 symbols, but we need "node"
                symbol = "node";
            }
            models.add(new ExportedDescribableModel(model, symbol));
        } catch (NoStaplerConstructorException e) {
        // Ignore!
        }
    }
    return models.toArray(new ExportedDescribableModel[0]);
}
Also used : DeclarativeAgentDescriptor(org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor) ArrayList(java.util.ArrayList) DescribableModel(org.jenkinsci.plugins.structs.describable.DescribableModel) NoStaplerConstructorException(org.kohsuke.stapler.NoStaplerConstructorException) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 3 with GET

use of org.kohsuke.stapler.verb.GET in project blueocean-plugin by jenkinsci.

the class PipelineMetadataService method doBuildConditions.

/**
 * Function to return the names of all build conditions present in the system when accessed through the REST API
 */
@GET
@TreeResponse
public ExportedBuildCondition[] doBuildConditions() {
    List<ExportedBuildCondition> exported = new ArrayList<>();
    for (BuildCondition c : BuildCondition.all()) {
        exported.add(new ExportedBuildCondition(symbolForObject(c), c.getDescription()));
    }
    Collections.sort(exported, new Comparator<ExportedBuildCondition>() {

        @Override
        public int compare(ExportedBuildCondition o1, ExportedBuildCondition o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    return exported.toArray(new ExportedBuildCondition[exported.size()]);
}
Also used : ArrayList(java.util.ArrayList) BuildCondition(org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 4 with GET

use of org.kohsuke.stapler.verb.GET in project blueocean-plugin by jenkinsci.

the class PipelineMetadataService method doPipelineStepMetadata.

/**
 * Function to return all step descriptors present in the system when accessed through the REST API
 */
@GET
@TreeResponse
public ExportedPipelineFunction[] doPipelineStepMetadata() {
    List<ExportedPipelineFunction> pd = new ArrayList<>();
    for (StepDescriptor d : StepDescriptor.all()) {
        if (includeStep(d)) {
            ExportedPipelineStep step = getStepMetadata(d);
            if (step != null) {
                pd.add(step);
            }
        }
    }
    List<Descriptor<?>> metaStepDescriptors = new ArrayList<Descriptor<?>>();
    populateMetaSteps(metaStepDescriptors, Builder.class);
    populateMetaSteps(metaStepDescriptors, Publisher.class);
    populateMetaSteps(metaStepDescriptors, BuildWrapper.class);
    for (Descriptor<?> d : metaStepDescriptors) {
        ExportedPipelineFunction metaStep = getStepMetadata(d);
        if (metaStep != null) {
            pd.add(metaStep);
        }
    }
    return pd.toArray(new ExportedPipelineFunction[pd.size()]);
}
Also used : ArrayList(java.util.ArrayList) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) DeclarativeAgentDescriptor(org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor) ToolDescriptor(hudson.tools.ToolDescriptor) Descriptor(hudson.model.Descriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Example 5 with GET

use of org.kohsuke.stapler.verb.GET in project blueocean-plugin by jenkinsci.

the class PipelineMetadataService method doToolMetadata.

/**
 * Function to return all {@link ExportedToolDescriptor}s present in the system when accessed through the REST API,
 * pipeline scripts need: symbol and name to specify tools
 */
@GET
@TreeResponse
public ExportedToolDescriptor[] doToolMetadata() {
    List<ExportedToolDescriptor> models = new ArrayList<>();
    for (ToolDescriptor<? extends ToolInstallation> d : ToolInstallation.all()) {
        ExportedToolDescriptor descriptor = new ExportedToolDescriptor(d.getDisplayName(), symbolForObject(d), d.getClass());
        models.add(descriptor);
        for (ToolInstallation installation : d.getInstallations()) {
            descriptor.addInstallation(new ExportedToolDescriptor.ExportedToolInstallation(installation.getName(), installation.getClass()));
        }
    }
    return models.toArray(new ExportedToolDescriptor[0]);
}
Also used : ArrayList(java.util.ArrayList) ToolInstallation(hudson.tools.ToolInstallation) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) GET(org.kohsuke.stapler.verb.GET)

Aggregations

TreeResponse (io.jenkins.blueocean.commons.stapler.TreeResponse)5 GET (org.kohsuke.stapler.verb.GET)5 ArrayList (java.util.ArrayList)4 DeclarativeAgentDescriptor (org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 Descriptor (hudson.model.Descriptor)1 Item (hudson.model.Item)1 ToolDescriptor (hudson.tools.ToolDescriptor)1 ToolInstallation (hudson.tools.ToolInstallation)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 GitHubSCMSource (org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)1 BuildCondition (org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition)1 DescribableModel (org.jenkinsci.plugins.structs.describable.DescribableModel)1 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)1 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)1 GHRepository (org.kohsuke.github.GHRepository)1 NoStaplerConstructorException (org.kohsuke.stapler.NoStaplerConstructorException)1 WebMethod (org.kohsuke.stapler.WebMethod)1