Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 1e86263

Browse files
committed
updated readme, added interface to plugin, updated example
1 parent 6e08f0c commit 1e86263

File tree

7 files changed

+119
-127
lines changed

7 files changed

+119
-127
lines changed

README.md

+49-46
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ Help Options:
1414
[run command options]
1515
-c, --config_location= location of the configuration file
1616
[$CONFIG_LOCATION]
17+
--update_timeout= amount of time per processing single update
18+
[$UPDATE_TIMEOUT]
1719
18-
store:
19-
--store.type=[bolt] type of storage [$STORE_TYPE]
20+
store:
21+
--store.type=[bolt] type of storage [$STORE_TYPE]
2022
21-
bolt:
22-
--store.bolt.path= parent dir for bolt files (default: ./var)
23-
[$STORE_BOLT_PATH]
24-
--store.bolt.timeout= bolt timeout (default: 30s)
25-
[$STORE_BOLT_TIMEOUT]
23+
bolt:
24+
--store.bolt.path= parent dir for bolt files (default: ./var)
25+
[$STORE_BOLT_PATH]
26+
--store.bolt.timeout= bolt timeout (default: 30s)
27+
[$STORE_BOLT_TIMEOUT]
2628
27-
webhook:
28-
--webhook.base_url= base url for webhooks [$WEBHOOK_BASE_URL]
29-
--webhook.addr= local address to listen [$WEBHOOK_ADDR]
29+
webhook:
30+
--webhook.base_url= base url for webhooks [$WEBHOOK_BASE_URL]
31+
--webhook.addr= local address to listen [$WEBHOOK_ADDR]
3032
```
3133

3234
### DSL
@@ -40,24 +42,33 @@ trackers:
4042
with:
4143
owner: cappuccinotm
4244
name: dastracker
43-
user: "{{ env \"GITHUB_USER\" }}"
44-
access_token: "{{ env \"GITHUB_ACCESS_TOKEN\" }}"
45+
user: '{{ env "GITHUB_USER" }}'
46+
access_token: '{{ env "GITHUB_ACCESS_TOKEN" }}'
4547

4648
- name: customrpc
4749
driver: rpc
4850
with:
49-
address: "{{ env \"CUSTOM_RPC_ADDRESS\" }}"
51+
address: '{{ env "CUSTOM_RPC_ADDRESS" }}'
52+
53+
triggers:
54+
- name: gh_task_updated
55+
in: gh_dastracker
56+
with:
57+
events: "issues"
5058

5159
jobs:
5260
- name: print task update if task is received
53-
on:
54-
tracker: gh_dastracker
55-
with:
56-
events: "issue"
61+
on: gh_task_updated
5762
do:
58-
- action: customrpc/Print
59-
with:
60-
message: "Task \"{{.Update.Title}}\" has been updated and printed to the terminal."
63+
- if: 'string_contains(Update.Title, "[PTT]")' # means "Print To Terminal"
64+
do:
65+
- action: customrpc/Print
66+
detached: true
67+
with:
68+
message: |
69+
Task "{{.Update.Title}}" has been updated and printed to the terminal.
70+
Body:
71+
{{.Update.Body}}
6172
```
6273
6374
This flow checks whether any issue in github is updated and sends an RPC call to Print method with the
@@ -69,35 +80,37 @@ The configuration file uses the [go template language](https://pkg.go.dev/text/t
6980

7081
Helper methods:
7182

72-
| Method name | Description |
73-
|-----------------------------------|--------------------------------------------------------------|
74-
| env(varname string) | returns the value of the environment variable |
75-
| values(m map[string]string) | returns a list of values of the map |
76-
| seq(l []string) | serializes the list in form of "string1,string2,string3,..." |
83+
| Method name | Description |
84+
|------------------------------------------|--------------------------------------------------------------|
85+
| env(varname string) | returns the value of the environment variable |
86+
| values(m map[string]string) | returns a list of values of the map |
87+
| seq(l []string) | serializes the list in form of "string1,string2,string3,..." |
88+
| string_contains(s string, substr string) | returns true if string contains substring |
7789

7890
### TODO
7991
- [X] RPC plugins support
80-
- [ ] Github support
81-
- [ ] Predicates for triggers
82-
- [ ] Asana support
83-
- [ ] Jira support
84-
- [ ] Increase test coverage
85-
- [ ] Update history
92+
- [ ] Github support (partially)
93+
- [X] Predicates for triggers
8694
- [ ] Special mappings
8795
- [ ] Detached actions
8896

8997
### Terminology
90-
We use a word "ticket" for a task in the context of "dastracker" and the word "task"
91-
in the context of the external task trackers.
98+
- Ticket - an issue in the context of "dastracker"
99+
- Task - an issue in the context of the end task tracker.
100+
- Subscription - a webhook or polling, for retrieving updates from the end task tracker.
92101

93102
### Plugin development
94103
The functionality of dastracker might be extended by using plugins. Each plugin is an independent process/container,
95104
implementing [Go RPC server](https://pkg.go.dev/net/rpc). Each exported method of the plugin handler must have a signature of `func(req lib.Request, res *lib.Response)`,
96105
these methods might be referred and called in the configuration.
97106

98-
Also, the `lib.Plugin` structure has a field, named `SetUpTrigger` with signature of `func(req SetUpTriggerReq, resp *SetUpTriggerResp) error`.
99-
This field might be filled in order to allow hanging on some triggers in the configuration file.
100-
If no method is provided, dastracker will do nothing on any trigger, hanged on the plugin.
107+
Plugin may provide methods to subscribe and unsubscribe from events, for that, plugin should implement interface:
108+
```go
109+
type SubscriptionSupporter interface {
110+
Subscribe(req SubscribeReq, resp *SubscribeResp) error
111+
Unsubscribe(req UnsubscribeReq, _ *struct{}) error
112+
}
113+
```
101114

102115
See [example](_example/plugin/main.go) for details.
103116

@@ -128,13 +141,3 @@ Some details about JSONRPC:
128141
"id": 0
129142
}
130143
```
131-
**(TODO)**
132-
133-
Demo:
134-
![](docs/demo.gif)
135-
136-
### Requirements
137-
Read about requirements in [here](docs/REQUIREMENTS.md)
138-
139-
### Design
140-
Read about design in [here](docs/DESIGN.md)

_example/plugin/main.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ import (
99

1010
type Handler struct{}
1111

12-
func (h *Handler) Print(req lib.Request, _ *lib.Response) error {
12+
func (Handler) Subscribe(req lib.SubscribeReq, _ *lib.SubscribeResp) error {
13+
log.Printf("Subscribe called: %+v", req)
14+
return nil
15+
}
16+
17+
func (Handler) Unsubscribe(req lib.UnsubscribeReq, _ *struct{}) error {
18+
log.Printf("Unsubscribe called: %+v", req)
19+
return nil
20+
}
21+
22+
func (Handler) Print(req lib.Request, _ *lib.Response) error {
1323
msg := req.Vars.Get("message")
1424
log.Printf("Received Print call with msg: %s", msg)
1525
return nil
@@ -18,16 +28,9 @@ func (h *Handler) Print(req lib.Request, _ *lib.Response) error {
1828
func main() {
1929
pl := lib.Plugin{
2030
Address: ":9000",
21-
SubscribeHandler: func(req lib.SubscribeReq) error {
22-
log.Printf("[INFO] requested subscription with webhook on %s", req.WebhookURL())
23-
return nil
24-
},
25-
UnsubscribeHandler: func(req lib.UnsubscribeReq) error {
26-
log.Printf("[INFO] requested unsubscription")
27-
return nil
28-
},
31+
Logger: log.Default(),
2932
}
30-
if err := pl.Listen(context.Background(), &Handler{}); err != nil {
33+
if err := pl.Listen(context.Background(), Handler{}); err != nil {
3134
log.Printf("[WARN] listener stopped, reason: %v", err)
3235
}
3336
}

_example/plugin/vendor/github.com/cappuccinotm/dastracker/lib/plugin.go

+16-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_example/plugin/vendor/github.com/cappuccinotm/dastracker/lib/rpc.go

+8-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_example/sync_tasks.yaml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# provided functions:
2-
# env "NAME" - returns the value of environment variable NAME
3-
# values <map> - returns a list of values of the map
4-
# seq <list[string]> - serializes the list in form of "string1,string2,string3,..."
5-
61
trackers:
72
- name: gh_dastracker
83
driver: github
@@ -22,6 +17,10 @@ triggers:
2217
in: gh_dastracker
2318
with:
2419
events: "issues"
20+
- name: test_customrpc_subscribe
21+
in: customrpc
22+
with:
23+
events: "subscribe"
2524

2625
jobs:
2726
- name: print task update if task is received

app/store/service/actor.go

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ func (s *Actor) registerTriggers(ctx context.Context) error {
194194
return fmt.Errorf("set tracker reference %q in subscription %q: %w", resp.TrackerRef, sub.ID, err)
195195
}
196196

197+
s.Log.Printf("[DEBUG] registered subscription %q in %q, received tracker reference: %q",
198+
trigger.Name, trigger.Tracker, resp.TrackerRef)
199+
197200
return nil
198201
})
199202
}
@@ -230,6 +233,9 @@ func (s *Actor) unregisterTriggers(ctx context.Context) {
230233
if err = s.SubscriptionsManager.Delete(ctx, sub.ID); err != nil {
231234
s.Log.Printf("[WARN] failed to delete subscription %q: %v", sub.ID, err)
232235
}
236+
237+
s.Log.Printf("[DEBUG] unregistered subscription with id %q, trigger %q in %q, with tracker reference: %q",
238+
sub.ID, sub.TriggerName, sub.TrackerName, sub.TrackerRef)
233239
}()
234240
}
235241

0 commit comments

Comments
 (0)