Skip to content

Commit 4dbce7e

Browse files
author
Hadrian De Oliveira
authored
feat: commit email in tooltip; display name with Sourcegraph username (#79)
* feat: commit email in tooltip; display name with Sourcegraph username * fix: lint * fix: test * fix: add test to cover decoration with username
1 parent 0c816b9 commit 4dbce7e

File tree

2 files changed

+89
-29
lines changed

2 files changed

+89
-29
lines changed

src/blame.test.ts

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const FIXTURE_HUNK_1: Hunk = {
1313
endLine: 2,
1414
author: {
1515
person: {
16+
1617
displayName: 'a',
18+
user: null,
1719
},
1820
date: '2018-09-10T21:52:45Z',
1921
},
@@ -29,7 +31,9 @@ const FIXTURE_HUNK_2: Hunk = {
2931
endLine: 3,
3032
author: {
3133
person: {
34+
3235
displayName: 'e',
36+
user: null,
3337
},
3438
date: '2018-11-10T21:52:45Z',
3539
},
@@ -45,7 +49,29 @@ const FIXTURE_HUNK_3: Hunk = {
4549
endLine: 4,
4650
author: {
4751
person: {
52+
4853
displayName: 'i',
54+
user: null,
55+
},
56+
date: '2018-10-10T21:52:45Z',
57+
},
58+
rev: 'j',
59+
message: 'k',
60+
commit: {
61+
url: 'l',
62+
},
63+
}
64+
65+
const FIXTURE_HUNK_4: Hunk = {
66+
startLine: 4,
67+
endLine: 5,
68+
author: {
69+
person: {
70+
71+
displayName: 'i',
72+
user: {
73+
username: 'testUserName',
74+
},
4975
},
5076
date: '2018-10-10T21:52:45Z',
5177
},
@@ -69,7 +95,7 @@ describe('getDecorationsFromHunk()', () => {
6995
backgroundColor: 'rgba(15, 43, 89, 0.65)',
7096
color: 'rgba(235, 235, 255, 0.55)',
7197
},
72-
hoverMessage: 'c',
98+
hoverMessage: `${FIXTURE_HUNK_1.author.person.email}${FIXTURE_HUNK_1.message}`,
7399
light: {
74100
backgroundColor: 'rgba(193, 217, 255, 0.65)',
75101
color: 'rgba(0, 0, 25, 0.55)',
@@ -105,7 +131,9 @@ describe('getDecorationsFromHunk()', () => {
105131
...FIXTURE_HUNK_1,
106132
author: {
107133
person: {
134+
108135
displayName: 'asdgjdsag asdklgbasdghladg asdgjlhbasdgjlhabsdg asdgilbadsgiobasgd',
136+
user: null,
109137
},
110138
date: '2018-09-10T21:52:45Z',
111139
},
@@ -123,7 +151,7 @@ describe('getDecorationsFromHunk()', () => {
123151
describe('getBlameDecorationsForSelections()', () => {
124152
it('adds decorations only for hunks that are within the selections', () => {
125153
const decorations = getBlameDecorationsForSelections(
126-
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3],
154+
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4],
127155
[new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(1, 0), new SOURCEGRAPH.Position(1, 0)) as any],
128156
NOW,
129157
SOURCEGRAPH as any
@@ -133,7 +161,7 @@ describe('getBlameDecorationsForSelections()', () => {
133161

134162
it('handles multiple selections', () => {
135163
const decorations = getBlameDecorationsForSelections(
136-
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3],
164+
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4],
137165
[
138166
new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(1, 0), new SOURCEGRAPH.Position(1, 0)) as any,
139167
new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(2, 0), new SOURCEGRAPH.Position(5, 0)) as any,
@@ -145,12 +173,13 @@ describe('getBlameDecorationsForSelections()', () => {
145173
expect(decorations).toEqual([
146174
getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any),
147175
getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any),
176+
getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any),
148177
])
149178
})
150179

151180
it('handles multiple hunks per selection', () => {
152181
const decorations = getBlameDecorationsForSelections(
153-
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3],
182+
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4],
154183
[new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(0, 0), new SOURCEGRAPH.Position(5, 0)) as any],
155184
NOW,
156185
SOURCEGRAPH as any
@@ -159,6 +188,7 @@ describe('getBlameDecorationsForSelections()', () => {
159188
getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any),
160189
getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any),
161190
getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any),
191+
getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any),
162192
])
163193
})
164194

@@ -182,11 +212,16 @@ describe('getBlameDecorationsForSelections()', () => {
182212
describe('getAllBlameDecorations()', () => {
183213
it('adds decorations for all hunks', () => {
184214
expect(
185-
getAllBlameDecorations([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3], NOW, SOURCEGRAPH as any)
215+
getAllBlameDecorations(
216+
[FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4],
217+
NOW,
218+
SOURCEGRAPH as any
219+
)
186220
).toEqual([
187221
getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any),
188222
getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any),
189223
getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any),
224+
getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any),
190225
])
191226
})
192227
})
@@ -201,7 +236,7 @@ describe('getBlameDecorations()', () => {
201236
},
202237
now: NOW,
203238
selections: null,
204-
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]),
239+
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4]),
205240
sourcegraph: SOURCEGRAPH as any,
206241
})
207242
).toEqual([])
@@ -216,13 +251,14 @@ describe('getBlameDecorations()', () => {
216251
},
217252
now: NOW,
218253
selections: null,
219-
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]),
254+
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4]),
220255
sourcegraph: SOURCEGRAPH as any,
221256
})
222257
).toEqual([
223258
getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any),
224259
getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any),
225260
getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any),
261+
getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any),
226262
])
227263
})
228264

