aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/arena-intake.yml
blob: 92459597df800e07fc0ee1dcf240e0a16e8d8a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
name: Arena intake

on:
  # We recommend `pull_request_target` so that github secrets are available.
  # In `pull_request` we wouldn't be able to change labels of fork PRs
  pull_request_target:
    types: [ opened, synchronize ]
    paths:
      - 'arena/**'

jobs:
  check:
    permissions:
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
    - name: Checkout PR
      uses: actions/checkout@v4
      with:
        ref: ${{ github.event.pull_request.head.sha }}

    - name: Check Arena entry
      uses: actions/github-script@v7
      with:
        script: |
          console.log('⚙️ Setting up...');

          const fs = require('fs');
          const path = require('path');

          const pr = context.payload.pull_request;
          const isFork = pr.head.repo.fork;

          console.log('↔️ Fetching PR diff metadata...');
          const prFilesChanged = (await github.rest.pulls.listFiles({
            owner: context.repo.owner,
            repo: context.repo.repo,
            pull_number: pr.number,
          })).data;
          console.debug(prFilesChanged);
          const arenaFilesChanged = prFilesChanged.filter(
            ({ filename: file }) => file.startsWith('arena/') && file.endsWith('.json')
          );
          const hasChangesInAutogptsFolder = prFilesChanged.some(
            ({ filename }) => filename.startsWith('autogpts/')
          );

          console.log(`ℹ️ ${arenaFilesChanged.length} arena entries affected`);
          console.debug(arenaFilesChanged);
          if (arenaFilesChanged.length === 0) {
            // If no files in `arena/` are changed, this job does not need to run.
            return;
          }

          let close = false;
          let flagForManualCheck = false;
          let issues = [];

          if (isFork) {
            if (arenaFilesChanged.length > 1) {
              // Impacting multiple entries in `arena/` is not allowed
              issues.push('This pull request impacts multiple arena entries');
            }
            if (hasChangesInAutogptsFolder) {
              // PRs that include the custom agent are generally not allowed
              issues.push(
                'This pull request includes changes in `autogpts/`.\n'
                + 'Please make sure to only submit your arena entry (`arena/*.json`), '
                + 'and not to accidentally include your custom agent itself.'
              );
            }
          }

          if (arenaFilesChanged.length === 1) {
            const newArenaFile = arenaFilesChanged[0]
            const newArenaFileName = path.basename(newArenaFile.filename)
            console.log(`ℹ️ Arena entry in PR: ${newArenaFile}`);

            if (pr.mergeable) {
              const newArenaEntry = JSON.parse(fs.readFileSync(newArenaFile.filename));
              const allArenaFiles = await (await glob.create('arena/*.json')).glob();
              console.debug(newArenaEntry);

              console.log(`ℹ️ Checking ${newArenaFileName} against existing entries...`);
              for (const file of allArenaFiles) {
                if (
                  path.basename(file) === newArenaFileName
                  && newArenaFile.status != 'added'
                ) {
                  flagForManualCheck = true;
                  continue;
                }

                console.debug(`Checking against ${file}`...)

                const arenaEntry = JSON.parse(fs.readFileSync(file));
                if (arenaEntry.github_repo_url === newArenaEntry.github_repo_url) {
                  console.log('⚠️ Duplicate detected: ${file}');
                  issues.push(
                    `The github_repo_url specified in __${newArenaFileName}__ `
                    + `already exists in __${file}__. `
                    + `This PR will be closed as duplicate.`
                  )
                  close = true;
                }
              }
            } else {
              console.log('⚠️ PR has conflicts');
              issues.push(
                `__${newArenaFileName}__ conflicts with existing entry with the same name`
              )
              close = true;
            }
          }  // end if (arenaFilesChanged.length === 1)

          console.log('ℹ️ Finished checking against existing entries')

          if (issues.length == 0) {
            console.log('✅ No issues detected');
            if (flagForManualCheck) {
              console.log('ℹ️ Requesting review from maintainers...')
              await github.rest.pulls.requestReviewers({
                owner: github.context.repo.owner,
                repo: github.context.repo.repo,
                pull_number: pr.number,
                team_reviewers: ['maintainers'],
              });
            } else {
              console.log('ℹ️ Approving PR...');
              await github.rest.pulls.createReview({
                owner: github.context.repo.owner,
                repo: github.context.repo.repo,
                pull_number: pr.number,
                event: 'APPROVE',
              });
            }
          } else {
            console.log(`⚠️ ${issues.length} issues detected`);

            console.log('ℹ️ Posting comment indicating issues...')
            await github.rest.issues.createComment({
              owner: github.context.repo.owner,
              repo: github.context.repo.repo,
              issue_number: pr.number,
              body: `Our automation found one or more issues with this submission:\n`
                + issues.map(i => `- ${i.replace('\n', '\n  ')}`).join('\n'),
            });

            console.log("ℹ️ Applying label 'invalid'...")
            await github.rest.issues.addLabels({
              owner: github.context.repo.owner,
              repo: github.context.repo.repo,
              issue_number: pr.number,
              labels: ['invalid'],
            });

            if (close) {
              console.log('ℹ️ Auto-closing PR...')
              await github.rest.pulls.update({
                owner: github.context.repo.owner,
                repo: github.context.repo.repo,
                pull_number: pr.number,
                state: 'closed',
              });
            }
          }