• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package controllers
2
3import (
4	"fmt"
5	"log"
6	"os"
7	"path/filepath"
8
9	ent "repodiff/entities"
10	"repodiff/mappers"
11	"repodiff/persistence/filesystem"
12	"repodiff/repositories"
13	"repodiff/utils"
14)
15
16func GenerateCommitReport(appConfig ent.ApplicationConfig) error {
17	for _, target := range appConfig.DiffTargets {
18		if err := generateCommitReport(appConfig, target); err != nil {
19			return err
20		}
21	}
22	return nil
23}
24
25func generateCommitReport(appConfig ent.ApplicationConfig, target ent.DiffTarget) error {
26	log.Printf("Generating commit report for (upstream) %s vs (downstream) %s\n", target.Upstream.Branch, target.Downstream.Branch)
27	sourceRepo, err := repositories.NewSourceRepository()
28	if err != nil {
29		return err
30	}
31	mappedTarget, err := sourceRepo.DiffTargetToMapped(target)
32	if err != nil {
33		return err
34	}
35	// TODO export the commitRepository type in order to add the possibility of helpers
36	commitRepo, err := repositories.NewCommitRepository(mappedTarget)
37	if err != nil {
38		return err
39	}
40
41	commitRows, err := commitRepo.GetMostRecentCommits()
42	if err != nil {
43		return err
44	}
45
46	dir := filepath.Join(appConfig.OutputDirectory, "reports")
47	fname := filepath.Join(dir, filenameForTarget(target))
48	os.MkdirAll(dir, os.ModePerm)
49	log.Printf("Writing to file %s\n", fname)
50
51	if err := filesystem.WriteCSVToFile(
52		mappers.CommitCSVHeader(),
53		mappers.CommitEntitiesToCSVRows(commitRows),
54		fname,
55	); err != nil {
56		return err
57	}
58	return nil
59}
60
61func filenameForTarget(target ent.DiffTarget) string {
62	return fmt.Sprintf(
63		"%s_upstream-%s_vs_downstream-%s.csv",
64		utils.TimestampToDate(
65			utils.TimestampSeconds(),
66		),
67		target.Upstream.Branch,
68		target.Downstream.Branch,
69	)
70
71}
72