-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor GitHubInfo.tsx into a reusable module
- Loading branch information
Showing
15 changed files
with
342 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
extends: ['@senecacdot/eslint-config-telescope'], | ||
|
||
env: { | ||
node: true, | ||
browser: true, | ||
commonjs: true, | ||
jest: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const baseConfig = require('../../jest.config.base'); | ||
|
||
module.exports = { | ||
...baseConfig, | ||
rootDir: '../..', | ||
testMatch: ['<rootDir>/src/github-url-parser/test/**/*.test.js'], | ||
collectCoverageFrom: ['<rootDir>/src/github-url-parser/src/**/*.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "@senecacdot/github-url-parser", | ||
"version": "0.1.0", | ||
"description": "A library for parsing GitHub URLs from HTM text", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "jest -c jest.config.js", | ||
"coverage": "jest -c jest.config.js --collect-coverage", | ||
"eslint": "eslint --config .eslintrc.js \"./**/*.js\"", | ||
"eslint-fix": "eslint --config .eslintrc.js \"./**/*.js\" --fix", | ||
"lint": "pnpm eslint" | ||
}, | ||
"repository": "Seneca-CDOT/telescope", | ||
"license": "BSD-2-Clause", | ||
"bugs": { | ||
"url": "https://github.com/Seneca-CDOT/telescope/issues" | ||
}, | ||
"devDependencies": { | ||
"jsdom": "18.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const parseGitHubUrl = require('./parse-github-url'); | ||
const reservedNames = require('./reserved-names'); | ||
|
||
module.exports = (gitHubUrls) => { | ||
const issues = new Set(); | ||
const pullRequests = new Set(); | ||
const repos = new Set(); | ||
const commits = new Set(); | ||
const users = new Set(); | ||
|
||
gitHubUrls | ||
.map((url) => parseGitHubUrl(url)) | ||
.filter(Boolean) | ||
.filter((url) => !reservedNames.includes(url.pathname.split('/').slice(1, 2)[0])) | ||
.forEach((url) => { | ||
const { pathname } = url; | ||
|
||
// Match urls that start with /<user> and optionally end with /<repo> or /<repo>/<anything-in-between>/<type>/<id> | ||
// <id> can be number, or a mixed of 40 alphanumeric (commit id) | ||
// Ex: /Seneca-CDOT/telescope/pull/2367 ✅ | ||
// Ex: /Seneca-CDOT/telescope ✅ | ||
// Ex: /Seneca-CDOT/telescope/pull/2367/commits/d3fagd3fagd3fagd3fagd3fagd3fag4d41265748 ✅ | ||
// Ex: /Seneca-CDOT/telescope/issues ✅ | ||
const matches = | ||
/^\/(?<user>[^/]+)(\/(?<repo>[^/]+)((\/(.*))?(\/(?<type>[^/]+)?\/(?<id>(\d+|\w{40}))\/?$))?)?/gi.exec( | ||
pathname | ||
); | ||
|
||
if (!matches?.groups) { | ||
return; | ||
} | ||
|
||
const { type, user, repo } = matches.groups; | ||
|
||
if (repo) { | ||
const repoUrl = `${user}/${repo}`; | ||
repos.add(repoUrl); | ||
} | ||
|
||
users.add(user); | ||
|
||
switch (type?.toLowerCase()) { | ||
case 'pull': | ||
pullRequests.add(pathname); | ||
break; | ||
case 'issues': | ||
issues.add(pathname); | ||
break; | ||
case 'commit': | ||
case 'commits': | ||
commits.add(pathname); | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
|
||
return { | ||
repos: Array.from(repos), | ||
issues: Array.from(issues), | ||
pullRequests: Array.from(pullRequests), | ||
commits: Array.from(commits), | ||
users: Array.from(users), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = (htmlString, parser) => { | ||
const doc = parser.parseFromString(htmlString, 'text/html'); | ||
|
||
const allGithubLinks = Array.from( | ||
// all links that have href that starts with 'https://github.com' | ||
doc.querySelectorAll("a[href^='https://github.com']"), | ||
(element) => element.href | ||
); | ||
|
||
return [...new Set(allGithubLinks)]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const extractGitHubInfo = require('./extract-github-info'); | ||
const htmlToUrls = require('./html-to-urls'); | ||
|
||
/** | ||
* Extract all GitHub URL info from an HTML string | ||
* @param {string} htmlString - a string of HTML | ||
* @param {{ DOMParser }} parser - an HTML DOM Parser | ||
*/ | ||
module.exports = (htmlString, { parser }) => { | ||
if (!parser && !('DOMParser' in globalThis)) { | ||
throw new Error('Missing parser property and environment does not support DOMParser'); | ||
} | ||
|
||
const urls = htmlToUrls(htmlString, parser || new globalThis.DOMParser()); | ||
return extractGitHubInfo(urls); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = (url) => { | ||
try { | ||
const ghUrl = new URL(url); | ||
if (ghUrl.hostname !== 'github.com') { | ||
return null; | ||
} | ||
return ghUrl; | ||
} catch (err) { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = [ | ||
'team', | ||
'enterprise', | ||
'explore', | ||
'marketplace', | ||
'pricing', | ||
'topics', | ||
'collections', | ||
'trending', | ||
'readme', | ||
'events', | ||
'sponsors', | ||
'mobile', | ||
'features', | ||
'customer-stories', | ||
'pulls', | ||
'issues', | ||
'notifications', | ||
'new', | ||
'organizations', | ||
'users', | ||
'settings', | ||
'discussions', | ||
'account', | ||
'about', | ||
'security', | ||
]; |
Oops, something went wrong.