Skip to content

Commit

Permalink
Update AWS SDK to V3
Browse files Browse the repository at this point in the history
  • Loading branch information
tarikdem committed Apr 5, 2024
1 parent e83df6f commit 822c0a4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 50 deletions.
62 changes: 37 additions & 25 deletions code-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
const core = require("@actions/core");
const github = require("@actions/github");

const { CloudWatchLogs } = require("@aws-sdk/client-cloudwatch-logs");
const { CodeBuild } = require("@aws-sdk/client-codebuild");
const { Lambda } = require("@aws-sdk/client-lambda");
const {
CloudWatchLogsClient,
GetLogEventsCommand,
} = require("@aws-sdk/client-cloudwatch-logs");
const {
CodeBuildClient,
BatchGetBuildsCommand,
} = require("@aws-sdk/client-codebuild");
const { LambdaClient, InvokeCommand } = require("@aws-sdk/client-lambda");

const assert = require("assert");

Expand Down Expand Up @@ -42,19 +48,22 @@ async function build(sdk, params, config) {
// Invoke the lambda to start the build
const buildTime = (Date.now() / 1000).toString();
const imageTag = `${params.sourceVersion}-${Math.floor(buildTime)}`;
const lambdaParams = {
FunctionName: "GeneralDockerBuildPipelineLambdaFunction",
Payload: JSON.stringify({
owner: params.owner,
repo: params.repo,
branch: params.branch,
sourceVersion: params.sourceVersion,
reproducible: params.reproducible,
imageTag,
}),
};
const response = await sdk.lambda.invoke(lambdaParams);
const start = JSON.parse(JSON.parse(response.Payload));
const response = await sdk.lambda.send(
new InvokeCommand({
FunctionName: "GeneralDockerBuildPipelineLambdaFunction",
Payload: JSON.stringify({
owner: params.owner,
repo: params.repo,
branch: params.branch,
sourceVersion: params.sourceVersion,
reproducible: params.reproducible,
imageTag,
}),
})
);
const start = JSON.parse(
JSON.parse(Buffer.from(response.Payload).toString())
);

await core.notice(`Built image tag: ${imageTag}`);

Expand Down Expand Up @@ -85,18 +94,21 @@ async function waitForBuildEndTime(
let errObject = false;
// Check the state
const [batch, cloudWatch = {}] = await Promise.all([
codeBuild.batchGetBuilds({ ids: [id] }),
// The promise() call was removed by aws-sdk-js-codemod v2-to-v3 transform using best guess.
// Please check that it is correct, and create an issue on GitHub to report this use case.
codeBuild.send(
new BatchGetBuildsCommand({
ids: [id],
})
),
!hideCloudWatchLogs &&
logGroupName &&
cloudWatchLogs // only make the call if hideCloudWatchLogs is not enabled and a logGroupName exists
.getLogEvents({
cloudWatchLogs.send(
new GetLogEventsCommand({
logGroupName,
logStreamName,
startFromHead,
nextToken,
}),
})
),
]).catch((err) => {
errObject = err;
/* Returning [] here so that the assignment above
Expand Down Expand Up @@ -257,21 +269,21 @@ function inputs2Parameters(inputs) {
}

function buildSdk() {
const codeBuild = new CodeBuild({
const codeBuild = new CodeBuildClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
customUserAgent: "brave-intl/aws-codebuild-run-build",
});

const cloudWatchLogs = new CloudWatchLogs({
const cloudWatchLogs = new CloudWatchLogsClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
customUserAgent: "brave-intl/aws-codebuild-run-build",
});

const lambda = new Lambda({
const lambda = new LambdaClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
Expand Down
60 changes: 35 additions & 25 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438);

const { CloudWatchLogs } = __nccwpck_require__(1573);
const { CodeBuild } = __nccwpck_require__(6872);
const { Lambda } = __nccwpck_require__(6584);
const { CloudWatchLogsClient, GetLogEventsCommand } = __nccwpck_require__(
1573
);
const { CodeBuildClient, BatchGetBuildsCommand } = __nccwpck_require__(
6872
);
const { LambdaClient, InvokeCommand } = __nccwpck_require__(6584);

const assert = __nccwpck_require__(9491);

Expand Down Expand Up @@ -54,19 +58,22 @@
// Invoke the lambda to start the build
const buildTime = (Date.now() / 1000).toString();
const imageTag = `${params.sourceVersion}-${Math.floor(buildTime)}`;
const lambdaParams = {
FunctionName: "GeneralDockerBuildPipelineLambdaFunction",
Payload: JSON.stringify({
owner: params.owner,
repo: params.repo,
branch: params.branch,
sourceVersion: params.sourceVersion,
reproducible: params.reproducible,
imageTag,
}),
};
const response = await sdk.lambda.invoke(lambdaParams).promise();
const start = JSON.parse(JSON.parse(response.Payload));
const response = await sdk.lambda.send(
new InvokeCommand({
FunctionName: "GeneralDockerBuildPipelineLambdaFunction",
Payload: JSON.stringify({
owner: params.owner,
repo: params.repo,
branch: params.branch,
sourceVersion: params.sourceVersion,
reproducible: params.reproducible,
imageTag,
}),
})
);
const start = JSON.parse(
JSON.parse(Buffer.from(response.Payload).toString())
);

await core.notice(`Built image tag: ${imageTag}`);

Expand Down Expand Up @@ -97,18 +104,21 @@
let errObject = false;
// Check the state
const [batch, cloudWatch = {}] = await Promise.all([
codeBuild.batchGetBuilds({ ids: [id] }),
// The promise() call was removed by aws-sdk-js-codemod v2-to-v3 transform using best guess.
// Please check that it is correct, and create an issue on GitHub to report this use case.
codeBuild.send(
new BatchGetBuildsCommand({
ids: [id],
})
),
!hideCloudWatchLogs &&
logGroupName &&
cloudWatchLogs // only make the call if hideCloudWatchLogs is not enabled and a logGroupName exists
.getLogEvents({
cloudWatchLogs.send(
new GetLogEventsCommand({
logGroupName,
logStreamName,
startFromHead,
nextToken,
}),
})
),
]).catch((err) => {
errObject = err;
/* Returning [] here so that the assignment above
Expand Down Expand Up @@ -273,21 +283,21 @@
}

function buildSdk() {
const codeBuild = new CodeBuild({
const codeBuild = new CodeBuildClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
customUserAgent: "brave-intl/aws-codebuild-run-build",
});

const cloudWatchLogs = new CloudWatchLogs({
const cloudWatchLogs = new CloudWatchLogsClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
customUserAgent: "brave-intl/aws-codebuild-run-build",
});

const lambda = new Lambda({
const lambda = new LambdaClient({
// The transformation for customUserAgent is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for customUserAgent.
Expand Down

0 comments on commit 822c0a4

Please sign in to comment.