Skip to content

Commit ab8cd68

Browse files
authored
Merge pull request #9 from bzz/fix-path
Fix path in comment text
2 parents b56533c + 4012b42 commit ab8cd68

File tree

8 files changed

+96
-20
lines changed

8 files changed

+96
-20
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ stages:
88
- name: release
99
if: tag IS present
1010

11+
go_import_path: github.com/src-d/lookout-gometalint-analyzer
12+
1113
jobs:
1214
include:
1315
- name: 'Unit tests'
@@ -37,7 +39,7 @@ jobs:
3739
- cat analyzer.log
3840
- cat ../lookout-install.log
3941

40-
- name: 'Generated code'
42+
- name: 'Check deps'
4143
install:
4244
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
4345
script:

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ $ lookout-sdk review -v ipv4://localhost:2001 \
4444
| -- | -- | -- |
4545
| `GOMETALINT_HOST` | `0.0.0.0` | IP address to bind the gRCP serve |
4646
| `GOMETALINT_PORT` | `2001` | Port to bind the gRPC server |
47-
| `GOMETALINT_SERVER_URL` | `ipv4://localhost:10302` | gRPC URL of the [Data service](https://github.com/src-d/lookout/tree/master/docs#components)
48-
| `GOMETALINT_LOG_LEVEL` | `info` | Logging level (info, debug, warning or error) |
47+
| `GOMETALINT_DATA_SERVICE_URL` | `ipv4://localhost:10301` | gRPC URL of the [Data service](https://github.com/src-d/lookout/tree/master/docs#components)
48+
| `GOMETALINT_LOG_LEVEL` | `info` | Logging level ("info", "debug", "warning" or "error") |
4949

5050

5151
# Licens

_tools/install-lookout-latest.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,34 @@
44
# Depends on GNU grep
55
set -x
66

7-
#TODO(bzz): check local cached version
7+
#TODO(bzz): check for local cached version first
88

9-
curl -s --connect-timeout 5 \
9+
OS="$(uname | tr '[:upper:]' '[:lower:]')"
10+
11+
oIFS=$IFS IFS=' '
12+
curl -v ${GITHUB_TOKEN:+'-H' "Authorization: token $GITHUB_TOKEN"} \
13+
--connect-timeout 5 \
1014
--max-time 10 \
1115
--retry 5 \
1216
--retry-delay 0 \
1317
--retry-max-time 40\
1418
"https://api.github.com/repos/src-d/lookout/releases/latest" \
15-
|& tee -a ../lookout-install.log \
19+
| tee -a ../lookout-install.log \
1620
| grep -oP '"browser_download_url": "\K(.*)(?=")' \
17-
| grep linux \
21+
| grep "${OS}" \
1822
| wget -qi -
1923

20-
if [[ "${PIPESTATUS[0]}" -ne 0 || "${PIPESTATUS[1]}" -ne 0 || "${PIPESTATUS[2]}" -ne 0 || "${PIPESTATUS[4]}" -ne 0 ]]; then
24+
if [[ "${PIPESTATUS[0]}" -ne 0 || \
25+
"${PIPESTATUS[1]}" -ne 0 || \
26+
"${PIPESTATUS[2]}" -ne 0 || \
27+
"${PIPESTATUS[3]}" -ne 0 || \
28+
"${PIPESTATUS[4]}" -ne 0 ]];
29+
then
2130
echo "Unable download latest lookout SDK release" >&2
2231
exit 2
2332
fi
33+
IFS=$oIFS; unset -v oIFS
34+
# http://mywiki.wooledge.org/BashFAQ/050#I_only_want_to_pass_options_if_the_runtime_data_needs_them
2435

2536
if ! tar -xvzf lookout-sdk_*.tar.gz ; then
2637
echo "Unable to extract lookout release archive" >&2

analyzer.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
133165
func 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
}
142172
func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *lookout.PushEvent) (*lookout.EventResponse, error) {

analyzer_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
types "github.com/gogo/protobuf/types"
77
"github.com/src-d/lookout/util/grpchelper"
8+
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
)
1011

@@ -75,3 +76,32 @@ func TestArgsCorrect(t *testing.T) {
7576
},
7677
})))
7778
}
79+
80+
var pathTests = []struct {
81+
in string
82+
out string
83+
}{
84+
{"a/b.go", "/tmp/a___.___b.go"},
85+
{"tmp/a/b.go", "/tmp/tmp___.___a___.___b.go"},
86+
{"a/b/c/d/e.go", "/tmp/a___.___b___.___c___.___d___.___e.go"},
87+
}
88+
89+
func TestPathTransformations(t *testing.T) {
90+
for _, tt := range pathTests {
91+
t.Run(tt.in, func(t *testing.T) {
92+
assert.Equal(t, tt.out, flattenPath(tt.in, "/tmp"))
93+
assert.Equal(t, tt.in, revertOriginalPath(tt.out, "/tmp"))
94+
})
95+
}
96+
}
97+
98+
func TestPathInTextTransformations(t *testing.T) {
99+
tmp := "/var/folders/rx/z9zyr71d70x92zwbn3rrjx4c0000gn/T/gometalint584398570"
100+
text := "duplicate of /var/folders/rx/z9zyr71d70x92zwbn3rrjx4c0000gn/T/gometalint584398570/provider___.___github___.___poster_test.go:549-554 (dupl)"
101+
expectedText := "duplicate of provider/github/poster_test.go:549-554 (dupl)"
102+
103+
newText := revertOriginalPathIn(text, tmp)
104+
if newText != expectedText {
105+
t.Fatalf("got %q, want %q", newText, expectedText)
106+
}
107+
}

cmd/gometalint-analyzer/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func main() {
5252
var conf config
5353
envconfig.MustProcess("GOMETALINT", &conf)
5454
log.Infof("Starting %s, %s", name, litter.Sdump(conf))
55+
log.DefaultFactory = &log.LoggerFactory{Level: conf.LogLevel}
56+
log.DefaultLogger = log.New(nil)
5557

5658
grpcAddr, err := grpchelper.ToGoGrpcAddress(conf.DataServiceURL)
5759
if err != nil {

gometalint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Comment struct {
3030
func RunGometalinter(args []string) []Comment {
3131
dArgs := append([]string(nil), defaultArgs...)
3232
args = append(dArgs, args...)
33-
log.Infof("Running '%s %v'\n", bin, args)
33+
log.Debugf("Running '%s %v'\n", bin, args)
3434
out, _ := exec.Command(bin, args...).Output() // nolint: gas
3535
// ignoring err, as it's always not nil if anything found
3636

0 commit comments

Comments
 (0)