Repository: python/mypy
Pull request: mypy_primer: truncate per-project output
Closes #14059.

Problem statement:
Truncating mypy_primer output per project
If there is a lot of mypy_primer output from a single project, the mypy primer is truncated so that output from some projects may not be visible at all. It could be better if we'd separately truncate the output of each project. We could still have a global output limit that is, for example, 3x the per-project output limit, to avoid huge output if a change breaks everything.

Hints from issue discussion:
Example PR where this could have been helpful: https://github.com/python/mypy/pull/14041

Commit message: improve truncation

Patch to review:
diff --git a/.github/workflows/mypy_primer_comment.yml b/.github/workflows/mypy_primer_comment.yml
index 2056fc5a40c0..c95350e74250 100644
--- a/.github/workflows/mypy_primer_comment.yml
+++ b/.github/workflows/mypy_primer_comment.yml
@@ -48,15 +48,29 @@ jobs:
         with:
           github-token: ${{ secrets.GITHUB_TOKEN }}
           script: |
+            const MAX_CHARACTERS = 30000
+            const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3
+
             const fs = require('fs')
             let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' })
-            // posting comment fails if too long, so truncate
-            if (data.length > 30000) {
-              let truncated_data = data.substring(0, 30000)
-              let lines_truncated = data.split('\n').length - truncated_data.split('\n').length
-              data = truncated_data + `\n\n... (truncated ${lines_truncated} lines) ...\n`
+
+            function truncateIfNeeded(original, maxLength) {
+              let truncated = original.substring(0, maxLength)
+              if (original.length === truncated.length) {
+                return original
+              }
+              // further, remove last line that might be truncated
+              truncated = truncated.substring(0, truncated.lastIndexOf('\n'))
+              let lines_truncated = original.split('\n').length - truncated.split('\n').length
+              return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...`
             }
 
+            const projects = data.split('\n\n')
+            // don't let one project dominate
+            data = projects.map(project => truncateIfNeeded(project, MAX_CHARACTERS_PER_PROJECT)).join('\n\n')
+            // posting comment fails if too long, so truncate
+            data = truncateIfNeeded(data, MAX_CHARACTERS)
+
             console.log("Diff from mypy_primer:")
             console.log(data)
 


Code review comments:
diff_hunk: @@ -48,15 +48,29 @@ jobs:
         with:
           github-token: ${{ secrets.GITHUB_TOKEN }}
           script: |
+            const MAX_CHARACTERS = 30000
+            const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3
+
             const fs = require('fs')
             let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' })
-            // posting comment fails if too long, so truncate
-            if (data.length > 30000) {
-              let truncated_data = data.substring(0, 30000)
-              let lines_truncated = data.split('\n').length - truncated_data.split('\n').length
-              data = truncated_data + `\n\n... (truncated ${lines_truncated} lines) ...\n`
+
+            function truncateIfNeeded(original, maxLength) {
+              let truncated = original.substring(0, maxLength)
+              if (original.length === truncated.length) {
+                return original
+              }
path: .github/workflows/mypy_primer_comment.yml
text: @user1:
nit:
```suggestion
              if (original.length <= maxLength) {
                return original
              }
              let truncated = original.substring(0, maxLength)
```