github.com/mattbailey/reviewdog@v0.10.0/service/github/githubutils/comment_writer.go (about)

     1  package githubutils
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/haya14busa/go-actions-toolkit/core"
    10  	"github.com/reviewdog/reviewdog"
    11  )
    12  
    13  const MaxLoggingAnnotationsPerStep = 10
    14  
    15  var _ reviewdog.CommentService = &GitHubActionLogWriter{}
    16  
    17  // GitHubActionLogWriter reports results via logging command to create
    18  // annotations.
    19  // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#example-5
    20  type GitHubActionLogWriter struct {
    21  	level     string
    22  	reportNum int
    23  }
    24  
    25  // NewGitHubActionLogWriter returns new GitHubActionLogWriter.
    26  func NewGitHubActionLogWriter(level string) *GitHubActionLogWriter {
    27  	return &GitHubActionLogWriter{level: level}
    28  }
    29  
    30  func (lw *GitHubActionLogWriter) Post(_ context.Context, c *reviewdog.Comment) error {
    31  	lw.reportNum++
    32  	if lw.reportNum == MaxLoggingAnnotationsPerStep {
    33  		WarnTooManyAnnotationOnce()
    34  	}
    35  	ReportAsGitHubActionsLog(c.ToolName, lw.level, c.CheckResult)
    36  	return nil
    37  }
    38  
    39  // Flush checks overall error at last.
    40  func (lw *GitHubActionLogWriter) Flush(_ context.Context) error {
    41  	if lw.reportNum > 9 {
    42  		return fmt.Errorf("GitHubActionLogWriter: reported too many annotation (N=%d)", lw.reportNum)
    43  	}
    44  	return nil
    45  }
    46  
    47  // ReportAsGitHubActionsLog reports results via logging command to create
    48  // annotations.
    49  // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#example-5
    50  func ReportAsGitHubActionsLog(toolName, level string, c *reviewdog.CheckResult) {
    51  	mes := fmt.Sprintf("[%s] reported by reviewdog 🐶\n%s\n\nRaw Output:\n%s",
    52  		toolName, c.Message, strings.Join(c.Lines, "\n"))
    53  	opt := &core.LogOption{
    54  		File: c.Path,
    55  		Line: c.Lnum,
    56  		Col:  c.Col,
    57  	}
    58  
    59  	switch level {
    60  	// no info command with location data.
    61  	case "warning", "info":
    62  		core.Warning(mes, opt)
    63  	case "error", "":
    64  		core.Error(mes, opt)
    65  	default:
    66  		core.Error(fmt.Sprintf("Unknown level: %s", level), nil)
    67  		core.Error(mes, opt)
    68  	}
    69  }
    70  
    71  func WarnTooManyAnnotationOnce() {
    72  	warnTooManyAnnotationOnce.Do(warnTooManyAnnotation)
    73  }
    74  
    75  var warnTooManyAnnotationOnce sync.Once
    76  
    77  func warnTooManyAnnotation() {
    78  	core.Error(`reviewdog: Too many results (annotations) in diff.
    79  You may miss some annotations due to GitHub limitation for annotation created by logging command.
    80  Please check GitHub Actions log console to see all results.
    81  
    82  Limitation:
    83  - 10 warning annotations and 10 error annotations per step
    84  - 50 annotations per job (sum of annotations from all the steps)
    85  - 50 annotations per run (separate from the job annotations, these annotations aren't created by users)
    86  
    87  Source: https://github.community/t5/GitHub-Actions/Maximum-number-of-annotations-that-can-be-created-using-GitHub/m-p/39085`, nil)
    88  }