Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Labeling tested PRs with 'test' label #17

Merged
merged 6 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.TEAMS_NOT_USING_ZENHUB = exports.DRY_RUN_SLEEP_MINS = exports.TEAM_NAME_TO_LABEL = exports.TEAM_LABEL_PREFIX = exports.ZENHUB_WORKSPACE_ID = exports.PARENT_TEAM_SLUG = exports.ORGANIZATION = void 0;
exports.TESTED_LABEL_NAME = exports.TEAMS_NOT_USING_ZENHUB = exports.DRY_RUN_SLEEP_MINS = exports.TEAM_NAME_TO_LABEL = exports.TEAM_LABEL_PREFIX = exports.ZENHUB_WORKSPACE_ID = exports.PARENT_TEAM_SLUG = exports.ORGANIZATION = void 0;
exports.ORGANIZATION = 'apify';
exports.PARENT_TEAM_SLUG = 'product-engineering';
exports.ZENHUB_WORKSPACE_ID = '5f6454160d9f82000fa6733f';
Expand All @@ -17,6 +17,7 @@ exports.TEAM_NAME_TO_LABEL = {
};
exports.DRY_RUN_SLEEP_MINS = 2;
exports.TEAMS_NOT_USING_ZENHUB = ['Tooling'];
exports.TESTED_LABEL_NAME = 'tested';


/***/ }),
Expand Down Expand Up @@ -53,7 +54,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getLinkedEpics = exports.getLinkedIssue = exports.fail = exports.ensureCorrectLinkingAndEstimates = exports.addTeamLabel = exports.getTeamLabelName = exports.fillCurrentMilestone = exports.assignPrCreator = exports.findCurrentTeamMilestone = exports.findUsersTeamName = void 0;
exports.isPullRequestTested = exports.isTestFilePath = exports.getLinkedEpics = exports.getLinkedIssue = exports.fail = exports.ensureCorrectLinkingAndEstimates = exports.addTeamLabel = exports.getTeamLabelName = exports.fillCurrentMilestone = exports.assignPrCreator = exports.findCurrentTeamMilestone = exports.findUsersTeamName = void 0;
const axios_1 = __importDefault(__nccwpck_require__(8757));
const core = __importStar(__nccwpck_require__(2186));
const consts_1 = __nccwpck_require__(4831);
Expand Down Expand Up @@ -315,6 +316,31 @@ function getLinkedEpics(timelineItems) {
}
exports.getLinkedEpics = getLinkedEpics;
;
function isTestFilePath(filePath) {
const testFileNameRegex = /(\.|_|\w)*tests?(\.|_|\w)*\.\w{2,3}$/;
return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| testFileNameRegex.test(filePath);
}
exports.isTestFilePath = isTestFilePath;
;
/**
* Fetches a list of changed files and mark those that contain changes in test files.
*/
async function isPullRequestTested(octokit, pullRequest) {
const files = await octokit.rest.pulls.listFiles({
owner: consts_1.ORGANIZATION,
repo: pullRequest.base.repo.name,
pull_number: pullRequest.number,
});
const filePaths = files.data.map((file) => file.filename);
const testFilePaths = filePaths.filter((filePath) => isTestFilePath(filePath));
console.log(`${testFilePaths.length} test files found`);
console.log(`- ${testFilePaths.join('\n- ')}`);
return testFilePaths.length > 0;
}
exports.isPullRequestTested = isPullRequestTested;
;


/***/ }),
Expand Down Expand Up @@ -382,6 +408,15 @@ async function run() {
const teamLabel = pullRequestContext.labels.find((label) => label.name.startsWith(consts_1.TEAM_LABEL_PREFIX));
if (!teamLabel)
await (0, helpers_1.addTeamLabel)(github.context, repoOctokit, pullRequest, teamName);
const isTested = await (0, helpers_1.isPullRequestTested)(repoOctokit, pullRequest);
if (!isTested) {
await repoOctokit.rest.issues.addLabels({
owner: consts_1.ORGANIZATION,
repo: pullRequest.base.repo.name,
issue_number: pullRequest.number,
labels: [consts_1.TESTED_LABEL_NAME],
});
}
try {
if (isTeamUsingZenhub)
await (0, helpers_1.ensureCorrectLinkingAndEstimates)(pullRequest, repoOctokit, true);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const TEAM_NAME_TO_LABEL: { [name: string]: string} = {
export const DRY_RUN_SLEEP_MINS = 2;

export const TEAMS_NOT_USING_ZENHUB = ['Tooling'];

export const TESTED_LABEL_NAME = 'tested';
36 changes: 35 additions & 1 deletion src/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getLinkedIssue,
getLinkedEpics,
ZenhubTimelineItem,
isTestFilePath,
} from './helpers';

type Milestone = components['schemas']['milestone'];
Expand Down Expand Up @@ -125,7 +126,33 @@ describe('ZenHub events extractors', () => {
});
});

