Skip to content

Commit 898f578

Browse files
committed
updated webview to include git commit history
1 parent 49c98bf commit 898f578

4 files changed

Lines changed: 160 additions & 107 deletions

File tree

src/authProviders/auth0AuthProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Auth0AuthenticationProvider extends AuthProvider {
7575
id: "commit-" + uuid(),
7676
accessToken: accessToken,
7777
account: {
78-
label: userInfo.name,
78+
label: `${userInfo.name} <${userInfo.email}>`,
7979
id: userInfo.id,
8080
},
8181
scopes: scopes,
Lines changed: 57 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import { API } from "../../@types/git";
2+
import { API, Commit } from "../../@types/git";
33
import { CommitAPI } from "../../commitAPI";
44
import { getWebviewContent } from "../../utils";
55

@@ -24,6 +24,11 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
2424
return;
2525
}
2626

27+
if (!projects) {
28+
vscode.window.showInformationMessage("No projects found");
29+
return;
30+
}
31+
2732
// Get default project
2833
const defaultProject: Project | undefined =
2934
context.workspaceState.get("defaultProject");
@@ -38,11 +43,6 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
3843
}
3944
}
4045

41-
if (!projects) {
42-
vscode.window.showInformationMessage("No projects found");
43-
return;
44-
}
45-
4646
// Show list of projects and get the selection
4747
let selectedProjectTitle = await vscode.window.showQuickPick(
4848
projects.map((project: Project) => {
@@ -72,71 +72,43 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
7272
vscode.window.showErrorMessage("Project not found");
7373
return;
7474
}
75-
76-
// Prompt user if they want multiline or single line update
77-
const isMultilineUpdate = await vscode.window.showQuickPick(
78-
["Yes", "No"],
75+
// Create a WebView Panel
76+
const shareProjectUpdatePanel = vscode.window.createWebviewPanel(
77+
"commitExtension",
78+
"Share Project Update",
79+
vscode.ViewColumn.One,
7980
{
80-
placeHolder: "Do you want to add a multiline update?",
81+
enableScripts: true,
8182
}
8283
);
8384

84-
if (isMultilineUpdate === "No" || isMultilineUpdate === undefined) {
85-
// Open TextInput dialog
86-
vscode.window
87-
.showInputBox({
88-
prompt: "Enter the project update",
89-
placeHolder: "Implementing new feature ...",
90-
value: await getCommitMessage(context),
91-
})
92-
.then((value) => {
93-
if (value) {
94-
try {
95-
commitAPI.addProjectUpdate(selectedProject!.id, value);
96-
97-
vscode.window.showInformationMessage(
98-
"Project update successfully added"
99-
);
100-
} catch (e) {
101-
// Show the error message
102-
vscode.window.showErrorMessage("Unable to add project update");
103-
}
104-
}
105-
});
106-
} else {
107-
// Create a WebView Panel
108-
const shareProjectUpdatePanel = vscode.window.createWebviewPanel(
109-
"commitExtension",
110-
"Share Project Update",
111-
vscode.ViewColumn.One,
112-
{
113-
enableScripts: true,
114-
}
115-
);
85+
// Get git commits
86+
const repositoryCommits: Commit[] | undefined = await getGitCommits(
87+
context
88+
);
11689

117-
// Send Project ID to the WebView
118-
shareProjectUpdatePanel.webview.postMessage({
119-
command: "setWebViewProject",
120-
data: JSON.stringify({
121-
projectId: selectedProject.id,
122-
lastCommitMessage: await getCommitMessage(context),
123-
}),
124-
});
125-
126-
shareProjectUpdatePanel.webview.onDidReceiveMessage(
127-
async (message: any) => {
128-
await processWebviewMessage(message, context);
129-
130-
// Close the WebView Panel
131-
shareProjectUpdatePanel.dispose();
132-
},
133-
undefined,
134-
context.subscriptions
135-
);
90+
// Send Project ID to the WebView3
91+
shareProjectUpdatePanel.webview.postMessage({
92+
command: "setWebViewProject",
93+
data: JSON.stringify({
94+
projectId: selectedProject.id,
95+
repositoryCommits: repositoryCommits || [],
96+
}),
97+
});
13698

137-
// Add HTML to the WebView
138-
shareProjectUpdatePanel.webview.html = getWebviewContent(context);
139-
}
99+
shareProjectUpdatePanel.webview.onDidReceiveMessage(
100+
async (message: any) => {
101+
await processWebviewMessage(message, context);
102+
103+
// Close the WebView Panel
104+
shareProjectUpdatePanel.dispose();
105+
},
106+
undefined,
107+
context.subscriptions
108+
);
109+
110+
// Add HTML to the WebView
111+
shareProjectUpdatePanel.webview.html = getWebviewContent(context);
140112
},
141113
};
142114
};
@@ -151,7 +123,6 @@ const processWebviewMessage = async (
151123
case "submitUpdate":
152124
const projectId = data.projectId;
153125
const udpateContent = `${data.updateContent}`;
154-
console.log(projectId, udpateContent);
155126
const commitAPI = context.workspaceState.get("commitAPI") as CommitAPI;
156127
try {
157128
await commitAPI.addProjectUpdate(projectId, udpateContent);
@@ -167,33 +138,34 @@ const processWebviewMessage = async (
167138
}
168139
};
169140

170-
const getCommitMessage = async (context: vscode.ExtensionContext) => {
141+
const getGitCommits: (
142+
context: vscode.ExtensionContext
143+
) => Promise<Commit[] | undefined> = async (
144+
context: vscode.ExtensionContext,
145+
maxEntries: number = 10
146+
) => {
171147
const gitAPI = context.workspaceState.get<API>("gitAPI");
148+
172149
if (!gitAPI) {
173-
return;
150+
return [];
174151
}
175152

176-
// Get workspace folder
153+
// Get Worspace folder
177154
const workspaceFolders = vscode.workspace.workspaceFolders;
178-
if (!workspaceFolders) {
179-
return;
180-
}
181155

182-
// Get root path
183-
const rootPath = workspaceFolders[0].uri.fsPath;
184-
185-
// Get repository
186-
const repository = gitAPI.repositories.find(
187-
(repo) => repo.rootUri.fsPath === rootPath
188-
);
189-
190-
if (!repository) {
191-
return;
156+
if (!workspaceFolders || workspaceFolders.length === 0) {
157+
return [];
192158
}
193159

194-
// Get commit message
195-
const commit = await repository.getCommit("HEAD");
196-
return commit?.message || "";
160+
// Get the respository in the first workspace folder
161+
// TODO: At this point this will always pick the the first workspace folder
162+
// TODO: Add support for multiple workspace folders
163+
const workspaceFolder = workspaceFolders[0];
164+
const repository = gitAPI.getRepository(workspaceFolder.uri);
165+
166+
return await repository?.log({
167+
maxEntries,
168+
});
197169
};
198170

199171
export default shareProjectUpdate;

src/commitAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ export class CommitAPI {
230230
// TODO: Get worktree changes and suggest to add update to Commit Project
231231
const worktreeChanges = repository?.state.workingTreeChanges;
232232

233+
const repositoryLogs = await repository?.log({ maxEntries: 10 });
234+
233235
if (!worktreeChanges?.length) {
234236
return;
235237
}

static/webview/index.html

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,86 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<!-- Add bootstrap stylesheet -->
88
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous" />
9+
<style>
10+
body.vscode-light {
11+
color: black;
12+
}
13+
14+
body.vscode-dark {
15+
color: white;
16+
}
17+
18+
body.vscode-high-contrast {
19+
color: red;
20+
}
21+
22+
td,
23+
th {
24+
padding: 0.5rem;
25+
}
26+
27+
.my-custom-scrollbar {
28+
position: relative;
29+
height: 200px;
30+
overflow: auto;
31+
}
32+
33+
.table-wrapper-scroll-y {
34+
display: block;
35+
}
36+
</style>
937
</head>
1038

11-
<body class="container bg-transparent text-white">
12-
<h1 class="d-flex justify-content-center py-2">Add Update Project</h1>
13-
<p class="d-flex justify-content-center fst-italic">Update field is pre populated with last commit message from local git history</p>
14-
<!-- Markdown Editor and Preview -->
15-
<div class="d-flex justify-content-between">
16-
<textarea class="px-2 py-1 rounded" style="width: 47%" id="update-input" rows="20"></textarea>
17-
<p class="px-2 py-1 rounded bg-light h-auto m-0 overflow-auto text-black" style="width: 47%" id="update-preview"></p>
18-
</div>
19-
20-
<!-- Button to refresh projects -->
21-
<div class="d-flex justify-content-center w-100 pt-4">
22-
<button type="button" class="btn btn-primary" id="submit-update" onclick="handleSubmitUpdateClick()">
23-
Submit Update
24-
</button>
39+
<body style="background-color: var(--vscode-editor-background);">
40+
<div class="container">
41+
<div class="d-flex flex-column justify-content-center align-items-center">
42+
<h1 class="fs-2">Add Project Updates</h1>
43+
<p class="fs-6 fst-italic text-muted">Previous git commits can be a good reference point</p>
44+
</div>
45+
<div class="table-wrapper-scroll-y my-custom-scrollbar">
46+
<table class="table" style="max-height: 100px; overflow-y: scroll; color: var(--vscode-editor-);">
47+
<thead>
48+
<tr>
49+
<th scope="col">Author</th>
50+
<th scope="col">Message</th>
51+
<th scope="col">Date</th>
52+
<th scope="col">Hash</th>
53+
</tr>
54+
</thead>
55+
<tbody id="commit-table-body" class="table-striped">
56+
</tbody>
57+
</table>
58+
</div>
59+
<hr class="hr" />
60+
<div class="d-flex justify-content-evenly gap-4" style="height: 300px;">
61+
<div class="d-flex flex-column w-100 m-0">
62+
<p class="text-muted m-0">Enter Update</p>
63+
<textarea class="px-2 py-1 rounded" id="update-input" rows="12"></textarea>
64+
</div>
65+
<div class="d-flex flex-column w-100 m-0">
66+
<p class="text-muted m-0">Preview (Markdown Supported)</p>
67+
<div class="px-2 py-1 rounded h-100 bg-white m-0 text-black overflow-scroll" style="background-color: var(--vscode-editor-);" id="update-preview"></div>
68+
</div>
69+
</div>
70+
<div class="d-flex justify-content-center w-100 pt-4">
71+
<button type="button" class="btn btn-primary" id="submit-update" onclick="handleSubmitUpdateClick()">
72+
Submit Update
73+
</button>
74+
</div>
2575
</div>
2676
<script src=" https://cdn.jsdelivr.net/npm/showdown@2.1.0/dist/showdown.min.js "></script>
2777
<script>
2878
// Import the API, Keyring and some utility functions
2979
const vscode = acquireVsCodeApi();
3080
var converter = new showdown.Converter();
3181
let projectId = "";
32-
let lastCommitMessage = "";
82+
let repositoryCommits = [];
3383

3484
window.onload = () => {
3585
console.log("Loading")
36-
// Set the value of the input field
3786
const updateInput = document.getElementById("update-input");
38-
updateInput.value = lastCommitMessage;
87+
const updatePreview = document.getElementById("update-preview");
88+
3989
// Get focus on the input field
4090
document.getElementById("update-input").focus();
4191
};
@@ -75,6 +125,36 @@ <h1 class="d-flex justify-content-center py-2">Add Update Project</h1>
75125
});
76126
};
77127

128+
const populatedCommitTable = () => {
129+
const tableBody = document.getElementById("commit-table-body");
130+
tableBody.innerHTML = "";
131+
repositoryCommits.forEach((commit) => {
132+
const row = document.createElement("tr");
133+
const author = document.createElement("td");
134+
const message = document.createElement("td");
135+
const date = document.createElement("td");
136+
const hash = document.createElement("td");
137+
138+
// Convert Date to string with format "Wed, 21 Oct 2015 07:28:00 GMT"
139+
commit.commitDate = new Date(commit.commitDate).toUTCString();
140+
141+
// Get shorter hash
142+
commit.hash = commit.hash.substring(0, 7);
143+
144+
author.innerHTML = commit.authorEmail;
145+
message.innerHTML = commit.message;
146+
date.innerHTML = commit.commitDate;
147+
hash.innerHTML = commit.hash;
148+
149+
row.appendChild(author);
150+
row.appendChild(message);
151+
row.appendChild(date);
152+
row.appendChild(hash);
153+
154+
tableBody.appendChild(row);
155+
});
156+
}
157+
78158
// Handle message posted from the extension
79159
window.addEventListener("message", (event) => {
80160
const eventData = event.data;
@@ -85,11 +165,10 @@ <h1 class="d-flex justify-content-center py-2">Add Update Project</h1>
85165
case "setWebViewProject":
86166
// Update the project id
87167
const parsedData = JSON.parse(data);
168+
console.log(parsedData);
88169
projectId = parsedData.projectId;
89-
lastCommitMessage = parsedData.lastCommitMessage;
90-
// Set the value of the input field
91-
const updateInput = document.getElementById("update-input");
92-
updateInput.value = lastCommitMessage;
170+
repositoryCommits = parsedData.repositoryCommits || [];
171+
populatedCommitTable();
93172
break;
94173
default:
95174
break;

0 commit comments

Comments
 (0)