Skip to content

Commit 5643bac

Browse files
authored
Merge pull request #28 from RefactorSecurity/set-comment-status-in-one-go
Improve note status marking behavior
2 parents e1d5318 + 31d2ef0 commit 5643bac

3 files changed

Lines changed: 59 additions & 37 deletions

File tree

src/extension.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,43 +181,62 @@ export function activate(context: vscode.ExtensionContext) {
181181
),
182182
);
183183

184-
// set note status as vulnerable button
184+
/**
185+
* Handles the common logic for setting a note's status via a command.
186+
*
187+
* @param reply The argument passed by the command (either CommentReply or just the thread).
188+
* @param status The NoteStatus to set (TODO, Vulnerable, Not Vulnerable).
189+
* @param noteMap The object storing all notes in memory.
190+
* @param remoteDb Remote db for collaboration.
191+
*/
192+
const handleSetStatusAction = (
193+
reply: vscode.CommentReply | { thread: vscode.CommentThread },
194+
status: NoteStatus,
195+
noteMap: Map<string, vscode.CommentThread>,
196+
remoteDb?: RemoteDb
197+
) => {
198+
const thread = reply.thread;
199+
// Extract the text of the reply box
200+
const text = 'text' in reply ? reply.text : undefined;
201+
202+
// Set the status (this function handles adding the status change comment)
203+
setNoteStatus(
204+
thread,
205+
status, // New status to set
206+
noteMap,
207+
'',
208+
remoteDb,
209+
text // Reply text
210+
);
211+
};
212+
213+
// --- Register the status commands ---
214+
215+
// Set note status as Vulnerable button
185216
context.subscriptions.push(
186217
vscode.commands.registerCommand(
187218
'security-notes.setNoteStatusVulnerable',
188-
(commentReply: vscode.CommentReply) =>
189-
setNoteStatus(
190-
commentReply.thread,
191-
NoteStatus.Vulnerable,
192-
noteMap,
193-
'',
194-
remoteDb,
195-
),
196-
),
219+
(reply: vscode.CommentReply | { thread: vscode.CommentThread }) =>
220+
handleSetStatusAction(reply, NoteStatus.Vulnerable, noteMap, remoteDb)
221+
)
197222
);
198223

199-
// set note status as not vulnerable button
224+
// Set note status as Not Vulnerable button
200225
context.subscriptions.push(
201226
vscode.commands.registerCommand(
202227
'security-notes.setNoteStatusNotVulnerable',
203-
(commentReply: vscode.CommentReply) =>
204-
setNoteStatus(
205-
commentReply.thread,
206-
NoteStatus.NotVulnerable,
207-
noteMap,
208-
'',
209-
remoteDb,
210-
),
211-
),
228+
(reply: vscode.CommentReply | { thread: vscode.CommentThread }) =>
229+
handleSetStatusAction(reply, NoteStatus.NotVulnerable, noteMap, remoteDb)
230+
)
212231
);
213232

214-
// set note status as TODO button
233+
// Set note status as TODO button
215234
context.subscriptions.push(
216235
vscode.commands.registerCommand(
217236
'security-notes.setNoteStatusToDo',
218-
(commentReply: vscode.CommentReply) =>
219-
setNoteStatus(commentReply.thread, NoteStatus.TODO, noteMap, '', remoteDb),
220-
),
237+
(reply: vscode.CommentReply | { thread: vscode.CommentThread }) =>
238+
handleSetStatusAction(reply, NoteStatus.TODO, noteMap, remoteDb)
239+
)
221240
);
222241

223242
// webview for importing tool results

src/helpers.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,29 @@ export const setNoteStatus = (
4343
noteMap: Map<string, vscode.CommentThread>,
4444
author?: string,
4545
remoteDb?: RemoteDb,
46+
replyText?: string,
4647
) => {
4748
const comment: vscode.Comment | any = thread.comments[0];
4849

49-
// Remove previous status if any
50-
let removed = false;
51-
Object.values(NoteStatus).forEach((noteStatus) => {
52-
if (!removed && comment.body.toString().startsWith(`[${noteStatus}] `)) {
53-
comment.body = comment.body.toString().slice(noteStatus.length + 3);
54-
removed = true;
55-
}
56-
});
50+
let originalText = comment.body.toString();
5751

58-
// Prepend new status on first note comment
59-
comment.body = `[${status}] ${comment.body}`;
60-
comment.savedBody = comment.body;
52+
// Clean up any existing status badges
53+
const statusValuesPattern = Object.values(NoteStatus).join('|');
54+
const statusRegex = new RegExp(`^\\[(${statusValuesPattern})\\] `, 'g');
55+
originalText = originalText.replace(statusRegex, '');
56+
57+
// Update the comment
58+
comment.body = `[${status}] ${originalText}`;
59+
comment.savedBody = originalText;
6160

6261
// Add note comment about status change
62+
const statusMessage = replyText ?
63+
`Status changed to ${status}.\n\n${replyText}` :
64+
`Status changed to ${status}.`;
65+
6366
saveNoteComment(
6467
thread,
65-
`Status changed to ${status}.`,
68+
statusMessage,
6669
false,
6770
noteMap,
6871
author ? author : '',

src/models/noteComment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class NoteComment implements vscode.Comment {
99
label: string | undefined;
1010
savedBody: string | vscode.MarkdownString; // for the Cancel button
1111
constructor(
12-
public body: string,
12+
public body: string | vscode.MarkdownString,
1313
public mode: vscode.CommentMode,
1414
public author: vscode.CommentAuthorInformation,
1515
public parent?: vscode.CommentThread,

0 commit comments

Comments
 (0)