reviewbot

Source code for the is-a.dev r...
Log | Files | Refs | Activity | README | LICENSE

root / src / webhooks / labeled.js

labeled.js (6137B)


      1 import fs from 'fs';
      2 import path from 'path';
      3 
      4 import { db } from '../index.js';
      5 
      6 const reasonLabels = [
      7     'reason: abuse risk',
      8     'reason: ai generated pr',
      9     'reason: commercial usage',
     10     'reason: impersonation',
     11     'reason: inaccessible website',
     12     'reason: incomplete pr',
     13     'reason: incomplete website',
     14     'reason: invalid file',
     15     'reason: invalid records',
     16     'reason: invalid social',
     17     'reason: merge conflict',
     18     'reason: not dev related',
     19     'reason: nsfw',
     20     'reason: other',
     21     'reason: unauthorized',
     22     'reason: incompatible records',
     23     'reason: tos non-compliant',
     24 ];
     25 const lowPriorityMessage = fs.readFileSync(
     26     path.join(import.meta.dirname, '../message/label/lowpriority.md'),
     27     'utf8'
     28 );
     29 
     30 export async function labeled(
     31     appOctokit,
     32     prLabelName,
     33     repoOwner,
     34     repoName,
     35     repoFullName,
     36     prNumber,
     37     prUpdatedAt,
     38     prUsername
     39 ) {
     40     let denied, invalid, lowpriority;
     41     if (prLabelName === 'status: denied') {
     42         denied = true;
     43     } else if (prLabelName === 'status: invalid') {
     44         invalid = true;
     45     } else if (prLabelName === 'status: low priority') {
     46         lowpriority = true;
     47     }
     48 
     49     if (denied === true || invalid === true) {
     50         const listOfLabels = [];
     51         // timeout so that it creates a little time window for the maintainer to add the rest of the labels
     52         await new Promise((resolve) => setTimeout(resolve, 5000));
     53         const data = await appOctokit.request(
     54             'GET /repos/{owner}/{repo}/pulls/{pull_number}',
     55             {
     56                 owner: repoOwner,
     57                 repo: repoName,
     58                 pull_number: prNumber,
     59             }
     60         );
     61         const labelData = data.data.labels;
     62         for (let i in labelData) {
     63             if (labelData[i].name) {
     64                 listOfLabels.push(labelData[i].name);
     65             }
     66         }
     67 
     68         // start adding the messages
     69         const allMessages = [];
     70         for (let i in listOfLabels) {
     71             if (reasonLabels.includes(listOfLabels[i])) {
     72                 let initialReason = listOfLabels[i]
     73                     .toString()
     74                     .replace(/reason:\s/i, '');
     75                 let finalReason = initialReason.replace(/\s+/g, '-');
     76                 let message = fs.readFileSync(
     77                     path.join(
     78                         import.meta.dirname,
     79                         `../message/label/${finalReason}.md`
     80                     ),
     81                     'utf8'
     82                 );
     83                 allMessages.push(message);
     84             } else if (listOfLabels[i] === 'status: needs preview') {
     85                 let message = fs.readFileSync(
     86                     path.join(
     87                         import.meta.dirname,
     88                         `../message/label/needs-preview.md`
     89                     ),
     90                     'utf8'
     91                 );
     92                 allMessages.push(message);
     93             }
     94         }
     95 
     96         const labelMessages = allMessages.join('\n\n');
     97         let body;
     98         if (!labelMessages.length) {
     99             if (denied) {
    100                 body = fs.readFileSync(
    101                     path.join(
    102                         import.meta.dirname,
    103                         `../message/deniednolabel.md`
    104                     ),
    105                     'utf8'
    106                 );
    107             } else if (invalid) {
    108                 body = fs.readFileSync(
    109                     path.join(
    110                         import.meta.dirname,
    111                         `../message/invalidnolabel.md`
    112                     ),
    113                     'utf8'
    114                 );
    115             }
    116         } else if (denied) {
    117             body = `
    118 # Pull Request Denied
    119 
    120 This pull request has been denied due to the following reason(s):
    121 
    122 ---
    123 ${labelMessages}
    124 
    125 ---
    126 
    127 If you believe this was a mistake, or if you need further clarification, please feel free to create an issue or reach out to our team in the [Discord server](https://discord.gg/is-a-dev-830872854677422150).
    128 
    129 `;
    130         } else if (invalid) {
    131             body = `
    132 # Invalid Pull Request
    133 
    134 This pull request is invalid due to the following reason(s):
    135 
    136 ---
    137 ${labelMessages}
    138 
    139 ---
    140 
    141 If you need any help, please create an issue or ask our team in the [Discord server](https://discord.gg/is-a-dev-830872854677422150)
    142 
    143 `;
    144         }
    145         await appOctokit.rest.issues.createComment({
    146             owner: repoOwner,
    147             repo: repoName,
    148             issue_number: prNumber,
    149             body: body,
    150         });
    151         console.log(
    152             `Sent reason messages at #${prNumber} from https://github.com/${repoFullName}`
    153         );
    154         if (denied === true) {
    155             await appOctokit.request(
    156                 'PATCH /repos/{owner}/{repo}/pulls/{pull_number}',
    157                 {
    158                     owner: repoOwner,
    159                     repo: repoName,
    160                     pull_number: prNumber,
    161                     state: 'closed',
    162                 }
    163             );
    164             console.log(
    165                 `Closed pull request at #${prNumber} from https://github.com/${repoFullName}`
    166             );
    167         }
    168     } else if (lowpriority === true) {
    169         let res = await db
    170             .prepare(`SELECT * FROM LIST WHERE username = ?;`)
    171             .get(prUsername);
    172         let resString = JSON.stringify(res);
    173         if (resString === undefined) {
    174             await appOctokit.rest.issues.createComment({
    175                 owner: repoOwner,
    176                 repo: repoName,
    177                 issue_number: prNumber,
    178                 body: lowPriorityMessage,
    179             });
    180             console.log(
    181                 `Sent low priority message to #${prNumber} from https://github.com/${repoFullName}`
    182             );
    183             await db
    184                 .prepare(`INSERT INTO LIST VALUES (?, ?, ?, ?, ?);`)
    185                 .run(
    186                     prUsername,
    187                     `${prNumber}`,
    188                     prUpdatedAt,
    189                     repoOwner,
    190                     repoName
    191                 );
    192             console.log(
    193                 `Logged #${prNumber} from https://github.com/${repoFullName} to the low priority database.`
    194             );
    195         }
    196     }
    197 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror