Collection of themes/skins for the Fossil SCM

⌈⌋ ⎇ branch:  Fossil Skins Extra


Check-in [0b03fda8d7]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add +hea:der, multi-url, FOSSIL_REPOSITORY= env support. Change more github-URLs to fossil/json api.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0b03fda8d72c479af081c50e244aca25a255763b
User & Date: mario 2021-10-24 22:14:04
Context
2021-10-25
18:54
trim wiki page name, remove more github remnants check-in: 01dcd6c9b8 user: mario tags: trunk
2021-10-24
22:14
Add +hea:der, multi-url, FOSSIL_REPOSITORY= env support. Change more github-URLs to fossil/json api. check-in: 0b03fda8d7 user: mario tags: trunk
06:20
standardize PMD type: further check-in: 7573908dd3 user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to extroot/hooks.

106
107
108
109
110
111
112
113


114
115
116
117
118
119
120
121
122
123
124
125
126
<h3>Web hooks</h3>
<p>
Add one of the standard webhook services:
</p>
<p>
<select id=web_set onchange="document.getElementById('hook_cmd').innerText=this.value">
<option value=''>
<option value="fossil-webhook -R '%R' --fossil http://service.rest/">fossil-webhook


<option value='curl -X POST -d "branches=trunk" -d "token=1234" https://readthedocs.org/api/v2/webhook/example-project/1/'>ReadTheDocs
<option value='wget https://127.0.0.1/jenkins/github-webook'>Jenkins
<option value='wget https://localhost:8080/ext/fx_meta'>fossil/ext/fx_meta
</select>
</p>

<h3>Docs</h3>
<ul>
<li> See <a href="https://fossil-scm.org/home/doc/trunk/www/hooks.md">hooks.md</a> on how commands are run.
<li> Emulate https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads …
<li> Probably needs a cmdline helper <code>fossil-webhook</code>
</ul>
</div>







|
>
>













106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<h3>Web hooks</h3>
<p>
Add one of the standard webhook services:
</p>
<p>
<select id=web_set onchange="document.getElementById('hook_cmd').innerText=this.value">
<option value=''>
<option value="FOSSIL_REPOSITORY='%R' fossil-webhook https://service.rest/">fossil-webhook
<option value="FOSSIL_REPOSITORY='%R' fossil-webhook --git https://service1.rest/">fossil-webhook --git
<option value="FOSSIL_REPOSITORY='%R' fossil-webhook --if:check-in https://service.rest/ https://service2.rest/ '+extra=parameter2' '+Token:Bearer 123'">fossil-webhook --if:check-in +Header
<option value='curl -X POST -d "branches=trunk" -d "token=1234" https://readthedocs.org/api/v2/webhook/example-project/1/'>ReadTheDocs
<option value='wget https://127.0.0.1/jenkins/github-webook'>Jenkins
<option value='wget https://localhost:8080/ext/fx_meta'>fossil/ext/fx_meta
</select>
</p>

<h3>Docs</h3>
<ul>
<li> See <a href="https://fossil-scm.org/home/doc/trunk/www/hooks.md">hooks.md</a> on how commands are run.
<li> Emulate https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads …
<li> Probably needs a cmdline helper <code>fossil-webhook</code>
</ul>
</div>

Changes to tools/fossil-webhook.

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172








173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213




214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/php -qC
<?php
# encoding: utf-8
# api: cli
# title: webhook
# description: to be run from fossil backoffice/after-receive hooks
# version: 0.1
# doc: https://fossil-scm.org/home/doc/trunk/www/hooks.md
#
# Meant to transform commits into common webhook JSON formats,
# and then send them off.

#
# Hook syntax:
#   fossil-webhook '%R' 'http://service.rest/api?token=123'

# Parameters:
#   %R        becomes repository filename (REQUIRED)
#   --fsl     fossil-style payload (default)
#   --git     crafts a github-style payload
#   --ping    basic ping
#   --if:wiki test artifact list for type (check-in, wiki, file)
#   httpt://  webhook service endpoint (REQUIRED)
#   +tok=123  inject additional parameters

#   





