@@ -7,10 +7,10 @@ package harness
7
7
import (
8
8
"context"
9
9
"fmt"
10
- "io"
11
10
"strings"
12
11
"time"
13
12
13
+ "github.com/bluekeyes/go-gitdiff/gitdiff"
14
14
"github.com/drone/go-scm/scm"
15
15
)
16
16
@@ -81,17 +81,9 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
81
81
func (s * gitService ) CompareChanges (ctx context.Context , repo , source , target string , _ scm.ListOptions ) ([]* scm.Change , * scm.Response , error ) {
82
82
harnessURI := buildHarnessURI (s .client .account , s .client .organization , s .client .project , repo )
83
83
path := fmt .Sprintf ("api/v1/repos/%s/compare/%s...%s" , harnessURI , source , target )
84
- res , err := s .client .do (ctx , "GET" , path , nil , nil )
85
- // convert response to a string
86
84
buf := new (strings.Builder )
87
- _ , _ = io .Copy (buf , res .Body )
88
- changes := []* scm.Change {
89
- {
90
- Path : "not implemented" ,
91
- Sha : buf .String (),
92
- },
93
- }
94
- return changes , res , err
85
+ res , err := s .client .do (ctx , "GET" , path , nil , buf )
86
+ return convertCompareChanges (buf .String ()), res , err
95
87
}
96
88
97
89
// native data structures
@@ -172,6 +164,26 @@ func convertCommitList(src []*commitInfo) []*scm.Commit {
172
164
return dst
173
165
}
174
166
167
+ func convertCompareChanges (src string ) []* scm.Change {
168
+ files , _ , err := gitdiff .Parse (strings .NewReader (src ))
169
+ if err != nil {
170
+ return nil
171
+ }
172
+
173
+ changes := make ([]* scm.Change , 0 )
174
+ for _ , f := range files {
175
+ changes = append (changes , & scm.Change {
176
+ Path : f .NewName ,
177
+ PrevFilePath : f .OldName ,
178
+ Added : f .IsNew ,
179
+ Deleted : f .IsDelete ,
180
+ Renamed : f .IsRename ,
181
+ })
182
+ }
183
+
184
+ return changes
185
+ }
186
+
175
187
func convertCommitInfo (src * commitInfo ) * scm.Commit {
176
188
return & scm.Commit {
177
189
Sha : src .Sha ,
0 commit comments