@@ -102,19 +102,18 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *lookout.ReviewEvent
102102 log .Debugf ("no Golang files found. skip running gometalinter" )
103103 return & lookout.EventResponse {AnalyzerVersion : a .Version }, nil
104104 }
105+ log .Debugf ("%d Golang files found. running gometalinter" , saved )
105106
106107 withArgs := append (append (a .Args , tmp ), a .linterArguments (e .Configuration )... )
107108 comments := RunGometalinter (withArgs )
108109 var allComments []* lookout.Comment
109110 for _ , comment := range comments {
110- //TrimLeft( , tmp) but \w rel path
111- file := comment . file [ strings . LastIndex (comment .file , tmp )+ len ( tmp ):]
111+ origPathFile := revertOriginalPath ( comment . file , tmp )
112+ origPathText := revertOriginalPathIn (comment .text , tmp )
112113 newComment := lookout.Comment {
113- File : strings .TrimLeft (
114- path .Join (strings .Split (file , artificialSep )... ),
115- string (os .PathSeparator )),
114+ File : origPathFile ,
116115 Line : comment .lino ,
117- Text : comment . text ,
116+ Text : origPathText ,
118117 }
119118 allComments = append (allComments , & newComment )
120119 log .Debugf ("Get comment %v" , newComment )
@@ -127,16 +126,47 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *lookout.ReviewEvent
127126 }, nil
128127}
129128
129+ // flattenPath flattens relative path and puts it inside tmp.
130+ func flattenPath (file string , tmp string ) string {
131+ nFile := strings .Join (strings .Split (file , string (os .PathSeparator )), artificialSep )
132+ nPath := path .Join (tmp , nFile )
133+ return nPath
134+ }
135+
136+ // revertOriginalPath reverses origina path from a flat one.
137+ func revertOriginalPath (file string , tmp string ) string {
138+ //TrimLeft(, tmp) but works for rel paths
139+ noTmpfile := file [strings .Index (file , tmp )+ len (tmp ):]
140+ origPathFile := strings .TrimLeft (
141+ path .Join (strings .Split (noTmpfile , artificialSep )... ),
142+ string (os .PathSeparator ))
143+ return origPathFile
144+ }
145+
146+ // revertOriginalPathIn a given text, recovers original path in words
147+ // that have 'artificialSep'.
148+ func revertOriginalPathIn (text string , tmp string ) string {
149+ if strings .LastIndex (text , artificialSep ) < 0 {
150+ return text
151+ }
152+ var words []string
153+ for _ , word := range strings .Fields (text ) {
154+ if strings .Index (word , artificialSep ) >= 0 {
155+ word = revertOriginalPath (word , tmp )
156+ }
157+ words = append (words , word )
158+ }
159+ return strings .Join (words , " " )
160+ }
161+
130162// tryToSaveTo saves a file to given dir, preserving it's original path.
131163// It only loggs any errors and does not fail. All files saved this way will
132164// be in the root of the same dir.
133165func tryToSaveTo (file * lookout.File , tmp string ) {
134- nFile := strings .Join (strings .Split (file .Path , string (os .PathSeparator )), artificialSep )
135- nPath := path .Join (tmp , nFile )
136- log .Debugf ("Saving file '%s', as '%s'" , file .Path , nPath )
137- err := ioutil .WriteFile (nPath , file .Content , 0644 )
166+ flatPath := flattenPath (file .Path , tmp )
167+ err := ioutil .WriteFile (flatPath , file .Content , 0644 )
138168 if err != nil {
139- log .Errorf (err , "failed to write a file %s " , nPath )
169+ log .Errorf (err , "failed to write a file %q " , flatPath )
140170 }
141171}
142172func (a * Analyzer ) NotifyPushEvent (ctx context.Context , e * lookout.PushEvent ) (* lookout.EventResponse , error ) {
0 commit comments