/
activity.go
80 lines (64 loc) · 2.07 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package githubissues implements activities to get GitHub issues
package githubissues
// Imports
import (
ctx "context"
"time"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// Constants
const (
ivGithubAccessToken = "token"
ivTimeInterval = "timeInterval"
ovResult = "result"
)
// log is the default package logger
var log = logger.GetLogger("activity-githubissues")
// MyActivity is a stub for your Activity implementation
type MyActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &MyActivity{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *MyActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements activity.Activity.Eval
func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {
// Get the data
githubToken := context.GetInput(ivGithubAccessToken).(string)
timeInterval := context.GetInput(ivTimeInterval).(int)
// Create a new GitHub client
ctxt := ctx.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctxt, ts)
client := github.NewClient(tc)
// Create a new time to make sure we check for new issues from the previous
// execution of this function.
interval := time.Duration(timeInterval) * time.Minute
t := time.Now().Add(-interval)
log.Infof("Check GitHub issues for the current user since [%s]", t)
// Get all the issues assigned to the current authenticated user
issueOpts := github.IssueListOptions{Since: t}
issues, _, err := client.Issues.List(ctxt, false, &issueOpts)
if err != nil {
log.Error(err.Error())
return true, err
}
log.Infof("GitHub returned %v issues", len(issues))
datamap := make([]interface{}, len(issues))
for idx, issue := range issues {
datamap[idx] = issue
}
// Set the output value in the context
context.SetOutput(ovResult, datamap)
return true, nil
}