@@ -235,11 +271,24 @@ describe('getBlameDecorations()', () => {
235271
},
236272
now: NOW,
237273
selections: [
238-
new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(2, 0), new SOURCEGRAPH.Position(2, 0)) as any,
274+
new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(3, 0), new SOURCEGRAPH.Position(3, 0)) as any,
239275
],
240-
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]),
276+
queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3, FIXTURE_HUNK_4]),
241277
sourcegraph: SOURCEGRAPH as any,
242278
})
243-
).toEqual([getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any)])
279+
).toEqual([getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any)])
280+
})
281+
282+
it('renders username in decoration content message', async () => {
283+
expect(
284+
getDecorationFromHunk(FIXTURE_HUNK_4, NOW, 3, SOURCEGRAPH as any).after!.contentText!.startsWith(
285+
`(${FIXTURE_HUNK_4.author.person.user!.username}) ${FIXTURE_HUNK_4.author.person.displayName}`
286+
)
287+
).toBe(true)
288+
expect(
289+
getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any).after!.contentText!.startsWith(
290+
`${FIXTURE_HUNK_3.author.person.displayName}`
291+
)
292+
).toBe(true)
244293
})
245294
})

src/blame.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ import { resolveURI } from './uri'
66
import { memoizeAsync } from './util/memoizeAsync'
77

88
export const getDecorationFromHunk = (
9-
hunk: Hunk,
9+
{ message, author, commit }: Hunk,
1010
now: number,
1111
decoratedLine: number,
1212
sourcegraph: typeof import('sourcegraph')
13-
): TextDocumentDecoration => ({
14-
range: new sourcegraph.Range(decoratedLine, 0, decoratedLine, 0),
15-
isWholeLine: true,
16-
after: {
17-
light: {
18-
color: 'rgba(0, 0, 25, 0.55)',
19-
backgroundColor: 'rgba(193, 217, 255, 0.65)',
13+
): TextDocumentDecoration => {
14+
const displayName = truncate(author.person.displayName, 25)
15+
const username = author.person.user ? `(${author.person.user.username}) ` : ''
16+
const distance = formatDistanceStrict(author.date, now, { addSuffix: true })
17+
return {
18+
range: new sourcegraph.Range(decoratedLine, 0, decoratedLine, 0),
19+
isWholeLine: true,
20+
after: {
21+
light: {
22+
color: 'rgba(0, 0, 25, 0.55)',
23+
backgroundColor: 'rgba(193, 217, 255, 0.65)',
24+
},
25+
dark: {
26+
color: 'rgba(235, 235, 255, 0.55)',
27+
backgroundColor: 'rgba(15, 43, 89, 0.65)',
28+
},
29+
contentText: `${username}${displayName}, ${distance}: • ${truncate(message, 45)}`,
30+
hoverMessage: `${author.person.email}${truncate(message, 1000)}`,
31+
linkURL: new URL(commit.url, sourcegraph.internal.sourcegraphURL.toString()).href,
2032
},
21-
dark: {
22-
color: 'rgba(235, 235, 255, 0.55)',
23-
backgroundColor: 'rgba(15, 43, 89, 0.65)',
24-
},
25-
contentText: `${truncate(hunk.author.person.displayName, 25)}, ${formatDistanceStrict(hunk.author.date, now, {
26-
addSuffix: true,
27-
})}: • ${truncate(hunk.message, 45)}`,
28-
hoverMessage: `${truncate(hunk.message, 1000)}`,
29-
linkURL: new URL(hunk.commit.url, sourcegraph.internal.sourcegraphURL.toString()).href,
30-
},
31-
})
33+
}
34+
}
3235

3336
export const getBlameDecorationsForSelections = (
3437
hunks: Hunk[],
@@ -75,7 +78,11 @@ const queryBlameHunks = memoizeAsync(
7578
endLine
7679
author {
7780
person {
81+
email
7882
displayName
83+
user {
84+
username
85+
}
7986
}
8087
date
8188
}
@@ -140,7 +147,11 @@ export interface Hunk {
140147
endLine: number
141148
author: {
142149
person: {
150+
email: string
143151
displayName: string
152+
user: {
153+
username: string
154+
} | null
144155
}
145156
date: string
146157
}

0 commit comments

Comments
 (0)