From d8aca82dce7add1ae59d80d04b133fc08799dd57 Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Mon, 19 Feb 2024 14:36:39 +0100 Subject: [PATCH] Added output generation (#7) Now it generates an output file (`report.md`) and two extra outputs variables. --- README.md | 8 ++++++++ action.yml | 4 ++++ src/analytics.ts | 12 ++++++++++-- src/cli.ts | 2 +- src/index.ts | 11 ++++++++++- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0dd022e..69b96c1 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,11 @@ jobs: #### Outputs In the [action's summary](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/), it will publish the markdown file, so be sure to see the `summary` (which is different than the logs). + +It will also produce a file with the same content as the summary in a file named `report.md`. Be sure to use this file if you need it for rendering. + +It will also output three variables: + +- `repo`: The name of the repo in owner/repo pattern. +- `pr-report`: A JSON string with the metrics obtained from the repository PRs. +- `issue-report`: A JSON string with the metrics obtained from the repository issues. diff --git a/action.yml b/action.yml index 9ca57ee..eea20e6 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,10 @@ inputs: outputs: repo: description: "The name of the repo in owner/repo pattern" + pr-report: + description: "A JSON string with the metrics obtained from the repository PRs" + issue-report: + description: "A JSON string with the metrics obtained from the repository issues" runs: using: "docker" diff --git a/src/analytics.ts b/src/analytics.ts index 4f6c508..77b306f 100644 --- a/src/analytics.ts +++ b/src/analytics.ts @@ -4,13 +4,20 @@ import { RepositoryApi } from "./github/repository"; import { ActionLogger, GitHubClient } from "./github/types"; import { IssueAnalytics } from "./report/issues"; import { PullRequestAnalytics } from "./report/pullRequests"; +import { IssuesMetrics, PullRequestMetrics } from "./report/types"; import { generateSummary } from "./reporter"; +type MetricsReport = { + prMetrics: PullRequestMetrics; + issueMetrics: IssuesMetrics; + summary: typeof summary; +}; + export const getMetrics = async ( api: GitHubClient, logger: ActionLogger, repo: { owner: string; repo: string }, -): Promise => { +): Promise => { const repoApi = new RepositoryApi(api, logger, repo); const prReporter = new PullRequestAnalytics(repoApi, logger, repo); const prReport = await prReporter.fetchMetricsForPullRequests(); @@ -18,5 +25,6 @@ export const getMetrics = async ( const issuesReport = new IssueAnalytics(repoApi, logger); const issueReport = await issuesReport.getAnalytics(); - return generateSummary(repo, prReport, issueReport); + const sum = generateSummary(repo, prReport, issueReport); + return { prMetrics: prReport, issueMetrics: issueReport, summary: sum }; }; diff --git a/src/cli.ts b/src/cli.ts index fd86d80..b585ca9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -33,7 +33,7 @@ const action = async () => { owner: env.OWNER, repo: env.REPO, }); - await writeFile("./report.md", report.stringify()); + await writeFile("./report.md", report.summary.stringify()); }; action() diff --git a/src/index.ts b/src/index.ts index cbb21e3..baa50ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { getInput, setFailed, setOutput } from "@actions/core"; import { context, getOctokit } from "@actions/github"; import { Context } from "@actions/github/lib/context"; +import { writeFile } from "fs/promises"; import { getMetrics } from "./analytics"; import { generateCoreLogger } from "./util"; @@ -25,5 +26,13 @@ setOutput("repo", `${repo.owner}/${repo.repo}`); const token = getInput("GITHUB_TOKEN", { required: true }); getMetrics(getOctokit(token), generateCoreLogger(), repo) - .then(async (result) => await result.write()) + .then(async (result) => { + // We write the job output + await result.summary.write(); + await writeFile("./report.md", result.summary.stringify()); + + // We set the report for both outputs + setOutput("pr-report", JSON.stringify(result.prMetrics)); + setOutput("issue-report", JSON.stringify(result.issueMetrics)); + }) .catch(setFailed);