#-- init
$argv = $_SERVER["argv"];
$cfg = [
    "start" => microtime(TRUE),
    "dbg" => preg_grep("~^-+(de?bu?g|du?mp)$~i", $argv),
    "git" => preg_grep("~^-+gi?t?$~i", $argv),
    "ping" => preg_grep("~^-+pi?n?g?$~i", $argv),
    "help" => preg_grep("~^-+he?l?p?$~i", $argv),

    "repo" => current(preg_grep("~^/\w+.+\.(fsl|fossil|sqlite)$~", $argv)),

    "add" => preg_grep("~^\+[\w.-]+[:=].+$~", $argv),
    "if" => preg_grep("~^-+if:[\w-]+$~i", $argv),
    "url" => current(preg_grep("~^https?://.+$~i", $argv)),
    "artifacts" => fread(STDIN, 1<<20),
        #e9ce272e806c47a10e3d4cf570549a1d33133133 file src/code.c
        #1db32c3ff6c5969191d575c65916f6322b833313 wiki HomePage
        #313908390381390813913813931931830938aaaa check-in to trunk by user on 2222-11-30 00:00
    "json" => TRUE,
    "user" => null,
];

if (empty($cfg["repo"]) || empty($cfg["url"])) {
   die("%R or url missing");
}
if ($cfg["if"] and preg_match("/if:(.+)/", $cfg["if"][0], $uu) and !preg_match("/\\b$uu[1]\\b/", $cfg["artificts"])) {
   die("--if: no match");

}

$cfg += [
    "basename" => preg_replace("~\.\w+$~", "", basename($cfg["repo"])),
    "baseurl" => get_baseurl(),
    "title" => get_config("project-title"),
];


#-- operation flag
if ($cfg["help"]) {
   die("Usage as hook:\n  fossil-webhook %R --git http://service.rest/TOKEN/ETC\n");
}
elseif ($cfg["git"]) {
    $request = git_request();
}
elseif ($cfg["ping"]) {
    $request = basic_ping();
}
else {
    $request = fossil_request();
}

#-- and send
add_params($request);
if ($cfg["dbg"]) { die(json_encode($request, JSON_PRETTY_PRINT)); }



send($request);





/**
 * Process +add=paramval arguments,
 * simply adding them to $request[] top-level.
 *
 */
function add_params(&$request) {
    global $cfg;
    #-- +add=params?
    foreach ($cfg["add"] as $add) {
        preg_match("~^\+([\w-.]+)[:=](.+)$~", $add, $kv);
        $request[$kv[1]] = $kv[2];
    }
}


















/**
 * Sending
 *
 */
function send($request) {
    global $cfg;




    #-- send query

    $c = curl_init($cfg["url"]);
    curl_setopt_array($c, [

        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $cfg["json"] ? json_encode($request, JSON_PRETTY_PRINT) : $request,

        CURLOPT_VERBOSE => 1,
    ]);
    $c->exec();

}



/**
 * Define a fossil-style webhook payload.

 *
 */
function fossil_request() {
    global $cfg;
    $url = get_baseurl();
    return [
        '$type' => "webhook",
        '$class' => "vcs fossil",
        '$ver' => "0.1",
        "action" => main_action($cfg["artifacts"]),
	"fossil" => get_config("server-code"),
	"url" => $url,
        "project" => [
            "name" => get_basename(),
            "title" => get_config("project-name"),
            "description" => get_config("project-description"),
            "size" => filesize($cfg["repo"]),
            "id" => get_config("project-code"),



            "url_stat" => "$url/json/stat",
            "url_timeline" => "$url/json/timeline/checkin",
            "url_dir" => "$url/json/dir",
            "url_trees" => "$url/ext/trees",







        ],
        "artifacts" => array_map(
            function($row) use($url) { return expand_artifact($row, $url); },


            get_artifacts($cfg["artifacts"])
        ),
        "user" => $cfg["user"],
	"timestamp" => time(),
        "procTimeMs" =>  1000 * (microtime(TRUE) - $cfg["start"]),
    ];
}


/**
 * More basic payload
 *
 */
function basic_ping() {
    global $cfg;
    $url = get_baseurl();
    return [
        '$type' => "webhook",
        '$class' => "ping basic",
        "action" => "edited",
	"url" => $url,
	"name" => get_basename(),
        "stdin" => $cfg["artifacts"],
	"timestamp" => time(),
    ];
}

 
/**
 * Simulate GitHub-style webhook.
 * (Unlikely that we can fill in all the minutae.)
 *
 */
