Skip to content

Commit e72ec3a

Browse files
committed
fix: prevent empty commits and remove unintentional leading blank lines
- Add logic to skip commits when solution content is unchanged - Remove unintentional leading blank lines that occur when submitting solution via CMD+Enter - Bump version of CodeHub in manifest.json
1 parent 7a2233e commit e72ec3a

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

manifest.backup.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "CodeHub",
33
"description": "Automatically pushes your Codewars submissions to GitHub",
44
"homepage_url": "https://github.com/FebinBellamy/CodeHub",
5-
"version": "1.0.0",
5+
"version": "2.0.2",
66
"author": "Febin Bellamy",
77
"action": {
88
"default_icon": {
@@ -23,11 +23,17 @@
2323
"type": "module",
2424
"persistent": false
2525
},
26-
"permissions": ["storage", "tabs", "scripting", "webNavigation", "activeTab"],
26+
"permissions": [
27+
"storage",
28+
"tabs",
29+
"scripting",
30+
"webNavigation",
31+
"activeTab"
32+
],
2733
"host_permissions": [
2834
"https://www.codewars.com/*",
2935
"https://github.com/*",
3036
"https://api.github.com/*"
3137
],
3238
"manifest_version": 3
33-
}
39+
}

manifest.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "CodeHub",
33
"description": "Automatically pushes your Codewars submissions to GitHub",
44
"homepage_url": "https://github.com/FebinBellamy/CodeHub",
5-
"version": "2.0.1",
5+
"version": "2.0.2",
66
"author": "Febin Bellamy",
77
"action": {
88
"default_icon": {
@@ -23,11 +23,17 @@
2323
"type": "module",
2424
"persistent": false
2525
},
26-
"permissions": ["storage", "tabs", "scripting", "webNavigation", "activeTab"],
26+
"permissions": [
27+
"storage",
28+
"tabs",
29+
"scripting",
30+
"webNavigation",
31+
"activeTab"
32+
],
2733
"host_permissions": [
2834
"https://www.codewars.com/*",
2935
"https://github.com/*",
3036
"https://api.github.com/*"
3137
],
3238
"manifest_version": 3
33-
}
39+
}

scripts/codewars.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,16 @@ chrome.storage.local.get(["isRepoConnected"], (result) => {
3636
const getUserSolution = () => {
3737
const userCodeBeforeParsing =
3838
document.querySelectorAll(".CodeMirror-lines")[0].innerText;
39-
const userCodeArr = userCodeBeforeParsing.split("\n");
40-
let parsedUserSolution = "";
41-
42-
for (let i = 0; i < userCodeArr.length; i++) {
43-
let elem = userCodeArr[i];
44-
if (/[^0-9]/.test(elem)) {
45-
parsedUserSolution += elem;
46-
if (i !== userCodeArr.length - 1) {
47-
parsedUserSolution += "\n";
48-
}
49-
}
39+
let userCodeArr = userCodeBeforeParsing.split("\n");
40+
41+
const firstNonEmptyIndex = userCodeArr.findIndex(line => /[^0-9]/.test(line) && line.trim() !== "");
42+
if (firstNonEmptyIndex > 0) {
43+
userCodeArr = userCodeArr.slice(firstNonEmptyIndex);
5044
}
51-
data["userSolution"] = parsedUserSolution;
45+
46+
data["userSolution"] = userCodeArr
47+
.filter(elem => /[^0-9]/.test(elem) && elem !== "")
48+
.join("\n");
5249
};
5350

5451
const interceptRedirection = () => {
@@ -78,10 +75,14 @@ chrome.storage.local.get(["isRepoConnected"], (result) => {
7875
document.addEventListener("keydown", (event) => {
7976
const isCmdOrCtrlKeyPressed = event.ctrlKey || event.metaKey;
8077
const isEnterKeyPressed = event.key === "Enter";
78+
const attemptButton = document.querySelector("#attempt_btn");
79+
const isSubmitButtonVisible =
80+
!submitButton.classList.contains("is-hidden") &&
81+
attemptButton.style.display === "none";
8182
if (
8283
isCmdOrCtrlKeyPressed &&
8384
isEnterKeyPressed &&
84-
!submitButton.classList.contains("is-hidden")
85+
isSubmitButtonVisible
8586
) {
8687
submitButton.click();
8788
}

scripts/codewarsToGithub.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ const checkFileExists = async (baseUrl, accessToken) => {
8989
};
9090
};
9191

92+
const isContentIdentical = (existingContentBase64, newContentBase64) => {
93+
try {
94+
const existingContentBase64Clean = existingContentBase64.replace(/\s/g, "");
95+
const existingContent = decodeURIComponent(
96+
escape(atob(existingContentBase64Clean))
97+
).replace(/\r\n/g, "\n").replace(/\r/g, "\n");
98+
99+
const newContent = decodeURIComponent(escape(atob(newContentBase64)))
100+
.replace(/\r\n/g, "\n")
101+
.replace(/\r/g, "\n");
102+
103+
const existingNormalized = existingContent.replace(/^\n+/, "").replace(/\n+$/, "");
104+
const newNormalized = newContent.replace(/^\n+/, "").replace(/\n+$/, "");
105+
106+
return existingNormalized === newNormalized;
107+
} catch (error) {
108+
console.log("Error comparing file content:", error);
109+
return false;
110+
}
111+
};
112+
92113
const getUrl = (
93114
githubUsername,
94115
repo,
@@ -164,6 +185,12 @@ const addOrUpdateSolution = async (
164185
};
165186

166187
if (fileExists) {
188+
if (isContentIdentical(fileData.content, encodedSolution)) {
189+
console.log(
190+
`There were no changes detected in your solution file! No update needed.`
191+
);
192+
return;
193+
}
167194
data.sha = fileData.sha;
168195
}
169196

0 commit comments

Comments
 (0)