Skip to content

Commit

Permalink
Merge pull request #35 from odair-pedro/fix/use-tag-to-delete-previou…
Browse files Browse the repository at this point in the history
…s-comment

Fixing behavior on delete previous comment
  • Loading branch information
odair-pedro committed Mar 7, 2022
2 parents 0e2583b + 72f6bfb commit e4582c9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func decoratePullRequest(qualityGate sonar.QualityGate, tag string, timeout time
return
}

err = decorator.ClearPreviousComments(qualityGate.Source)
err = decorator.ClearPreviousComments(qualityGate.Source, tag)
if err != nil {
log.Printf("Failue at remove old comments from pull request (%s): %s", qualityGate.Source, err.Error())
}
Expand Down
10 changes: 6 additions & 4 deletions decoration/azuredevops/clear_previous_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
const routeListPullRequestThreadsComments = "%s/_apis/git/repositories/%s/pullRequests/%s/threads?api-version=6.0"
const routeDeletePullRequestThreadComment = "%s/_apis/git/repositories/%s/pullRequests/%s/threads/%d/comments/%d?api-version=6.0"

func (decorator *PullRequestDecorator) ClearPreviousComments(pullRequest string) error {
comments, err := decorator._loadMyPullRequestThreadsComments(pullRequest)
func (decorator *PullRequestDecorator) ClearPreviousComments(pullRequest string, tag string) error {
comments, err := decorator._loadMyPullRequestThreadsComments(pullRequest, tag)
if err != nil {
return err
}
Expand All @@ -32,7 +32,7 @@ func (decorator *PullRequestDecorator) ClearPreviousComments(pullRequest string)
return nil
}

func (decorator *PullRequestDecorator) _loadMyPullRequestThreadsComments(pullRequest string) ([]_commentToDelete, error) {
func (decorator *PullRequestDecorator) _loadMyPullRequestThreadsComments(pullRequest string, tag string) ([]_commentToDelete, error) {
chBuff, chErr := decorator.Get(fmt.Sprintf(routeListPullRequestThreadsComments, formatPath(decorator.Project),
formatPath(decorator.Repository), pullRequest))
err := <-chErr
Expand All @@ -49,7 +49,9 @@ func (decorator *PullRequestDecorator) _loadMyPullRequestThreadsComments(pullReq

var commentsToDelete []_commentToDelete
for _, thread := range threadsWrapper.Value {
if !thread.IsDeleted && strings.ToLower(thread.Properties.GeneratedBySonarCI.Value) == "true" {
if !thread.IsDeleted &&
strings.ToLower(thread.Properties.GeneratedBySonarCI.Value) == "true" &&
thread.Properties.Tag.Value == tag {
for _, comment := range thread.Comments {
if !comment.IsDeleted {
commentsToDelete = append(commentsToDelete,
Expand Down
47 changes: 36 additions & 11 deletions decoration/azuredevops/clear_previous_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPullRequestDecorator_ClearPreviousComments_CheckErrorOnLoadComments(t *
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
gotError := decorator.ClearPreviousComments("pullrequest-test")
gotError := decorator.ClearPreviousComments("pullrequest-test", "")

if gotError != wantError {
t.Fail()
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestPullRequestDecorator_ClearPreviousComments_CheckErrorOnDeleteComments(t
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
gotError := decorator.ClearPreviousComments("pullrequest-test")
gotError := decorator.ClearPreviousComments("pullrequest-test", "")

if gotError != wantError {
t.Fail()
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestPullRequestDecorator_ClearPreviousComments_CheckNoError(t *testing.T) {
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
gotError := decorator.ClearPreviousComments("pullrequest-test")
gotError := decorator.ClearPreviousComments("pullrequest-test", "")

if gotError != nil {
t.Fail()
Expand All @@ -119,7 +119,7 @@ func TestPullRequestDecorator_loadMyPullRequestThreadsComments_CheckErrorOnReque
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
_, gotErr := decorator._loadMyPullRequestThreadsComments("anything")
_, gotErr := decorator._loadMyPullRequestThreadsComments("anything", "")

if gotErr != wantError {
t.FailNow()
Expand All @@ -140,7 +140,7 @@ func TestPullRequestDecorator_loadMyPullRequestThreadsComments_CheckErrorOnReadR
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
_, gotErr := decorator._loadMyPullRequestThreadsComments("anything")
_, gotErr := decorator._loadMyPullRequestThreadsComments("anything", "")

if gotErr == nil {
t.FailNow()
Expand All @@ -156,36 +156,61 @@ func TestPullRequestDecorator_loadMyPullRequestThreadsComments_CheckResult(t *te
{Id: 11, IsDeleted: true},
{Id: 12, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "True"}},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "True"},
Tag: models.ThreadPropertySonarCIModel{Value: "123"},
},
},
{Id: 2, IsDeleted: true,
Comments: []models.ThreadCommentModel{
{Id: 20, IsDeleted: false},
{Id: 21, IsDeleted: false},
{Id: 22, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "True"}},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "True"},
Tag: models.ThreadPropertySonarCIModel{Value: "123"},
},
},
{Id: 3, IsDeleted: false,
Comments: []models.ThreadCommentModel{
{Id: 31, IsDeleted: false},
{Id: 32, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "true"}},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "true"},
Tag: models.ThreadPropertySonarCIModel{Value: "123"},
},
},
{Id: 4, IsDeleted: false,
Comments: []models.ThreadCommentModel{
{Id: 41, IsDeleted: false},
{Id: 42, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "false"}},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "false"},
Tag: models.ThreadPropertySonarCIModel{Value: "123"},
},
},
{Id: 5, IsDeleted: false,
Comments: []models.ThreadCommentModel{
{Id: 51, IsDeleted: false},
{Id: 52, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "anything"}},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "false"},
Tag: models.ThreadPropertySonarCIModel{Value: "123"},
},
},
{Id: 6, IsDeleted: false,
Comments: []models.ThreadCommentModel{
{Id: 61, IsDeleted: false},
{Id: 62, IsDeleted: false},
},
Properties: models.ThreadPropertyModel{
GeneratedBySonarCI: models.ThreadPropertySonarCIModel{Value: "true"},
Tag: models.ThreadPropertySonarCIModel{Value: "999"},
},
},
},
}
Expand All @@ -212,7 +237,7 @@ func TestPullRequestDecorator_loadMyPullRequestThreadsComments_CheckResult(t *te
}

decorator := NewPullRequestDecorator(mockConn, &mocks.MockEngine{}, "project-test", "repo-test")
gotThreads, err := decorator._loadMyPullRequestThreadsComments(pullRequest)
gotThreads, err := decorator._loadMyPullRequestThreadsComments(pullRequest, "123")

if err != nil {
t.Fail()
Expand Down
2 changes: 1 addition & 1 deletion decoration/azuredevops/comment_qualitygate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (decorator *PullRequestDecorator) CommentQualityGate(qualityGate sonar.Qual
return err
}

commentModel := models.ParseCommentModel(qualityGate, report)
commentModel := models.ParseCommentModel(qualityGate, report, tag)
body, _ := json.Marshal(commentModel)

endpoint := fmt.Sprintf(routeCommentPullRequest, formatPath(decorator.Project), formatPath(decorator.Repository), qualityGate.Source)
Expand Down
7 changes: 4 additions & 3 deletions decoration/azuredevops/models/comment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ type CommentWrapperModel struct {
}

type CommentPropertyModel struct {
GeneratedBySonarCI bool
GeneratedBySonarCI bool `json:"generatedBySonarCi"`
Tag string `json:"tag"`
}

type CommentModel struct {
Content string `json:"content"`
CommentType string `json:"commentType"`
}

func ParseCommentModel(qualityGate sonar.QualityGate, report string) CommentWrapperModel {
func ParseCommentModel(qualityGate sonar.QualityGate, report string, tag string) CommentWrapperModel {
var status string
if qualityGate.HasPassed() {
status = statusClosed
Expand All @@ -34,7 +35,7 @@ func ParseCommentModel(qualityGate sonar.QualityGate, report string) CommentWrap

return CommentWrapperModel{
Status: status,
Properties: CommentPropertyModel{GeneratedBySonarCI: true},
Properties: CommentPropertyModel{GeneratedBySonarCI: true, Tag: tag},
Comments: []CommentModel{
{CommentType: commentType, Content: report},
}}
Expand Down
4 changes: 2 additions & 2 deletions decoration/azuredevops/models/comment_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func Test_ParseCommentModel_CheckStatus(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseCommentModel(tt.args.qualityGate, tt.args.report); got.Status != tt.wantStatus {
if got := ParseCommentModel(tt.args.qualityGate, tt.args.report, "any-tag"); got.Status != tt.wantStatus {
t.Errorf("parseCommentModel() Status = %v, want %v", got.Status, tt.wantStatus)
}
})
}
}

func Test_ParseCommentModel_CheckPropertiesGeneratedBySonarCI_IsTrue(t *testing.T) {
got := ParseCommentModel(sonar.QualityGate{Status: "anything"}, "report-test").Properties.GeneratedBySonarCI
got := ParseCommentModel(sonar.QualityGate{Status: "anything"}, "report-test", "any-tag").Properties.GeneratedBySonarCI
if got != true {
t.Errorf("parseCommentModel() Properties.GeneratedBySonarCI want true")
}
Expand Down
1 change: 1 addition & 0 deletions decoration/azuredevops/models/thread_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ThreadCommentModel struct {

type ThreadPropertyModel struct {
GeneratedBySonarCI ThreadPropertySonarCIModel `json:"generatedBySonarCI"`
Tag ThreadPropertySonarCIModel `json:"tag"`
}

type ThreadPropertySonarCIModel struct {
Expand Down
2 changes: 1 addition & 1 deletion decoration/pullrequest_decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package decoration
import "sonarci/sonar"

type PullRequestDecorator interface {
ClearPreviousComments(pullRequest string) error
ClearPreviousComments(pullRequest string, tag string) error
CommentQualityGate(qualityGate sonar.QualityGate, tag string) error
}

0 comments on commit e4582c9

Please sign in to comment.