function git_request() {
    global $cfg;








    return [
      'action' => 'edited',
      'rule' => [
        'id' => crc32($cfg["artifacts"]),
        'repository_id' => $rep_id = hexdec(substr(get_config("project-code", "12FFFF"), 0, 6)),
        'name' => $basename = get_basename(),
        'created_at' => iso8601(mtime_event("MIN")),
        'updated_at' => iso8601(mtime_event("MAX")),
        'pull_request_reviews_enforcement_level' => 'off',
        'required_approving_review_count' => 0,
        'dismiss_stale_reviews_on_push' => false,
        'require_code_owner_review' => false,
        'authorized_dismissal_actors_only' => false,
        'ignore_approvals_from_contributors' => false,
        'required_status_checks' => [
          0 => 'basic-CI',
        ],
        'required_status_checks_enforcement_level' => 'non_admins',
        'strict_required_status_checks_policy' => false,
        'signature_requirement_enforcement_level' => 'off',
        'linear_history_requirement_enforcement_level' => 'off',
        'admin_enforced' => false,
        'allow_force_pushes_enforcement_level' => 'off',
        'allow_deletions_enforcement_level' => 'off',
        'merge_queue_enforcement_level' => 'off',
        'required_deployments_enforcement_level' => 'off',
        'required_conversation_resolution_level' => 'off',
        'authorized_actors_only' => true,
        'authorized_actor_names' => [
          0 => $user = get_main_user(),
        ],
      ],
      'changes' => [
        'authorized_actors_only' => [
          'from' => false,
        ],
        'authorized_actor_names' => [
          'from' => [
          ],
        ],
      ],




      'repository' => [
        'id' => $rep_id,
        'node_id' => 'MDEwOlJlcG9zaXRvcnkxNzI3MzA1MQ==',
        'name' => 'octo-repo',
        'full_name' => 'octo-org/octo-repo',
        'private' => true,
        'owner' => [
          'login' => 'octo-org',
          'id' => 6811672,
          'node_id' => 'MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=',
          'avatar_url' => 'https://avatars.githubusercontent.com/u/6811672?v=4',
          'gravatar_id' => '',
          'url' => 'https://api.github.com/users/octo-org',
          'html_url' => 'https://github.com/octo-org',
          'followers_url' => 'https://api.github.com/users/octo-org/followers',
          'following_url' => 'https://api.github.com/users/octo-org/following{/other_user}',
          'gists_url' => 'https://api.github.com/users/octo-org/gists{/gist_id}',
          'starred_url' => 'https://api.github.com/users/octo-org/starred{/owner}{/repo}',
          'subscriptions_url' => 'https://api.github.com/users/octo-org/subscriptions',
          'organizations_url' => 'https://api.github.com/users/octo-org/orgs',
          'repos_url' => 'https://api.github.com/users/octo-org/repos',
          'events_url' => 'https://api.github.com/users/octo-org/events{/privacy}',
          'received_events_url' => 'https://api.github.com/users/octo-org/received_events',
          'type' => 'Organization',
          'site_admin' => false,
        ],
        'html_url' => 'https://github.com/octo-org/octo-repo',
        'description' => 'My first repo on GitHub!',
        'fork' => false,
        'url' => 'https://api.github.com/repos/octo-org/octo-repo',
        'forks_url' => 'https://api.github.com/repos/octo-org/octo-repo/forks',
        'keys_url' => 'https://api.github.com/repos/octo-org/octo-repo/keys{/key_id}',
        'collaborators_url' => 'https://api.github.com/repos/octo-org/octo-repo/collaborators{/collaborator}',
        'teams_url' => 'https://api.github.com/repos/octo-org/octo-repo/teams',
        'hooks_url' => 'https://api.github.com/repos/octo-org/octo-repo/hooks',
        'issue_events_url' => 'https://api.github.com/repos/octo-org/octo-repo/issues/events{/number}',
        'events_url' => 'https://api.github.com/repos/octo-org/octo-repo/events',
        'assignees_url' => 'https://api.github.com/repos/octo-org/octo-repo/assignees{/user}',
        'branches_url' => 'https://api.github.com/repos/octo-org/octo-repo/branches{/branch}',
        'tags_url' => 'https://api.github.com/repos/octo-org/octo-repo/tags',
        'blobs_url' => 'https://api.github.com/repos/octo-org/octo-repo/git/blobs{/sha}',


        'git_tags_url' => 'https://api.github.com/repos/octo-org/octo-repo/git/tags{/sha}',
        'git_refs_url' => 'https://api.github.com/repos/octo-org/octo-repo/git/refs{/sha}',
        'trees_url' => 'https://api.github.com/repos/octo-org/octo-repo/git/trees{/sha}',
        'statuses_url' => 'https://api.github.com/repos/octo-org/octo-repo/statuses/{sha}',
        'languages_url' => 'https://api.github.com/repos/octo-org/octo-repo/languages',
        'stargazers_url' => 'https://api.github.com/repos/octo-org/octo-repo/stargazers',
        'contributors_url' => 'https://api.github.com/repos/octo-org/octo-repo/contributors',
        'subscribers_url' => 'https://api.github.com/repos/octo-org/octo-repo/subscribers',
        'subscription_url' => 'https://api.github.com/repos/octo-org/octo-repo/subscription',
        'commits_url' => 'https://api.github.com/repos/octo-org/octo-repo/commits{/sha}',
        'git_commits_url' => 'https://api.github.com/repos/octo-org/octo-repo/git/commits{/sha}',
        'comments_url' => 'https://api.github.com/repos/octo-org/octo-repo/comments{/number}',
        'issue_comment_url' => 'https://api.github.com/repos/octo-org/octo-repo/issues/comments{/number}',
        'contents_url' => 'https://api.github.com/repos/octo-org/octo-repo/contents/{+path}',
        'compare_url' => 'https://api.github.com/repos/octo-org/octo-repo/compare/{base}...{head}',
        'merges_url' => 'https://api.github.com/repos/octo-org/octo-repo/merges',
        'archive_url' => 'https://api.github.com/repos/octo-org/octo-repo/{archive_format}{/ref}',
        'downloads_url' => 'https://api.github.com/repos/octo-org/octo-repo/downloads',
        'issues_url' => 'https://api.github.com/repos/octo-org/octo-repo/issues{/number}',
        'pulls_url' => 'https://api.github.com/repos/octo-org/octo-repo/pulls{/number}',
        'milestones_url' => 'https://api.github.com/repos/octo-org/octo-repo/milestones{/number}',
        'notifications_url' => 'https://api.github.com/repos/octo-org/octo-repo/notifications{?since,all,participating}',
        'labels_url' => 'https://api.github.com/repos/octo-org/octo-repo/labels{/name}',
        'releases_url' => 'https://api.github.com/repos/octo-org/octo-repo/releases{/id}',
        'deployments_url' => 'https://api.github.com/repos/octo-org/octo-repo/deployments',
        'created_at' => '2014-02-28T02:42:51Z',
        'updated_at' => '2021-03-11T14:54:13Z',
        'pushed_at' => '2021-03-11T14:54:10Z',
        'git_url' => 'git://github.com/octo-org/octo-repo.git',
        'ssh_url' => 'org-6811672@github.com:octo-org/octo-repo.git',
        'clone_url' => 'https://github.com/octo-org/octo-repo.git',
        'svn_url' => 'https://github.com/octo-org/octo-repo',
        'homepage' => '',
        'size' => 300,
        'stargazers_count' => 0,
        'watchers_count' => 0,
        'language' => 'C',
        'has_issues' => true,
        'has_projects' => false,
        'has_downloads' => true,
        'has_wiki' => false,
        'has_pages' => true,
        'forks_count' => 0,
        'mirror_url' => NULL,
        'archived' => false,
        'disabled' => false,
        'open_issues_count' => 39,
        'license' => NULL,
        'forks' => 0,
        'open_issues' => 39,
        'watchers' => 0,
        'default_branch' => 'trunk',
      ],
      'organization' => [
        'login' => 'octo-org',
        'id' => 6811672,
        'node_id' => 'MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=',
        'url' => 'https://api.github.com/orgs/octo-org',
        'repos_url' => 'https://api.github.com/orgs/octo-org/repos',
        'events_url' => 'https://api.github.com/orgs/octo-org/events',
        'hooks_url' => 'https://api.github.com/orgs/octo-org/hooks',
        'issues_url' => 'https://api.github.com/orgs/octo-org/issues',
        'members_url' => 'https://api.github.com/orgs/octo-org/members{/member}',
        'public_members_url' => 'https://api.github.com/orgs/octo-org/public_members{/member}',
        'avatar_url' => 'https://avatars.githubusercontent.com/u/6811672?v=4',
        'description' => 'Working better together!',
      ],
      'sender' => [
        'login' => $user,
        'id' => crc32($user),
        'node_id' => base64_encode($user),
        'avatar_url' => 'https://avatars1.githubusercontent.com/u/21031067?v=4',
        'gravatar_id' => '',
        'url' => 'https://api.github.com/users/Codertocat',
        'html_url' => 'https://github.com/Codertocat',
        'followers_url' => 'https://api.github.com/users/Codertocat/followers',
        'following_url' => 'https://api.github.com/users/Codertocat/following{/other_user}',
        'gists_url' => 'https://api.github.com/users/Codertocat/gists{/gist_id}',
        'starred_url' => 'https://api.github.com/users/Codertocat/starred{/owner}{/repo}',
        'subscriptions_url' => 'https://api.github.com/users/Codertocat/subscriptions',
        'organizations_url' => 'https://api.github.com/users/Codertocat/orgs',
        'repos_url' => 'https://api.github.com/users/Codertocat/repos',
        'events_url' => 'https://api.github.com/users/Codertocat/events{/privacy}',
        'received_events_url' => 'https://api.github.com/users/Codertocat/received_events',
        'type' => 'User',
        'site_admin' => false,
      ],
    ];
    # do...
}


