Skip to content

Adding the CodePipelineEvent #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

package com.amazonaws.services.lambda.runtime.events;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
* Represents an CodePipeline event sent to Lambda.
* See: <a href="https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html">Invoke an AWS Lambda function in a pipeline in CodePipeline</a>
*/
@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class CodePipelineEvent implements Serializable {
private static final long serialVersionUID = -4828716548429210697L;

@JsonProperty("CodePipeline.job")
private Job codePipelineJob;

@lombok.Data
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully qualified because there is a member variable named data

@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Job implements Serializable {
private static final long serialVersionUID = 2211711169692638977L;

private String id;
private String accountId;
private Data data;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Data implements Serializable {
private static final long serialVersionUID = 8786599041834868262L;

private ActionConfiguration actionConfiguration;
private List<Artifact> inputArtifacts;
private List<Artifact> outputArtifacts;
private ArtifactCredentials artifactCredentials;
private String continuationToken;
private EncryptionKey encryptionKey;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class ActionConfiguration implements Serializable {
private static final long serialVersionUID = -7285651174501621217L;

private Configuration configuration;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Configuration implements Serializable {
private static final long serialVersionUID = 580024317691702894L;

@JsonProperty("FunctionName")
private String functionName;
@JsonProperty("UserParameters")
private String userParameters;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Artifact implements Serializable {
private static final long serialVersionUID = 6406621244704594358L;

private String name;
private String revision;
private Location location;

@JsonInclude
public String getRevision() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added specific getter and setter for this attribute because Jackson was not including the attribute when it was attached to the parameter.

@andclt suspected it might be a problem with Lombok and Jackson not working together.

return revision;
}

@JsonInclude
public void setRevision(String revision) {
this.revision = revision;
}
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Location implements Serializable {
private static final long serialVersionUID = 149382199413534713L;

private String type;
private S3Location s3Location;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class S3Location implements Serializable {
private static final long serialVersionUID = -8922449809993769709L;

private String bucketName;
private String objectKey;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class ArtifactCredentials implements Serializable {
private static final long serialVersionUID = 7710347495607396747L;

private String accessKeyId;
private String secretAccessKey;
private String sessionToken;
}

@lombok.Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class EncryptionKey implements Serializable {
private static final long serialVersionUID = -9105569908901180610L;

String id;
String type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ public void testCodeCommitEvent() throws IOException, JSONException {
assertEquals(expected, actual, STRICT);
}

@Test
public void testCodePipelineEvent() throws IOException, JSONException {
String expected = EventUtils.readEvent("code_pipeline_event.json");
String actual = deserializeSerializeJsonToString(expected, CodePipelineEvent.class);

assertEquals(expected, actual, STRICT);
}

@Test
public void testCognitoEvent() throws IOException, JSONException {
String expected = EventUtils.readEvent("cognito_event.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"CodePipeline.job": {
"id": "11111111-abcd-1111-abcd-111111abcdef",
"accountId": "111111111111",
"data": {
"actionConfiguration": {
"configuration": {
"FunctionName": "MyLambdaFunctionForAWSCodePipeline",
"UserParameters": "some-input-such-as-a-URL"
}
},
"inputArtifacts": [
{
"location": {
"s3Location": {
"bucketName": "the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890",
"objectKey": "the name of the application, for example CodePipelineDemoApplication.zip"
},
"type": "S3"
},
"revision": null,
"name": "ArtifactName"
}
],
"outputArtifacts": [],
"artifactCredentials": {
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"sessionToken": "MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE=",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE"
},
"continuationToken": "A continuation token if continuing job",
"encryptionKey": {
"id": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
"type": "KMS"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public static CodeCommitEvent loadCodeCommitEvent(String filename) {
return loadEvent(filename, CodeCommitEvent.class);
}

public static CodePipelineEvent loadCodePipelineEvent(String filename) {
return loadEvent(filename, CodePipelineEvent.class);
}

public static ConfigEvent loadConfigEvent(String filename) {
return loadEvent(filename, ConfigEvent.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ public void testLoadCodeCommitEvent() {
assertThat(reference.getRef()).isEqualTo("refs/heads/master");
}

@Test
public void testLoadCodePipelineEvent() {
CodePipelineEvent event = EventLoader.loadCodePipelineEvent("codepipeline_event.json");

assertThat(event).isNotNull();
assertThat(event.getCodePipelineJob().getId()).isEqualTo("11111111-abcd-1111-abcd-111111abcdef");
assertThat(event.getCodePipelineJob().getAccountId()).isEqualTo("111111111111");
assertThat(event.getCodePipelineJob().getData().getActionConfiguration().getConfiguration().getFunctionName()).isEqualTo("MyLambdaFunctionForAWSCodePipeline");
assertThat(event.getCodePipelineJob().getData().getActionConfiguration().getConfiguration().getUserParameters()).isEqualTo("some-input-such-as-a-URL");
assertThat(event.getCodePipelineJob().getData().getInputArtifacts()).hasSize(1);
assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getType()).isEqualTo("S3");
assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getS3Location().getBucketName()).isEqualTo("the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890");
assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getLocation().getS3Location().getObjectKey()).isEqualTo("the name of the application, for example CodePipelineDemoApplication.zip");
assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getRevision()).isNull();
assertThat(event.getCodePipelineJob().getData().getInputArtifacts().get(0).getName()).isEqualTo("ArtifactName");
assertThat(event.getCodePipelineJob().getData().getOutputArtifacts()).hasSize(0);
assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getSecretAccessKey()).isEqualTo("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getSessionToken()).isEqualTo("MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE=");
assertThat(event.getCodePipelineJob().getData().getArtifactCredentials().getAccessKeyId()).isEqualTo("AKIAIOSFODNN7EXAMPLE");
assertThat(event.getCodePipelineJob().getData().getContinuationToken()).isEqualTo("A continuation token if continuing job");
assertThat(event.getCodePipelineJob().getData().getEncryptionKey().getId()).isEqualTo("arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab");
assertThat(event.getCodePipelineJob().getData().getEncryptionKey().getType()).isEqualTo("KMS");

}

@Test
public void testLoadCloudWatchLogsEvent() {
CloudWatchLogsEvent cloudWatchLogsEvent = EventLoader.loadCloudWatchLogsEvent("cloudwatchlogs_event.json");
Expand Down
38 changes: 38 additions & 0 deletions aws-lambda-java-tests/src/test/resources/codepipeline_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"CodePipeline.job": {
"id": "11111111-abcd-1111-abcd-111111abcdef",
"accountId": "111111111111",
"data": {
"actionConfiguration": {
"configuration": {
"FunctionName": "MyLambdaFunctionForAWSCodePipeline",
"UserParameters": "some-input-such-as-a-URL"
}
},
"inputArtifacts": [
{
"location": {
"s3Location": {
"bucketName": "the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890",
"objectKey": "the name of the application, for example CodePipelineDemoApplication.zip"
},
"type": "S3"
},
"revision": null,
"name": "ArtifactName"
}
],
"outputArtifacts": [],
"artifactCredentials": {
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"sessionToken": "MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9wEXAMPLE=",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE"
},
"continuationToken": "A continuation token if continuing job",
"encryptionKey": {
"id": "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
"type": "KMS"
}
}
}
}