Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: plan too long for status api #18

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
working-directory: ${{ env.TERRAFORM_WORKING_DIRECTORY }}
report-title: Single Argument Run

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra empty line


- uses: ./
with:
Expand Down Expand Up @@ -70,6 +71,12 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
working-directory: ${{ env.TERRAFORM_WORKING_DIRECTORY }}
report-title: Single Argument Run Second Job
save-directory: ./artifacts

- uses: actions/upload-artifact@v2
with:
name: terraform-output
path: ./artifacts/*

release:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
required: false
description: 'The name of the terraform plan to use when posting the results to the Pull Request'
default: 'Terraform Plan Report'
save-directory:
required: false
description: 'The directory path to store the full stdout / stderr from the terraform plan execution. A std.out and std.err file will be created with the data.'
default: ''
runs:
using: 'node12'
main: 'dist/index.js'
39 changes: 33 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export async function createStatusCheck(
\`\`\``;
}

if (details.length > 65000) {
details = `${details.substr(0, 65400)} ... truncated`;
}

const context = github.context;
const pr = context.payload.pull_request;
await octokit.checks.create({
Expand All @@ -47,14 +51,24 @@ export async function createStatusCheck(
}

function getPlanSummary(output: string): string {
const planLineStart = output.indexOf('Plan:');
if (planLineStart > 0) {
const endOfPlanLine = output.indexOf('\n', planLineStart);
if (endOfPlanLine > 0) {
return output.substr(planLineStart, endOfPlanLine - planLineStart);
let summary = findLine('Plan:', output);
if (summary === '') {
summary = findLine('No changes.', output);
}

return summary;
}

function findLine(wording: string, output: string): string {
const wordingIndex = output.indexOf('Plan:');
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be using the wording parameter?

if (wordingIndex > 0) {
const endOfLineIndex = output.indexOf('\n', wordingIndex);
if (endOfLineIndex > 0) {
return output.substring(wordingIndex, endOfLineIndex);
} else {
return output.substr(planLineStart);
return output.substr(wordingIndex);
}
}

return '';
}
18 changes: 18 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as core from '@actions/core';
import { exec, ExecOptions } from '@actions/exec';
import io from '@actions/io';
import fs from 'fs';
import path from 'path';
import { createStatusCheck } from './github';
import { TerraformResults } from './terraform-results';

Expand All @@ -14,6 +17,7 @@ async function run(): Promise<void> {
const continueOnError: boolean =
(core.getInput('continue-on-error', { required: false }) || 'false') ===
'true';
const saveDirectory = core.getInput('save-directory');

const stdOut: Buffer[] = [];
const stdErr: Buffer[] = [];
Expand Down Expand Up @@ -59,6 +63,10 @@ async function run(): Promise<void> {
core.debug(error);
core.debug(' ------ Standard Error from Plan -----');

if (saveDirectory !== undefined) {
writeFile(saveDirectory, output, error);
}

createStatusCheck(
githubToken,
reportTitle,
Expand All @@ -73,6 +81,16 @@ async function run(): Promise<void> {
}
}

async function writeFile(
directory: string,
output: string,
error: string
): Promise<void> {
io.mkdirP(directory);
await fs.promises.writeFile(path.join(directory, 'std.out'), output);
await fs.promises.writeFile(path.join(directory, 'std.err'), error);
}

function writeBufferToString(data: Buffer[]): string {
return data.map((buffer) => buffer.toString()).join('');
}
Expand Down