/**










|
>


|
>






|
|
>
|
>
>
>
>









>
|
>
|

|
|



|


>
|


|
<
>

>

|



>

















|
>
>
>
|
>













|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|

>
>
>
>

>
|
|
>
|
|
>
|
|
|
>





|
>









|



|




>
>
>
|
|
|
|
>
>
>
>
>
>
>


|
>
>
|




















|
|












>
>
>
>
>
>
>
>

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|


|
|
|
<
<
<
<
<
|
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268





269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/php -qC
<?php
# encoding: utf-8
# api: cli
# title: webhook
# description: to be run from fossil backoffice/after-receive hooks
# version: 0.1
# doc: https://fossil-scm.org/home/doc/trunk/www/hooks.md
#
# Meant to transform commits into common webhook JSON formats,
# and send them off. Does a litte preprocessing and expansion
# of the commit list, adds raw/ urls to ease payload processing.
#
# Hook syntax:
#   fossil-webhook '%R' --ping 'http://service.rest/api?token=123'
#
# Parameters:
#   %R        becomes repository filename (REQUIRED)
#   --fsl     fossil-style payload (default)
#   --git     crafts a github-style payload
#   --ping    basic ping
#   --if:wiki test artifact list for type (check-in, wiki, file)
#   https://  webhook service endpoints (REQUIRED; can be multiple)
#   +tok=123  inject additional parameters into payload
#   +Tok:Bear add HTTP request headers
#
# The %R parameter can also be fed in using the env variable:
#   FOSSIL_REPOSITORY='%R' fossil-webhook --fsl 'https://target.rest/'
#


#-- init
$argv = $_SERVER["argv"];
$cfg = [
    "start" => microtime(TRUE),
    "dbg" => preg_grep("~^-+(de?bu?g|du?mp)$~i", $argv),
    "git" => preg_grep("~^-+gi?t?$~i", $argv),
    "ping" => preg_grep("~^-+pi?n?g?$~i", $argv),
    "help" => preg_grep("~^-+he?l?p?$~i", $argv),
    "repo" => empty($_SERVER["FOSSIL_REPOSITORY"])
              ? current(preg_grep("~^/\w+.+\.(fsl|fossil|sqlite)$~", $argv))
              : $_SERVER["FOSSIL_REPOSITORY"],
    "add" => preg_grep("~^\++[\w.-]+[:=].+$~", $argv),
    "if" => preg_grep("~^-+if:[\w-]+$~i", $argv),
    "service_urls" => preg_grep("~^https?://.+$~i", $argv),
    "stdin" => fread(STDIN, 1<<20),
        #e9ce272e806c47a10e3d4cf570549a1d33133133 file src/code.c
        #1db32c3ff6c5969191d575c65916f6322b833313 wiki HomePage
        #313908390381390813913813931931830938aaaa check-in to trunk by user on 2222-11-30 00:00
    "json" => TRUE, #!preg_grep("-multipart")
    "user" => null,
];
#-- basic parameter checks
if (empty($cfg["repo"]) || empty($cfg["service_urls"])) {
   die("%R or url missing");
}
elseif ($cfg["if"]) {

    if_checks($cfg["if"]);
}
#-- dependent options
$cfg += [
    "basename" => get_basename(),
    "baseurl" => get_baseurl(),
    "title" => get_config("project-title"),
];


#-- operation flag
if ($cfg["help"]) {
   die("Usage as hook:\n  fossil-webhook %R --git http://service.rest/TOKEN/ETC\n");
}
elseif ($cfg["git"]) {
    $request = git_request();
}
elseif ($cfg["ping"]) {
    $request = basic_ping();
}
else {
    $request = fossil_request();
}

#-- and send
add_params($request);
if ($cfg["dbg"]) { 
    die(json_encode($request, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
else {
    send($request, extra_headers());
}




/**
 * Process +add=paramval arguments,
 * simply adding them to $request[] top-level.
 *
 */
