From d72da8d8e59733b77a3a1567f6fd8189c8fd6ad8 Mon Sep 17 00:00:00 2001 From: Manuel de la Torre Date: Thu, 12 Dec 2024 11:49:34 -0600 Subject: [PATCH] Improvement: Add tests for execute function --- src/__tests__/execute.test.js | 190 ++++++++++++++++++ src/execute.js | 136 +++---------- .../__tests__/buildJsonOutput.test.js | 16 +- src/interactors/__tests__/getEntries.test.js | 45 +++++ src/interactors/__tests__/publish.test.js | 151 ++++++++++++++ src/interactors/buildJsonOutput.js | 8 +- src/interactors/getEntries.js | 26 +++ src/interactors/index.js | 4 + src/interactors/publish.js | 81 ++++++++ 9 files changed, 541 insertions(+), 116 deletions(-) create mode 100644 src/__tests__/execute.test.js create mode 100644 src/interactors/__tests__/getEntries.test.js create mode 100644 src/interactors/__tests__/publish.test.js create mode 100644 src/interactors/getEntries.js create mode 100644 src/interactors/publish.js diff --git a/src/__tests__/execute.test.js b/src/__tests__/execute.test.js new file mode 100644 index 0000000..3913c40 --- /dev/null +++ b/src/__tests__/execute.test.js @@ -0,0 +1,190 @@ +const core = require('@actions/core'); +const github = require('@actions/github'); +const { t } = require('../i18n'); +const { Telemetry } = require('../services'); +const { getGithubApiUrl } = require('../config'); +const { fetchPullRequestById } = require('../fetchers'); +const { + alreadyPublished, + checkSponsorship, + getPulls, + getEntries, + publish, +} = require('../interactors'); +const execute = require('../execute'); + +jest.mock('@actions/core', () => ({ + info: jest.fn(), + debug: jest.fn(), +})); + +jest.mock('@actions/github', () => ({ + getOctokit: jest.fn(), +})); + +jest.mock('../services', () => ({ + Telemetry: jest.fn(), +})); + +jest.mock('../config', () => ({ + getGithubApiUrl: jest.fn(), +})); + +jest.mock('../fetchers', () => ({ + fetchPullRequestById: jest.fn(), +})); + +jest.mock('../interactors', () => ({ + alreadyPublished: jest.fn(), + checkSponsorship: jest.fn(), + getPulls: jest.fn(), + getEntries: jest.fn(), + publish: jest.fn(), +})); + +describe('execute', () => { + const githubUrl = 'https://github.com'; + const entries = ['ENTRY1', 'ENTRY2', 'ENTRY3']; + const pulls = ['PULL1', 'PULL2', 'PULL3']; + const pullRequest = 'PULL_REQUEST'; + const isSponsor = 'isSponsor'; + const telemetry = { + start: jest.fn(), + success: jest.fn(), + error: jest.fn(), + }; + const inputs = { + org: 'org', + repos: 'repos', + mainStats: 'mainStats', + limit: 'limit', + sortBy: 'sortBy', + periodLength: 'periodLength', + disableLinks: 'disableLinks', + displayCharts: 'displayCharts', + publishAs: 'publishAs', + pullRequestId: 'pullRequestId', + slack: 'slack', + teams: 'teams', + webhook: 'webhook', + telemetry: 'telemetry', + githubToken: 'githubToken', + personalToken: 'personalToken', + }; + const globalOctokit = `OCTOKIT_${inputs.githubToken}`; + const personalOctokit = `OCTOKIT_${inputs.personalToken}`; + + Telemetry.mockReturnValue(telemetry); + getGithubApiUrl.mockReturnValue(githubUrl); + checkSponsorship.mockResolvedValue(isSponsor); + github.getOctokit.mockImplementation((token) => `OCTOKIT_${token}`); + fetchPullRequestById.mockResolvedValue(pullRequest); + alreadyPublished.mockReturnValue(false); + getPulls.mockResolvedValue(pulls); + getEntries.mockResolvedValue(entries); + + beforeEach(jest.clearAllMocks); + + it('executes the action successfully', async () => { + const results = await execute(inputs); + expect(results).toEqual({ entries, pullRequest }); + expect(fetchPullRequestById).toBeCalledWith(globalOctokit, inputs.pullRequestId); + expect(alreadyPublished).toBeCalledWith(pullRequest); + expect(getPulls).toBeCalledWith({ + org: inputs.org, + repos: inputs.repos, + octokit: personalOctokit, + startDate: expect.any(Date), + }); + expect(getEntries).toBeCalledWith({ + core, + pulls, + excludeStr: inputs.excludeStr, + periodLength: inputs.periodLength, + }); + expect(publish).toBeCalledWith({ + core, + octokit: globalOctokit, + entries, + pullRequest, + inputs: { ...inputs, isSponsor }, + }); + }); + + it('does not request the pull request when not sending an id', async () => { + const results = await execute({ ...inputs, pullRequestId: null }); + expect(results).toEqual({ entries, pullRequest: null }); + expect(fetchPullRequestById).not.toBeCalled(); + expect(alreadyPublished).toBeCalledWith(null); + expect(getPulls).toBeCalled(); + expect(getEntries).toBeCalled(); + expect(publish).toBeCalled(); + }); + + it('aborts the execution when stats are already published', async () => { + alreadyPublished.mockReturnValueOnce(true); + const results = await execute(inputs); + expect(results).toBeNull(); + expect(fetchPullRequestById).toBeCalledWith(globalOctokit, inputs.pullRequestId); + expect(alreadyPublished).toBeCalledWith(pullRequest); + expect(getPulls).not.toBeCalled(); + expect(getEntries).not.toBeCalled(); + expect(publish).not.toBeCalled(); + }); + + it('configures octokit correctly', async () => { + await execute(inputs); + expect(github.getOctokit).toBeCalledWith(inputs.githubToken, { baseUrl: githubUrl }); + expect(github.getOctokit).toBeCalledWith(inputs.personalToken, { baseUrl: githubUrl }); + }); + + describe('Sponsorship', () => { + it('checks if the user is a sponsor', async () => { + checkSponsorship.mockResolvedValueOnce(true); + await execute(inputs); + expect(checkSponsorship).toBeCalledWith({ + octokit: globalOctokit, + org: inputs.org, + repos: inputs.repos, + }); + expect(core.info).toBeCalledWith(t('execution.logs.sponsors')); + expect(Telemetry).toBeCalledWith(expect.objectContaining({ isSponsor: true })); + }); + + it('disables feature when the user is not a sponsor', async () => { + checkSponsorship.mockResolvedValueOnce(false); + await execute(inputs); + expect(checkSponsorship).toBeCalled(); + expect(core.info).not.toBeCalledWith(t('execution.logs.sponsors')); + expect(Telemetry).toBeCalledWith(expect.objectContaining({ isSponsor: false })); + }); + }); + + describe('Telemetry', () => { + it('tracks the start and end of a successful execution', async () => { + const results = await execute(inputs); + expect(Telemetry).toBeCalledWith({ + core, + isSponsor, + telemetry: inputs.telemetry, + }); + expect(telemetry.start).toBeCalledWith(inputs); + expect(telemetry.success).toBeCalledWith(results); + expect(telemetry.error).not.toBeCalled(); + }); + + it('tracks the start and end of a failed execution', async () => { + const error = new Error('Failed execution'); + publish.mockRejectedValueOnce(error); + await expect(execute(inputs)).rejects.toThrow(error); + expect(Telemetry).toBeCalledWith({ + core, + isSponsor, + telemetry: inputs.telemetry, + }); + expect(telemetry.start).toBeCalledWith(inputs); + expect(telemetry.success).not.toBeCalled(); + expect(telemetry.error).toBeCalledWith(error); + }); + }); +}); diff --git a/src/execute.js b/src/execute.js index 14e916b..78d760c 100644 --- a/src/execute.js +++ b/src/execute.js @@ -6,43 +6,16 @@ const { Telemetry } = require('./services'); const { fetchPullRequestById } = require('./fetchers'); const { getGithubApiUrl } = require('./config'); const { - getPulls, - buildTable, - postComment, - fulfillEntries, - getPullRequestStats, - getReviewStats, - getUsers, - mergeStats, - buildComment, - buildJsonOutput, - buildMarkdown, - checkSponsorship, alreadyPublished, - postSlackMessage, - postSummary, - postTeamsMessage, - postWebhook, + checkSponsorship, + getPulls, + getEntries, + publish, } = require('./interactors'); -const run = async (params) => { - const { - org, - repos, - limit, - sortBy, - octokit, - mainStats, - publishAs, - periodLength, - disableLinks, - personalToken, - displayCharts, - pullRequestId, - } = params; - - const pullRequest = pullRequestId - ? await fetchPullRequestById(octokit, pullRequestId) +const run = async ({ inputs, octokit }) => { + const pullRequest = inputs.pullRequestId + ? await fetchPullRequestById(octokit, inputs.pullRequestId) : null; if (alreadyPublished(pullRequest)) { @@ -51,77 +24,28 @@ const run = async (params) => { } const pulls = await getPulls({ - org, - repos, - octokit: github.getOctokit(personalToken, { baseUrl: getGithubApiUrl() }), - startDate: subtractDaysToDate(new Date(), periodLength), + org: inputs.org, + repos: inputs.repos, + octokit: github.getOctokit(inputs.personalToken, { baseUrl: getGithubApiUrl() }), + startDate: subtractDaysToDate(new Date(), inputs.periodLength), }); core.info(`Found ${pulls.length} pull requests to analyze`); - const users = await getUsers(pulls, { excludeStr: params.excludeStr }); - core.info(`Found ${users.length} collaborators to analyze`); - - const pullRequestStats = getPullRequestStats(pulls); - core.info(`Analyzed stats for ${pullRequestStats.length} authors`); - - const reviewStats = getReviewStats(pulls); - core.info(`Analyzed stats for ${reviewStats.length} reviewers`); - - const entries = fulfillEntries( - mergeStats({ users, pullRequestStats, reviewStats }), - { periodLength }, - ); - core.debug(`Analyzed users: ${entries.length}`); - - const table = buildTable({ - limit, - sortBy, - entries, - mainStats, - disableLinks, - displayCharts, - }); - core.debug('Table content built successfully'); - - const markdownTable = buildMarkdown({ table }); - core.debug('Markdown table built successfully'); - - const content = buildComment({ - org, - repos, - periodLength, - markdownTable, - isSponsor: params.isSponsor, + const entries = await getEntries({ + core, + pulls, + excludeStr: inputs.excludeStr, + periodLength: inputs.periodLength, }); - core.debug(`Commit content built successfully: ${content}`); + core.debug(`Analyzed entries: ${entries.length}`); - const whParams = { + await publish({ core, - org, - repos, - table, - periodLength, + octokit, + entries, pullRequest, - isSponsor: params.isSponsor, - }; - const jsonOutput = buildJsonOutput({ params, entries, pullRequest }); - await postWebhook({ core, payload: jsonOutput, webhook: params.webhook }); - await postSlackMessage({ ...whParams, slack: params.slack }); - await postTeamsMessage({ ...whParams, teams: params.teams }); - await postSummary({ core, content }); - await core.setOutput('resultsMd', markdownTable); - await core.setOutput('resultsJson', jsonOutput); - - if (pullRequestId) { - await postComment({ - octokit, - content, - publishAs, - pullRequestId, - currentBody: pullRequest.body, - }); - core.debug('Posted comment successfully'); - } + inputs, + }); return { entries, @@ -129,19 +53,23 @@ const run = async (params) => { }; }; -module.exports = async (params) => { - core.debug(`Params: ${JSON.stringify(params, null, 2)}`); +module.exports = async (inputs) => { + core.debug(`Inputs: ${JSON.stringify(inputs, null, 2)}`); - const { githubToken, org, repos } = params; + const { githubToken, org, repos } = inputs; const octokit = github.getOctokit(githubToken, { baseUrl: getGithubApiUrl() }); const isSponsor = await checkSponsorship({ octokit, org, repos }); - const telemetry = new Telemetry({ core, isSponsor, telemetry: params.telemetry }); + const telemetry = new Telemetry({ core, isSponsor, telemetry: inputs.telemetry }); if (isSponsor) core.info(t('execution.logs.sponsors')); try { - telemetry.start(params); - const results = await run({ ...params, isSponsor, octokit }); + telemetry.start(inputs); + const results = await run({ + octokit, + inputs: { ...inputs, isSponsor }, + }); telemetry.success(results); + return results; } catch (error) { telemetry.error(error); throw error; diff --git a/src/interactors/__tests__/buildJsonOutput.test.js b/src/interactors/__tests__/buildJsonOutput.test.js index 8dc1bef..b012dba 100644 --- a/src/interactors/__tests__/buildJsonOutput.test.js +++ b/src/interactors/__tests__/buildJsonOutput.test.js @@ -1,7 +1,7 @@ const buildJsonOutput = require('../buildJsonOutput'); describe('Interactors | .alreadyPublished', () => { - const params = { + const inputs = { githubToken: 'GITHUB_TOKEN', personalToken: 'PERSONAL_TOKEN', org: 'ORGANIZATION', @@ -11,24 +11,24 @@ describe('Interactors | .alreadyPublished', () => { }; const entries = 'ENTRIES'; const pullRequest = 'PULL_REQUEST'; - const input = { params, entries, pullRequest }; + const input = { inputs, entries, pullRequest }; - it('removes tokens and unknown params', () => { + it('removes tokens and unknown inputs', () => { const results = buildJsonOutput(input); expect(results).not.toHaveProperty('githubToken'); expect(results).not.toHaveProperty('personalToken'); expect(results).not.toHaveProperty('foo'); }); - it('keeps entries, pull request and important params', () => { + it('keeps entries, pull request and important inputs', () => { const results = buildJsonOutput(input); expect(results).toEqual(expect.objectContaining({ entries, pullRequest, options: expect.objectContaining({ - organization: params.org, + organization: inputs.org, repositories: null, - periodLength: params.periodLength, + periodLength: inputs.periodLength, }), })); }); @@ -36,12 +36,12 @@ describe('Interactors | .alreadyPublished', () => { it('uses repos when org is not sent', () => { const results = buildJsonOutput({ ...input, - params: { ...params, org: '' }, + inputs: { ...inputs, org: '' }, }); expect(results).toEqual(expect.objectContaining({ options: expect.objectContaining({ organization: null, - repositories: params.repos, + repositories: inputs.repos, }), })); }); diff --git a/src/interactors/__tests__/getEntries.test.js b/src/interactors/__tests__/getEntries.test.js new file mode 100644 index 0000000..2b9084a --- /dev/null +++ b/src/interactors/__tests__/getEntries.test.js @@ -0,0 +1,45 @@ +const { users, reviewStats, pullRequestStats } = require('../../../tests/mocks'); +const fulfillEntries = require('../fulfillEntries'); +const getPullRequestStats = require('../getPullRequestStats'); +const getReviewStats = require('../getReviewStats'); +const getUsers = require('../getUsers'); +const mergeStats = require('../mergeStats'); +const getEntries = require('../getEntries'); + +jest.mock('../fulfillEntries', () => jest.fn()); +jest.mock('../getPullRequestStats', () => jest.fn()); +jest.mock('../getReviewStats', () => jest.fn()); +jest.mock('../getUsers', () => jest.fn()); +jest.mock('../mergeStats', () => jest.fn()); + +describe('Interactors | .getEntries', () => { + const core = { info: jest.fn() }; + const pulls = ['PULL1', 'PULL2', 'PULL3']; + const entries = ['ENTRY1', 'ENTRY2', 'ENTRY3']; + const merged = 'MERGED'; + const params = { + core, + pulls, + excludeStr: 'EXCLUDE', + periodLength: 'PERIOD_LENGTH', + }; + + getUsers.mockReturnValue(users); + getPullRequestStats.mockReturnValue(pullRequestStats); + getReviewStats.mockReturnValue(reviewStats); + mergeStats.mockReturnValue(merged); + fulfillEntries.mockReturnValue(entries); + + beforeEach(jest.clearAllMocks); + + it('calls the correct interactors with the expected params', async () => { + const results = await getEntries(params); + expect(results).toBe(entries); + expect(getUsers).toBeCalledWith(pulls, { excludeStr: params.excludeStr }); + expect(getPullRequestStats).toBeCalledWith(pulls); + expect(getReviewStats).toBeCalledWith(pulls); + expect(mergeStats).toBeCalledWith({ users, pullRequestStats, reviewStats }); + expect(fulfillEntries).toBeCalledWith(merged, { periodLength: params.periodLength }); + expect(core.info).toHaveBeenCalledTimes(3); + }); +}); diff --git a/src/interactors/__tests__/publish.test.js b/src/interactors/__tests__/publish.test.js new file mode 100644 index 0000000..fa7797d --- /dev/null +++ b/src/interactors/__tests__/publish.test.js @@ -0,0 +1,151 @@ +const buildTable = require('../buildTable'); +const buildComment = require('../buildComment'); +const buildJsonOutput = require('../buildJsonOutput'); +const buildMarkdown = require('../buildMarkdown'); +const postComment = require('../postComment'); +const postSlackMessage = require('../postSlackMessage'); +const postSummary = require('../postSummary'); +const postTeamsMessage = require('../postTeamsMessage'); +const postWebhook = require('../postWebhook'); +const publish = require('../publish'); + +jest.mock('../buildTable', () => jest.fn()); +jest.mock('../buildComment', () => jest.fn()); +jest.mock('../buildJsonOutput', () => jest.fn()); +jest.mock('../buildMarkdown', () => jest.fn()); +jest.mock('../postComment', () => jest.fn()); +jest.mock('../postSlackMessage', () => jest.fn()); +jest.mock('../postSummary', () => jest.fn()); +jest.mock('../postTeamsMessage', () => jest.fn()); +jest.mock('../postWebhook', () => jest.fn()); + +describe('Interactors | .publish', () => { + const entries = ['ENTRY1', 'ENTRY2', 'ENTRY3']; + const table = 'TABLE'; + const octokit = 'OCTOKIT'; + const pullRequest = 'PULL_REQUEST'; + const markdownTable = 'MARKDOWN_TABLE'; + const comment = 'COMMENT'; + const jsonOutput = 'JSON_OUTPUT'; + const core = { + setOutput: jest.fn(), + debug: jest.fn(), + }; + const inputs = { + org: 'org', + repos: 'repos', + mainStats: 'mainStats', + limit: 'limit', + sortBy: 'sortBy', + periodLength: 'periodLength', + disableLinks: 'disableLinks', + displayCharts: 'displayCharts', + publishAs: 'publishAs', + pullRequestId: 'pullRequestId', + isSponsor: 'isSponsor', + slack: 'slack', + teams: 'teams', + webhook: 'webhook', + }; + const params = { + core, + octokit, + entries, + pullRequest, + inputs, + }; + + buildTable.mockReturnValue(table); + buildMarkdown.mockReturnValue(markdownTable); + buildComment.mockReturnValue(comment); + buildJsonOutput.mockReturnValue(jsonOutput); + + beforeEach(jest.clearAllMocks); + + it('calls the correct interactors with the expected params', async () => { + await publish(params); + + expect(buildTable).toBeCalled(); + expect(buildMarkdown).toBeCalledWith({ table }); + expect(buildComment).toBeCalled(); + expect(buildJsonOutput).toBeCalledWith({ inputs, entries, pullRequest }); + expect(postWebhook).toBeCalledWith({ core, payload: jsonOutput, webhook: inputs.webhook }); + expect(postSlackMessage).toBeCalled(); + expect(postTeamsMessage).toBeCalled(); + expect(postSummary).toBeCalledWith({ core, content: comment }); + expect(core.setOutput).toBeCalledWith('resultsMd', markdownTable); + expect(core.setOutput).toBeCalledWith('resultsJson', jsonOutput); + expect(postComment).toBeCalled(); + expect(core.debug).toBeCalledTimes(4); + }); + + it('builds the table with the correct params', async () => { + await publish(params); + expect(buildTable).toBeCalledWith({ + entries, + limit: inputs.limit, + sortBy: inputs.sortBy, + mainStats: inputs.mainStats, + disableLinks: inputs.disableLinks, + displayCharts: inputs.displayCharts, + }); + }); + + it('build the markdown comment with the correct params', async () => { + await publish(params); + expect(buildComment).toBeCalledWith({ + org: inputs.org, + repos: inputs.repos, + periodLength: inputs.periodLength, + markdownTable, + isSponsor: inputs.isSponsor, + }); + }); + + it('posts on slack with the correct params', async () => { + await publish(params); + expect(postSlackMessage).toBeCalledWith({ + core, + org: inputs.org, + repos: inputs.repos, + table, + periodLength: inputs.periodLength, + pullRequest, + isSponsor: inputs.isSponsor, + slack: inputs.slack, + }); + }); + + it('posts on teams with the correct params', async () => { + await publish(params); + expect(postTeamsMessage).toBeCalledWith({ + core, + org: inputs.org, + repos: inputs.repos, + table, + periodLength: inputs.periodLength, + pullRequest, + isSponsor: inputs.isSponsor, + teams: inputs.teams, + }); + }); + + it('posts a comment the pull request id is provided', async () => { + await publish(params); + expect(postComment).toBeCalledWith({ + octokit, + content: comment, + publishAs: inputs.publishAs, + pullRequestId: inputs.pullRequestId, + currentBody: pullRequest.body, + }); + }); + + it('does not post a comment if the pull request id is not provided', async () => { + await publish({ + ...params, + inputs: { ...inputs, pullRequestId: null }, + }); + expect(postComment).not.toBeCalled(); + }); +}); diff --git a/src/interactors/buildJsonOutput.js b/src/interactors/buildJsonOutput.js index 6601e5d..6f59ee1 100644 --- a/src/interactors/buildJsonOutput.js +++ b/src/interactors/buildJsonOutput.js @@ -1,12 +1,12 @@ module.exports = ({ - params, + inputs, entries, pullRequest, }) => ({ options: { - organization: params.org || null, - repositories: params.org ? null : params.repos, - periodLength: params.periodLength, + organization: inputs.org || null, + repositories: inputs.org ? null : inputs.repos, + periodLength: inputs.periodLength, }, entries, pullRequest, diff --git a/src/interactors/getEntries.js b/src/interactors/getEntries.js new file mode 100644 index 0000000..e50b7f3 --- /dev/null +++ b/src/interactors/getEntries.js @@ -0,0 +1,26 @@ +const fulfillEntries = require('./fulfillEntries'); +const getPullRequestStats = require('./getPullRequestStats'); +const getReviewStats = require('./getReviewStats'); +const getUsers = require('./getUsers'); +const mergeStats = require('./mergeStats'); + +module.exports = async ({ + core, + pulls, + excludeStr, + periodLength, +}) => { + const users = await getUsers(pulls, { excludeStr }); + core.info(`Found ${users.length} collaborators to analyze`); + + const pullRequestStats = getPullRequestStats(pulls); + core.info(`Analyzed stats for ${pullRequestStats.length} authors`); + + const reviewStats = getReviewStats(pulls); + core.info(`Analyzed stats for ${reviewStats.length} reviewers`); + + return fulfillEntries( + mergeStats({ users, pullRequestStats, reviewStats }), + { periodLength }, + ); +}; diff --git a/src/interactors/index.js b/src/interactors/index.js index 44793a1..51c2d3c 100644 --- a/src/interactors/index.js +++ b/src/interactors/index.js @@ -5,6 +5,7 @@ const buildJsonOutput = require('./buildJsonOutput'); const buildMarkdown = require('./buildMarkdown'); const checkSponsorship = require('./checkSponsorship'); const fulfillEntries = require('./fulfillEntries'); +const getEntries = require('./getEntries'); const getPulls = require('./getPulls'); const getPullRequestStats = require('./getPullRequestStats'); const getReviewStats = require('./getReviewStats'); @@ -15,6 +16,7 @@ const postSlackMessage = require('./postSlackMessage'); const postSummary = require('./postSummary'); const postTeamsMessage = require('./postTeamsMessage'); const postWebhook = require('./postWebhook'); +const publish = require('./publish'); module.exports = { alreadyPublished, @@ -24,6 +26,7 @@ module.exports = { buildMarkdown, checkSponsorship, fulfillEntries, + getEntries, getPulls, getPullRequestStats, getReviewStats, @@ -34,4 +37,5 @@ module.exports = { postSummary, postTeamsMessage, postWebhook, + publish, }; diff --git a/src/interactors/publish.js b/src/interactors/publish.js new file mode 100644 index 0000000..c5c6880 --- /dev/null +++ b/src/interactors/publish.js @@ -0,0 +1,81 @@ +const buildTable = require('./buildTable'); +const buildComment = require('./buildComment'); +const buildJsonOutput = require('./buildJsonOutput'); +const buildMarkdown = require('./buildMarkdown'); +const postComment = require('./postComment'); +const postSlackMessage = require('./postSlackMessage'); +const postSummary = require('./postSummary'); +const postTeamsMessage = require('./postTeamsMessage'); +const postWebhook = require('./postWebhook'); + +module.exports = async ({ + core, + octokit, + entries, + pullRequest, + inputs, +}) => { + const { + org, + repos, + mainStats, + limit, + sortBy, + periodLength, + disableLinks, + displayCharts, + publishAs, + pullRequestId, + isSponsor, + } = inputs; + + const table = buildTable({ + entries, + limit, + sortBy, + mainStats, + disableLinks, + displayCharts, + }); + core.debug('Table content built successfully'); + + const markdownTable = buildMarkdown({ table }); + core.debug('Markdown table built successfully'); + + const content = buildComment({ + org, + repos, + periodLength, + markdownTable, + isSponsor, + }); + core.debug(`Commit content built successfully: ${content}`); + + const whParams = { + core, + org, + repos, + table, + periodLength, + pullRequest, + isSponsor, + }; + const jsonOutput = buildJsonOutput({ inputs, entries, pullRequest }); + await postWebhook({ core, payload: jsonOutput, webhook: inputs.webhook }); + await postSlackMessage({ ...whParams, slack: inputs.slack }); + await postTeamsMessage({ ...whParams, teams: inputs.teams }); + await postSummary({ core, content }); + await core.setOutput('resultsMd', markdownTable); + await core.setOutput('resultsJson', jsonOutput); + + if (pullRequestId) { + await postComment({ + octokit, + content, + publishAs, + pullRequestId, + currentBody: pullRequest.body, + }); + core.debug('Posted comment successfully'); + } +};