Dough Git

clove/isATracker

src/lib/server/rules/ownerUsernameMismatch.ts

back to src/lib/server/rules

/* src/lib/server/rules/ownerUsernameMismatch.ts
 * MIT — (c) Doughmination System®
 */

import type { Rule, Violation } from './types.ts';
import { isPlainObject } from './types.ts';

function readOwnerUsername(json: unknown): string | null {
	if (!isPlainObject(json) || !isPlainObject(json.owner)) {
		return null;
	}

	const username = json.owner.username;

	return typeof username === 'string' && username.trim() !== '' ? username.trim() : null;
}

export const ownerUsernameMismatchRule: Rule = {
	id: 'ownerUsernameMismatch',
	title: 'PR author differs from the registration owner',
	severity: 2,
	actionType: 'requestChanges',
	actionLabel: 'Request Changes',

	findLine(file) {
		const lineIndex = file.content.split('\n').findIndex((line) => /"username"\s*:/.test(line));

		return lineIndex === -1 ? null : lineIndex + 1;
	},

	async evaluate(context) {
		const author = context.pullRequest.author;
		const violations: Violation[] = [];

		for (const file of context.files) {
			if (file.status !== 'modified' && file.status !== 'renamed') {
				continue;
			}

			const ownerUsername = readOwnerUsername(file.json);

			if (ownerUsername === null || ownerUsername.toLowerCase() === author.toLowerCase()) {
				continue;
			}

			violations.push({
				ruleId: 'ownerUsernameMismatch',
				filePath: file.path,
				reason: `PR author (${author}) differs from owner.username (${ownerUsername}).`
			});
		}

		return violations;
	}
};