33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
function createUpdateFollowUp() {
|
|
const clean = (value, limit) => String(value || '').replace(/\s+/g, ' ').trim().slice(0, limit);
|
|
const safeRepository = value => {
|
|
const candidate = String(value || '');
|
|
return /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(candidate) &&
|
|
candidate.split('/').every(part => part !== '.' && part !== '..') ? candidate : '';
|
|
};
|
|
const safeUrl = value => {
|
|
try {
|
|
const parsed = new URL(String(value || ''));
|
|
return parsed.protocol === 'https:' || parsed.protocol === 'http:' ? parsed.href : '';
|
|
} catch (_error) { return ''; }
|
|
};
|
|
|
|
function draft(detail = {}) {
|
|
const source = safeUrl(detail.url || detail.latest_comment?.url);
|
|
const context = clean(detail.latest_comment?.body || detail.subject_body, 9000);
|
|
const title = clean(detail.title, 228);
|
|
const sections = [];
|
|
if (source) sections.push('Source: ' + source);
|
|
if (context) sections.push('Latest context:\n> ' + context.replace(/\n/g, '\n> '));
|
|
return {
|
|
repository: safeRepository(detail.repository),
|
|
title: ('Follow up: ' + (title || 'Unread update')).slice(0, 240),
|
|
body: sections.join('\n\n').slice(0, 9500),
|
|
};
|
|
}
|
|
|
|
return { draft };
|
|
}
|
|
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = createUpdateFollowUp;
|