Skip to content

Commit cac5bf6

Browse files
Copilotalexr00
andcommitted
Extract COMMIT_SHA_EXPRESSION as exported constant
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent f1f3db5 commit cac5bf6

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/github/prComment.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ export class TemporaryComment extends CommentBase {
217217
const SUGGESTION_EXPRESSION = /```suggestion(\u0020*(\r\n|\n))((?<suggestion>[\s\S]*?)(\r\n|\n))?```/;
218218
const IMG_EXPRESSION = /<img .*src=['"](?<src>.+?)['"].*?>/g;
219219
const UUID_EXPRESSION = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}/;
220+
export const COMMIT_SHA_EXPRESSION = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
220221

221222
export class GHPRComment extends CommentBase {
222223
private static ID = 'GHPRComment';
@@ -431,9 +432,7 @@ ${lineContents}
431432
// - Either 7 or 40 hex characters
432433
// - Not already part of a URL or markdown link
433434
// - Not inside code blocks (backticks)
434-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
435-
436-
return body.replace(commitShaRegex, (match, shortSha, remaining, offset) => {
435+
return body.replace(COMMIT_SHA_EXPRESSION, (match, shortSha, remaining, offset) => {
437436
// Don't replace if inside code blocks
438437
const beforeMatch = body.substring(0, offset);
439438
const backtickCount = (beforeMatch.match(/`/g)?.length ?? 0);

src/test/github/prComment.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,52 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { default as assert } from 'assert';
7-
import { replaceImages } from '../../github/prComment';
7+
import { COMMIT_SHA_EXPRESSION, replaceImages } from '../../github/prComment';
88

99
describe('commit SHA replacement', function () {
1010
it('should match 7-character commit SHAs', function () {
11-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
1211
const text = 'Fixed in commit 5cf56bc and also in abc1234';
13-
const matches = Array.from(text.matchAll(commitShaRegex));
12+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
1413
assert.strictEqual(matches.length, 2);
1514
assert.strictEqual(matches[0][1], '5cf56bc');
1615
assert.strictEqual(matches[1][1], 'abc1234');
1716
});
1817

1918
it('should match 40-character commit SHAs', function () {
20-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
2119
const text = 'Fixed in commit 5cf56bc1234567890abcdef1234567890abcdef0';
22-
const matches = Array.from(text.matchAll(commitShaRegex));
20+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
2321
assert.strictEqual(matches.length, 1);
2422
assert.strictEqual(matches[0][0], '5cf56bc1234567890abcdef1234567890abcdef0');
2523
});
2624

2725
it('should not match SHAs in URLs', function () {
28-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
2926
const text = 'https://github.com/owner/repo/commit/5cf56bc';
30-
const matches = Array.from(text.matchAll(commitShaRegex));
27+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
3128
assert.strictEqual(matches.length, 0);
3229
});
3330

3431
it('should not match SHAs in code blocks', function () {
35-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
3632
const text = 'Fixed in commit 5cf56bc but not in `abc1234`';
37-
const matches = Array.from(text.matchAll(commitShaRegex));
33+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
3834
// The regex will match both, but the replacement logic checks backtick count
3935
assert.strictEqual(matches.length, 2);
4036
});
4137

4238
it('should not match non-hex strings', function () {
43-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
4439
const text = 'Not a SHA: 1234xyz or ABCDEFG';
45-
const matches = Array.from(text.matchAll(commitShaRegex));
40+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
4641
assert.strictEqual(matches.length, 0);
4742
});
4843

4944
it('should not match SHAs with alphanumeric prefix', function () {
50-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
5145
const text = 'prefix5cf56bc is not a SHA';
52-
const matches = Array.from(text.matchAll(commitShaRegex));
46+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
5347
assert.strictEqual(matches.length, 0);
5448
});
5549

5650
it('should not match SHAs with alphanumeric suffix', function () {
57-
const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
5851
const text = '5cf56bcsuffix is not a SHA';
59-
const matches = Array.from(text.matchAll(commitShaRegex));
52+
const matches = Array.from(text.matchAll(COMMIT_SHA_EXPRESSION));
6053
assert.strictEqual(matches.length, 0);
6154
});
6255
});

0 commit comments

Comments
 (0)