Example usage for com.amazonaws.services.codepipeline AWSCodePipelineClient pollForJobs

List of usage examples for com.amazonaws.services.codepipeline AWSCodePipelineClient pollForJobs

Introduction

In this page you can find the example usage for com.amazonaws.services.codepipeline AWSCodePipelineClient pollForJobs.

Prototype

@Override
public PollForJobsResult pollForJobs(PollForJobsRequest request) 

Source Link

Document

Returns information about any jobs for AWS CodePipeline to act on.

Usage

From source file:jetbrains.buildServer.buildTriggers.codepipeline.CodePipelineAsyncPolledBuildTrigger.java

License:Apache License

@Nullable
@Override//from ww w  .  ja v  a2  s.  c  o  m
public String triggerBuild(@Nullable String previousValue, @NotNull PolledTriggerContext context)
        throws BuildTriggerException {
    final Map<String, String> properties = validateParams(context.getTriggerDescriptor().getProperties());
    try {
        AWSCommonParams.withAWSClients(properties, clients -> {
            final AWSCodePipelineClient codePipelineClient = clients.createCodePipeLineClient();

            final PollForJobsRequest request = new PollForJobsRequest()
                    .withActionTypeId(new ActionTypeId().withCategory(ActionCategory.Build)
                            .withOwner(ActionOwner.Custom).withProvider(TEAMCITY_ACTION_PROVIDER)
                            .withVersion(getActionTypeVersion(codePipelineClient)))
                    .withQueryParam(CollectionsUtil.asMap(ACTION_TOKEN_CONFIG_PROPERTY,
                            CodePipelineUtil.getActionToken(properties)))
                    .withMaxBatchSize(1);

            final List<Job> jobs = codePipelineClient.pollForJobs(request).getJobs();

            if (jobs.size() > 0) {
                if (jobs.size() > 1) {
                    LOG.warn(msgForBt(
                            "Received " + jobs.size()
                                    + ", but only one was expected. Will process only the first job",
                            context.getBuildType()));
                }

                final Job job = jobs.get(0);
                LOG.info(msgForBt(
                        "Received job request with ID: " + job.getId() + " and nonce: " + job.getNonce(),
                        context.getBuildType()));

                try {
                    final AcknowledgeJobRequest acknowledgeJobRequest = new AcknowledgeJobRequest()
                            .withJobId(job.getId()).withNonce(job.getNonce());

                    final String jobStatus = codePipelineClient.acknowledgeJob(acknowledgeJobRequest)
                            .getStatus();
                    if (jobStatus.equals(JobStatus.InProgress.name())) {

                        final BuildCustomizer buildCustomizer = myBuildCustomizerFactory
                                .createBuildCustomizer(context.getBuildType(), null);
                        buildCustomizer.setParameters(getCustomBuildParameters(job, context));

                        final BuildPromotion promotion = buildCustomizer.createPromotion();
                        promotion.addToQueue(TRIGGER_DISPLAY_NAME + " job with ID: " + job.getId());

                        LOG.info(msgForBt(
                                "Acknowledged job with ID: " + job.getId() + " and nonce: " + job.getNonce()
                                        + ", created build promotion " + promotion.getId(),
                                context.getBuildType()));

                    } else {
                        LOG.warn(
                                msgForBt(
                                        "Job ignored with ID: " + job.getId() + " and nonce: " + job.getNonce()
                                                + " because job status is " + jobStatus,
                                        context.getBuildType()));
                    }
                } catch (Throwable e) {
                    final BuildTriggerException buildTriggerException = processThrowable(e);
                    codePipelineClient
                            .putJobFailureResult(new PutJobFailureResultRequest().withJobId(job.getId())
                                    .withFailureDetails(new FailureDetails().withType(FailureType.JobFailed)
                                            .withMessage(buildTriggerException.getMessage())));
                    throw buildTriggerException;
                }
            } else {
                LOG.debug(msgForBt("No jobs found", context.getBuildType()));
            }
            return null;
        });
    } catch (Throwable e) {
        throw processThrowable(e);
    }
    return null;
}