Skip to content

Commit

Permalink
v2.15.0: Support for alternative Github env (Enterprise)
Browse files Browse the repository at this point in the history
* ghes-friendly mods

* export functions to get github api and server urls

support GITHUB_SERVER_URL with tests

* update package version, changelog

* lint fixes

* build output
  • Loading branch information
erodewald authored Apr 28, 2024
1 parent 5d06691 commit b069f4c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.15.0] - 2024-04-27
### Added
- Support for hosted GitHub variants with non-standard domains (e.g., GitHub Enterprise)

## [2.14.0] - 2024-03-03
### Added
- Announcing the web version development.
Expand Down
21 changes: 14 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41197,10 +41197,13 @@ const getSlackLimits = () => ({
blocks: 50,
});
const getTeamsBytesLimit = () => 27_000;

const getGithubApiUrl = () => process.env.GITHUB_API_URL || 'https://api.github.com';
const getGithubServerUrl = () => process.env.GITHUB_SERVER_URL || 'https://github.com';
module.exports = {
getSlackLimits,
getTeamsBytesLimit,
getGithubApiUrl,
getGithubServerUrl,
};


Expand Down Expand Up @@ -41244,6 +41247,7 @@ const github = __nccwpck_require__(5438);
const { subtractDaysToDate } = __nccwpck_require__(9988);
const { Telemetry } = __nccwpck_require__(4786);
const { fetchPullRequestById } = __nccwpck_require__(8001);
const { getGithubApiUrl } = __nccwpck_require__(1855);
const {
getPulls,
buildTable,
Expand Down Expand Up @@ -41286,7 +41290,7 @@ const run = async (params) => {
const pulls = await getPulls({
org,
repos,
octokit: github.getOctokit(personalToken),
octokit: github.getOctokit(personalToken, { baseUrl: getGithubApiUrl() }),
startDate: subtractDaysToDate(new Date(), periodLength),
});
core.info(`Found ${pulls.length} pull requests to analyze`);
Expand Down Expand Up @@ -41339,7 +41343,7 @@ module.exports = async (params) => {
core.debug(`Params: ${JSON.stringify(params, null, 2)}`);

const { githubToken, org, repos } = params;
const octokit = github.getOctokit(githubToken);
const octokit = github.getOctokit(githubToken, { baseUrl: getGithubApiUrl() });
const isSponsor = await checkSponsorship({ octokit, org, repos });
const telemetry = new Telemetry({ core, isSponsor, telemetry: params.telemetry });
if (isSponsor) core.info('Thanks for sponsoring this project! 💙');
Expand Down Expand Up @@ -41705,8 +41709,9 @@ module.exports = (pullRequest) => {

const { t } = __nccwpck_require__(6830);
const { buildSources } = __nccwpck_require__(9988);
const { getGithubServerUrl } = __nccwpck_require__(1855);

const buildGithubLink = ({ description, path }) => `[${description}](https://github.com/${path})`;
const buildGithubLink = ({ description, path }) => `[${description}](${getGithubServerUrl()}/${path})`;

module.exports = ({
table,
Expand Down Expand Up @@ -42321,14 +42326,15 @@ module.exports = ({
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const { buildSources } = __nccwpck_require__(9988);
const { getGithubServerUrl } = __nccwpck_require__(1855);

const getPRText = (pullRequest) => {
const { url, number } = pullRequest || {};
if (!url || !number) return '';
return ` (<${url}|#${number}>)`;
};

const buildGithubLink = ({ description, path }) => `<https://github.com/${path}|${description}>`;
const buildGithubLink = ({ description, path }) => `<${getGithubServerUrl()}/${path}|${description}>`;

module.exports = ({
t,
Expand Down Expand Up @@ -42656,14 +42662,15 @@ module.exports = ({
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const { buildSources } = __nccwpck_require__(9988);
const { getGithubServerUrl } = __nccwpck_require__(1855);

const getPRText = (pullRequest) => {
const { url, number } = pullRequest || {};
if (!url || !number) return '';
return ` ([#${number}](${url}))`;
};

const buildGithubLink = ({ description, path }) => `[${description}](https://github.com/${path})`;
const buildGithubLink = ({ description, path }) => `[${description}](${getGithubServerUrl()}/${path})`;

module.exports = ({
t,
Expand Down Expand Up @@ -48055,7 +48062,7 @@ module.exports = JSON.parse('{"name":"mixpanel","description":"A simple server-s
/***/ ((module) => {

"use strict";
module.exports = JSON.parse('{"name":"pull-request-stats","version":"2.14.0","description":"Github action to print relevant stats about Pull Request reviewers","main":"dist/index.js","type":"commonjs","scripts":{"build":"eslint src && ncc build src/index.js -o dist -a","test":"jest","lint":"eslint ./"},"keywords":[],"author":"Manuel de la Torre","license":"MIT","jest":{"testEnvironment":"node","testMatch":["**/?(*.)+(spec|test).[jt]s?(x)"]},"dependencies":{"@actions/core":"^1.10.1","@actions/github":"^6.0.0","axios":"^1.6.7","humanize-duration":"^3.31.0","i18n-js":"^3.9.2","jsurl":"^0.1.5","lodash.get":"^4.4.2","markdown-table":"^2.0.0","mixpanel":"^0.18.0"},"devDependencies":{"@vercel/ncc":"^0.38.1","eslint":"^8.56.0","eslint-config-airbnb-base":"^15.0.0","eslint-plugin-import":"^2.29.1","eslint-plugin-jest":"^27.6.3","jest":"^29.7.0"},"funding":"https://github.com/sponsors/manuelmhtr","packageManager":"[email protected]"}');
module.exports = JSON.parse('{"name":"pull-request-stats","version":"2.15.0","description":"Github action to print relevant stats about Pull Request reviewers","main":"dist/index.js","type":"commonjs","scripts":{"build":"eslint src && ncc build src/index.js -o dist -a","test":"jest","lint":"eslint ./"},"keywords":[],"author":"Manuel de la Torre","license":"MIT","jest":{"testEnvironment":"node","testMatch":["**/?(*.)+(spec|test).[jt]s?(x)"]},"dependencies":{"@actions/core":"^1.10.1","@actions/github":"^6.0.0","axios":"^1.6.7","humanize-duration":"^3.31.0","i18n-js":"^3.9.2","jsurl":"^0.1.5","lodash.get":"^4.4.2","markdown-table":"^2.0.0","mixpanel":"^0.18.0"},"devDependencies":{"@vercel/ncc":"^0.38.1","eslint":"^8.56.0","eslint-config-airbnb-base":"^15.0.0","eslint-plugin-import":"^2.29.1","eslint-plugin-jest":"^27.6.3","jest":"^29.7.0"},"funding":"https://github.com/sponsors/manuelmhtr","packageManager":"[email protected]"}');

/***/ }),

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pull-request-stats",
"version": "2.14.0",
"version": "2.15.0",
"description": "Github action to print relevant stats about Pull Request reviewers",
"main": "dist/index.js",
"type": "commonjs",
Expand Down
5 changes: 4 additions & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const getSlackLimits = () => ({
blocks: 50,
});
const getTeamsBytesLimit = () => 27_000;

const getGithubApiUrl = () => process.env.GITHUB_API_URL || 'https://api.github.com';
const getGithubServerUrl = () => process.env.GITHUB_SERVER_URL || 'https://github.com';
module.exports = {
getSlackLimits,
getTeamsBytesLimit,
getGithubApiUrl,
getGithubServerUrl,
};
5 changes: 3 additions & 2 deletions src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const github = require('@actions/github');
const { subtractDaysToDate } = require('./utils');
const { Telemetry } = require('./services');
const { fetchPullRequestById } = require('./fetchers');
const { getGithubApiUrl } = require('./config');
const {
getPulls,
buildTable,
Expand Down Expand Up @@ -45,7 +46,7 @@ const run = async (params) => {
const pulls = await getPulls({
org,
repos,
octokit: github.getOctokit(personalToken),
octokit: github.getOctokit(personalToken, { baseUrl: getGithubApiUrl() }),
startDate: subtractDaysToDate(new Date(), periodLength),
});
core.info(`Found ${pulls.length} pull requests to analyze`);
Expand Down Expand Up @@ -98,7 +99,7 @@ module.exports = async (params) => {
core.debug(`Params: ${JSON.stringify(params, null, 2)}`);

const { githubToken, org, repos } = params;
const octokit = github.getOctokit(githubToken);
const octokit = github.getOctokit(githubToken, { baseUrl: getGithubApiUrl() });
const isSponsor = await checkSponsorship({ octokit, org, repos });
const telemetry = new Telemetry({ core, isSponsor, telemetry: params.telemetry });
if (isSponsor) core.info('Thanks for sponsoring this project! 💙');
Expand Down
13 changes: 13 additions & 0 deletions src/interactors/__tests__/buildComment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ const linkRepo = (repo) => `[${getRepoName(repo)}](https://github.com/${repo})`;
describe('Interactors | .buildComment', () => {
const title = '## Pull reviewers stats';

describe('when GITHUB_SERVER_URL is present', () => {
const periodLength = 1;
const message = `Stats of the last day for [${ORG}](https://github.example.io/${ORG}):`;

it('builds an environment-specific comment using this URL', () => {
process.env.GITHUB_SERVER_URL = 'https://github.example.io';
const expected = `${title}\n${message}\n${TABLE_MOCK}\n${FOOTER}`;
const response = buildComment({ periodLength, table: TABLE_MOCK, org: ORG });
delete process.env.GITHUB_SERVER_URL;
expect(response).toEqual(expected);
});
});

describe('when period length is 1', () => {
const periodLength = 1;
const message = `Stats of the last day for ${linkOrg(ORG)}:`;
Expand Down
3 changes: 2 additions & 1 deletion src/interactors/buildComment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { t } = require('../i18n');
const { buildSources } = require('../utils');
const { getGithubServerUrl } = require('../config');

const buildGithubLink = ({ description, path }) => `[${description}](https://github.com/${path})`;
const buildGithubLink = ({ description, path }) => `[${description}](${getGithubServerUrl()}/${path})`;

module.exports = ({
table,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ describe('Interactors | postSlackMessage | .buildSubtitle', () => {
org: ORG,
};

describe('when GITHUB_SERVER_URL is present', () => {
it('returns a subtitle with custom github server URL', () => {
process.env.GITHUB_SERVER_URL = 'https://github.example.io';
const pullRequestWithCustomDomain = {
number: 13,
url: 'https://github.example.io/manuelmhtr/pulls/13',
};
const linkOrgWithCustomDomain = (org) => `<https://github.example.io/${org}|${org}>`;
const response = buildSubtitle({ ...baseParams, pullRequest: pullRequestWithCustomDomain });
const prLinkWithCustomDomain = `(<${pullRequestWithCustomDomain.url}|#${pullRequestWithCustomDomain.number}>)`;
const sources = linkOrgWithCustomDomain(ORG);
delete process.env.GITHUB_SERVER_URL;
expect(response).toEqual([
{
type: 'section',
text: {
type: 'mrkdwn',
text: `${t('table.subtitle', { sources, count: periodLength })} ${prLinkWithCustomDomain}`,
},
},
{
type: 'divider',
},
]);
});
});

describe('when sending a pull request', () => {
it('returns a subtitle with no pull request data', () => {
const response = buildSubtitle({ ...baseParams, pullRequest });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { buildSources } = require('../../../utils');
const { getGithubServerUrl } = require('../../../config');

const getPRText = (pullRequest) => {
const { url, number } = pullRequest || {};
if (!url || !number) return '';
return ` (<${url}|#${number}>)`;
};

const buildGithubLink = ({ description, path }) => `<https://github.com/${path}|${description}>`;
const buildGithubLink = ({ description, path }) => `<${getGithubServerUrl()}/${path}|${description}>`;

module.exports = ({
t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ describe('Interactors | postTeamsMessage | .buildSubtitle', () => {
org: ORG,
};

describe('when GITHUB_SERVER_URL is present', () => {
it('returns a subtitle with custom github server URL', () => {
process.env.GITHUB_SERVER_URL = 'https://github.example.io';
const pullRequestWithCustomDomain = {
number: 13,
url: 'https://github.example.io/manuelmhtr/pulls/13',
};
const linkOrgWithCustomDomain = (org) => `[${org}](https://github.example.io/${org})`;
const response = buildSubtitle({ ...baseParams, pullRequest: pullRequestWithCustomDomain });
const prLinkWithCustomDomain = `([#${pullRequestWithCustomDomain.number}](${pullRequestWithCustomDomain.url}))`;
const sources = linkOrgWithCustomDomain(ORG);
const text = `${t('table.subtitle', { sources, count: periodLength })} ${prLinkWithCustomDomain}`;
delete process.env.GITHUB_SERVER_URL;
expect(response).toEqual(wrapText(text));
});
});

describe('when sending a pull request', () => {
it('returns a subtitle with no pull request data', () => {
const response = buildSubtitle({ ...baseParams, pullRequest });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { buildSources } = require('../../../utils');
const { getGithubServerUrl } = require('../../../config');

const getPRText = (pullRequest) => {
const { url, number } = pullRequest || {};
if (!url || !number) return '';
return ` ([#${number}](${url}))`;
};

const buildGithubLink = ({ description, path }) => `[${description}](https://github.com/${path})`;
const buildGithubLink = ({ description, path }) => `[${description}](${getGithubServerUrl()}/${path})`;

module.exports = ({
t,
Expand Down

0 comments on commit b069f4c

Please sign in to comment.