Skip to content

Commit

Permalink
Added output generation (#7)
Browse files Browse the repository at this point in the history
Now it generates an output file (`report.md`) and two extra outputs
variables.
  • Loading branch information
Bullrich authored Feb 19, 2024
1 parent 7649d64 commit d8aca82
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ 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<typeof summary> => {
): Promise<MetricsReport> => {
const repoApi = new RepositoryApi(api, logger, repo);
const prReporter = new PullRequestAnalytics(repoApi, logger, repo);
const prReport = await prReporter.fetchMetricsForPullRequests();

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 };
};
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);

0 comments on commit d8aca82

Please sign in to comment.