Browse Source

feat(PrPage): display patch msg without tag

- display the commit message from the patch content if description tag
  is not present
- fix '[unknown commit id]' display

to run typescript tests ts-ject was added as a dev dependancy
master
DanConwayDev 2 years ago
parent
commit
0aa626454f
No known key found for this signature in database
GPG Key ID: 68E15486D73F75E1
  1. 5
      jest.config.cjs
  2. 2
      package.json
  3. 11
      src/lib/components/events/content/Patch.svelte
  4. 41
      src/lib/components/events/content/utils.spec.ts
  5. 11
      src/lib/components/events/content/utils.ts
  6. 4
      src/lib/wrappers/EventCard.svelte
  7. 45
      yarn.lock

5
jest.config.cjs

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

2
package.json

@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
"@sveltejs/adapter-node": "^1.2.3",
"@sveltejs/kit": "^1.5.0",
"@tailwindcss/typography": "^0.5.10",
"@types/jest": "^29.5.12",
"@types/jest-image-snapshot": "^6.2.1",
"@types/node": "^20.8.2",
"@types/ramda": "^0.29.10",
@ -53,6 +54,7 @@ @@ -53,6 +54,7 @@
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"tailwindcss": "^3.3.3",
"ts-jest": "^29.1.2",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.2.0"

11
src/lib/components/events/content/Kind317.svelte → src/lib/components/events/content/Patch.svelte

@ -2,12 +2,17 @@ @@ -2,12 +2,17 @@
import type { NDKTag } from '@nostr-dev-kit/ndk'
import parseDiff from 'parse-diff'
import ParsedContent from './ParsedContent.svelte'
import { extractPatchMessage } from './utils'
export let content: string = ''
export let tags: NDKTag[] = []
let commit_id = extractTagContent('commit') || '[unknown commit_id]'
let commit_message = extractTagContent('description') || '[untitled]'
let commit_id_shorthand =
extractTagContent('commit')?.substring(0, 8) || '[commit_id unknown]'
let commit_message =
extractTagContent('description') ||
extractPatchMessage(content) ||
'[untitled]'
let files = parseDiff(content)
function extractTagContent(name: string): string | undefined {
@ -29,7 +34,7 @@ @@ -29,7 +34,7 @@
<tr>
<td class="text-xs">Changes: </td>
<td class="text-right">
<span class="font-mono text-xs">{commit_id.substring(0, 8)}</span>
<span class="font-mono text-xs">{commit_id_shorthand}</span>
</td>
</tr>
{#each files as file}

41
src/lib/components/events/content/utils.spec.ts

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
import { extractPatchMessage } from './utils'
// const simple =
// const example = `From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf <fiatjaf@gmail.com>\nDate: Mon, 29 Jan 2024 09:41:27 -0300\nSubject: [PATCH] fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak\n\n helpers.go | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/helpers.go b/helpers.go\nindex 0b3790d..9b5c3da 100644\n--- a/helpers.go\n+++ b/helpers.go\n@@ -176,7 +176,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) {\n \t\t}\n \t\tsec, err := nip49.Decrypt(ncryptsec1, password)\n \t\tif err != nil {\n-\t\t\tfmt.Fprintf(os.Stderr, "failed to decrypt: %s", err)\n+\t\t\tcontinue\n \t\t}\n \t\treturn sec, nil\n \t}\n--\n2.43.0\n', tags: (3) […], kind: 1617, id: "fd5d1be541bf2d20c51ca63265cc893eecb4be8720db9b42abec21b9ca9747de", sig: "d4733b8b32c05d1fb33a76105926fc537e4060df25405521b3f74f91ed7d65f345386260e8a825c79d67c3dd67f5e7eea7d532cda48cb8d45f09f9be19775289", pubkey: "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d", … }`
test('extractPatchMessage - normal message end', () => {
expect(
extractPatchMessage(
'From 5ec8fb38b7e4d7b2081e276be456519e2dc76d46 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf <fiatjaf@gmail.com>\nDate: Mon, 29 Jan 2024 09:49:32 -0300\nSubject: [PATCH] invert alias order for `git str send --to`\n\n---\n send.go | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/send.go b/send.go\nindex bc81c00..d9017b6 100644\n--- a/send.go\n+++ b/send.go\n@@ -25,8 +25,8 @@ var send = &cli.Command{\n \t\t\tUsage: "if we should save the secret key to git config --local",\n \t\t},\n \t\t&cli.StringFlag{\n-\t\t\tName: "repository",\n-\t\t\tAliases: []string{"a", "to"},\n+\t\t\tName: "to",\n+\t\t\tAliases: []string{"a", "repository"},\n \t\t\tUsage: "repository reference, as an naddr1... code",\n \t\t},\n \t\t&cli.StringSliceFlag{\n@@ -170,7 +170,7 @@ func getAndApplyTargetRepository(\n \t\treturn nil, nil\n \t}\n \n-\ttarget := c.String("repository")\n+\ttarget := c.String("to")\n \tvar stored string\n \tif target == "" {\n \t\ttarget, _ = git("config", "--local", "str.upstream")\n-- \n2.43.0'
)
).toEqual('invert alias order for `git str send --to`')
})
test('extractPatchMessage - unusual message end', () => {
expect(
extractPatchMessage(
`From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf <fiatjaf@gmail.com>\nDate: Mon, 29 Jan 2024 09:41:27 -0300\nSubject: [PATCH] fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak\n\n helpers.go | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/helpers.go b/helpers.go\nindex 0b3790d..9b5c3da 100644\n--- a/helpers.go\n+++ b/helpers.go\n@@ -176,7 +176,7 @@ func promptDecrypt(ncryptsec1 string) (string, error) {\n \t\t}\n \t\tsec, err := nip49.Decrypt(ncryptsec1, password)\n \t\tif err != nil {\n-\t\t\tfmt.Fprintf(os.Stderr, "failed to decrypt: %s", err)\n+\t\t\tcontinue\n \t\t}\n \t\treturn sec, nil\n \t}\n--\n2.43.0\n`
)
).toEqual(
'fix multi-attempt password prompt.\n\nthe print was doing nothing\nand the continue was missing\n---\nfound this bug while copying these functions to be used in nak'
)
})
test('extractPatchMessage - returns undefined if not parsed', () => {
expect(
extractPatchMessage(
`From 35ef1fe53b5a460266a1666709d886560d99cd67 Mon Sep 17 00:00:00 2001\nFrom: fiatjaf <fiatjaf@gmail.com>\nDate: Mon, 29 Jan 20`
)
).toBeUndefined()
})
// TODO make this pass
test.skip('extractPatchMessage - anotherunusual message end', () => {
expect(
extractPatchMessage(
`From 1263051aa4426937c5ef4f7616e06e9a8ea021e0 Mon Sep 17 00:00:00 2001\nFrom: William Casarin <jb55@jb55.com>\nDate: Mon, 22 Jan 2024 14:41:54 -0800\nSubject: [PATCH] Revert "mention: fix broken mentions when there is text is\n directly after"\n\nThis reverts commit af75eed83a2a1dd0eb33a0a27ded71c9f44dacbd.\n---\n damus/Views/PostView.swift | 7 -------\n damusTests/PostViewTests.swift | 22 ----------------------\n 2 files changed, 29 deletions(-)\n\ndiff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift\nindex 21ca0498..934ed7de 100644\n--- a/damus/Views/PostView.swift\n+++ b/damus/Views/PostView.swift\n@@ -619,13 +619,6 @@ func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts?\n func build_post(state: DamusState, post: NSMutableAttributedString, action: PostAction, uploadedMedias: [UploadedMedia], references: [RefId]) -> NostrPost {\n post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in\n if let link = attributes[.link] as? String {\n- let nextCharIndex = range.upperBound\n- if nextCharIndex < post.length,\n- let nextChar = post.attributedSubstring(from: NSRange(location: nextCharIndex, length: 1)).string.first,\n- !nextChar.isWhitespace {\n- post.insert(NSAttributedString(string: " "), at: nextCharIndex)\n- }\n-\n let normalized_link: String\n if link.hasPrefix("damus:nostr:") {\n // Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted.\ndiff --git a/damusTests/PostViewTests.swift b/damusTests/PostViewTests.swift\nindex 51976cad..ae78c3e6 100644\n--- a/damusTests/PostViewTests.swift\n+++ b/damusTests/PostViewTests.swift\n@@ -142,28 +142,6 @@ final class PostViewTests: XCTestCase {\n checkMentionLinkEditorHandling(content: content, replacementText: "", replacementRange: NSRange(location: 5, length: 28), shouldBeAbleToChangeAutomatically: true)\n \n }\n- \n- func testMentionLinkEditorHandling_noWhitespaceAfterLink1_addsWhitespace() {\n- var content: NSMutableAttributedString\n-\n- content = NSMutableAttributedString(string: "Hello @user ")\n- content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))\n- checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in\n- XCTAssertEqual(newManuallyEditedContent.string, "Hello @user up")\n- XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))\n- })\n- }\n- \n- func testMentionLinkEditorHandling_noWhitespaceAfterLink2_addsWhitespace() {\n- var content: NSMutableAttributedString\n-\n- content = NSMutableAttributedString(string: "Hello @user test")\n- content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))\n- checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in\n- XCTAssertEqual(newManuallyEditedContent.string, "Hello @user uptest")\n- XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))\n- })\n- }\n }\n \n func checkMentionLinkEditorHandling(\n\nbase-commit: c67741983e3f07f2386eaa388cb8a1475e8e0471\n-- \n2.42.0\n\n`
)
).toEqual(
'Revert "mention: fix broken mentions when there is text is\n directly after"\n\nThis reverts commit af75eed83a2a1dd0eb33a0a27ded71c9f44dacbd.'
)
})

11
src/lib/components/events/content/utils.ts

@ -93,3 +93,14 @@ export const parseContent = ({ content }: ContentArgs): ParsedPart[] => { @@ -93,3 +93,14 @@ export const parseContent = ({ content }: ContentArgs): ParsedPart[] => {
return result
}
/** this doesn't work for all patch formats and options */
export const extractPatchMessage = (s: string): string | undefined => {
try {
const t = s.split('\nSubject: [')[1].split('] ')[1]
if (t.split('\n\n---\n ').length > 1) return t.split('\n\n---\n ')[0]
return t.split('\n\ndiff --git ')[0].split('\n\n ').slice(0, -1).join('')
} catch {
return undefined
}
}

4
src/lib/wrappers/EventCard.svelte

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
<script lang="ts">
import EventWrapper from '$lib/components/events/EventWrapper.svelte'
import Kind19851985 from '$lib/components/events/content/Kind19851985.svelte'
import Kind317 from '$lib/components/events/content/Kind317.svelte'
import Patch from '$lib/components/events/content/Patch.svelte'
import ParsedContent from '$lib/components/events/content/ParsedContent.svelte'
import { defaults as user_defaults } from '$lib/components/users/type'
import { patch_kind, pr_status_kind } from '$lib/kinds'
@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
event_id={event.id}
>
{#if event.kind == patch_kind}
<Kind317 content={event.content} tags={event.tags} />
<Patch content={event.content} tags={event.tags} />
{:else if event.kind === pr_status_kind}
<Kind19851985 tags={event.tags} />
{:else}

45
yarn.lock

@ -3272,6 +3272,14 @@ @@ -3272,6 +3272,14 @@
expect "^29.0.0"
pretty-format "^29.0.0"
"@types/jest@^29.5.12":
version "29.5.12"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"
integrity sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"
"@types/json-schema@^7.0.12":
version "7.0.15"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
@ -4047,6 +4055,13 @@ browserslist@^4.21.10, browserslist@^4.22.2: @@ -4047,6 +4055,13 @@ browserslist@^4.21.10, browserslist@^4.22.2:
node-releases "^2.0.14"
update-browserslist-db "^1.0.13"
bs-logger@0.x:
version "0.2.6"
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
dependencies:
fast-json-stable-stringify "2.x"
bser@2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.1.tgz#e6787da20ece9d07998533cfd9de6f5c38f4bc05"
@ -5432,7 +5447,7 @@ fast-glob@^3.2.7, fast-glob@^3.2.9, fast-glob@^3.3.0: @@ -5432,7 +5447,7 @@ fast-glob@^3.2.7, fast-glob@^3.2.9, fast-glob@^3.3.0:
merge2 "^1.3.0"
micromatch "^4.0.4"
fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
@ -7046,7 +7061,7 @@ jest-snapshot@^29.7.0: @@ -7046,7 +7061,7 @@ jest-snapshot@^29.7.0:
pretty-format "^29.7.0"
semver "^7.5.3"
jest-util@^29.7.0:
jest-util@^29.0.0, jest-util@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-29.7.0.tgz#23c2b62bfb22be82b44de98055802ff3710fc0bc"
integrity sha512-z6EbKajIpqGKU56y5KBUgy1dt1ihhQJgWzUlZHArA/+X2ad7Cb5iF+AK1EWVL/Bo7Rz9uurpqw6SiBCefUbCGA==
@ -7346,6 +7361,11 @@ lodash.isplainobject@^4.0.6: @@ -7346,6 +7361,11 @@ lodash.isplainobject@^4.0.6:
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
integrity sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==
lodash.memoize@4.x:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==
lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
@ -7436,6 +7456,11 @@ make-dir@^4.0.0: @@ -7436,6 +7456,11 @@ make-dir@^4.0.0:
dependencies:
semver "^7.5.3"
make-error@1.x:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
makeerror@1.0.12:
version "1.0.12"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.12.tgz#3e5dd2079a82e812e983cc6610c4a2cb0eaa801a"
@ -9574,6 +9599,20 @@ ts-interface-checker@^0.1.9: @@ -9574,6 +9599,20 @@ ts-interface-checker@^0.1.9:
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==
ts-jest@^29.1.2:
version "29.1.2"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.1.2.tgz#7613d8c81c43c8cb312c6904027257e814c40e09"
integrity sha512-br6GJoH/WUX4pu7FbZXuWGKGNDuU7b8Uj77g/Sp7puZV6EXzuByl6JrECvm0MzVzSTkSHWTihsXt+5XYER5b+g==
dependencies:
bs-logger "0.x"
fast-json-stable-stringify "2.x"
jest-util "^29.0.0"
json5 "^2.2.3"
lodash.memoize "4.x"
make-error "1.x"
semver "^7.5.3"
yargs-parser "^21.0.1"
ts-toolbelt@^9.6.0:
version "9.6.0"
resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-9.6.0.tgz#50a25426cfed500d4a09bd1b3afb6f28879edfd5"
@ -10246,7 +10285,7 @@ yargs-parser@^18.1.2: @@ -10246,7 +10285,7 @@ yargs-parser@^18.1.2:
camelcase "^5.0.0"
decamelize "^1.2.0"
yargs-parser@^21.1.1:
yargs-parser@^21.0.1, yargs-parser@^21.1.1:
version "21.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

Loading…
Cancel
Save