From 78efd940fac256c41d2a769c22c3b4b1edf07bf9 Mon Sep 17 00:00:00 2001 From: FG-TUM Date: Fri, 9 Feb 2024 15:07:43 +0100 Subject: [PATCH] PR About Info (#11) * add footer to PR message with URL * Utility function to strip URLs from text * add URL to header --- __tests__/utils.test.ts | 8 ++++++++ badges/coverage.svg | 2 +- dist/index.js | 28 +++++++++++++++++++++++++--- src/main.ts | 23 ++++++++++++++++++++--- src/utils.ts | 10 ++++++++++ 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 29c5176..48210c2 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -3,6 +3,7 @@ */ import { + stripURLs, assertNonNull, getUrlToFile, getUrlToChanges, @@ -143,4 +144,11 @@ describe('utils.ts context dependent', () => { const duplicatesFiltered = duplicates.filter(uniqueFilter) expect(duplicatesFiltered).toEqual(noDuplicates) }) + + it('stripURLs(): Test stripping', () => { + const textWithoutURL = 'some text' + const textWithURL = 'some [text](leURL)' + const textStripped = stripURLs(textWithURL) + expect(textWithoutURL).toEqual(textStripped) + }) }) diff --git a/badges/coverage.svg b/badges/coverage.svg index 6d43479..846c4a6 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 95.05%Coverage95.05% \ No newline at end of file +Coverage: 95.16%Coverage95.16% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 83881e4..cca14a7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -28941,6 +28941,11 @@ const github = __importStar(__nccwpck_require__(5438)); const fs = __importStar(__nccwpck_require__(7147)); const path = __importStar(__nccwpck_require__(1017)); const utils_1 = __nccwpck_require__(1314); +/** + * The URL to the project. + * Mainly used in the PR messages. + */ +const projectURL = 'https://github.com/AutoPas/DocTagChecker'; /** * Checks if file extensions start with a '.' and then only consist of letters and numbers. * @param extension The extensions to check. @@ -29063,6 +29068,10 @@ function checkDocumentation(userdocs, changes, docFileExtensions, srcFileExtensi /** * Remove last comment made by this action to avoid spam. * If no previous comment can be found do nothing. + * + * The header is used as a identification tag to find the correct message. + * This comparison is done ignoring any embedded URLs. + * * @param ghToken GitHub Token * @param header Header to identify the last bot message. * @returns {Promise} Resolves when the function is complete. @@ -29080,7 +29089,7 @@ async function deleteLastComment(ghToken, header) { const expectedUser = 'github-actions[bot]'; for (let i = commentsResponse.data.length - 1; i >= 0; --i) { if ((0, utils_1.assertNonNull)(commentsResponse.data[i].user).login === expectedUser && - (0, utils_1.assertNonNull)(commentsResponse.data[i].body).includes(header)) { + (0, utils_1.stripURLs)((0, utils_1.assertNonNull)(commentsResponse.data[i].body)).includes((0, utils_1.stripURLs)(header))) { return commentsResponse.data[i].id; } } @@ -29146,6 +29155,9 @@ The following doc files are unchanged, but some related sources were changed. Ma message += `- [ ] [${path.basename(docfile)}](${(0, utils_1.getUrlToFile)(docfile)}) (changed: ${tagsToUrls(tags)})\n`; } } + // Add footer with information about the bot. + message += `--- +[What is this?](${projectURL})`; return message; } /** @@ -29257,7 +29269,7 @@ async function run() { const { unchangedDoc, unknownTags } = checkDocumentation(docFiles, changedFiles, docFileExtensions, srcFileExtensions, dirTagSectionRegexStr); // ------------------------- Process the analysis ------------------------- // Common header to identify this bot's messages. - const header = '# DocTagChecker\n\n'; + const header = `# [DocTagChecker](${projectURL})\n\n`; // Remove the last comment to avoid spam. await deleteLastComment(ghToken, header); // Set outputs for other workflow steps to use. @@ -29321,11 +29333,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getUrlToChanges = exports.getUrlToFile = exports.findFileByName = exports.assertNonNull = exports.uniqueFilter = void 0; +exports.getUrlToChanges = exports.getUrlToFile = exports.findFileByName = exports.assertNonNull = exports.uniqueFilter = exports.stripURLs = void 0; const fs = __importStar(__nccwpck_require__(7147)); const path = __importStar(__nccwpck_require__(1017)); const github = __importStar(__nccwpck_require__(5438)); const crypto_1 = __importDefault(__nccwpck_require__(6113)); +/** + * Strips all named URLs down to only the name. + * '[leName](someURL)' -> 'leName' + * @param text The text to strip. + * @returns Stripped text + */ +function stripURLs(text) { + return text.replace(/\[(.*)\]\(.*\)/g, '$1'); +} +exports.stripURLs = stripURLs; /** * Filter function to remove duplicates in an array. * @param value Value to check for duplication diff --git a/src/main.ts b/src/main.ts index 235138c..59947dd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,9 +7,16 @@ import { findFileByName, getUrlToChanges, getUrlToFile, - uniqueFilter + uniqueFilter, + stripURLs } from './utils' +/** + * The URL to the project. + * Mainly used in the PR messages. + */ +const projectURL = 'https://github.com/AutoPas/DocTagChecker' + /** * Checks if file extensions start with a '.' and then only consist of letters and numbers. * @param extension The extensions to check. @@ -173,6 +180,10 @@ function checkDocumentation( /** * Remove last comment made by this action to avoid spam. * If no previous comment can be found do nothing. + * + * The header is used as a identification tag to find the correct message. + * This comparison is done ignoring any embedded URLs. + * * @param ghToken GitHub Token * @param header Header to identify the last bot message. * @returns {Promise} Resolves when the function is complete. @@ -195,7 +206,9 @@ async function deleteLastComment( for (let i = commentsResponse.data.length - 1; i >= 0; --i) { if ( assertNonNull(commentsResponse.data[i].user).login === expectedUser && - assertNonNull(commentsResponse.data[i].body).includes(header) + stripURLs(assertNonNull(commentsResponse.data[i].body)).includes( + stripURLs(header) + ) ) { return commentsResponse.data[i].id } @@ -279,6 +292,10 @@ The following doc files are unchanged, but some related sources were changed. Ma } } + // Add footer with information about the bot. + message += `--- +[What is this?](${projectURL})` + return message } @@ -431,7 +448,7 @@ export async function run(): Promise { // ------------------------- Process the analysis ------------------------- // Common header to identify this bot's messages. - const header = '# DocTagChecker\n\n' + const header = `# [DocTagChecker](${projectURL})\n\n` // Remove the last comment to avoid spam. await deleteLastComment(ghToken, header) // Set outputs for other workflow steps to use. diff --git a/src/utils.ts b/src/utils.ts index cb92dac..b6e420f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,16 @@ import * as path from 'path' import * as github from '@actions/github' import crypto from 'crypto' +/** + * Strips all named URLs down to only the name. + * '[leName](someURL)' -> 'leName' + * @param text The text to strip. + * @returns Stripped text + */ +export function stripURLs(text: string): string { + return text.replace(/\[(.*)\]\(.*\)/g, '$1') +} + /** * Filter function to remove duplicates in an array. * @param value Value to check for duplication