Skip to content

Commit

Permalink
New filter: is:bot
Browse files Browse the repository at this point in the history
Closes #20

Related: #131
  • Loading branch information
brunolemos committed Jun 21, 2019
1 parent 99481ce commit 8785b2a
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 24 deletions.
Expand Up @@ -9,6 +9,7 @@ import {
getGitHubURLForRepo,
getIssueOrPullRequestIconAndColor,
getIssueOrPullRequestNumberFromUrl,
getItemIsBot,
getOwnerAndRepo,
getRepoFullNameFromUrl,
GitHubIssueOrPullRequestSubjectType,
Expand Down Expand Up @@ -84,7 +85,6 @@ export const IssueOrPullRequestCard = React.memo(
repository_url,
saved,
url,
user,
html_url: htmlURL,
} = issueOrPullRequest as EnhancedGitHubIssueOrPullRequest

Expand Down Expand Up @@ -116,9 +116,7 @@ export const IssueOrPullRequestCard = React.memo(
html_url: getGitHubURLForRepo(repoOwnerName!, repoName!)!,
}

const isBot = Boolean(
user && user.login && user.login.indexOf('[bot]') >= 0,
)
const isBot = getItemIsBot('issue_or_pr', issueOrPullRequest)

const cardIconDetails = getIssueOrPullRequestIconAndColor(
type,
Expand Down
65 changes: 65 additions & 0 deletions packages/components/src/components/columns/ColumnOptions.tsx
Expand Up @@ -103,6 +103,7 @@ export interface ColumnOptionsProps {
}

export type ColumnOptionCategory =
| 'bot'
| 'draft'
| 'event_action'
| 'inbox'
Expand Down Expand Up @@ -183,6 +184,7 @@ export const ColumnOptions = React.memo((props: ColumnOptionsProps) => {
'unread',
'state',
'draft',
'bot',
_shouldShowInvolvesFilter && 'involves',
'subject_types',
column.type === 'activity' && 'event_action',
Expand Down Expand Up @@ -236,6 +238,7 @@ export const ColumnOptions = React.memo((props: ColumnOptionsProps) => {
const setColummStateTypeFilter = useReduxAction(
actions.setColummStateTypeFilter,
)
const setColummBotFilter = useReduxAction(actions.setColummBotFilter)
const setColummDraftFilter = useReduxAction(actions.setColummDraftFilter)
const setColummSubjectTypeFilter = useReduxAction(
actions.setColummSubjectTypeFilter,
Expand Down Expand Up @@ -816,6 +819,68 @@ export const ColumnOptions = React.memo((props: ColumnOptionsProps) => {
)
})()}

{allColumnOptionCategories.includes('bot') &&
(() => {
const bot = column.filters && column.filters.bot
const defaultBooleanValue = true

const filteredItemsMetadata = getItemsFilterMetadata(
column.type,
getFilteredItems(
column.type,
allItems,
{ ...column.filters, bot: undefined },
getFilteredItemsOptions,
),
)

return (
<ColumnOptionsRow
analyticsLabel="bot_options_row"
enableBackgroundHover={allowToggleCategories}
hasChanged={typeof bot === 'boolean'}
headerItemFixedIconSize={columnHeaderItemContentSize}
iconName="hubot"
isOpen={openedOptionCategories.has('bot')}
onToggle={
allowToggleCategories
? () => toggleOpenedOptionCategory('bot')
: undefined
}
title="Bots"
// right={
// bot === true
// ? 'Bots only'
// : bot === false
// ? 'Excluded'
// : 'Included'
// }
>
<Checkbox
key="bot-type-option"
analyticsLabel={undefined}
checked={typeof bot === 'boolean' ? bot : null}
containerStyle={
sharedColumnOptionsStyles.fullWidthCheckboxContainerWithPadding
}
defaultValue={defaultBooleanValue}
squareContainerStyle={
sharedColumnOptionsStyles.checkboxSquareContainer
}
enableIndeterminateState
label="Bots"
onChange={value => {
setColummBotFilter({
columnId: column.id,
bot: typeof value === 'boolean' ? value : undefined,
})
}}
right={getCheckboxRight(filteredItemsMetadata.bot)}
/>
</ColumnOptionsRow>
)
})()}

{allColumnOptionCategories.includes('subject_types') &&
(() => {
const filters =
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/redux/actions/columns.ts
Expand Up @@ -100,6 +100,13 @@ export function setColummStateTypeFilter<T extends GitHubStateType>(payload: {
return createAction('SET_COLUMN_STATE_FILTER', payload)
}

export function setColummBotFilter(payload: {
columnId: string
bot: ColumnFilters['bot']
}) {
return createAction('SET_COLUMN_BOT_FILTER', payload)
}

export function setColummDraftFilter(payload: {
columnId: string
draft: ColumnFilters['draft']
Expand Down
13 changes: 13 additions & 0 deletions packages/components/src/redux/reducers/columns.ts
Expand Up @@ -332,6 +332,19 @@ export const columnsReducer: Reducer<State> = (
draft.updatedAt = new Date().toISOString()
})

case 'SET_COLUMN_BOT_FILTER':
return immer(state, draft => {
if (!draft.byId) return

const column = draft.byId[action.payload.columnId]
if (!column) return

column.filters = column.filters || {}
column.filters.bot = action.payload.bot

draft.updatedAt = new Date().toISOString()
})

case 'SET_COLUMN_DRAFT_FILTER':
return immer(state, draft => {
if (!draft.byId) return
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/helpers/filters.ts
Expand Up @@ -15,6 +15,7 @@ import {
import {
getIssueOrPullRequestState,
getIssueOrPullRequestSubjectType,
getItemIsBot,
getItemIssueOrPullRequest,
getItemOwnersAndRepos,
getItemSearchableStrings,
Expand Down Expand Up @@ -306,9 +307,10 @@ function baseColumnHasAnyFilter(filters: BaseColumnFilters | undefined) {
if (!filters) return false

if (filters.clearedAt) return true
if (typeof filters.bot === 'boolean') return true
if (typeof filters.draft === 'boolean') return true
if (typeof filters.private === 'boolean') return true
if (typeof filters.saved === 'boolean') return true
if (typeof filters.draft === 'boolean') return true
if (typeof filters.unread === 'boolean') return true
if (filters.query) return true

Expand Down Expand Up @@ -416,6 +418,12 @@ export function getFilteredIssueOrPullRequests(
)
return false

if (
typeof filters.bot === 'boolean' &&
filters.bot !== getItemIsBot('issue_or_pr', item)
)
return false

if (
(filters.draft === true &&
(!issueOrPR || filters.draft !== isDraft(issueOrPR))) ||
Expand Down Expand Up @@ -516,6 +524,12 @@ export function getFilteredNotifications(
)
return false

if (
typeof filters.bot === 'boolean' &&
filters.bot !== getItemIsBot('notifications', item)
)
return false

if (
(filters.draft === true &&
(!issueOrPR || filters.draft !== isDraft(issueOrPR))) ||
Expand Down Expand Up @@ -617,6 +631,12 @@ export function getFilteredEvents(
)
return false

if (
typeof filters.bot === 'boolean' &&
filters.bot !== getItemIsBot('activity', item)
)
return false

if (
(filters.draft === true &&
(!issueOrPR || filters.draft !== isDraft(issueOrPR))) ||
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/helpers/github/events.ts
Expand Up @@ -31,6 +31,7 @@ import {
getBranchNameFromRef,
getCommitIconAndColor,
getIssueIconAndColor,
getItemIsBot,
getOwnerAndRepo,
getPullRequestIconAndColor,
getReleaseIconAndColor,
Expand Down Expand Up @@ -986,7 +987,7 @@ export function getGitHubEventSubItems(event: EnhancedGitHubEvent) {
const isForcePush = isPush && (payload as GitHubPushEvent).forced
const isPrivate = isEventPrivate(event)

const isBot = Boolean(actor.login && actor.login.indexOf('[bot]') >= 0)
const isBot = getItemIsBot('activity', event)

// GitHub returns the wrong avatar_url for app bots on actor.avatar_url,
// but the correct avatar on payload.abc.user.avatar_url,
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/helpers/github/notifications.ts
Expand Up @@ -19,6 +19,7 @@ import { capitalize, isNotificationPrivate } from '../shared'
import {
getCommitIconAndColor,
getIssueIconAndColor,
getItemIsBot,
getOwnerAndRepo,
getPullRequestIconAndColor,
getReleaseIconAndColor,
Expand Down Expand Up @@ -590,17 +591,7 @@ export function getGitHubNotificationSubItems(
(notification.repository.full_name || notification.repository.name)) ||
''

const actor =
(comment && comment.user) ||
(commit && commit.author) ||
(release && release.author) ||
(issue && issue.user) ||
(pullRequest && pullRequest.user) ||
null

const isBot = Boolean(
actor && actor.login && actor.login.indexOf('[bot]') >= 0,
)
const isBot = getItemIsBot('notifications', notification)

return {
comment,
Expand Down
55 changes: 48 additions & 7 deletions packages/core/src/helpers/github/shared.ts
Expand Up @@ -1025,6 +1025,49 @@ export function getItemOwnersAndRepos(
}
}

export function getItemIsBot(
type: ColumnSubscription['type'],
item: EnhancedItem,
): boolean {
if (!item) return false

switch (type) {
case 'activity': {
const event = item as EnhancedGitHubEvent
const { actor } = event
return !!(actor && actor.login && actor.login.indexOf('[bot]') >= 0)
}

case 'issue_or_pr': {
const issueOrPR = item as EnhancedGitHubIssueOrPullRequest
const { user } = issueOrPR

return !!(user && user.login && user.login.indexOf('[bot]') >= 0)
}

case 'notifications': {
const notification = item as EnhancedGitHubNotification
const { comment, commit, release, issue, pullRequest } = notification

const actor =
(comment && comment.user) ||
(commit && commit.author) ||
(release && release.author) ||
(issue && issue.user) ||
(pullRequest && pullRequest.user) ||
null

return (
!!(actor && actor.login && actor.login.indexOf('[bot]') >= 0) ||
notification.reason === 'security_alert'
)
}

default:
return false
}
}

export function getFilteredItems(
type: ColumnSubscription['type'] | undefined,
items: EnhancedItem[],
Expand Down Expand Up @@ -1081,6 +1124,7 @@ const _defaultItemsFilterMetadata: ItemsFilterMetadata = {
closed: getDefaultItemFilterCountMetadata(),
merged: getDefaultItemFilterCountMetadata(),
},
bot: getDefaultItemFilterCountMetadata(),
draft: getDefaultItemFilterCountMetadata(),
// involves: {},
subjectType: {}, // { issue: getDefaultItemFilterCountMetadata(), ... }
Expand Down Expand Up @@ -1149,6 +1193,8 @@ export function getItemsFilterMetadata(

if (isDraft(issueOrPR)) updateNestedCounter(result.draft)

if (getItemIsBot(type, item)) updateNestedCounter(result.bot)

if (subjectType) {
if (!result.subjectType[subjectType])
result.subjectType[subjectType] = getDefaultItemFilterCountMetadata()
Expand Down Expand Up @@ -1237,7 +1283,6 @@ export function getItemSearchableStrings(
const issueOrPullRequest = getItemIssueOrPullRequest(type, item)

let comment: GitHubComment | undefined
let isBot: boolean | undefined
let release: Partial<GitHubRelease> | undefined

const id = item.id
Expand Down Expand Up @@ -1286,7 +1331,7 @@ export function getItemSearchableStrings(
forkRepoFullName,
// forkee,
// id,
isBot: _isBot,
// isBot,
// isBranchMainEvent,
// isForcePush,
// isPrivate,
Expand All @@ -1308,7 +1353,6 @@ export function getItemSearchableStrings(
} = getGitHubEventSubItems(event)

comment = _comment
isBot = _isBot
release = _release

strings.push(`${(actor && actor.login) || ''}`)
Expand Down Expand Up @@ -1353,7 +1397,7 @@ export function getItemSearchableStrings(
commit,
createdAt,
// id,
isBot: _isBot,
// isBot,
// isPrivate,
// isPrivateAndCantSee,
// isRead,
Expand All @@ -1370,7 +1414,6 @@ export function getItemSearchableStrings(
} = getGitHubNotificationSubItems(notification)

comment = _comment
isBot = _isBot
release = _release as any // TODO: Fix type error

if (commit) {
Expand Down Expand Up @@ -1398,8 +1441,6 @@ export function getItemSearchableStrings(
//
}

if (isBot) strings.push('is:bot')

if (comment) {
strings.push(`${(comment.user && comment.user.login) || ''}`)
strings.push(`${comment.body || ''}`)
Expand Down

0 comments on commit 8785b2a

Please sign in to comment.