forked from JonasHiltl/openchangelog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract changelog & article loading to Loader struct & add rss feed
- Loading branch information
Jonas Hiltl
committed
Aug 26, 2024
1 parent
8192e78
commit cff3cf2
Showing
17 changed files
with
532 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package changelog | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jonashiltl/openchangelog/internal/config" | ||
"github.com/jonashiltl/openchangelog/internal/store" | ||
"github.com/naveensrinivasan/httpcache" | ||
) | ||
|
||
// Groups multiple ways of loading a changelog. Either from the config, by it's subdomain or workspace. | ||
// After loading the changelog it can easily be parsed. | ||
type Loader struct { | ||
cfg config.Config | ||
store store.Store | ||
cache httpcache.Cache | ||
parser Parser | ||
} | ||
|
||
func NewLoader(cfg config.Config, store store.Store, cache httpcache.Cache) *Loader { | ||
return &Loader{ | ||
cfg: cfg, | ||
store: store, | ||
cache: cache, | ||
parser: NewParser(), | ||
} | ||
} | ||
|
||
func (l *Loader) FromConfig(ctx context.Context, page Pagination) (*LoadedChangelog, error) { | ||
store := store.NewConfigStore(l.cfg) | ||
cl, err := store.GetChangelog(ctx, "", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := l.load(ctx, cl, page) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LoadedChangelog{ | ||
cl: cl, | ||
res: res, | ||
parser: l.parser, | ||
}, nil | ||
} | ||
|
||
func (l *Loader) FromSubdomain(ctx context.Context, subdomain string, page Pagination) (*LoadedChangelog, error) { | ||
cl, err := l.store.GetChangelogBySubdomain(ctx, subdomain) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := l.load(ctx, cl, page) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LoadedChangelog{ | ||
cl: cl, | ||
res: res, | ||
parser: l.parser, | ||
}, nil | ||
} | ||
|
||
func (l *Loader) FromWorkspace(ctx context.Context, wID, cID string, page Pagination) (*LoadedChangelog, error) { | ||
parsedWID, err := store.ParseWID(wID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parsedCID, err := store.ParseCID(cID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cl, err := l.store.GetChangelog(ctx, parsedWID, parsedCID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := l.load(ctx, cl, page) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LoadedChangelog{ | ||
cl: cl, | ||
res: res, | ||
parser: l.parser, | ||
}, nil | ||
} | ||
|
||
func (l *Loader) load(ctx context.Context, cl store.Changelog, page Pagination) (LoadResult, error) { | ||
var source Source | ||
if cl.LocalSource.Valid { | ||
source = newLocalSourceFromStore(cl.LocalSource.ValueOrZero()) | ||
} else if cl.GHSource.Valid { | ||
s, err := newGHSourceFromStore(l.cfg, cl.GHSource.ValueOrZero(), l.cache) | ||
if err != nil { | ||
return LoadResult{}, err | ||
} | ||
source = s | ||
} | ||
|
||
if source != nil { | ||
res, err := source.Load(ctx, page) | ||
if err != nil { | ||
return LoadResult{}, err | ||
} | ||
return res, nil | ||
} | ||
return LoadResult{}, nil | ||
} | ||
|
||
type LoadedChangelog struct { | ||
cl store.Changelog | ||
res LoadResult | ||
parser Parser | ||
} | ||
|
||
type ParsedChangelog struct { | ||
CL store.Changelog | ||
Articles []ParsedArticle | ||
HasMore bool | ||
} | ||
|
||
func (c *LoadedChangelog) Parse(ctx context.Context) (ParsedChangelog, error) { | ||
parsed, err := c.parser.Parse(ctx, c.res.Articles) | ||
if err != nil { | ||
return ParsedChangelog{}, err | ||
} | ||
|
||
return ParsedChangelog{ | ||
CL: c.cl, | ||
Articles: parsed, | ||
HasMore: c.res.HasMore, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.