Skip to content
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

build(deps): bump the npm-dependencies group with 9 updates #232

Merged
merged 2 commits into from
Apr 2, 2024
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
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ user or role you login to needs the following permissions:
"codepipeline:GetPipelineExecution",
"codepipeline:ListPipelineExecutions"
],
"Resource": ["arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}"]
"Resource": [
"arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}"
]
},
{
"Effect": "Allow",
"Action": ["codebuild:ListBuildsForProject", "codebuild:BatchGetBuilds"],
"Resource": ["arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CodeBuildProjectName}"]
"Resource": [
"arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CodeBuildProjectName}"
]
},
{
"Effect": "Allow",
"Action": ["logs:GetLogEvents"],
"Resource": ["arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${CodeBuildProjectName}:*"]
"Resource": [
"arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${CodeBuildProjectName}:*"
]
}
]
}
Expand Down
4,815 changes: 2,127 additions & 2,688 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3",
"typescript": "^5.4.3",
"@vercel/ncc": "^0.38.1"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@aws-sdk/client-cloudwatch-logs": "^3.525.0",
"@aws-sdk/client-codebuild": "^3.525.0",
"@aws-sdk/client-codepipeline": "^3.525.0",
"@aws-sdk/client-cloudwatch-logs": "^3.540.0",
"@aws-sdk/client-codebuild": "^3.545.0",
"@aws-sdk/client-codepipeline": "^3.540.0",
"@aws-sdk/types": "^3.460.0",
"@moia-oss/eslint-prettier-typescript-config": "^2.0.24",
"@types/node": "^20.11.24",
"@moia-oss/eslint-prettier-typescript-config": "^3.0.5",
"@types/node": "^20.12.2",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
Expand Down
28 changes: 21 additions & 7 deletions src/cloudwatch-logs-forwarder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as core from '@actions/core';
import { CloudWatchLogsClient, GetLogEventsCommand } from '@aws-sdk/client-cloudwatch-logs';
import { BatchGetBuildsCommand, CodeBuildClient } from '@aws-sdk/client-codebuild';

import { sleep } from './util';
import * as core from '@actions/core';
import {
CloudWatchLogsClient,
GetLogEventsCommand,
} from '@aws-sdk/client-cloudwatch-logs';
import {
BatchGetBuildsCommand,
CodeBuildClient,
} from '@aws-sdk/client-codebuild';

const CLIENT = new CloudWatchLogsClient({});
const CODEBUILD_CLIENT = new CodeBuildClient({});
Expand All @@ -27,7 +32,10 @@ export class CloudWatchLogsForwarder {
this.streamName = streamName;
}