// mtrunkat: I use this one to test the action locally.
describe('isTestFilePath', () => {
test('works with filenames', () => {
expect(isTestFilePath('/dasdasd.test.js')).toBe(true);
expect(isTestFilePath('asdasdlddd.ahoj.ss')).toBe(false);
expect(isTestFilePath('bla.test.py')).toBe(true);
expect(isTestFilePath('some-dir/another/test.py')).toBe(true);
expect(isTestFilePath('asds/test/test.js')).toBe(true);
expect(isTestFilePath('inte')).toBe(false);
expect(isTestFilePath('bla.tests.py')).toBe(true);
expect(isTestFilePath('testk.py')).toBe(true);
expect(isTestFilePath('asds/test/test.js')).toBe(true);
expect(isTestFilePath('ahoj.mjs')).toBe(false);
expect(isTestFilePath('ahoj/test.mjs')).toBe(true);
expect(isTestFilePath('ahoj/zdar/tests.py')).toBe(true);
expect(isTestFilePath('my.tests.mjs')).toBe(true);
expect(isTestFilePath('ahoj/test_basic.py')).toBe(true);
expect(isTestFilePath('simething/jknkjnkj/js')).toBe(false);
});

test('works with directories', () => {
expect(isTestFilePath('something/test/something')).toBe(true);
expect(isTestFilePath('something/tests/something')).toBe(true);
expect(isTestFilePath('something/non-test/something')).toBe(false);
});
});

// mtrunkat: I use these to test the action locally.
/*
describe('ensureCorrectLinkingAndEstimates', () => {
test('works correctly with a PR', async () => {
Expand All @@ -135,4 +162,11 @@ describe('ensureCorrectLinkingAndEstimates', () => {
await ensureCorrectLinkingAndEstimates(pullRequest, octokit, false);
});
});

describe('isPullRequestTested', () => {
test('correctly returns true for tested PR', async () => {
const pullRequest = require('./mocks/pull_request.json'); // eslint-disable-line
console.log(await isPullRequestTested(getOctokit('xxx'), pullRequest));
});
});
*/
26 changes: 26 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,29 @@ export function getLinkedEpics(timelineItems: ZenhubTimelineItem[]): ZenhubIssue

return [...connectedEpics.values()];
};

export function isTestFilePath(filePath: string): boolean {
const testFileNameRegex = /(\.|_|\w)*tests?(\.|_|\w)*\.\w{2,3}$/;

return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| testFileNameRegex.test(filePath);
};

/**
* Fetches a list of changed files and mark those that contain changes in test files.
*/
export async function isPullRequestTested(octokit: OctokitType, pullRequest: PullRequest) {
const files = await octokit.rest.pulls.listFiles({
owner: ORGANIZATION,
repo: pullRequest.base.repo.name,
pull_number: pullRequest.number,
});
const filePaths = files.data.map((file) => file.filename);
const testFilePaths = filePaths.filter((filePath) => isTestFilePath(filePath));

console.log(`${testFilePaths.length} test files found`);
console.log(`- ${testFilePaths.join('\n- ')}`);

return testFilePaths.length > 0;
};
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
findUsersTeamName,
addTeamLabel,
ensureCorrectLinkingAndEstimates,
isPullRequestTested,
} from './helpers';
import {
TEAM_LABEL_PREFIX,
DRY_RUN_SLEEP_MINS,
TEAMS_NOT_USING_ZENHUB,
ORGANIZATION,
TESTED_LABEL_NAME,
} from './consts';

type Assignee = components['schemas']['simple-user'];
Expand Down Expand Up @@ -49,6 +52,15 @@ async function run(): Promise<void> {

const teamLabel = pullRequestContext.labels.find((label: Label) => label.name.startsWith(TEAM_LABEL_PREFIX));
if (!teamLabel) await addTeamLabel(github.context, repoOctokit, pullRequest, teamName);
const isTested = await isPullRequestTested(repoOctokit, pullRequest);
if (!isTested) {
await repoOctokit.rest.issues.addLabels({
owner: ORGANIZATION,
repo: pullRequest.base.repo.name,
issue_number: pullRequest.number,
labels: [TESTED_LABEL_NAME],
});
}

try {
if (isTeamUsingZenhub) await ensureCorrectLinkingAndEstimates(pullRequest, repoOctokit, true);
Expand Down