function add_params(&$request) {
    global $cfg;
    #-- +add=params?
    foreach ($cfg["add"] as $add) {
        preg_match("~^\++([\w-.]+)[=](.+)$~", $add, $kv);
        $request[$kv[1]] = $kv[2];
    }
}

/**
 * Process +Http:Headers arguments,
 * collecting them for CURL request.
 *
 */
function extra_headers() {
    global $cfg;
    $headers = [];
    #-- +Ttoken:params?
    foreach ($cfg["add"] as $add) {
        preg_match("~^\++([\w-]+)[:](.+)$~", $add, $kv);
        $headers[] = "$kv[1]: $kv[2]";
    }
    return $headers;
}


/**
 * Sending
 *
 */
function send($request, $headers=[]) {
    global $cfg;
    if ($cfg["json"]) {
        $request = json_encode($request, JSON_PRETTY_PRINT);
        $headers[] = "Content-Type: application/json";
    }
    #-- send query
    foreach ($cfg["service_urls"] as $url) {
        $c = curl_init();
        curl_setopt_array($c, [
            CURLOPT_URL => $url,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $request,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_VERBOSE => 1,
        ]);
        $c->exec();
    }
}



/**
 * Define a fossil-esque webhook payload.
 *   (Not much resemblence, shortened attributtes, no payload:{} wrapping, etc.)
 *
 */