public forwardLogEventsToGithubActions = async (buildId: string, nextToken?: string): Promise<boolean> => {
public forwardLogEventsToGithubActions = async (
buildId: string,
nextToken?: string,
): Promise<boolean> => {
const batchGetBuildCommand = new BatchGetBuildsCommand({ ids: [buildId] });
const getLogEventsCommand = new GetLogEventsCommand({
logGroupName: this.groupName,
Expand All @@ -48,7 +56,10 @@ export class CloudWatchLogsForwarder {
const [currentBuild] = getBuildOutput.builds;
const { nextForwardToken, events = [] } = getLogEventsOutput;

if (events.length === 0 && (this.totalEvents > 0 || currentBuild.endTime)) {
if (
events.length === 0 &&
(this.totalEvents > 0 || currentBuild.endTime)
) {
this.consecutiveEmptyLogs += 1;
} else {
this.consecutiveEmptyLogs = 0;
Expand All @@ -63,7 +74,10 @@ export class CloudWatchLogsForwarder {
}

await sleep(this.wait);
return await this.forwardLogEventsToGithubActions(buildId, nextForwardToken);
return await this.forwardLogEventsToGithubActions(
buildId,
nextForwardToken,
);
} catch (error) {
const err = error as Error;
if (err.message && err.message.search('Rate exceeded') !== -1) {
Expand Down
77 changes: 56 additions & 21 deletions src/codebuild.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CloudWatchLogsForwarder } from './cloudwatch-logs-forwarder';
import * as core from '@actions/core';
import {
BatchGetBuildsCommand,
Expand All @@ -6,28 +7,35 @@ import {
StatusType,
} from '@aws-sdk/client-codebuild';
import { GetPipelineCommand } from '@aws-sdk/client-codepipeline';
import type { ActionDeclaration, CodePipelineClient, StageDeclaration } from '@aws-sdk/client-codepipeline';

import { CloudWatchLogsForwarder } from './cloudwatch-logs-forwarder';
import type {
ActionDeclaration,
CodePipelineClient,
StageDeclaration,
} from '@aws-sdk/client-codepipeline';

const CLIENT = new CodeBuildClient({});

type ProjectToBuildBatchId = [string, string];

export const getCodeBuildsFromActions = (actions: ActionDeclaration[]): string[] =>
export const getCodeBuildsFromActions = (
actions: ActionDeclaration[],
): string[] =>
actions
.map((action) => {
if (action.actionTypeId?.category === 'Build' && action.actionTypeId?.provider === 'CodeBuild') {
.map(action => {
if (
action.actionTypeId?.category === 'Build' &&
action.actionTypeId?.provider === 'CodeBuild'
) {
if (action.configuration !== undefined) {
return action.configuration.ProjectName;
}
}
return '';
})
.filter((x) => x !== '');
.filter(x => x !== '');

export const getCodeBuildsFromStages = (stages: StageDeclaration[]): string[] =>
stages.flatMap((stage) => {
stages.flatMap(stage => {
if (stage.actions !== undefined) {
return getCodeBuildsFromActions(stage.actions);
}
Expand All @@ -47,10 +55,14 @@ export const getCodebuildProjectsForPipeline = async (
if (output.pipeline?.stages !== undefined) {
return getCodeBuildsFromStages(output.pipeline?.stages);
}
core.warning("Couldn't get pipeline information. You are probably missing permissions.");
core.warning(
"Couldn't get pipeline information. You are probably missing permissions.",
);
return [];
} catch (error) {
core.error(`An error occured while getting pipeline information for '${codePipelineName}'`);
core.error(
`An error occured while getting pipeline information for '${codePipelineName}'`,
);
return [];
}
};
Expand All @@ -77,14 +89,18 @@ const isBuildIdInProgress = async (buildId: string): Promise<boolean> => {
/*
* Searches through the passed CodeBuild Projects and returns build IDs, which are IN_PROGRESS
*/
export const getInProgressBuildId = async (codebuildProjectName: string): Promise<string | undefined> => {
export const getInProgressBuildId = async (
codebuildProjectName: string,
): Promise<string | undefined> => {
const command = new ListBuildsForProjectCommand({
projectName: codebuildProjectName,
});
try {
const output = await CLIENT.send(command);
if (output.ids === undefined || output.ids.length === 0) {
core.info(`CodeBuild Project ${codebuildProjectName} is currently not in progress`);
core.info(
`CodeBuild Project ${codebuildProjectName} is currently not in progress`,
);
return undefined;
}
if (await isBuildIdInProgress(output.ids[0])) {
Expand All @@ -102,21 +118,33 @@ export const getInProgressBuildId = async (codebuildProjectName: string): Promis
}
};

export const forwardLogEventsFromCodebuild = async ([projectName, buildId]: ProjectToBuildBatchId) => {
export const forwardLogEventsFromCodebuild = async ([
projectName,
buildId,
]: ProjectToBuildBatchId) => {
const command = new BatchGetBuildsCommand({ ids: [buildId] });
try {
const output = await CLIENT.send(command);
if (output.builds !== undefined) {
const [build] = output.builds;
if (build.logs?.groupName && build.logs?.streamName) {
core.info(`**** Build ${buildId} has started. Following it's output ****`);

const forwarder = new CloudWatchLogsForwarder(build.logs.groupName, build.logs.streamName);
core.info(
`**** Build ${buildId} has started. Following it's output ****`,
);

const forwarder = new CloudWatchLogsForwarder(
build.logs.groupName,
build.logs.streamName,
);
await forwarder.forwardLogEventsToGithubActions(buildId, undefined);

core.info(`**** Build ${buildId} finished. Back to tracking pipeline status ****`);
core.info(
`**** Build ${buildId} finished. Back to tracking pipeline status ****`,
);
} else {
core.warning(`CodeBuild Project ${projectName} ${buildId} is not ready!`);
core.warning(
`CodeBuild Project ${projectName} ${buildId} is not ready!`,
);
}
}
} catch (error) {
Expand All @@ -130,14 +158,21 @@ export const forwardLogEventsFromCodebuild = async ([projectName, buildId]: Proj
/*
* Used in filter methods to get out just the ProjectToBuildBatchIds which have a build id set
*/
export const isProjectToBuildBatchId = (x: (string | undefined)[]): x is ProjectToBuildBatchId => !!x[1];
export const isProjectToBuildBatchId = (
x: (string | undefined)[],
): x is ProjectToBuildBatchId => !!x[1];

/*
* Get the BuildBatchIds of the passed CodeBuild Projects.
*/
export const getInProgressProjectToBatchIds = async (codebuildProjects: string[]): Promise<ProjectToBuildBatchId[]> => {
export const getInProgressProjectToBatchIds = async (
codebuildProjects: string[],
): Promise<ProjectToBuildBatchId[]> => {
const projectsToBuildBatches: (string | undefined)[][] = await Promise.all(
codebuildProjects.map(async (codebuildProject) => [codebuildProject, await getInProgressBuildId(codebuildProject)]),
codebuildProjects.map(async codebuildProject => [
codebuildProject,
await getInProgressBuildId(codebuildProject),
]),
);

return projectsToBuildBatches.filter(isProjectToBuildBatchId);
Expand Down
68 changes: 51 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as codebuild from './codebuild';
import { sleep } from './util';
import * as core from '@actions/core';
import {
CodePipelineClient,
Expand All @@ -7,15 +9,18 @@ import {
ListPipelineExecutionsCommand,
} from '@aws-sdk/client-codepipeline';

import * as codebuild from './codebuild';
import { sleep } from './util';

const CLIENT = new CodePipelineClient({});

const getNewestExecutionId = async (pipelineName: string): Promise<string> => {
const command = new ListPipelineExecutionsCommand({ pipelineName, maxResults: 1 });
const command = new ListPipelineExecutionsCommand({
pipelineName,
maxResults: 1,
});
const data = await CLIENT.send(command);
if (data.pipelineExecutionSummaries && data.pipelineExecutionSummaries.length > 0) {
if (
data.pipelineExecutionSummaries &&
data.pipelineExecutionSummaries.length > 0
) {
const executionId = data.pipelineExecutionSummaries[0].pipelineExecutionId;
if (executionId) {
return executionId;
Expand All @@ -31,7 +36,10 @@ const waitForPipeline = async (
codebuilds?: string[],
): Promise<boolean> => {
await sleep(10);
const command = new GetPipelineExecutionCommand({ pipelineName, pipelineExecutionId });
const command = new GetPipelineExecutionCommand({
pipelineName,
pipelineExecutionId,
});
try {
const data = await CLIENT.send(command);

Expand All @@ -43,18 +51,31 @@ const waitForPipeline = async (
switch (status) {
case PipelineExecutionStatus.InProgress: {
if (codebuilds) {
const projectToBuildBatchId = await codebuild.getInProgressProjectToBatchIds(codebuilds);
const projectToBuildBatchId =
await codebuild.getInProgressProjectToBatchIds(codebuilds);
if (projectToBuildBatchId.length > 0) {
await codebuild.forwardLogEventsFromCodebuild(projectToBuildBatchId[0]);
await codebuild.forwardLogEventsFromCodebuild(
projectToBuildBatchId[0],
);
}
}
core.info(`Pipeline '${pipelineName}' in progress waiting for 10 more seconds.`);
return await waitForPipeline(pipelineName, pipelineExecutionId, codebuilds);
core.info(
`Pipeline '${pipelineName}' in progress waiting for 10 more seconds.`,
);
return await waitForPipeline(
pipelineName,
pipelineExecutionId,
codebuilds,
);
}
case PipelineExecutionStatus.Cancelled: {
core.info(`Pipeline '${pipelineName}' was canceled. Trying to get new execution ID.`);
core.info(
`Pipeline '${pipelineName}' was canceled. Trying to get new execution ID.`,
);
const newExecutionId = await getNewestExecutionId(pipelineName);
core.info(`Waiting on pipeline '${pipelineName}' with new execution id '${newExecutionId}'`);
core.info(
`Waiting on pipeline '${pipelineName}' with new execution id '${newExecutionId}'`,
);
return await waitForPipeline(pipelineName, newExecutionId, codebuilds);
}
case PipelineExecutionStatus.Succeeded:
Expand All @@ -67,7 +88,9 @@ const waitForPipeline = async (
core.error(`Pipeline '${pipelineName}' stopped.`);
return false;
case PipelineExecutionStatus.Superseded:
core.warning(`Pipeline '${pipelineName}' was superseded. Skipping rest of the execution.`);
core.warning(
`Pipeline '${pipelineName}' was superseded. Skipping rest of the execution.`,
);
return true;
default:
core.error(`Unexpected status: ${status} given.`);
Expand All @@ -85,7 +108,9 @@ const run = async (): Promise<void> => {
let codebuilds;
const pipelineName: string = core.getInput('pipeline', { required: true });
const wait: boolean = core.getBooleanInput('wait', { required: false });
const followCodeBuild: boolean = core.getBooleanInput('follow-codebuild', { required: false });
const followCodeBuild: boolean = core.getBooleanInput('follow-codebuild', {
required: false,
});

const command = new StartPipelineExecutionCommand({ name: pipelineName });

Expand All @@ -96,16 +121,25 @@ const run = async (): Promise<void> => {
}
if (wait) {
if (followCodeBuild) {
codebuilds = await codebuild.getCodebuildProjectsForPipeline(CLIENT, pipelineName);
codebuilds = await codebuild.getCodebuildProjectsForPipeline(
CLIENT,
pipelineName,
);
}

const executionResult = await waitForPipeline(pipelineName, data.pipelineExecutionId, codebuilds);
const executionResult = await waitForPipeline(
pipelineName,
data.pipelineExecutionId,
codebuilds,
);
if (!executionResult) {
throw new Error('Execution was unsucessful.');
}
}
} catch (error) {
core.error(`An error occured while starting Codepipeline '${pipelineName}'`);
core.error(
`An error occured while starting Codepipeline '${pipelineName}'`,
);
throw error;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* Sleep for amount in seconds
*/
export const sleep = (s: number) =>
new Promise((resolve) => {
new Promise(resolve => {
setTimeout(resolve, s * 1000);
});