From bfd7a450209c6c6b7c42940f914bd16c84ff4521 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Tue, 9 Oct 2018 08:53:30 +0300 Subject: [PATCH 1/4] Export the popup functions to be able to test them --- popup.js | 248 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 126 insertions(+), 122 deletions(-) diff --git a/popup.js b/popup.js index 58bc7e8..4ab84a8 100644 --- a/popup.js +++ b/popup.js @@ -39,128 +39,132 @@ btn.onclick = function() { result.innerHTML = "Please enter a valid Github Username"; } }; - -function getMessage(total_count) { - var message = ""; - switch (total_count) { - case 0: - return "It is never too late to start."; - case 1: - return "Still long way to go."; - case 2: - return "Awesome, you are almost half way through."; - case 3: - return "Almost there."; - case 4: - return "Just one more to go."; - default: - return "Congratulations, you have completed hacktoberfest 2018."; - } -} - -function updateMostRecentUsers(user) { - - chrome.storage.sync.get("mostRecentUsers", function(data) { - var mostRecentUsers = data["mostRecentUsers"] || []; - var hasUser = false; - if (mostRecentUsers.length > 0) { - for (var i = 0; i < mostRecentUsers.length; i++) { - if (mostRecentUsers[i].name === user.name) { - hasUser = true; +module.exports = { + getMessage(total_count) { + switch (total_count) { + case 0: + return "It is never too late to start."; + case 1: + return "Still long way to go."; + case 2: + return "Awesome, you are almost half way through."; + case 3: + return "Almost there."; + case 4: + return "Just one more to go."; + default: + return "Congratulations, you have completed hacktoberfest 2018."; } - } - } - - if (!hasUser) { - mostRecentUsers.push(user); - } - - if (mostRecentUsers.length > 5) { - mostRecentUsers.shift(); - } - - chrome.storage.sync.set({ mostRecentUsers: mostRecentUsers }, function() { - var html = ""; - for (var i = 0; i < mostRecentUsers.length; i++) { - html += `${mostRecentUsers[i].name}`; - } - var parent = document.getElementById("mostRecentUsers"); - parent.addEventListener("click", function(e) { - setData(e.target.id); - }); - parent.innerHTML = html; - }); - }); -} - -function setData(handle) { - document.getElementById("githubHandle").value = handle; - initData(handle); -} - -function getXHR(url) { - return new Promise(function(resolve, reject) { - var req = new XMLHttpRequest(); - req.open("GET", url, true); - req.onreadystatechange = function() { - if (this.status === 200 && this.readyState === 4) { - resolve(JSON.parse(req.responseText)); - } - }; - req.onerror = function() { - reject(Error("Network Error")); - }; - req.send(); - }); -} - -function initData(handle) { - var avatarUrl = ""; - - getXHR(`https://api.github.com/users/${handle}`) - .then(function(data) { - avatarUrl = data.avatar_url; - return getXHR( - `https://api.github.com/search/issues?q=author:${handle}+type:pr+created:2018-09-30T00:00:00-12:00..2018-10-31T23:59:59-12:00+is:public` - ); - }) - .then(function(data) { - var prCount = data.items.length || 0; - - updateMostRecentUsers({ name: handle, thumbnail: avatarUrl }); - chrome.storage.sync.set( - { lastSearched: handle, thumbnail: avatarUrl }, - function() { - var res = ""; - res += ``; - res += `
${handle}
`; - var count = (prCount > 5 ? "5" : prCount) + "/5"; - res += `
${count}
`; - var message = getMessage(prCount); - res += `
${message}
`; - document.getElementById("result").innerHTML = res; - - var newestPRs = prCount > 4 ? data.items.slice(0, 5) : data.items; - if (newestPRs.length > 0) { - var content = ""; - var prs = newestPRs.map((v, i) => { - return `
  • #${ - v["number"] - } - ${v["title"]}
  • `; + }, + + updateMostRecentUsers(user) { + chrome.storage.sync.get("mostRecentUsers", function (data) { + var mostRecentUsers = data["mostRecentUsers"] || []; + var hasUser = false; + if (mostRecentUsers.length > 0) { + for (var i = 0; i < mostRecentUsers.length; i++) { + if (mostRecentUsers[i].name === user.name) { + hasUser = true; + } + } + } + + if (!hasUser) { + mostRecentUsers.push(user); + } + + if (mostRecentUsers.length > 5) { + mostRecentUsers.shift(); + } + + chrome.storage.sync.set({ + mostRecentUsers: mostRecentUsers + }, function () { + var html = ""; + for (var i = 0; i < mostRecentUsers.length; i++) { + html += `${mostRecentUsers[i].name}`; + } + var parent = document.getElementById("mostRecentUsers"); + parent.addEventListener("click", function (e) { + setData(e.target.id); + }); + parent.innerHTML = html; }); - content += `

    Pull requests

    `; - - document.getElementById("dialogContent").innerHTML = content; - document.getElementById("show").style.visibility = "visible"; - } else { - document.getElementById("show").style.visibility = "collapse"; - } - } - ); - }) - .catch(function(error) { - console.error(error); - }); + }); + }, + + setData(handle) { + document.getElementById("githubHandle").value = handle; + initData(handle); + }, + + getXHR(url) { + return new Promise(function (resolve, reject) { + var req = new XMLHttpRequest(); + req.open("GET", url, true); + req.onreadystatechange = function () { + if (this.status === 200 && this.readyState === 4) { + resolve(JSON.parse(req.responseText)); + } + }; + req.onerror = function () { + reject(Error("Network Error")); + }; + req.send(); + }); + }, + + initData(handle) { + var avatarUrl = ""; + + getXHR(`https://api.github.com/users/${handle}`) + .then(function (data) { + avatarUrl = data.avatar_url; + return getXHR( + `https://api.github.com/search/issues?q=author:${handle}+type:pr+created:2018-09-30T00:00:00-12:00..2018-10-31T23:59:59-12:00+is:public` + ); + }) + .then(function (data) { + var prCount = data.items.length || 0; + + updateMostRecentUsers({ + name: handle, + thumbnail: avatarUrl + }); + + chrome.storage.sync.set({ + lastSearched: handle, + thumbnail: avatarUrl + }, + function () { + var res = ""; + res += ``; + res += `
    ${handle}
    `; + var count = (prCount > 5 ? "5" : prCount) + "/5"; + res += `
    ${count}
    `; + var message = getMessage(prCount); + res += `
    ${message}
    `; + document.getElementById("result").innerHTML = res; + + var newestPRs = prCount > 4 ? data.items.slice(0, 5) : data.items; + if (newestPRs.length > 0) { + var content = ""; + var prs = newestPRs.map((v, i) => { + return `
  • #${v["number"]} - ${v["title"]}
  • `; + }); + content += `

    Pull requests

    `; + + document.getElementById("dialogContent").innerHTML = content; + document.getElementById("show").style.visibility = "visible"; + } else { + document.getElementById("show").style.visibility = "collapse"; + } + } + ); + }) + .catch(function (error) { + console.error(error); + }); + } } From c9d2f858e8e0ee9f8bfa77178ec0109ab9dd8f75 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Tue, 9 Oct 2018 08:54:22 +0300 Subject: [PATCH 2/4] Separate the main javascript code into another file --- main.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ popup.html | 2 +- popup.js | 41 ----------------------------------------- 3 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 main.js diff --git a/main.js b/main.js new file mode 100644 index 0000000..787a6cc --- /dev/null +++ b/main.js @@ -0,0 +1,44 @@ +const setData = require("./popup").setData; +const initData = require("./popup").initData; + +var inputHandle = document.getElementById("githubHandle"); +var btn = document.getElementById("check"); + +if ( + document.getElementById("githubHandle").value === "" && + chrome.storage !== undefined +) { + chrome.storage.sync.get("lastSearched", function (data) { + var storedHandle = data["lastSearched"]; + if (storedHandle != "" && storedHandle != null) { + setData(storedHandle); + } + }); +} + +inputHandle.onkeydown = function (event) { + if (event.keyCode == 13) { + btn.click(); + } +}; + +document.getElementById("show").onclick = function () { + document.getElementById("dialog").showModal(); + document.getElementById("overlay").style.visibility = "visible"; +}; + +document.getElementById("closeBtn").onclick = function () { + document.getElementById("dialog").close(); + document.getElementById("overlay").style.visibility = "collapse"; +}; + +btn.onclick = function () { + var handle = document.getElementById("githubHandle").value; + var result = document.getElementById("result"); + if (handle != "" && handle != null) { + result.innerHTML = "Loading..."; + initData(handle); + } else { + result.innerHTML = "Please enter a valid Github Username"; + } +}; diff --git a/popup.html b/popup.html index 7279b4b..8a8cdf4 100644 --- a/popup.html +++ b/popup.html @@ -30,6 +30,6 @@ - + diff --git a/popup.js b/popup.js index 4ab84a8..8734e9c 100644 --- a/popup.js +++ b/popup.js @@ -1,44 +1,3 @@ -var inputHandle = document.getElementById("githubHandle"); -var btn = document.getElementById("check"); - -if ( - document.getElementById("githubHandle").value === "" && - chrome.storage !== undefined -) { - chrome.storage.sync.get("lastSearched", function(data) { - var storedHandle = data["lastSearched"]; - if (storedHandle != "" && storedHandle != null) { - setData(storedHandle); - } - }); -} - -inputHandle.onkeydown = function(event) { - if (event.keyCode == 13) { - btn.click(); - } -}; - -document.getElementById("show").onclick = function() { - document.getElementById("dialog").showModal(); - document.getElementById("overlay").style.visibility = "visible"; -}; - -document.getElementById("closeBtn").onclick = function() { - document.getElementById("dialog").close(); - document.getElementById("overlay").style.visibility = "collapse"; -}; - -btn.onclick = function() { - var handle = document.getElementById("githubHandle").value; - var result = document.getElementById("result"); - if (handle != "" && handle != null) { - result.innerHTML = "Loading..."; - initData(handle); - } else { - result.innerHTML = "Please enter a valid Github Username"; - } -}; module.exports = { getMessage(total_count) { switch (total_count) { From b88df873d7ff7d44a28dbc2a2ac77abb155132ae Mon Sep 17 00:00:00 2001 From: Ehsan Date: Tue, 9 Oct 2018 08:55:22 +0300 Subject: [PATCH 3/4] Test getMessage function --- test/test.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/test/test.js b/test/test.js index b525a01..127e8b2 100644 --- a/test/test.js +++ b/test/test.js @@ -1,8 +1,23 @@ -var assert = require("assert"); -describe("Array", function() { - describe("#indexOf()", function() { - it("should return -1 when the value is not present", function() { - assert.equal([1, 2, 3].indexOf(4), -1); +const assert = require("assert"); +const getMessage = require("../popup").getMessage; +const getXHR = require("../popup").getXHR; + +const messages = [ + "It is never too late to start.", + "Still long way to go.", + "Awesome, you are almost half way through.", + "Almost there.", + "Just one more to go.", + "Congratulations, you have completed hacktoberfest 2018." +]; + +function randomTotalCount(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +describe("#getMessage", function () { + it("returns the proper message based on number of total count", function() { + var totalCount = randomTotalCount(0, 5); + assert.equal(getMessage(totalCount), messages[totalCount]); }); - }); }); From 48500dcaefa928a62e53b34556eb2c0b6d5cfee4 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Thu, 11 Oct 2018 16:56:28 +0300 Subject: [PATCH 4/4] Add a contributor's name and avatar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fffd670..82001c7 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,4 @@ Thanks goes to these wonderful people for their contributions: | [
    Abhishek Deora](https://github.com/adeora7/) | [
    Duy Nguyen](https://github.com/zuik) | [
    Will Bray](https://github.com/raindogg) | [
    Luke Taylor](https://github.com/lmcjt37) | [
    Andreas Kleinbub](https://github.com/ehnydeel) | [
    Bhavesh Gohel](https://github.com/bhaveshgohel) | [
    Rafael Klaessen](https://github.com/rafaelklaessen) | | :---: | :---: | :---: | :---: | :--: | :--: | :--: | -| [
    Richard Robinson](https://github.com/kincade71) | [
    Fredrik Mäkilä](https://github.com/GitHug) | [
    Joseph Allen](https://github.com/GitHug) | +| [
    Richard Robinson](https://github.com/kincade71) | [
    Fredrik Mäkilä](https://github.com/GitHug) | [
    Joseph Allen](https://github.com/GitHug) | [
    Ehsan Zumrut](https://github.com/EhsanZ) |