Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVXT-1941 GitHub Discussions Search #191

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ techdocs:
- www.youtube.com
- miro.com

search:
collators:
githubDiscussions:
url: https://github.com/guidanti/github-discussions-fetcher
# https://github.com/backstage/community-plugins/pull/1744

catalog:
providers:
githubOrg:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import React, { ReactNode } from 'react';
import Box from '@material-ui/core/Box';
import Chip from '@material-ui/core/Chip';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import { Link } from '@backstage/core-components';
import { ResultHighlight } from '@backstage/plugin-search-common';
import { HighlightedSearchResultText } from '@backstage/plugin-search-react';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { EntityRefLink } from '@backstage/plugin-catalog-react';

export interface GithubDiscussionsDocument extends IndexableDocument {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious about using IndexableDocument vs SearchDocument as per the documentation's comment about:

IndexableDocument is only useful for backends working directly with documents being inserted or retrieved from search indexes. When dealing with documents in the frontend, use SearchDocument

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch; we should use SearchDocument.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the changes to use SearchDocument and also made the changes in my PR in community-plugins (see commit). So once that gets merged and the packages get published we'll be able to import the interface directly from the common package.

author: string;
category: string;
labels: {
name: string;
color: string;
}[];
comments: {
author: string;
bodyText: string;
replies: {
author: string;
bodyText: string;
}[];
}[];
}

const useStyles = makeStyles(theme => ({
item: {
display: 'flex',
},
flexContainer: {
flexWrap: 'wrap',
},
itemText: {
width: '100%',
wordBreak: 'break-all',
marginBottom: '1rem',
},
user: {
display: 'inline-flex',
margin: theme.spacing(1),
},
}));

/**
* Props for {@link GithubDiscussionsSearchResultListItem}.
*
* @public
*/
export interface GithubDiscussionsSearchResultListItemProps {
icon?: ReactNode;
result?: GithubDiscussionsDocument;
highlight?: ResultHighlight;
lineClamp?: number;
}

/** @public */
export function GithubDiscussionsSearchResultListItem(
props: GithubDiscussionsSearchResultListItemProps,
) {
const { result, highlight, icon } = props;
const classes = useStyles();
if (!result) return null;

return (
<div className={classes.item}>
{icon && <ListItemIcon>{icon}</ListItemIcon>}
<div className={classes.flexContainer}>
<ListItemText
className={classes.itemText}
primaryTypographyProps={{ variant: 'h6' }}
primary={
<Link noTrack to={result.location}>
{highlight?.fields.title ? (
<HighlightedSearchResultText
text={highlight.fields.title}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
) : (
result.title
)}
</Link>
}
secondary={
<Typography
component="span"
style={{
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
WebkitLineClamp: props.lineClamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add lineClamp as part of the props destructure above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

overflow: 'hidden',
}}
color="textSecondary"
variant="body2"
>
{highlight?.fields.text ? (
<HighlightedSearchResultText
text={highlight.fields.text}
preTag={highlight.preTag}
postTag={highlight.postTag}
/>
) : (
result.text
)}
</Typography>
}
/>
<Box>
{result.author && (
<div className={classes.user}>
<EntityRefLink entityRef={`user:default/${result.author}`} />
MonicaG marked this conversation as resolved.
Show resolved Hide resolved
</div>
)}
{result.category && <Chip label={result.category} size="small" />}
{result.labels.length > 0 &&
result.labels.map(({ name }) => {
return <Chip key={name} label={name} size="small" />;
})}
</Box>
</div>
</div>
);
}
125 changes: 71 additions & 54 deletions packages/app/src/components/search/SearchResultCustomList.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,81 @@
import React from 'react';

import { List } from '@material-ui/core';
import { SearchResult, DefaultResultListItem } from '@backstage/plugin-search-react';
import {
SearchResult,
DefaultResultListItem,
} from '@backstage/plugin-search-react';

import { CatalogSearchResultListItem } from '@backstage/plugin-catalog';
import { StackOverflowSearchResultListItem, StackOverflowIcon } from '@backstage-community/plugin-stack-overflow';
import {
StackOverflowSearchResultListItem,
StackOverflowIcon,
} from '@backstage-community/plugin-stack-overflow';
import { CatalogIcon, DocsIcon } from '@backstage/core-components';
import { TechDocsSearchResultCustomListItem } from './TechDocsSearchResultCustomListItem';
import {
GithubDiscussionsSearchResultListItem,
GithubDiscussionsDocument,
} from './GithubDiscussionsSearchResultListItem';

const SearchResultCustomList = () => {
return (
<SearchResult>
{({ results }) => (
<List>
{results.map(({ type, document, highlight, rank }) => {
switch (type) {
case 'software-catalog':
return (
<CatalogSearchResultListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
icon={<CatalogIcon />}
/>
);
case 'techdocs':
return (
<TechDocsSearchResultCustomListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
asListItem={true}
icon={<DocsIcon />}
/>
);
case 'stack-overflow':
return (
<StackOverflowSearchResultListItem
key={document.location}
result={document}
icon={<StackOverflowIcon />}
/>
);
default:
return (
<DefaultResultListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
/>
);
}
})}
</List>
)}
</SearchResult>
)
}
return (
<SearchResult>
{({ results }) => (
<List>
{results.map(({ type, document, highlight, rank }) => {
switch (type) {
case 'software-catalog':
return (
<CatalogSearchResultListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
icon={<CatalogIcon />}
/>
);
case 'techdocs':
return (
<TechDocsSearchResultCustomListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
asListItem
icon={<DocsIcon />}
/>
);
case 'stack-overflow':
return (
<StackOverflowSearchResultListItem
key={document.location}
result={document}
icon={<StackOverflowIcon />}
/>
);
case 'github-discussions':
return (
<GithubDiscussionsSearchResultListItem
result={document as GithubDiscussionsDocument}
highlight={highlight}
/>
);
default:
return (
<DefaultResultListItem
key={document.location}
result={document}
highlight={highlight}
rank={rank}
/>
);
}
})}
</List>
)}
</SearchResult>
);
};

export const searchResultCustomList = <SearchResultCustomList />;
export const searchResultCustomList = <SearchResultCustomList />;
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build-image": "docker build ../.. -f Dockerfile --tag backstage"
},
"dependencies": {
"@backstage-community/plugin-search-backend-module-github-discussions": "file:./search-collator-0.5.0.tgz",
"@backstage-community/plugin-stack-overflow-backend": "^0.2.22",
"@backstage/backend-common": "^0.25.0",
"@backstage/backend-defaults": "^0.5.2",
Expand Down
Binary file added packages/backend/search-collator-0.5.0.tgz
Binary file not shown.
6 changes: 6 additions & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ backend.add(import('@backstage/plugin-search-backend-module-techdocs'));
backend.add(
import('@backstage/plugin-search-backend-module-stack-overflow-collator'),
);
backend.add(
import(
'@backstage-community/plugin-search-backend-module-github-discussions'
),
);

backend.add(import('@backstage/plugin-techdocs-backend'));

backend.add(import('@backstage/plugin-permission-backend'));
Expand Down
Loading