Skip to content

Commit

Permalink
Merge pull request #1792 from reviewdog/metadata-comment
Browse files Browse the repository at this point in the history
Introduce comment metadata and fingerprint for identifying existing posted comments
  • Loading branch information
haya14busa authored Jun 22, 2024
2 parents 619d473 + b501e9b commit 3e844a6
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rdformat.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: RDFormat
name: Proto
on: [push,pull_request]
jobs:
build:
Expand All @@ -7,7 +7,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build
working-directory: proto/rdf/
working-directory: proto/
run: ./update.sh
- name: Check diff
run: git diff --exit-code
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ...

### :bug: Fixes
- ...
- [#1792](https://github.com/reviewdog/reviewdog/pull/1792) Introduce `__reviewdog__` HTML comment metadata and fingerprint for identifying existing posted comments for `github-pr-review` reporter. This resolve duplicated comments issue with related location feature.

### :rotating_light: Breaking changes
- ...
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions proto/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
protoc --proto_path=./rdf --go_out=./rdf --go_opt=paths=source_relative --jsonschema_out=./rdf/jsonschema ./rdf/reviewdog.proto
protoc --proto_path=./metacomment --go_out=./metacomment --go_opt=paths=source_relative ./metacomment/metacomment.proto
166 changes: 166 additions & 0 deletions proto/metacomment/metacomment.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions proto/metacomment/metacomment.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package reviewdog.rdf;

option go_package = "github.com/reviewdog/reviewdog/proto/metacomment";

// Represents a metadata of a diagnostic result.
// It's expected to be base64 encoded and included into reporter comments such
// as GitHub Pull Request Review comment.
//
// This metadata allow reviewdog to identify the same existing comment and
// avoid posting duplicated comments. It can also be used for resolving or
// deleting existing comments.
message MetaComment {
// An unique identity, or "fingerprint", of the diagnostic result.
string fingerprint = 1;

// Source (tool) name of the diagnostic result.
// It's important to have source name so that reviewdog can handle existing
// comments with the same source properly.
string source_name = 2;
}
2 changes: 0 additions & 2 deletions proto/rdf/entrypoint.sh

This file was deleted.

File renamed without changes.
20 changes: 20 additions & 0 deletions scripts/decode-metacomment/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"log"
"os"

"github.com/reviewdog/reviewdog/service/github"
)

func main() {
if len(os.Args) == 1 {
log.Fatal("require one argument")
}
meta, err := github.DecodeMetaComment(os.Args[1])
if err != nil {
log.Fatalf("failed to decode meta comment: %v", err)
}
fmt.Printf("%v\n", meta)
}
10 changes: 5 additions & 5 deletions service/commentutil/commentutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/reviewdog/reviewdog/proto/rdf"
)

// `path` to `position`(Lnum for new file) to comment `body`s
// `path` to `position`(Lnum for new file) to comment `body` or `finterprint`

Check warning on line 12 in service/commentutil/commentutil.go

View workflow job for this annotation

GitHub Actions / golint-github-check

[golint-github-check] service/commentutil/commentutil.go#L12

comment on exported type PostedComments should be of the form "PostedComments ..." (with optional leading article)
Raw output
service/commentutil/commentutil.go:12:1: comment on exported type PostedComments should be of the form "PostedComments ..." (with optional leading article)
type PostedComments map[string]map[int][]string

// IsPosted returns true if a given comment has been posted in code review service already,
// otherwise returns false. It sees comments with same path, same position,
// and same body as same comments.
func (p PostedComments) IsPosted(c *reviewdog.Comment, lineNum int, body string) bool {
func (p PostedComments) IsPosted(c *reviewdog.Comment, lineNum int, bodyOrFingerprint string) bool {
path := c.Result.Diagnostic.GetLocation().GetPath()
if _, ok := p[path]; !ok {
return false
Expand All @@ -25,22 +25,22 @@ func (p PostedComments) IsPosted(c *reviewdog.Comment, lineNum int, body string)
return false
}
for _, b := range bodies {
if b == body {
if b == bodyOrFingerprint {
return true
}
}
return false
}

// AddPostedComment adds a posted comment.
func (p PostedComments) AddPostedComment(path string, lineNum int, body string) {
func (p PostedComments) AddPostedComment(path string, lineNum int, bodyOrFingerprint string) {
if _, ok := p[path]; !ok {
p[path] = make(map[int][]string)
}
if _, ok := p[path][lineNum]; !ok {
p[path][lineNum] = make([]string, 0)
}
p[path][lineNum] = append(p[path][lineNum], body)
p[path][lineNum] = append(p[path][lineNum], bodyOrFingerprint)
}

// DebugLog outputs posted comments as log for debugging.
Expand Down
Loading

0 comments on commit 3e844a6

Please sign in to comment.