├── .github ├── config.yml └── workflows │ └── probot-sentiment-bot.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test ├── events └── payload.json └── index.js /.github/config.yml: -------------------------------------------------------------------------------- 1 | updateDocsWhiteList: 2 | - BUG 3 | - Chore 4 | - minor 5 | 6 | updateDocsComment: > 7 | Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would update some of our documentation based on your changes. 8 | 9 | requestInfoReplyComment: > 10 | We would appreciate it if you could provide us with more info about this issue/pr! 11 | 12 | requestInfoLabelToAdd: request-more-info 13 | 14 | newPRWelcomeComment: > 15 | Thanks so much for opening your first PR here! 16 | 17 | firstPRMergeComment: > 18 | Congrats on merging your first pull request here! :tada: How awesome! 19 | 20 | newIssueWelcomeComment: > 21 | Thanks for opening this issue, a maintainer will get back to you shortly! 22 | 23 | sentimentBotToxicityThreshold: .7 24 | 25 | sentimentBotReplyComment: > 26 | Please be sure to review the code of conduct and be respectful of other users. cc/ @hiimbex 27 | 28 | lockThreads: 29 | toxicityThreshold: .7 30 | numComments: 2 31 | setTimeInHours: 72 32 | replyComment: > 33 | This thread is being locked due to exceeding the toxicity minimums. cc/ @hiimbex 34 | -------------------------------------------------------------------------------- /.github/workflows/probot-sentiment-bot.yml: -------------------------------------------------------------------------------- 1 | name: probot-sentiment-bot 2 | on: [push] 3 | jobs: 4 | build: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - name: Check out repository code 8 | uses: actions/checkout@v2 9 | - name: Use Node.js 10 | uses: actions/setup-node@v2 11 | with: 12 | node-version: '14.x' 13 | - name: Cache node modules 14 | uses: actions/cache@v2 15 | env: 16 | cache-name: cache-node-modules 17 | with: 18 | path: ~/.npm 19 | key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} 20 | restore-keys: | 21 | ${{ runner.os }}-build-${{ env.cache-name }}- 22 | ${{ runner.os }}-build- 23 | ${{ runner.os }}- 24 | - name: Install dependencies 25 | run: npm ci 26 | - name: Run tests 27 | run: npm test 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules 3 | *.pem 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at hiimbex@github.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | [fork]: https://github.com/behaviorbot/sentiment-bot/fork 4 | [pr]: https://github.com/behaviorbot/sentiment-bot/compare 5 | [style]: https://github.com/probot/eslint-config-probot 6 | [code-of-conduct]: CODE_OF_CONDUCT.md 7 | 8 | Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. 9 | 10 | Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms. 11 | 12 | ## Submitting a pull request 13 | 14 | 0. [Fork][fork] and clone the repository 15 | 0. Configure and install the dependencies: `npm install` 16 | 0. Make sure the tests pass on your machine: `npm test` 17 | 0. Create a new branch: `git checkout -b my-branch-name` 18 | 0. Make your change, add tests, and make sure the tests still pass 19 | 0. Push to your fork and [submit a pull request][pr] 20 | 0. Pat your self on the back and wait for your pull request to be reviewed and merged. 21 | 22 | Here are a few things you can do that will increase the likelihood of your pull request being accepted: 23 | 24 | - Follow the [style guide][style] which is a custom eslint linter. 25 | - Write and update tests. 26 | - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. 27 | - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). 28 | 29 | Work in Progress pull request are also welcome to get feedback early on, or if there is something blocked you. 30 | 31 | ## Resources 32 | 33 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) 34 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) 35 | - [GitHub Help](https://help.github.com) 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Bex Warner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sentiment-bot 2 | 3 | > a GitHub App built with [probot](https://github.com/probot/probot) that replies to toxic comments with a maintainer designated reply and a link to the repo's code of conduct. It does so by taking data from a `.github/config.yml`. 4 | 5 | screen shot 2017-08-15 at 8 51 38 am 6 | You can tell from this example just how toxic I am 😜 7 | 8 | ## Usage 9 | 10 | 1. Install the bot on the intended repositories. The plugin requires the following **Permissions and Events**: 11 | - Issues: **Read & Write** 12 | - **Issue Comment** events 13 | - Organization members: **Read** (necessary to tag teams, i.e. cc/ @behaviorbot/moderators) 14 | 2. Add a `.github/config.yml` file that contains the following: 15 | 16 | ```yml 17 | # Configuration for sentiment-bot - https://github.com/behaviorbot/sentiment-bot 18 | 19 | # *Required* toxicity threshold between 0 and .99 with the higher numbers being the most toxic 20 | # Anything higher than this threshold will be marked as toxic and commented on 21 | sentimentBotToxicityThreshold: .7 22 | 23 | # *Required* Comment to reply with 24 | sentimentBotReplyComment: > 25 | Please be sure to review the code of conduct and be respectful of other users. cc/ @hiimbex 26 | 27 | ``` 28 | 3. Be sure to check out the [Perspective API](https://www.perspectiveapi.com/) before choosing your toxicity threshold to get a feel for what kind of comments would register at what toxicity threshold. 29 | 30 | ## Setup 31 | 32 | ``` 33 | # Install dependencies 34 | npm install 35 | 36 | # Run the bot 37 | npm start 38 | ``` 39 | 40 | See [the probot deployment docs](https://github.com/probot/probot/blob/master/docs/deployment.md) if you would like to run your own instance of this plugin. 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const Perspective = require('perspective-api-client') 2 | 3 | module.exports = app => { 4 | app.perspective = new Perspective(({ apiKey: process.env.PERSPECTIVE_API_KEY })) 5 | app.on('issue_comment', async context => { 6 | let codeOfConduct 7 | const config = await context.config('config.yml') 8 | if (config && !context.isBot) { 9 | if (config.sentimentBotToxicityThreshold && config.sentimentBotReplyComment) { 10 | const toxicityThreshold = config.sentimentBotToxicityThreshold 11 | const body = context.payload.comment.body 12 | const repoData = await context.github.repos.get(context.repo()) 13 | // Only check for Code of Conduct for public, non-fork repos, since 14 | // the community profile API only returns info for those repos 15 | if (!repoData.data.private && !repoData.data.fork) { 16 | const communityData = await context.github.repos.retrieveCommunityProfileMetrics(context.repo()) 17 | if (communityData.data.code_of_conduct_file) { 18 | codeOfConduct = Object.assign({}, communityData.data.code_of_conduct_file) 19 | } 20 | } 21 | const response = await app.perspective.analyze(body, { truncate: true }) 22 | const toxicValue = response.attributeScores.TOXICITY.summaryScore.value 23 | // If the comment is toxic, comment the comment 24 | if (toxicValue >= toxicityThreshold) { 25 | let comment 26 | if (codeOfConduct && codeOfConduct.html_url) { 27 | comment = config.sentimentBotReplyComment + 'Keep in mind, this repository has a [Code of Conduct](' + codeOfConduct.html_url + ').' 28 | } else { 29 | comment = config.sentimentBotReplyComment 30 | } 31 | context.github.issues.createComment(context.issue({ body: comment })) 32 | } 33 | } 34 | } 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sentiment-bot", 3 | "version": "1.0.3", 4 | "description": "Comment a link to a repo's code of conduct in response to toxic issue comments", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "probot run ./index.js", 8 | "test": "mocha && standard" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/behaviorbot/sentiment-bot.git" 13 | }, 14 | "author": "hiimbex", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/behaviorbot/sentiment-bot/issues" 18 | }, 19 | "homepage": "https://github.com/behaviorbot/sentiment-bot#readme", 20 | "dependencies": { 21 | "perspective-api-client": "sloria/perspective-api-client#c56db6481b37e8eaceaf7bf8735006b2032de0cd", 22 | "probot": "^9.14.2" 23 | }, 24 | "devDependencies": { 25 | "eslint-plugin-import": "^2.14.0", 26 | "expect": "^1.20.2", 27 | "mocha": "^9.1.1", 28 | "standard": "^12.0.1" 29 | }, 30 | "engines": { 31 | "node": ">= 14.17" 32 | }, 33 | "standard": { 34 | "env": [ 35 | "mocha" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/events/payload.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "issue_comment", 3 | "payload": { 4 | "action": "created", 5 | "issue": { 6 | "number": 21, 7 | "user": { 8 | "login": "hiimbex" 9 | }, 10 | "title": "This is a pr" 11 | }, 12 | "repository": { 13 | "name": "testing-things", 14 | "owner": { 15 | "login": "hiimbex" 16 | } 17 | }, 18 | "comment": { 19 | "body": "gr im grumpy and toxic and mean and i hate being a good person! anger" 20 | }, 21 | "installation": { 22 | "id": 35029 23 | }, 24 | "sender": { 25 | "type": "user" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const expect = require('expect') 2 | const { Application } = require('probot') 3 | const plugin = require('..') 4 | const payload = require('./events/payload') 5 | 6 | const createTestApp = ({ toxicity, isPrivate = false, isFork = false }) => { 7 | // PERSPECTIVE_API_KEY must be set 8 | process.env.PERSPECTIVE_API_KEY = 'mock-key' 9 | const app = new Application() 10 | plugin(app) 11 | 12 | const github = { 13 | repos: { 14 | getContents: expect.createSpy().andReturn(Promise.resolve({ 15 | data: { 16 | content: Buffer.from(` 17 | sentimentBotToxicityThreshold: 0.3 18 | sentimentBotReplyComment: "That comment was toxic"`).toString('base64') 19 | } 20 | })), 21 | get: expect.createSpy().andReturn(Promise.resolve({ 22 | data: { 23 | private: isPrivate, 24 | fork: isFork 25 | } 26 | })), 27 | retrieveCommunityProfileMetrics: expect.createSpy().andReturn(Promise.resolve({ 28 | data: { 29 | code_of_conduct_file: Buffer.from(` 30 | url: https://api.github.com/repos/hiimbex/testing-things/contents/CODE_OF_CONDUCT.md, 31 | html_url: https://github.com/hiimbex/testing-things/blob/master/CODE_OF_CONDUCT.md`).toString('base64') 32 | } 33 | })) 34 | }, 35 | issues: { 36 | createComment: expect.createSpy() 37 | } 38 | } 39 | 40 | // Mock perspective API client 41 | const perspective = { 42 | analyze: expect.createSpy().andReturn(Promise.resolve( 43 | { 44 | attributeScores: { 45 | TOXICITY: { 46 | spanScores: [ 47 | { 48 | begin: 0, 49 | end: 56, 50 | score: { 51 | value: toxicity, 52 | type: 'PROBABILITY' 53 | } 54 | } 55 | ], 56 | summaryScore: { 57 | value: toxicity, 58 | type: 'PROBABILITY' 59 | } 60 | } 61 | }, 62 | languages: [ 63 | 'en' 64 | ] 65 | } 66 | )) 67 | } 68 | 69 | app.auth = () => Promise.resolve(github) 70 | app.perspective = perspective 71 | return app 72 | } 73 | 74 | describe('sentiment-bot', () => { 75 | describe('code of conduct', () => { 76 | it('does not fetch CoC for private repo', async () => { 77 | const app = createTestApp({ toxicity: 0.8, isPrivate: true }) 78 | const github = await app.auth() 79 | const perspective = app.perspective 80 | await app.receive(payload) 81 | expect(github.repos.getContents).toHaveBeenCalledWith({ 82 | owner: 'hiimbex', 83 | repo: 'testing-things', 84 | path: '.github/config.yml' 85 | }) 86 | expect(github.repos.get).toHaveBeenCalled({ 87 | owner: 'hiimbex', 88 | repo: 'testing-things' 89 | }) 90 | expect(github.repos.retrieveCommunityProfileMetrics).toNotHaveBeenCalled() 91 | expect(perspective.analyze).toHaveBeenCalled() 92 | expect(github.issues.createComment).toHaveBeenCalled() 93 | }) 94 | 95 | it('does not fetch CoC for forked repo', async () => { 96 | const app = createTestApp({ toxicity: 0.8, isFork: true }) 97 | const github = await app.auth() 98 | const perspective = app.perspective 99 | await app.receive(payload) 100 | expect(github.repos.getContents).toHaveBeenCalledWith({ 101 | owner: 'hiimbex', 102 | repo: 'testing-things', 103 | path: '.github/config.yml' 104 | }) 105 | expect(github.repos.get).toHaveBeenCalled({ 106 | owner: 'hiimbex', 107 | repo: 'testing-things' 108 | }) 109 | expect(github.repos.retrieveCommunityProfileMetrics).toNotHaveBeenCalled() 110 | expect(perspective.analyze).toHaveBeenCalled() 111 | expect(github.issues.createComment).toHaveBeenCalled() 112 | }) 113 | }) 114 | 115 | describe('sentiment-bot success', () => { 116 | it('posts a comment because the user was toxic', async () => { 117 | const app = createTestApp({ toxicity: 0.8 }) 118 | const github = await app.auth() 119 | const perspective = app.perspective 120 | await app.receive(payload) 121 | expect(github.repos.getContents).toHaveBeenCalledWith({ 122 | owner: 'hiimbex', 123 | repo: 'testing-things', 124 | path: '.github/config.yml' 125 | }) 126 | expect(github.repos.get).toHaveBeenCalled({ 127 | owner: 'hiimbex', 128 | repo: 'testing-things' 129 | }) 130 | expect(github.repos.retrieveCommunityProfileMetrics).toHaveBeenCalledWith({ 131 | owner: 'hiimbex', 132 | repo: 'testing-things' 133 | }) 134 | expect(perspective.analyze).toHaveBeenCalled() 135 | expect(github.issues.createComment).toHaveBeenCalled() 136 | }) 137 | }) 138 | 139 | describe('sentiment-bot fail', () => { 140 | it('does not post a comment because the user was not toxic', async () => { 141 | const app = createTestApp({ toxicity: 0.2 }) 142 | const github = await app.auth() 143 | const perspective = app.perspective 144 | await app.receive(payload) 145 | 146 | expect(github.repos.getContents).toHaveBeenCalledWith({ 147 | owner: 'hiimbex', 148 | repo: 'testing-things', 149 | path: '.github/config.yml' 150 | }) 151 | expect(github.repos.get).toHaveBeenCalled({ 152 | owner: 'hiimbex', 153 | repo: 'testing-things' 154 | }) 155 | expect(github.repos.retrieveCommunityProfileMetrics).toHaveBeenCalledWith({ 156 | owner: 'hiimbex', 157 | repo: 'testing-things' 158 | }) 159 | expect(perspective.analyze).toHaveBeenCalled() 160 | expect(github.issues.createComment).toNotHaveBeenCalled() 161 | }) 162 | }) 163 | }) 164 | --------------------------------------------------------------------------------