Skip to content

Commit

Permalink
Ensure there’s a separate PR comment when filename is different (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdubroy committed Jan 31, 2022
1 parent 1a31067 commit bbd530a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func commentOnGitHubPullRequest(o *options, prNodeID string) func(map[string][]s
return err
}

id, err := existingCommentId(prNodeID)
id, err := existingCommentId(prNodeID, o.filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func commitCount(prNodeID string) (int, error) {
return data.Node.Commits.TotalCount, err
}

func existingCommentId(prNodeID string) (string, error) {
func existingCommentId(prNodeID string, filename string) (string, error) {
data := struct {
Node struct {
Comments struct {
Expand Down Expand Up @@ -301,7 +301,7 @@ func existingCommentId(prNodeID string) (string, error) {
}

for _, comment := range data.Node.Comments.Nodes {
if strings.HasPrefix(comment.Body, markdownCommentTitle) {
if strings.HasPrefix(comment.Body, markdownCommentTitle(filename)) {
return comment.Id, nil
}
}
Expand Down Expand Up @@ -379,7 +379,9 @@ type options struct {
print func(notifs map[string][]string) error
}

const markdownCommentTitle = "<!-- codenotify report -->\n"
func markdownCommentTitle(filename string) string {
return fmt.Sprintf("<!-- codenotify:%s report -->\n", filename)
}

func (o *options) writeNotifications(w io.Writer, notifs map[string][]string) error {
subs := []string{}
Expand All @@ -401,7 +403,7 @@ func (o *options) writeNotifications(w io.Writer, notifs map[string][]string) er
}
return nil
case "markdown":
fmt.Fprint(w, markdownCommentTitle)
fmt.Fprint(w, markdownCommentTitle(o.filename))
fmt.Fprintf(w, "[Codenotify](https://github.com/sourcegraph/codenotify): Notifying subscribers in %s files for diff %s...%s.\n\n", o.filename, o.baseRef, o.headRef)
if len(notifs) == 0 {
fmt.Fprintln(w, "No notifications.")
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestWriteNotifications(t *testing.T) {
},
notifs: nil,
output: []string{
"<!-- codenotify report -->",
"<!-- codenotify:CODENOTIFY report -->",
"[Codenotify](https://github.com/sourcegraph/codenotify): Notifying subscribers in CODENOTIFY files for diff a...b.",
"",
"No notifications.",
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestWriteNotifications(t *testing.T) {
"@js": {"file.js", "dir/file.js"},
},
output: []string{
"<!-- codenotify report -->",
"<!-- codenotify:CODENOTIFY report -->",
"[Codenotify](https://github.com/sourcegraph/codenotify): Notifying subscribers in CODENOTIFY files for diff a...b.",
"",
"| Notify | File(s) |",
Expand Down

0 comments on commit bbd530a

Please sign in to comment.