function fossil_request() {
    global $cfg;
    $url = get_baseurl();
    return [
        '$type' => "webhook",
        '$class' => "vcs fossil",
        '$ver' => "0.1",
        "action" => main_action($cfg["stdin"]),
	"fossil" => get_config("server-code"),
	"url" => $url,
        "project" => [
            "name" => $cfg["basename"],
            "title" => get_config("project-name"),
            "description" => get_config("project-description"),
            "size" => filesize($cfg["repo"]),
            "id" => get_config("project-code"),
            "changes" => implode("\n", array_column(db("SELECT comment FROM event ORDER BY mtime DESC LIMIT 10"), "comment")),
        ],
        "api" => [
            "stat" => "$url/json/stat",
            "timeline" => "$url/json/timeline/checkin",
            "dir" => "$url/json/dir",
            "trees" => "$url/ext/trees",
            "branch" => "$url/json/branch",
            "version" => "$url/json/version",
            "report" => "$url/json/report",
            "query" => "$url/json/query?sql={sql}",
            "wiki_list" => "$url/json/wiki/list",
            "wiki_get" => "$url/json/wiki/get/{name}",
            "changelog" => "$url/ext/changelog",
        ],
        "artifacts" => array_map(
            function($row) use($url) {
                return expand_artifact($row, $url);
            },
            get_artifacts($cfg["stdin"])
        ),
        "user" => $cfg["user"],
	"timestamp" => time(),
        "procTimeMs" =>  1000 * (microtime(TRUE) - $cfg["start"]),
    ];
}


/**
 * More basic payload
 *
 */
function basic_ping() {
    global $cfg;
    $url = get_baseurl();
    return [
        '$type' => "webhook",
        '$class' => "ping basic",
        "action" => "edited",
	"url" => $url,
	"name" => $cfg["basename"],
        "stdin" => $cfg["stdin"],
	"timestamp" => time(),
    ];
}

 
/**
 * Simulate GitHub-style webhook.
 * (Unlikely that we can fill in all the minutae.)
 *
 */
