Skip to content

Commit

Permalink
show commits in Github sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewnt219 committed Oct 22, 2021
1 parent bce0b21 commit 4c2d115
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
75 changes: 75 additions & 0 deletions src/web/src/components/Posts/Commits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { VscGitCommit } from 'react-icons/vsc';
import { createStyles, makeStyles, Theme } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
link: {
textDecoration: 'none',
color: theme.palette.text.primary,
'&:hover': {
textDecorationLine: 'underline',
},
},
GitHubInfo: {
lineHeight: '2rem',
fontSize: '1.2rem',
},
GitHubLinkTitle: {
fontSize: '1.4rem',
margin: 0,
paddingTop: '1rem',
},
icon: {
fontSize: '2rem',
marginRight: '1rem',
verticalAlign: 'text-bottom',
},
commits: {
display: 'flex',
flexWrap: 'wrap',
margin: 0,
},
commit: {
marginRight: '2rem',
},
})
);

const SHORT_SHA_LENGTH = 7;

const getCommitNumber = (url: string, length?: number) =>
url.replace(/.+\/(commit|commits)\/(\w{40}).*/, '$2').substr(0, length);

type Props = {
commitUrls: string[];
};

const Commits = ({ commitUrls }: Props) => {
const classes = useStyles();

return (
<div className={classes.GitHubInfo}>
<h2 className={classes.GitHubLinkTitle}>
<VscGitCommit className={classes.icon}></VscGitCommit>
{commitUrls.length === 1 ? 'Commit' : 'Commits'}
</h2>
<p className={classes.commits}>
{commitUrls.map((url) => (
<p key={url} className={classes.commit}>
<a
href={`https://github.com${url}`}
rel="bookmark"
target="_blank"
title={'Commit ' + getCommitNumber(url)}
className={classes.link}
>
{getCommitNumber(url, SHORT_SHA_LENGTH)}
</a>
</p>
))}
</p>
</div>
);
};

export default Commits;
9 changes: 6 additions & 3 deletions src/web/src/components/Posts/GitHubInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createStyles, makeStyles, Theme, ListSubheader } from '@material-ui/cor
import Repos from './Repos';
import Issues from './Issues';
import PullRequests from './PullRequests';
import Commits from './Commits';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand Down Expand Up @@ -37,11 +38,12 @@ const filterGitHubUrls = (urls: string[]) => {
const { pathname } = url;

// Match urls that start with /<user>/<repo>, and optionally end with /<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/d3fag
// Ex: /Seneca-CDOT/telescope/pull/2367/commits/d3fagd3fagd3fagd3fagd3fagd3fag4d41265748
// Ex: /Seneca-CDOT/telescope/issues ✅
const matches = /^\/(?<user>[^\/]+)\/(?<repo>[^\/]+)((\/(.*))?\/(?<type>[^\/]+)\/(?<id>(\d+))\/?$)?/i.exec(
const matches = /^\/(?<user>[^\/]+)\/(?<repo>[^\/]+)((\/(.*))?\/(?<type>[^\/]+)\/(?<id>(\d+|\w{40}))\/?$)?/i.exec(
pathname
);
if (matches?.groups === undefined) {
Expand Down Expand Up @@ -92,14 +94,15 @@ const parseGitHubUrl = (url: string): URL | null => {

const GitHubInfo = ({ ghUrls }: Props) => {
const classes = useStyles();
const { repos, issues, pullRequests } = filterGitHubUrls(ghUrls);
const { repos, issues, pullRequests, commits } = filterGitHubUrls(ghUrls);

return (
<ListSubheader className={classes.root}>
<div className={classes.GitHubInfoContainer}>
{!!repos.length && <Repos repoUrls={repos} />}
{!!issues.length && <Issues issueUrls={issues} />}
{!!pullRequests.length && <PullRequests prUrls={pullRequests} />}
{!!commits.length && <Commits commitUrls={commits} />}
</div>
</ListSubheader>
);
Expand Down
1 change: 1 addition & 0 deletions src/web/src/components/Posts/Issues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useStyles = makeStyles((theme: Theme) =>
},
issues: {
display: 'flex',
flexWrap: 'wrap',
margin: 0,
},
issue: {
Expand Down
1 change: 1 addition & 0 deletions src/web/src/components/Posts/PullRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useStyles = makeStyles((theme: Theme) =>
},
pullRequests: {
display: 'flex',
flexWrap: 'wrap',
margin: 0,
},
pullRequest: {
Expand Down

0 comments on commit 4c2d115

Please sign in to comment.