Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add backend interface for closing 30-day expired issues.
  • Loading branch information
shuiwei committed Jul 10, 2018
1 parent 3bd9148 commit 8c86f3e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gh/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gh
import (
"context"
"strings"
"time"

"github.com/google/go-github/github"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -119,3 +120,27 @@ func (c *Client) IssueHasComment(num int, element string) (int, bool) {
}
return -1, false
}

// CloseExpiredIssues returns true if expired thus closed
func (c *Client) CloseExpiredIssue(num int) bool {
c.Mutex.Lock()
defer c.Mutex.Unlock()

ret := 0
comments, err := c.ListComments(num)
if err != nil {
return -1, false
}

now := time.Now()

d, _ := time.ParseDuration("-24h")
d30 := now.add(30 * d)

for _, comment := range comments {
if comment.UpdatedAt != nil && subres = d30.Sub(comment.UpdatedAt) && subres.Duration > 0 {
c.Client.Issues.CloseIssue(num) /// FIXME: CloseComment not implemented
}
}
return ret
}
35 changes: 35 additions & 0 deletions processor/issueProcessor/expired.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package issueProcessor

import (
"fmt"
"time"

"github.com/google/go-github/github"
"github.com/pouchcontainer/pouchrobot/utils"
)

func (ip *IssueProcessor) ActToIssueExpired(issue *github.Issue) error {
ip.ActToCloseExpire(issue)
return nil
}

func (ip *IssueProcessor) ActToCloseExpire(issue *github.Issue) error {
now = time.Now()
d, _ := time.ParseDuration("-24h")
d30 := now.add(30 * d)

if _, exist := ip.Client.IssueHasComment(*(issue.Number), utils.IssueNeedP1CommentSubStr); exist {
if res := d30.Sub(issue.UpdatedAt); res.Duation < 0 {
return nil
}
} else if _, exist := ip.Client.IssueHasComment(*(issue.Number), utils.IssueNeedP1CommentSubStr); !exist {
return nil
}

body := fmt.Sprintf(utils.IssueNeedP1Comment, *(issue.User.Login))
newComment := &github.IssueComment{
Body: &body,
}

return ip.Client.CloseExpireIssues(*(issue.Number))
}
4 changes: 4 additions & 0 deletions processor/issueProcessor/issue_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (ip *IssueProcessor) Process(data []byte) error {
if err := ip.ActToIssueLabeled(&issue); err != nil {
return nil
}
case "expired":
if err := ip.ActToIssueExpired(&issue); err != nil {
return nil
}
case "reopened":
default:
return fmt.Errorf("unknown action type %s in issue: ", actionType)
Expand Down

0 comments on commit 8c86f3e

Please sign in to comment.