Skip to content

Commit

Permalink
introduce strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker committed Feb 27, 2024
1 parent 9a88566 commit 7a672f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/scripts/inclusion-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const {
optOut,
slack: { addEmojiReaction, postEphemeralResponse },
stats: { incrementStats },
helpMessage,
Expand Down Expand Up @@ -41,6 +42,12 @@ module.exports = async (app) => {
"Charlie passively listens for language with racist, ableist, sexist, or other exclusionary histories. When it hears such words or phrases, it quietly lets the speaker know and offers some suggestions. What a great bot, helping nudge us all to thoughtful, inclusive language!",
);

const optout = optOut(
"inclusion_bot_strict",
"Inclusion Bot | Strict mode",
"Inclusion Bot operates in strict mode by default, highlighting all language that we have identified as racist, ableist, sexist, or otherwise exclusionary. We believe this is the ideal way to use Inclusion Bot. However, we recognize that some people may not want to be reminded about some of the language identified, so we offer a way to have Inclusion Bot only highlight language that we, as an organization, believe strongly should not be used. We encourage people to keep the stricter mode enabled as a learning aid, but if it makes you feel excluded yourself, please opt out of it.",
);

// Use the module exported version here, so that it can be stubbed for testing
const { link, message, triggers } = module.exports.getTriggers();

Expand Down Expand Up @@ -120,6 +127,7 @@ module.exports = async (app) => {
},
],
},
...optout.button,
],
},
],
Expand Down
37 changes: 23 additions & 14 deletions sync-inclusion-bot-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,41 @@ const markdownRowToTrigger = ([, matches, alternatives, why]) => ({
.replace(/this term/i, '":TERM:"'),
});

const getTriggerIgnoreMap = (currentConfig) => {
const triggerWithIgnore = (newTrigger) => {
const getTriggerExtraMetadataMap = (currentConfig) => {
const triggerWithExtraMetadata = (newTrigger) => {
// Find a trigger in the existing config that has at least one of the same
// matches as the new trigger. There may not be one, but if...
const existing = currentConfig.triggers.find(
({ matches: currentMatches }) =>
currentMatches.some((v) => newTrigger.matches.includes(v)),
);

// Create a new mapped trigger. We don't use an object spread notation here
// so we control the order the properties get rendered into YAML, to keep
// things consistent and nice.
const mapped = {
matches: newTrigger.matches,
alternatives: newTrigger.alternatives,
ignore: existing.ignore,
};

// If there is an existing config that matches AND it has an ignore property
// return a new trigger that includes the ignore property. (We do it this
// way instead of using an object spread so we get the properties in the
// order we want for readability when it gets written to yaml.)
// add the existing ignore property on to the new trigger.
if (existing?.ignore) {
return {
matches: newTrigger.matches,
alternatives: newTrigger.alternatives,
ignore: existing.ignore,
why: newTrigger.why,
};
mapped.ignore = existing.ignore;
}
// If there is an existing config that matches AND it has an ignore property
// add the existing ignore property on to the new trigger.
if (existing?.strict) {
mapped.strict = existing.strict;
}

mapped.why = newTrigger.why;

// Otherwise just return what we got.
return newTrigger;
return mapped;
};
return triggerWithIgnore;
return triggerWithExtraMetadata;
};

const main = async () => {
Expand All @@ -82,7 +91,7 @@ const main = async () => {
// Also find the frontmatter comments so we can preserve it.
const frontmatter = getYamlFrontmatter(currentYamlStr);

const triggerWithIgnore = getTriggerIgnoreMap(currentConfig);
const triggerWithIgnore = getTriggerExtraMetadataMap(currentConfig);

// Read and parse the markdown.
const mdf = await fs.readFile("InclusionBot.md", { encoding: "utf-8" });
Expand Down

0 comments on commit 7a672f0

Please sign in to comment.