Skip to content

Commit

Permalink
Fix item date management
Browse files Browse the repository at this point in the history
Use a fork of gofeed with stable published time
Filter before loop
  • Loading branch information
guilhem committed May 18, 2020
1 parent c378350 commit 1bbdc6f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ require (
github.com/thoas/go-funk v0.6.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)

replace github.com/mmcdole/gofeed => github.com/guilhem/gofeed v1.0.1-0.20200518231152-db85dfc4ee46
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ github.com/google/go-github/v31 v31.0.0/go.mod h1:NQPZol8/1sMoWYGN2yaALIBytu17gA
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/guilhem/gofeed v1.0.1-0.20200518231152-db85dfc4ee46 h1:efRwt6EKZG/vbdqiwxS9P17UHaCeKnMmjFHDdABkx0o=
github.com/guilhem/gofeed v1.0.1-0.20200518231152-db85dfc4ee46/go.mod h1:tkVcyzS3qVMlQrQxJoEH1hkTiuo9a8emDzkMi7TZBu0=
github.com/haya14busa/go-actions-toolkit v0.0.0-20200105081403-ca0307860f01 h1:HiJF8Mek+I7PY0Bm+SuhkwaAZSZP83sw6rrTMrgZ0io=
github.com/haya14busa/go-actions-toolkit v0.0.0-20200105081403-ca0307860f01/go.mod h1:1DWDZmeYf0LX30zscWb7K9rUMeirNeBMd5Dum+seUhc=
github.com/mmcdole/gofeed v1.0.0 h1:PHqwr8fsEm8xarj9s53XeEAFYhRM3E9Ib7Ie766/LTE=
github.com/mmcdole/gofeed v1.0.0/go.mod h1:tkVcyzS3qVMlQrQxJoEH1hkTiuo9a8emDzkMi7TZBu0=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
78 changes: 39 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,74 @@ import (
)

func main() {

ghaLogOption := &gha.LogOption{File: "main.go"}

// Parse repository in form owner/name
repo := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")

// Parse limit time option
var limitTime time.Time
if d, err := time.ParseDuration(gha.GetInput("lastTime")); err == nil {
// Make duration negative
if d > 0 {
d = -d
}
limitTime = time.Now().Add(d)
} else {
gha.Debug(fmt.Sprintf("Fail to parse last time %s", gha.GetInput("lastTime")), ghaLogOption)
}
gha.Debug(fmt.Sprintf("limitTime %s", limitTime), ghaLogOption)

// Parse Labels
labels := strings.Split(gha.GetInput("labels"), ",")
gha.Debug(fmt.Sprintf("labels %v", labels), ghaLogOption)

ctx := context.Background()

// Instanciate GitHub client
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gha.GetInput("repo-token")},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

// Instanciate feed parser
fp := gofeed.NewParser()

feed, err := fp.ParseURL(gha.GetInput("feed"))
feed, err := fp.ParseURLWithContext(gha.GetInput("feed"), ctx)
if err != nil {
gha.Error(fmt.Sprintf("Cannot parse feed '%s': '%s'", gha.GetInput("feed"), err), ghaLogOption)
os.Exit(1)
}
gha.Info(feed.Title)

IssueListByRepoOption := &github.IssueListByRepoOptions{}
repo := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
// Instanciate HTML to markdown
converter := md.NewConverter("", true, nil)

// Remove old items in feed
feed.Items = funk.Filter(feed.Items, func(x *gofeed.Item) bool {
return x.PublishedParsed.After(limitTime)
}).([]*gofeed.Item)

// Get all issues
IssueListByRepoOption := &github.IssueListByRepoOptions{
State: "all",
Labels: labels,
}

issues, _, err := client.Issues.ListByRepo(ctx, repo[0], repo[1], IssueListByRepoOption)
if err != nil {
gha.Error(fmt.Sprint(err), ghaLogOption)
os.Exit(1)
}
gha.Debug(fmt.Sprintf("Issues %v", issues), ghaLogOption)

converter := md.NewConverter("", true, nil)
gha.Debug(fmt.Sprintf("%d issues", len(issues)), ghaLogOption)

var createdIssues []string
var limitTime time.Time
if d, err := time.ParseDuration(gha.GetInput("lastTime")); err == nil {
// Make duration negative
if d > 0 {
d = -d
}
limitTime = time.Now().Add(d)
} else {
gha.Debug(fmt.Sprintf("Fail to parse last time %s", gha.GetInput("lastTime")), ghaLogOption)
}
gha.Debug(fmt.Sprintf("limitTime %s", limitTime), ghaLogOption)

labels := strings.Split(gha.GetInput("labels"), ",")
gha.Debug(fmt.Sprintf("labels %v", labels), ghaLogOption)

// Iterate
for _, item := range feed.Items {
title := strings.Join([]string{gha.GetInput("prefix"), item.Title}, " ")
gha.Debug(fmt.Sprintf("Issue '%s'", title), ghaLogOption)

var t *time.Time
if item.UpdatedParsed != nil {
t = item.UpdatedParsed
gha.Debug("Use 'updated' field", ghaLogOption)
} else if item.PublishedParsed != nil {
t = item.PublishedParsed
gha.Debug("Use 'published' field", ghaLogOption)
} else {
gha.Warning("No timed field", ghaLogOption)
}

if t != nil && t.Before(limitTime) {
gha.Debug(fmt.Sprintf("Item date '%s' is before limit", t), ghaLogOption)
continue
}

if issue := funk.Find(issues, func(x *github.Issue) bool {
return *x.Title == title
}); issue != nil {
Expand Down

0 comments on commit 1bbdc6f

Please sign in to comment.