-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.php
147 lines (137 loc) · 5.08 KB
/
index.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
load([
'pju\\KirbyWebhookField\\WebhookField' => 'classes/WebhookField.php'
], __DIR__);
use \pju\KirbyWebhookField\WebhookField;
Kirby::plugin('pju/webhook-field', [
'options' => [
'endpoint' => 'webhook',
'hooks' => [],
'labels' => [
'new' => [
'name' => 'New',
'cta' => 'Deploy now',
'text' => 'The site has not been deployed yet.',
],
'progress' => [
'name' => 'Site is being deployed',
'cta' => 'Trigger new deploy',
'text' => 'The site is being deployed.',
],
'success' => [
'name' => 'Site is live',
'cta' => 'Deploy again',
'text' => 'The site is live and up to date.',
],
'error' => [
'name' => 'Error',
'cta' => 'Trigger new deploy',
'text' => 'There was an error while trying to deploy.',
],
'outdated' => [
'name' => 'Undeployed changes',
'cta' => 'Deploy now',
'text' => 'There where changes after the last deployment.',
],
'hooksEmpty' => [
'name' => 'Hooks are not defined',
'cta' => 'Deploy not available',
'text' => 'You need to set the hooks in your Kirby configuration.',
],
'hookNotfound' => [
'name' => 'Service not found',
'cta' => 'Deploy not available',
'text' => 'The hook "%hookName%" was not found.',
],
'hookNoUrl' => [
'name' => 'No URL set',
'cta' => 'Deploy not available',
'text' => 'The url for the hook "%hookName%" is not defined.',
],
],
'cache' => true
],
'fields' => [
'webhook' => [
'props' => [
'label' => function (string $title = 'Deploy Status') {
return $title;
},
'name' => function (string $name = 'webhook') {
return $name;
},
'hook' => function (string $name = '') {
return WebhookField::getHook($name);
},
'debug' => function (bool $debugEnabled = true) {
return $debugEnabled;
},
'monochrome' => function (bool $isMonochrome = false) {
return $isMonochrome;
}
],
'computed' => [
'endpoint' => function () {
return kirby()->option('pju.webhook-field.endpoint');
},
'statusInitial' => function () {
$state = WebhookField::getState($this->hook['name']);
return $state['status'];
},
'hookUpdated' => function () {
$state = WebhookField::getState($this->hook['name']);
return isset($state['updated']) ? $state['updated'] : 0;
},
'siteModified' => function () {
return kirby()->site()->modified();
},
'labels' => function () {
$labels = kirby()->option('pju.webhook-field.labels');
return array_map(function ($label) {
return array_map(function ($text) {
$name = $this->name;
return str_replace('%hookName%', $name, $text);
}, $label);
}, $labels);
}
]
]
],
'routes' => function ($kirby) {
$endpoint = $kirby->option('pju.webhook-field.endpoint');
if (!$endpoint) {
throw new InvalidArgumentException('Webhook plugin endpoint is not defined');
}
return [
[
'pattern' => $endpoint . '/(:any)/status',
'action' => function ($hook) {
return WebhookField::getState($hook);
},
'method' => 'GET'
],
[
'pattern' => $endpoint . '/(:any)/(:any)',
'action' => function ($hook, $status) {
$message = WebhookField::setState($hook, $status);
try {
WebhookField::runCallback($hook, $status);
} catch (Throwable $e) {
$message = 'error running callback for ' . $hook . ': ' . $e->getMessage();
}
return $message;
},
'method' => 'POST'
],
[
'pattern' => $endpoint . '/site-modified',
'action' => function () {
return [
'modified' => kirby()->site()->modified()
];
},
'method' => 'GET'
]
];
}
]);