<commit_before>package webhook

import (
	"cred-alert/git"
	"cred-alert/logging"
	"net/http"

	"github.com/google/go-github/github"
	"github.com/pivotal-golang/lager"

	gh "cred-alert/github"
)

//go:generate counterfeiter . Scanner

type Scanner interface {
	ScanPushEvent(lager.Logger, github.PushEvent)
}

type PushEventScanner struct {
	fetchDiff func(lager.Logger, github.PushEvent) (string, error)
	scan      func(lager.Logger, string) []git.Line
	emitter   logging.Emitter
}

func NewPushEventScanner(fetchDiff func(lager.Logger, github.PushEvent) (string, error), scan func(lager.Logger, string) []git.Line, emitter logging.Emitter) *PushEventScanner {
	return &PushEventScanner{
		fetchDiff: fetchDiff,
		scan:      scan,
		emitter:   emitter,
	}
}

func (s PushEventScanner) ScanPushEvent(logger lager.Logger, event github.PushEvent) {
	logger = logger.Session("scan-event")

	diff, err := s.fetchDiff(logger, event)
	if err != nil {
		return
	}

	lines := s.scan(logger, diff)

	for _, line := range lines {
		logger.Info("found-credential", lager.Data{
			"path":        line.Path,
			"line-number": line.LineNumber,
		})

		s.emitter.CountViolation(logger, len(lines))
	}
}

func FetchDiff(logger lager.Logger, event github.PushEvent) (string, error) {
	httpClient := &http.Client{}
	githubClient := gh.NewClient("https://api.github.com/", httpClient)<commit_msg>Fix CountViolation line length<commit_after>package webhook

import (
	"cred-alert/git"
	"cred-alert/logging"
	"net/http"

	"github.com/google/go-github/github"
	"github.com/pivotal-golang/lager"

	gh "cred-alert/github"
)

//go:generate counterfeiter . Scanner

type Scanner interface {
	ScanPushEvent(lager.Logger, github.PushEvent)
}

type PushEventScanner struct {
	fetchDiff func(lager.Logger, github.PushEvent) (string, error)
	scan      func(lager.Logger, string) []git.Line
	emitter   logging.Emitter
}

func NewPushEventScanner(fetchDiff func(lager.Logger, github.PushEvent) (string, error), scan func(lager.Logger, string) []git.Line, emitter logging.Emitter) *PushEventScanner {
	return &PushEventScanner{
		fetchDiff: fetchDiff,
		scan:      scan,
		emitter:   emitter,
	}
}

func (s PushEventScanner) ScanPushEvent(logger lager.Logger, event github.PushEvent) {
	logger = logger.Session("scan-event")

	diff, err := s.fetchDiff(logger, event)
	if err != nil {
		return
	}

	lines := s.scan(logger, diff)

	for _, line := range lines {
		logger.Info("found-credential", lager.Data{
			"path":        line.Path,
			"line-number": line.LineNumber,
		})

	}

	s.emitter.CountViolation(logger, len(lines))
}

func FetchDiff(logger lager.Logger, event github.PushEvent) (string, error) {
	httpClient := &http.Client{}
	githubClient := gh.NewClient("https://api.github.com/", httpClient)
{'fraction_non_alphanumeric': 0.13651137594799567, 'fraction_numerical': 0.0, 'mean_word_length': 4.474308300395257, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '7579789', 'n_tokens_mistral': 1018, 'n_tokens_neox': 957, 'n_words': 196}