-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial waiting for label logic (#446)
* add initial waiting for label logic * use label constants in test * clean up tests
- Loading branch information
1 parent
e808090
commit 57ee6e7
Showing
8 changed files
with
510 additions
and
50 deletions.
There are no files selected for viewing
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,106 @@ | ||
import { EmitterWebhookEvent } from '@octokit/webhooks'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
import { isFromABot } from '@utils/isFromABot'; | ||
|
||
const REPOS_TO_TRACK_FOR_TRIAGE = new Set(['sentry', 'sentry-docs']); | ||
import { ClientType } from '@/api/github/clientType'; | ||
import { | ||
isNotFromAnExternalOrGTMUser, | ||
shouldSkip, | ||
} from '@/brain/issueLabelHandler/helpers'; | ||
import { | ||
WAITING_FOR_COMMUNITY_LABEL, | ||
WAITING_FOR_LABEL_PREFIX, | ||
WAITING_FOR_PRODUCT_OWNER_LABEL, | ||
} from '@/config'; | ||
import { getClient } from '@api/github/getClient'; | ||
|
||
function isNotInARepoWeCareAboutForTriage(payload) { | ||
return !REPOS_TO_TRACK_FOR_TRIAGE.has(payload.repository.name); | ||
} | ||
|
||
function isNotWaitingForCommunity(payload) { | ||
const { issue } = payload; | ||
return !issue?.labels.some( | ||
({ name }) => name === WAITING_FOR_COMMUNITY_LABEL | ||
); | ||
} | ||
|
||
// Markers of State | ||
|
||
export async function updateCommunityFollowups({ | ||
id, | ||
payload, | ||
...rest | ||
}: EmitterWebhookEvent<'issue_comment.created'>) { | ||
const tx = Sentry.startTransaction({ | ||
op: 'brain', | ||
name: 'issueLabelHandler.updateCommunityFollowups', | ||
}); | ||
|
||
const reasonsToDoNothing = [ | ||
isNotInARepoWeCareAboutForTriage, | ||
isNotFromAnExternalOrGTMUser, | ||
isNotWaitingForCommunity, | ||
isFromABot, | ||
]; | ||
if (await shouldSkip(payload, reasonsToDoNothing)) { | ||
return; | ||
} | ||
|
||
const owner = payload.repository.owner.login; | ||
const octokit = await getClient(ClientType.App, owner); | ||
|
||
await octokit.issues.removeLabel({ | ||
owner, | ||
repo: payload.repository.name, | ||
issue_number: payload.issue.number, | ||
name: WAITING_FOR_COMMUNITY_LABEL, | ||
}); | ||
|
||
await octokit.issues.addLabels({ | ||
owner, | ||
repo: payload.repository.name, | ||
issue_number: payload.issue.number, | ||
labels: [WAITING_FOR_PRODUCT_OWNER_LABEL], | ||
}); | ||
|
||
tx.finish(); | ||
} | ||
|
||
export async function ensureOneWaitingForLabel({ | ||
id, | ||
payload, | ||
...rest | ||
}: EmitterWebhookEvent<'issues.labeled'>) { | ||
const tx = Sentry.startTransaction({ | ||
op: 'brain', | ||
name: 'issueLabelHandler.ensureOneWaitingForLabel', | ||
}); | ||
|
||
const reasonsToDoNothing = [isFromABot]; | ||
if (await shouldSkip(payload, reasonsToDoNothing)) { | ||
return; | ||
} | ||
|
||
const { issue, label } = payload; | ||
const owner = payload.repository.owner.login; | ||
const octokit = await getClient(ClientType.App, owner); | ||
|
||
if (label?.name.startsWith(WAITING_FOR_LABEL_PREFIX)) { | ||
const labelToRemove = | ||
issue.labels?.find( | ||
({ name }) => | ||
name.startsWith(WAITING_FOR_LABEL_PREFIX) && name != label?.name | ||
)?.name || ''; | ||
await octokit.issues.removeLabel({ | ||
owner: owner, | ||
repo: payload.repository.name, | ||
issue_number: payload.issue.number, | ||
name: labelToRemove, | ||
}); | ||
} | ||
|
||
tx.finish(); | ||
} |
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,22 @@ | ||
import { getOssUserType } from '@utils/getOssUserType'; | ||
|
||
// Validation Helpers | ||
|
||
export async function shouldSkip(payload, reasonsToSkip) { | ||
// Could do Promise-based async here, but that was getting complicated[1] and | ||
// there's not really a performance concern (famous last words). | ||
// | ||
// [1] https://github.com/getsentry/eng-pipes/pull/212#discussion_r657365585 | ||
|
||
for (const skipIf of reasonsToSkip) { | ||
if (await skipIf(payload)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export async function isNotFromAnExternalOrGTMUser(payload) { | ||
const type = await getOssUserType(payload); | ||
return !(type === 'external' || type === 'gtm'); | ||
} |
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
Oops, something went wrong.