Skip to content

Commit

Permalink
use local action
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-harvey committed Apr 23, 2024
1 parent 076cfed commit b210019
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
21 changes: 21 additions & 0 deletions .github/actions/report-dso-event/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: report-event
description: 'Sends event data to the MACBIS DevSecOps Metrics API'
inputs:
args:
description: Arguments to pass to the report-event program. Please see Enterprise-CMCS/mac-fc-dso-metrics/cmd/report-event/README.md for documentation
version:
description: The version constraint for the report-event program in semantic version syntax. Defaults to the latest version (we recommend pinning to a specific version or range)
default: "*"
token:
description: The GitHub token used to list the (public) releases of report-event. A token is required to avoid the low non-authenticated rate limit. We recommend using the default GITHUB_TOKEN
default: ${{ github.token }}
outputs:
exit-code:
description: The exit code of the program
output:
description: The output of the program (stdout)
error-output:
description: The error output of the program (stderr)
runs:
using: node20
main: ../../../build/index.js
36 changes: 19 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,34 @@ runs:
with:
message: 'Failed to assume DSO metrics cross-account role when reporting DSO Metrics. Please investigate the failure.'


- name: Report test metrics
id: report-test-metrics
if: |
!cancelled() &&
steps.run-command.outputs.start-time != '' &&
inputs.event-type == 'test'
continue-on-error: true
shell: bash
run: |
node ${{ github.action_path }}/build/index.js \
"-app "${{ inputs.app }}" \
-id "${{ inputs.id }}" \
-team "${{ inputs.team }}" \
${{ inputs.event-type }} \
-start "${{ steps.run-command.outputs.start-time }}" \
-end "${{ steps.run-command.outputs.end-time }}" \
-result "${{ steps.run-command.outputs.exit-code == '0' && 'pass' || 'fail' }}"" \
${{ inputs.report-event-version }} \
${{ github.token }}
uses: ${{ github.action_path }}/.github/actions/report-dso-event
with:
# TODO add coverage
args: >
-app "${{ inputs.app }}"
-id "${{ inputs.id }}"
-team "${{ inputs.team }}"
${{ inputs.event-type }}
-start "${{ steps.run-command.outputs.start-time }}"
-end "${{ steps.run-command.outputs.end-time }}"
-result "${{ steps.run-command.outputs.exit-code == '0' && 'pass' || 'fail' }}"
version: ${{ inputs.report-event-version }}

- name: Warn on failure
if: ${{ !cancelled() && steps.report-test-metrics.outcome == 'failure' }}
uses: ./.github/actions/warn-and-continue
with:
message: 'Failed to report test event metrics to the DSO Metrics API. Please investigate the failure.'

# TODO copy
# - name: Report deploy metrics
# id: report-deploy-metrics
# if: |
Expand All @@ -124,8 +126,8 @@ runs:
# -result ${{ steps.run-command.outputs.exit-code == '0' && 'success' || 'failure' }}"
# version: ${{ inputs.report-event-version }}

- name: Warn on failure
uses: ./.github/actions/warn-and-continue
if: ${{ !cancelled() && steps.report-deploy-metrics.outcome == 'failure' }}
with:
message: 'Failed to report deploy event metrics to the DSO Metrics API. Please investigate the failure.'
# - name: Warn on failure
# uses: ./.github/actions/warn-and-continue
# if: ${{ !cancelled() && steps.report-deploy-metrics.outcome == 'failure' }}
# with:
# message: 'Failed to report deploy event metrics to the DSO Metrics API. Please investigate the failure.'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "build/index.js",
"scripts": {
"compile": "ncc build src/report-event.ts -o build --license licenses.txt"
"compile": "ncc build src/index.ts -o build --license licenses.txt"
},
"keywords": [],
"author": "",
Expand Down
50 changes: 26 additions & 24 deletions src/report-event.ts → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawnSync } from "child_process";
import { arch, type } from "os";
import * as core from "@actions/core";
import { Octokit } from "@octokit/rest";
import { Endpoints } from "@octokit/types";
import * as semver from "semver";
import fs from "fs/promises";
import { GetResponseDataTypeFromEndpointMethod } from "@octokit/types";
Expand All @@ -11,8 +11,13 @@ const releaseRepo = "mac-fc-report-event-releases";
const releaseBinaryName = "report-event";
const windowsType = "Windows_NT"; // https://nodejs.org/api/os.html#ostype

type ListReleasesResponseDataType =
Endpoints["GET /repos/{owner}/{repo}/releases"]["response"]["data"];
const octokit = new Octokit({
auth: core.getInput("token"),
});

type ListReleasesResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.repos.listReleases
>;

function formatReleaseName(version: string, type: string, arch: string) {
let name = `${releaseBinaryName}_${version}_${type}_${arch}`;
Expand All @@ -38,10 +43,7 @@ async function downloadAsset(name: string, url: string) {
}
}

async function downloadRelease(
octokit: Octokit,
version: string
): Promise<string> {
async function downloadRelease(version: string): Promise<string> {
let releases: ListReleasesResponseDataType;
try {
const response = await octokit.repos.listReleases({
Expand Down Expand Up @@ -89,38 +91,38 @@ async function downloadRelease(
}

async function main() {
if (process.argv.length !== 5) {
console.error(
"Missing required arguments. Usage: node report-event.ts <args> <version> <token>"
);
process.exit(1);
}

const args = process.argv[2].split(" ");
const version = process.argv[3];
const token = process.argv[4];

const octokit = new Octokit({
auth: token,
});
const version = core.getInput("version");
const args = core.getInput("args").split(" ");

let releaseName;
try {
releaseName = await downloadRelease(octokit, version);
releaseName = await downloadRelease(version);
} catch (error) {
console.error(`Error downloading release: ${error}`);
process.exit(1);
}

let returns;
try {
returns = spawnSync(`./${releaseName}`, args, { stdio: "inherit" });
returns = spawnSync(`./${releaseName}`, args);
} catch (error) {
console.error(`Error spawning child process: ${error}`);
process.exit(1);
}

process.exit(returns.status ?? 1);
const status = returns.status ?? 1;
const stdout = returns.stdout.toString();
const stderr = returns.stderr.toString();

// print the report-event output in the GitHub Action logs
console.log(stdout);
console.error(stderr);

core.setOutput("exit-code", status);
core.setOutput("output", stdout);
core.setOutput("error-output", stderr);

process.exit(status);
}

if (require.main === module) {
Expand Down

0 comments on commit b210019

Please sign in to comment.