diff --git a/tests/cmdline.t b/tests/cmdline.t index ad14f25..170140e 100755 --- a/tests/cmdline.t +++ b/tests/cmdline.t @@ -7,9 +7,14 @@ webhooker tests init: $ POST() { > curl -s --data-urlencode payload@$1 http://localhost:1234/ > } + $ LINES=0 + $ LOGS() { + > awk "NR > $LINES" logs + > LINES=$(cat logs | wc -l) + > } $ ./webhooker -p 1234 \ - > 'octokitty/testing:master=echo OTM' \ - > '.*=echo $REPO' \ + > 'octokitty/testing:main=echo OTM' \ + > 'hellothere/.+=echo $REPO' \ > > logs & Usage: @@ -54,14 +59,25 @@ Usage: Check that it works: $ POST $TESTDIR/example.json - 'echo OTM' for octokitty/testing output: + 'echo OTM' for 'octokitty/testing:main' output: OTM $ POST $TESTDIR/other.json - 'echo $REPO' for hellothere/other output: + 'echo $REPO' for 'hellothere/other:lolster' output: hellothere/other - $ cat logs - [\d/: ]+ 'echo OTM' for octokitty/testing output: OTM (re) - [\d/: ]+ 'echo \$REPO' for hellothere/other output: hellothere/other (re) + $ LOGS + [\d/: ]+ 'echo OTM' for 'octokitty/testing:main' output: OTM (re) + [\d/: ]+ 'echo \$REPO' for 'hellothere/other:lolster' output: hellothere/other (re) + +Check that errors are correctly processed: + + $ POST nothing + unexpected end of JSON input + $ POST $TESTDIR/nocommit.json + No handlers for 'piranha/unknown:' + $ LOGS + [\d/: ]+ unexpected end of JSON input (re) + [\d/: ]+ No handlers for 'piranha/unknown:' (re) + Cool down: diff --git a/tests/example.json b/tests/example.json index 4f80ff1..eb9b7b6 100644 --- a/tests/example.json +++ b/tests/example.json @@ -115,27 +115,11 @@ "email":"lolwut@noway.biz", "name":"Garen Torikian" }, - "ref":"refs/heads/master", + "ref":"refs/heads/main", "repository":{ - "created_at":1332977768, - "description":"", - "fork":false, - "forks":0, - "has_downloads":true, - "has_issues":true, - "has_wiki":true, - "homepage":"", - "id":3860742, - "language":"Ruby", - "master_branch":"master", "name":"testing", "full_name": "octokitty/testing", - "open_issues":2, "private":false, - "pushed_at":1363295520, - "size":2156, - "stargazers":1, - "url":"https://github.com/octokitty/testing", - "watchers":1 + "url":"https://github.com/octokitty/testing" } } diff --git a/tests/nocommit.json b/tests/nocommit.json new file mode 100644 index 0000000..fd7f786 --- /dev/null +++ b/tests/nocommit.json @@ -0,0 +1,151 @@ +{ + "zen": "Favor focus over features.", + "hook_id": 409817937, + "hook": { + "type": "Repository", + "id": 409817937, + "name": "web", + "active": true, + "events": [ + "push" + ], + "config": { + "content_type": "json", + "insecure_ssl": "0", + "url": "https://unknown.com/webhook" + }, + "updated_at": "2023-04-14T07:59:50Z", + "created_at": "2023-04-14T07:59:50Z", + "url": "https://api.github.com/repos/piranha/unknown/hooks/409817937", + "test_url": "https://api.github.com/repos/piranha/unknown/hooks/409817937/test", + "ping_url": "https://api.github.com/repos/piranha/unknown/hooks/409817937/pings", + "deliveries_url": "https://api.github.com/repos/piranha/unknown/hooks/409817937/deliveries", + "last_response": { + "code": null, + "status": "unused", + "message": null + } + }, + "repository": { + "id": 596040297, + "node_id": "R_kgDOI4baaQ", + "name": "unknown", + "full_name": "piranha/unknown", + "private": true, + "owner": { + "login": "piranha", + "id": 6553, + "node_id": "MDQ6VXNlcjY1NTM=", + "avatar_url": "https://avatars.githubusercontent.com/u/6553?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/piranha", + "html_url": "https://github.com/piranha", + "followers_url": "https://api.github.com/users/piranha/followers", + "following_url": "https://api.github.com/users/piranha/following{/other_user}", + "gists_url": "https://api.github.com/users/piranha/gists{/gist_id}", + "starred_url": "https://api.github.com/users/piranha/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/piranha/subscriptions", + "organizations_url": "https://api.github.com/users/piranha/orgs", + "repos_url": "https://api.github.com/users/piranha/repos", + "events_url": "https://api.github.com/users/piranha/events{/privacy}", + "received_events_url": "https://api.github.com/users/piranha/received_events", + "type": "User", + "site_admin": false + }, + "html_url": "https://github.com/piranha/unknown", + "description": null, + "fork": false, + "url": "https://api.github.com/repos/piranha/unknown", + "forks_url": "https://api.github.com/repos/piranha/unknown/forks", + "keys_url": "https://api.github.com/repos/piranha/unknown/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/piranha/unknown/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/piranha/unknown/teams", + "hooks_url": "https://api.github.com/repos/piranha/unknown/hooks", + "issue_events_url": "https://api.github.com/repos/piranha/unknown/issues/events{/number}", + "events_url": "https://api.github.com/repos/piranha/unknown/events", + "assignees_url": "https://api.github.com/repos/piranha/unknown/assignees{/user}", + "branches_url": "https://api.github.com/repos/piranha/unknown/branches{/branch}", + "tags_url": "https://api.github.com/repos/piranha/unknown/tags", + "blobs_url": "https://api.github.com/repos/piranha/unknown/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/piranha/unknown/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/piranha/unknown/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/piranha/unknown/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/piranha/unknown/statuses/{sha}", + "languages_url": "https://api.github.com/repos/piranha/unknown/languages", + "stargazers_url": "https://api.github.com/repos/piranha/unknown/stargazers", + "contributors_url": "https://api.github.com/repos/piranha/unknown/contributors", + "subscribers_url": "https://api.github.com/repos/piranha/unknown/subscribers", + "subscription_url": "https://api.github.com/repos/piranha/unknown/subscription", + "commits_url": "https://api.github.com/repos/piranha/unknown/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/piranha/unknown/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/piranha/unknown/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/piranha/unknown/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/piranha/unknown/contents/{+path}", + "compare_url": "https://api.github.com/repos/piranha/unknown/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/piranha/unknown/merges", + "archive_url": "https://api.github.com/repos/piranha/unknown/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/piranha/unknown/downloads", + "issues_url": "https://api.github.com/repos/piranha/unknown/issues{/number}", + "pulls_url": "https://api.github.com/repos/piranha/unknown/pulls{/number}", + "milestones_url": "https://api.github.com/repos/piranha/unknown/milestones{/number}", + "notifications_url": "https://api.github.com/repos/piranha/unknown/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/piranha/unknown/labels{/name}", + "releases_url": "https://api.github.com/repos/piranha/unknown/releases{/id}", + "deployments_url": "https://api.github.com/repos/piranha/unknown/deployments", + "created_at": "2023-02-01T10:41:41Z", + "updated_at": "2023-02-01T10:41:57Z", + "pushed_at": "2023-04-14T07:57:01Z", + "git_url": "git://github.com/piranha/unknown.git", + "ssh_url": "git@github.com:piranha/unknown.git", + "clone_url": "https://github.com/piranha/unknown.git", + "svn_url": "https://github.com/piranha/unknown", + "homepage": null, + "size": 557, + "stargazers_count": 0, + "watchers_count": 0, + "language": "JavaScript", + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": false, + "has_pages": false, + "has_discussions": false, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 0, + "license": null, + "allow_forking": true, + "is_template": false, + "web_commit_signoff_required": false, + "topics": [ + + ], + "visibility": "private", + "forks": 0, + "open_issues": 0, + "watchers": 0, + "default_branch": "main" + }, + "sender": { + "login": "piranha", + "id": 6553, + "node_id": "MDQ6VXNlcjY1NTM=", + "avatar_url": "https://avatars.githubusercontent.com/u/6553?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/piranha", + "html_url": "https://github.com/piranha", + "followers_url": "https://api.github.com/users/piranha/followers", + "following_url": "https://api.github.com/users/piranha/following{/other_user}", + "gists_url": "https://api.github.com/users/piranha/gists{/gist_id}", + "starred_url": "https://api.github.com/users/piranha/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/piranha/subscriptions", + "organizations_url": "https://api.github.com/users/piranha/orgs", + "repos_url": "https://api.github.com/users/piranha/repos", + "events_url": "https://api.github.com/users/piranha/events{/privacy}", + "received_events_url": "https://api.github.com/users/piranha/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/tests/webhooker b/tests/webhooker deleted file mode 100755 index 3830fd1..0000000 Binary files a/tests/webhooker and /dev/null differ diff --git a/webhooker.go b/webhooker.go index be23288..6b74de1 100644 --- a/webhooker.go +++ b/webhooker.go @@ -86,8 +86,16 @@ func (g *GithubPayload) BranchName() string { } func (g *GithubPayload) EnvData() []string { - commit := g.Commits[0] + if len(g.Commits) == 0 { + return []string{ + env("REPO", g.RepoName()), + env("REPO_URL", g.Repository.Url), + env("PRIVATE", fmt.Sprintf("%t", g.Repository.Private)), + env("BRANCH", g.Ref), + } + } + commit := g.Commits[0] return []string{ env("REPO", g.RepoName()), env("REPO_URL", g.Repository.Url), @@ -127,8 +135,8 @@ func (r *PatRule) Run(data Payload) (string, error) { ) out, err := cmd.CombinedOutput() - log.Printf("'%s' for %s output: %s", r.Command, data.RepoName(), out) - return fmt.Sprintf("'%s' for %s output:\n%s", r.Command, data.RepoName(), out), err + log.Printf("'%s' for '%s' output: %s", r.Command, GetPath(data), out) + return fmt.Sprintf("'%s' for '%s' output:\n%s", r.Command, GetPath(data), out), err } /// actual work @@ -161,7 +169,7 @@ func (c Config) ExecutePayload(data Payload) (string, error) { } } - msg := fmt.Sprintf("No handlers for %s", path) + msg := fmt.Sprintf("No handlers for '%s'\n", path) log.Print(msg) return msg, nil }