day.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package hercules
  2. import (
  3. "time"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/plumbing/object"
  6. )
  7. // DaysSinceStart provides the relative date information for every commit.
  8. // It is a PipelineItem.
  9. type DaysSinceStart struct {
  10. day0 time.Time
  11. previousDay int
  12. }
  13. const (
  14. // DependencyDay is the name of the dependency which DaysSinceStart provides - the number
  15. // of days since the first commit in the analysed sequence.
  16. DependencyDay = "day"
  17. )
  18. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  19. func (days *DaysSinceStart) Name() string {
  20. return "DaysSinceStart"
  21. }
  22. // Provides returns the list of names of entities which are produced by this PipelineItem.
  23. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  24. // to this list. Also used by hercules.Registry to build the global map of providers.
  25. func (days *DaysSinceStart) Provides() []string {
  26. arr := [...]string{DependencyDay}
  27. return arr[:]
  28. }
  29. // Requires returns the list of names of entities which are needed by this PipelineItem.
  30. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  31. // entities are Provides() upstream.
  32. func (days *DaysSinceStart) Requires() []string {
  33. return []string{}
  34. }
  35. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  36. func (days *DaysSinceStart) ListConfigurationOptions() []ConfigurationOption {
  37. return []ConfigurationOption{}
  38. }
  39. // Configure sets the properties previously published by ListConfigurationOptions().
  40. func (days *DaysSinceStart) Configure(facts map[string]interface{}) {}
  41. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  42. // calls. The repository which is going to be analysed is supplied as an argument.
  43. func (days *DaysSinceStart) Initialize(repository *git.Repository) {
  44. days.day0 = time.Time{}
  45. days.previousDay = 0
  46. }
  47. // Consume runs this PipelineItem on the next commit data.
  48. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  49. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  50. // This function returns the mapping with analysis results. The keys must be the same as
  51. // in Provides(). If there was an error, nil is returned.
  52. func (days *DaysSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  53. commit := deps["commit"].(*object.Commit)
  54. index := deps["index"].(int)
  55. if index == 0 {
  56. // first iteration - initialize the file objects from the tree
  57. days.day0 = commit.Author.When
  58. // our precision is 1 day
  59. days.day0 = days.day0.Truncate(24 * time.Hour)
  60. }
  61. day := int(commit.Author.When.Sub(days.day0).Hours() / 24)
  62. if day < days.previousDay {
  63. // rebase works miracles, but we need the monotonous time
  64. day = days.previousDay
  65. }
  66. days.previousDay = day
  67. return map[string]interface{}{DependencyDay: day}, nil
  68. }
  69. func init() {
  70. Registry.Register(&DaysSinceStart{})
  71. }