function git_request() {
    global $cfg;
    # prepare values
    $user = get_main_user();
    $url = $cfg["baseurl"];
    $basename = $cfg["basename"];
    $rep_id = hexdec(substr(get_config("project-code", "12FFFF"), 0, 6));
    $art = get_artifacts($cfg["stdin"]);
    $sender = get_user($art[0]["uuid"]);
    # assemble
    return [
      "action" => "edited",
      "rule" => [
        "id" => crc32($cfg["stdin"]),
        "repository_id" => $rep_id,
        "name" => $basename,
        "created_at" => iso8601(mtime_event("MIN")),
        "updated_at" => iso8601(mtime_event("MAX")),
        "pull_request_reviews_enforcement_level" => "off",
        "required_approving_review_count" => 0,
        "dismiss_stale_reviews_on_push" => false,
        "require_code_owner_review" => false,
        "authorized_dismissal_actors_only" => false,
        "ignore_approvals_from_contributors" => false,
        "required_status_checks" => [
          0 => "basic-CI",
        ],
        "required_status_checks_enforcement_level" => "non_admins",
        "strict_required_status_checks_policy" => false,
        "signature_requirement_enforcement_level" => "off",
        "linear_history_requirement_enforcement_level" => "off",
        "admin_enforced" => false,
        "allow_force_pushes_enforcement_level" => "off",
        "allow_deletions_enforcement_level" => "off",
        "merge_queue_enforcement_level" => "off",
        "required_deployments_enforcement_level" => "off",
        "required_conversation_resolution_level" => "off",
        "authorized_actors_only" => true,
        "authorized_actor_names" => [
          0 => $user,
        ],
      ],
      "changes" => [
        "authorized_actors_only" => [
          "from" => false,





        ],
        "authorized_actor_names" => [
          "from" => [],
        ],
      ],
      "repository" => [
        "id" => $rep_id,
        "node_id" => base64_encode($url),
        "name" => $cfg["basename"],
        "full_name" => $cfg["basename"],
        "private" => false,
        "owner" => [
          "login" => $user,
          "id" => crc32($user),
          "node_id" => base64_encode($user),
          "avatar_url" => "$url/logo",
          "gravatar_id" => "",
          "url" => "$url/json/user/get/$user",
          "html_url" => "$url/timeline?u=$user",
          "followers_url" => NULL,
          "following_url" => NULL,
          "gists_url" => "$url/unversioned",
          "starred_url" => NULL,
          "subscriptions_url" => NULL,
          "organizations_url" => NULL,
          "repos_url" => dirname($url),
          "events_url" => "$url/timeline",
          "received_events_url" => "$url/json/timeline",
          "type" => "Organization",
          "site_admin" => $user==$sender,
        ],
        "html_url" => "$url",
        "description" => get_config("project-description"),
        "fork" => false,
        "url" => "$url",
        "forks_url" => "$url",
        "keys_url" => NULL,
        "collaborators_url" => NULL,
        "teams_url" => NULL,
        "hooks_url" => "$url/ext/hooks",
        "issue_events_url" => "$url/json/report/list",
        "events_url" => "$url/json/timeline/event",
        "assignees_url" => NULL,
        "branches_url" => "$url/json/branch/list",


        "tags_url" => "$url/json/tag/list",
        "blobs_url" => "$url/json/",#
        "git_tags_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/git/tags{/sha}",
        "git_refs_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/git/refs{/sha}",
        "trees_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/git/trees{/sha}",
        "statuses_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/statuses/{sha}",
        "languages_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/languages",
        "stargazers_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/stargazers",
        "contributors_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/contributors",
        "subscribers_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/subscribers",
        "subscription_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/subscription",
        "commits_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/commits{/sha}",
        "git_commits_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/git/commits{/sha}",
        "comments_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/comments{/number}",
        "issue_comment_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/issues/comments{/number}",
        "contents_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/contents/{+path}",
        "compare_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/compare/{base}...{head}",
        "merges_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/merges",
        "archive_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/{archive_format}{/ref}",
        "downloads_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/downloads",
        "issues_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/issues{/number}",
        "pulls_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/pulls{/number}",
        "milestones_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/milestones{/number}",
        "notifications_url" => "$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/notifications{?since,all,participating}",
        "labels_url" => NULL,#"$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/labels{/name}",
        "releases_url" => "$url/ext/project.json",
        "deployments_url" => NULL,#"$url/json/",#"https://api.github.com/repos/octo-org/octo-repo/deployments",
        "created_at" => iso8601(mtime_event("MIN")),
        "updated_at" => iso8601(mtime_event("MAX")),
        "pushed_at" => iso8601(mtime_event("MAX", "WHERE type='ci'")),   # ERR??
        "git_url" => NULL, #"git://github.com/octo-org/octo-repo.git",
        "ssh_url" => "$url",
        "clone_url" => "$url",

        "svn_url" => NULL,
        "size" => filesize($cfg["repo"]) >> 10,
        "stargazers_count" => 0,
        "watchers_count" => 0,
        "language" => language_count(),
        "has_issues" => true,
        "has_projects" => false,
        "has_downloads" => true,
        "has_wiki" => false,
        "has_pages" => true,
        "forks_count" => 0,
        "mirror_url" => NULL,
        "archived" => false,
        "disabled" => false,
        "open_issues_count" => $tickets = intval(db("SELECT COUNT(1) as c FROM ticket WHERE status!='Closed'")[0]["c"]),
        "license" => NULL,
        "forks" => 0,
        "open_issues" => $tickets,
        "watchers" => 0,
        "default_branch" => "trunk",
      ],
      "organization" => [
        "login" => "$basename",
        "id" => crc32($basename),
        "node_id" => base64_encode($basename),
        "url" => "$url",
        "repos_url" => "$url",
        "events_url" => NULL,
        "hooks_url" => NULL,
        "issues_url" => NULL,
        "members_url" => NULL,
        "public_members_url" => NULL,
        "avatar_url" => NULL,
        "description" => get_config("project-name"),
      ],
      "sender" => [
        "login" => $sender,
        "id" => crc32($sender),
        "node_id" => base64_encode($sender),
        "avatar_url" => "$url/logo",   # do we have a user PHOTO lookup?
        "gravatar_id" => "",
        "url" => "$url/json/user/get/$sender",
        "html_url" => "$url/timeline?u=$sender",
        "followers_url" => NULL,
        "following_url" => NULL,
        "gists_url" => NULL,
        "starred_url" => NULL,
        "subscriptions_url" => NULL,
        "organizations_url" => NULL,
        "repos_url" => NULL,
        "events_url" => NULL,
        "received_events_url" => NULL,
        "type" => "User",
        "site_admin" => false,
      ],
    ];
    # do...
}


