Skip to content

Commit

Permalink
PR About Info (#11)
Browse files Browse the repository at this point in the history
* add footer to PR message with URL
* Utility function to strip URLs from text
* add URL to header
  • Loading branch information
FG-TUM authored Feb 9, 2024
1 parent b0c53a3 commit 78efd94
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
8 changes: 8 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import {
stripURLs,
assertNonNull,
getUrlToFile,
getUrlToChanges,
Expand Down Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 25 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<void>} Resolves when the function is complete.
Expand All @@ -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
}
Expand Down Expand Up @@ -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 += `---
<sub>[What is this?](${projectURL})</sub>`

return message
}

Expand Down Expand Up @@ -431,7 +448,7 @@ export async function run(): Promise<void> {

// ------------------------- 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.
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78efd94

Please sign in to comment.