<commit_before>package main

import (
	"bytes"
	"fmt"
	"os/exec"
	"strings"

	"github.com/nlopes/slack"
)

type task func(*slack.RTM, *slack.MessageEvent, []string)

func taskLint(rtm *slack.RTM, ev *slack.MessageEvent, params []string) {

	if params[0] == "-help" {
		text := "`lint repository#branch`, branch is facultative, `master` is the default branch"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	branchSplit := strings.Split(params[0], "#")

	branch := "master"
	repo := params[0]

	if len(branchSplit) == 2 {
		branch = branchSplit[1]
		if branch == "" {
			branch = "master"
		}
		repo = branchSplit[0]
	}

	rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf(":rocket: Roger that, clone the `%s` repository and set HEAD to `%s`", repo, branch), ev.Channel))

	repository, err := NewRepository("<EMAIL>:eliocity", repo)
	repository.currentBranch = branch

	if err != nil {
		text := ":warning: Holy crap, an error occured"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	repository.clone(branch)
	defer repository.clean()

	if err != nil {
		text := ":warning: Holy crap, an error occured while trying to clone the repo, please check that the syntax is correct"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	prm := slack.NewPostMessageParameters()
	prm.Markdown = true

	results := lint(repository.localPath)

	for i, r := range results {

		at := slack.Attachment{}
		at.Color = "warning"
		at.AuthorName = r.linter
		at.Title = "See the code on bitbucket.org"
		at.TitleLink = repository.getBitbucketLink(r.path, r.line)
		at.Text = r.message
		at.Footer = fmt.Sprintf("%s - line %d", r.path, r.line)

		at.Fields = []slack.AttachmentField{
			{
				Title: "Level",
				Value: r.level,
				Short: true,
			},
		}

		prm.Attachments = append(prm.Attachments, at)

		if i == 9 {
			break
		}
	}
	message := fmt.Sprintf(":checkered_flag: We are done, please find the results below. Found %d points", len(results))
	rtm.PostMessage(ev.Channel, message, prm)

}

func lint(s string) []*lintResult {

	buffer := []*lintResult{}

	cmd := exec.Command("gometalinter",
		s+"/...",
		"--deadline=30s",
		"--vendor",
		"--disable-all",
		"--enable=errcheck",
		"--enable=vet",
		"--enable=vetshadow",
		"--enable=gosimple",
		"--enable=golint",
		"--enable=gofmt",
		"--enable=goimports",<commit_msg>feat(tasklint): better message if lint returns nothing<commit_after>package main

import (
	"bytes"
	"fmt"
	"os/exec"
	"strings"

	"github.com/nlopes/slack"
)

type task func(*slack.RTM, *slack.MessageEvent, []string)

func taskLint(rtm *slack.RTM, ev *slack.MessageEvent, params []string) {

	if params[0] == "-help" {
		text := "`lint repository#branch`, branch is facultative, `master` is the default branch"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	branchSplit := strings.Split(params[0], "#")

	branch := "master"
	repo := params[0]

	if len(branchSplit) == 2 {
		branch = branchSplit[1]
		if branch == "" {
			branch = "master"
		}
		repo = branchSplit[0]
	}

	rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf(":rocket: Roger that, clone the `%s` repository and set HEAD to `%s`", repo, branch), ev.Channel))

	repository, err := NewRepository("<EMAIL>:eliocity", repo)
	repository.currentBranch = branch

	if err != nil {
		text := ":warning: Holy crap, an error occured"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	repository.clone(branch)
	defer repository.clean()

	if err != nil {
		text := ":warning: Holy crap, an error occured while trying to clone the repo, please check that the syntax is correct"
		rtm.SendMessage(rtm.NewOutgoingMessage(text, ev.Channel))
		return
	}

	prm := slack.NewPostMessageParameters()
	prm.Markdown = true

	results := lint(repository.localPath)

	for i, r := range results {

		at := slack.Attachment{}
		at.Color = "warning"
		at.AuthorName = r.linter
		at.Title = "See the code on bitbucket.org"
		at.TitleLink = repository.getBitbucketLink(r.path, r.line)
		at.Text = r.message
		at.Footer = fmt.Sprintf("%s - line %d", r.path, r.line)

		at.Fields = []slack.AttachmentField{
			{
				Title: "Level",
				Value: r.level,
				Short: true,
			},
		}

		prm.Attachments = append(prm.Attachments, at)

		if i == 9 {
			break
		}
	}

	msg := "Well done ! All clear."

	if len(results) > 0 {
		msg = fmt.Sprintf(":checkered_flag: Done, please find the results below. Found %d points", len(results))
	}

	if len(results) > 10 {
		msg = fmt.Sprintf("%s, only the 10 first one will be shown", msg)
	}

	rtm.PostMessage(ev.Channel, msg, prm)

}

func lint(s string) []*lintResult {

	buffer := []*lintResult{}

	cmd := exec.Command("gometalinter",
		s+"/...",
		"--deadline=30s",
		"--vendor",
		"--disable-all",
		"--enable=errcheck",
		"--enable=vet",
		"--enable=vetshadow",
		"--enable=gosimple",
		"--enable=golint",
		"--enable=gofmt",
		"--enable=goimports",
{'fraction_non_alphanumeric': 0.1618181818181818, 'fraction_numerical': 0.004646464646464647, 'mean_word_length': 3.8067961165048545, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2300637', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1765, 'n_words': 429}