/**
454
455
456
457
458
459
460
461

462
463
464
465
466
467
468
function expand_artifact($row, $url, $q="urlencode") {
    global $cfg;
    switch ($row["type"]) {
        case "attachment":
        case "file":
            $row["name"] = $row["comment"];
            $row["url_raw"] = "$url/raw/$row[uuid]?at={$q($row['name'])}";
            $row["url_json"] = "$url/json/artifact/{$q($row['name'])}";

            break;
        case "wiki":
            $row["name"] = $row["comment"];
            $row["url_web"] = "$url/wiki/{$q($row['name'])}";
            $row["url_json"] = "$url/json/wiki/get/{$q($row['name'])}";
            break;
        case "check-in":







|
>







513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
function expand_artifact($row, $url, $q="urlencode") {
    global $cfg;
    switch ($row["type"]) {
        case "attachment":
        case "file":
            $row["name"] = $row["comment"];
            $row["url_raw"] = "$url/raw/$row[uuid]?at={$q($row['name'])}";
            $row["url_json"] = "$url/json/artifact/{$q($row['uuid'])}";
            $row["url_finfo"] = "$url/json/finfo?name={$q($row['name'])}";
            break;
        case "wiki":
            $row["name"] = $row["comment"];
            $row["url_web"] = "$url/wiki/{$q($row['name'])}";
            $row["url_json"] = "$url/json/wiki/get/{$q($row['name'])}";
            break;
        case "check-in":
483
484
485
486
487
488
489

490
491
492
493

494
495
496

























497
        if (preg_match("/^\w+\s$t\\b/m", $art)) {
            return $t;
        }
    }
    return "after-receive";
}


function iso8601($t) {
    return strftime("%Y-%m-%dT%H:%M:%SZ", $t);
}


function mtime_event($FN="MIN") {
    return db("SELECT strftime('%s', $FN(mtime)) AS dt FROM event LIMIT 1")[0]["dt"];
}

























?>







>




>
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
        if (preg_match("/^\w+\s$t\\b/m", $art)) {
            return $t;
        }
    }
    return "after-receive";
}

#-- datetime format
function iso8601($t) {
    return strftime("%Y-%m-%dT%H:%M:%SZ", $t);
}

#-- get earliest or last `event` timestamp
function mtime_event($FN="MIN", $WHERE="") {
    return db("SELECT strftime('%s', $FN(mtime)) AS dt FROM event $WHERE LIMIT 1")[0]["dt"];
}

#-- tests --if:type or --if:filename presence in stdin
function if_checks($if_list) {
    global $cfg;
    foreach ($if_list as $if) {
        preg_match("/if:(.+)/", $if, $if) and $if=$if[1];
        if (!preg_match("/\\b$if\\b/", $cfg["stdin"])) {
            die("--if: no match for '$if'");
        }
    }
}

#-- select filename extension
function language_count($ext=[]) {
    foreach (db("SELECT name FROM filename") as $row) {
        if (preg_match("/\.(\w+)$/", $row["name"], $uu)) {
            @$ext[$uu[1]] += 1;
        }
    }
    if ($ext) {
        arsort($ext);
        return array_keys($ext)[0];
    }
}

?>