Skip to content

Commit

Permalink
fix #71: complete contribution source with packer integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Sep 15, 2021
1 parent fb06a65 commit 11ebc98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
20 changes: 4 additions & 16 deletions internal/command/github/exec/contribution_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ func ContributionDiff(cnf *config.Tool) Runner {
return wrap(err, input)
}

base = &contribution.UpstreamSource{
Provider: service,
Year: year,
}
base = contribution.NewUpstreamSource(service, year)
} else {
base = &contribution.FileSource{
Provider: cnf.FS,
Path: input,
}
base = contribution.NewFileSource(cnf.FS, input)
}

var head run.ContributionSource
Expand All @@ -54,15 +48,9 @@ func ContributionDiff(cnf *config.Tool) Runner {
return wrap(err, input)
}

head = &contribution.UpstreamSource{
Provider: service,
Year: year,
}
head = contribution.NewUpstreamSource(service, year)
} else {
head = &contribution.FileSource{
Provider: cnf.FS,
Path: input,
}
head = contribution.NewFileSource(cnf.FS, input)
}

return run.ContributionDiff(cmd.Context(), base, head, cmd)
Expand Down
5 changes: 1 addition & 4 deletions internal/model/github/contribution/heatmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ func TestSuggest(t *testing.T) {
}

func golden(t testing.TB, name string) HeatMap {
src := FileSource{
Provider: afero.NewBasePathFs(afero.NewOsFs(), "testdata"),
Path: name,
}
src := NewFileSource(afero.NewBasePathFs(afero.NewOsFs(), "testdata"), name)
chm, err := src.Fetch(context.TODO())
require.NoError(t, err)
return chm
Expand Down
37 changes: 31 additions & 6 deletions internal/model/github/contribution/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ func init() {
)
}

type FileSource struct {
func NewFileSource(src afero.Fs, path string) *fileSource {
return &fileSource{
Provider: src,
Path: path,
}
}

type fileSource struct {
Provider afero.Fs
Path string

data HeatMap
}

func (src FileSource) Location() string {
func (src fileSource) Location() string {
return fmt.Sprintf("file:%s", src.Path)
}

func (src *FileSource) Fetch(_ context.Context) (HeatMap, error) {
func (src *fileSource) Fetch(_ context.Context) (HeatMap, error) {
if src.data != nil {
return src.data, nil
}
Expand All @@ -60,18 +67,36 @@ func (src *FileSource) Fetch(_ context.Context) (HeatMap, error) {
return src.data, nil
}

type UpstreamSource struct {
func (src *fileSource) Store(chm HeatMap) error {
f, err := src.Provider.Create(src.Path)
if err != nil {
return err
}
defer safe.Close(f, unsafe.Ignore)

src.data = chm
return packer.Pack(f, src.data)
}

func NewUpstreamSource(src Contributor, year time.Time) *upstreamSource {
return &upstreamSource{
Provider: src,
Year: year,
}
}

type upstreamSource struct {
Provider Contributor
Year time.Time

data HeatMap
}

func (src UpstreamSource) Location() string {
func (src upstreamSource) Location() string {
return fmt.Sprintf("upstream:year(%d)", src.Year.Year())
}

func (src *UpstreamSource) Fetch(ctx context.Context) (HeatMap, error) {
func (src *upstreamSource) Fetch(ctx context.Context) (HeatMap, error) {
if src.data != nil {
return src.data, nil
}
Expand Down

0 comments on commit 11ebc98

Please sign in to comment.