reviewbot

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

labeled.js (5997B)


      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, 3000));
     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(import.meta.dirname, `../message/deniednolabel.md`),
    102                     'utf8'
    103                 );
    104             } else if (invalid) {
    105                 body = fs.readFileSync(
    106                     path.join(import.meta.dirname, `../message/invalidnolabel.md`),
    107                     'utf8'
    108                 );
    109             }
    110         } else if (denied) {
    111             body = `
    112 # Pull Request Denied
    113 
    114 This pull request has been denied due to the following reason(s):
    115 
    116 ---
    117 ${labelMessages}
    118 
    119 ---
    120 
    121 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).
    122 
    123 `;
    124         } else if (invalid) {
    125             body = `
    126 # Invalid Pull Request
    127 
    128 This pull request is invalid due to the following reason(s):
    129 
    130 ---
    131 ${labelMessages}
    132 
    133 ---
    134 
    135 If you need any help, please create an issue or ask our team in the [Discord server](https://discord.gg/is-a-dev-830872854677422150)
    136 
    137 `;
    138         }
    139         await appOctokit.rest.issues.createComment({
    140             owner: repoOwner,
    141             repo: repoName,
    142             issue_number: prNumber,
    143             body: body,
    144         });
    145         console.log(
    146             `Sent reason messages at #${prNumber} from https://github.com/${repoFullName}`
    147         );
    148         if (denied === true) {
    149             await appOctokit.request(
    150                 'PATCH /repos/{owner}/{repo}/pulls/{pull_number}',
    151                 {
    152                     owner: repoOwner,
    153                     repo: repoName,
    154                     pull_number: prNumber,
    155                     state: 'closed',
    156                 }
    157             );
    158             console.log(
    159                 `Closed pull request at #${prNumber} from https://github.com/${repoFullName}`
    160             );
    161         }
    162     } else if (lowpriority === true) {
    163         let res = await db
    164             .prepare(`SELECT * FROM LIST WHERE username = ?;`)
    165             .get(prUsername);
    166         let resString = JSON.stringify(res);
    167         if (resString === undefined) {
    168             await appOctokit.rest.issues.createComment({
    169                 owner: repoOwner,
    170                 repo: repoName,
    171                 issue_number: prNumber,
    172                 body: lowPriorityMessage,
    173             });
    174             console.log(
    175                 `Sent low priority message to #${prNumber} from https://github.com/${repoFullName}`
    176             );
    177             await db
    178                 .prepare(`INSERT INTO LIST VALUES (?, ?, ?, ?, ?);`)
    179                 .run(
    180                     prUsername,
    181                     `${prNumber}`,
    182                     prUpdatedAt,
    183                     repoOwner,
    184                     repoName
    185                 );
    186             console.log(
    187                 `Logged #${prNumber} from https://github.com/${repoFullName} to the low priority database.`
    188             );
    189         }
    190     }
    191 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror