Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed infra-860, bot did not delete previous comments #4

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (m *GithubClient) DeletePreviousComments(prNumber string) error {
}

for _, e := range getComments.Repository.PullRequest.Comments.Edges {
if e.Node.Author.Login == getComments.Viewer.Login {
if cleanBotUserName(e.Node.Author.Login) == cleanBotUserName(getComments.Viewer.Login) {
_, err := m.V3.Issues.DeleteComment(context.TODO(), m.Owner, m.Repository, e.Node.DatabaseId)
if err != nil {
return err
Expand All @@ -413,3 +413,10 @@ func parseRepository(s string) (string, string, error) {
}
return parts[0], parts[1], nil
}

func cleanBotUserName(username string) string{
if strings.HasSuffix(username, "[bot]"){
return strings.TrimSuffix(username, "[bot]")
}
return username
}
50 changes: 50 additions & 0 deletions github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package resource

import "testing"

func Test_cleanBotUserName(t *testing.T) {
type args struct {
username string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Main Test Case",
args: args{
username: "infratest[bot]",
},
want: "infratest",
},
{
name: "Without suffix bot",
args: args{
username: "infratest",
},
want: "infratest",
},
{
name: "Bot only",
args: args{
username: "[bot]",
},
want: "",
},
{
name: "Bot at start",
args: args{
username: "[bot]infratest",
},
want: "[bot]infratest",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cleanBotUserName(tt.args.username); got != tt.want {
t.Errorf("cleanBotUserName() = %v, want %v", got, tt.want)
}
})
}
}