From 1adf5482a615a46afd0b992968f894eabe36ddc6 Mon Sep 17 00:00:00 2001 From: Armin Stebich Date: Mon, 23 May 2022 20:24:55 +0200 Subject: [PATCH 1/5] added verbose function for debugging --- github-wiki-notify.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/github-wiki-notify.php b/github-wiki-notify.php index 3aaa581..7a4aebf 100755 --- a/github-wiki-notify.php +++ b/github-wiki-notify.php @@ -6,6 +6,9 @@ * Usage is simple, just cron it: * * github-wiki-notify.php --path=/path/to/repo --email=list@example.com --subject="Wiki updated!" + * + * Optional parameter: + * --verbose=LEVEL LEVEL = 0..5, see class Level, 0 = quiet .. 5 = debugging, default = 0 * * @author Anthony Bush * @version 1.0.1 @@ -14,6 +17,18 @@ * @package default **/ +/** + * verbose level + **/ +class Level { + const NOTHING = 0; + const ALERT = 1; + const ERROR = 2; + const WARNING = 3; + const INFO = 4; + const DEBUG = 5; +} + /** * Define DocBlock **/ @@ -21,6 +36,7 @@ $path = null; $email = null; $subject = null; +$verbose = Level::NOTHING; foreach ($argv as $arg) { if (preg_match('/--path=(.*)/', $arg, $match)) { @@ -29,13 +45,15 @@ $email = $match[1]; } else if (preg_match('/--subject=(.*)/', $arg, $match)) { $subject = $match[1]; + } else if (preg_match('/--verbose=(.*)/', $arg, $match)) { + $verbose = $match[1]; } } if (is_null($path) || is_null($email)) { echo("Usage:\n"); - echo(" " . basename(__FILE__) . " --path=/path/to/repo --email=list@example.com\n"); + echo(" " . basename(__FILE__) . " --path=/path/to/repo --email=list@example.com [--verbose=(0..5)]\n"); exit(1); } @@ -58,4 +76,17 @@ $body = "To see the changes, visit:\n" . $wikiDiffUrl . "\n\nChangelog:\n" . $changeLog . "\n"; mail($email, $subject, $body, "From: $email"); } -// else no updates +else { + verbose( "No match in pullResult!", Level::INFO); + verbose( "\npullResult:"); + verbose( $pullResult); +} + +function verbose($message, $level = Level::DEBUG) +{ + global $verbose; + + if ($verbose >= $level) { + echo $message . "\n"; + } +} From 8f911713a5c4154376b81a3b5f11b1060ad20c3f Mon Sep 17 00:00:00 2001 From: Armin Stebich Date: Mon, 23 May 2022 20:49:51 +0200 Subject: [PATCH 2/5] added optional from parameter for email sender address --- github-wiki-notify.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/github-wiki-notify.php b/github-wiki-notify.php index 7a4aebf..568a49a 100755 --- a/github-wiki-notify.php +++ b/github-wiki-notify.php @@ -8,7 +8,8 @@ * github-wiki-notify.php --path=/path/to/repo --email=list@example.com --subject="Wiki updated!" * * Optional parameter: - * --verbose=LEVEL LEVEL = 0..5, see class Level, 0 = quiet .. 5 = debugging, default = 0 + * --from=sender@example.com sender email address, otherwise the --email address is used + * --verbose=LEVEL LEVEL = 0..5, see class Level, 0 = quiet .. 5 = debugging, default = 0 * * @author Anthony Bush * @version 1.0.1 @@ -35,6 +36,7 @@ class Level { $path = null; $email = null; +$from = null; $subject = null; $verbose = Level::NOTHING; foreach ($argv as $arg) @@ -43,6 +45,8 @@ class Level { $path = $match[1]; } else if (preg_match('/--email=(.*)/', $arg, $match)) { $email = $match[1]; + } else if (preg_match('/--from=(.*)/', $arg, $match)) { + $from = $match[1]; } else if (preg_match('/--subject=(.*)/', $arg, $match)) { $subject = $match[1]; } else if (preg_match('/--verbose=(.*)/', $arg, $match)) { @@ -53,10 +57,15 @@ class Level { if (is_null($path) || is_null($email)) { echo("Usage:\n"); - echo(" " . basename(__FILE__) . " --path=/path/to/repo --email=list@example.com [--verbose=(0..5)]\n"); + echo(" " . basename(__FILE__) . " --path=/path/to/repo --email=list@example.com [--from=sender@example.com] [--verbose=(0..5)]\n"); exit(1); } +if (is_null($from)) +{ + $from = $email; +} + if (!chdir($path)) { echo("Path does not exist: " . $path . "\n"); exit(2); @@ -74,7 +83,7 @@ class Level { $subject = '[SCM]: ' . $repo . ' was updated'; } $body = "To see the changes, visit:\n" . $wikiDiffUrl . "\n\nChangelog:\n" . $changeLog . "\n"; - mail($email, $subject, $body, "From: $email"); + mail($email, $subject, $body, "From: $from"); } else { verbose( "No match in pullResult!", Level::INFO); From 15fda312a38e5ae826f0d68448beb927fbfc048e Mon Sep 17 00:00:00 2001 From: Armin Stebich Date: Sat, 25 Jun 2022 12:23:03 +0200 Subject: [PATCH 3/5] use git pull -v for consistent output --- github-wiki-notify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github-wiki-notify.php b/github-wiki-notify.php index 568a49a..e41f990 100755 --- a/github-wiki-notify.php +++ b/github-wiki-notify.php @@ -71,7 +71,7 @@ class Level { exit(2); } -$pullResult = `git pull 2>&1`; +$pullResult = `git pull -v 2>&1`; if (preg_match('/From github\.com:(.*)\n\s*([^\s]+)/', $pullResult, $match)) { $repo = $match[1]; From 65b53f349770f6f2da3a4089857ea6713a3c52c6 Mon Sep 17 00:00:00 2001 From: Armin Stebich Date: Sat, 25 Jun 2022 13:19:06 +0200 Subject: [PATCH 4/5] fixed pullResult regex for localized git output --- github-wiki-notify.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/github-wiki-notify.php b/github-wiki-notify.php index e41f990..cedf215 100755 --- a/github-wiki-notify.php +++ b/github-wiki-notify.php @@ -71,11 +71,26 @@ class Level { exit(2); } -$pullResult = `git pull -v 2>&1`; -if (preg_match('/From github\.com:(.*)\n\s*([^\s]+)/', $pullResult, $match)) -{ +// request repo-URL with `git remote` +// which is for HTTPS: https://github.com/OWNER/REPO.wiki.git +// and for SSH: git@github.com:OWNER/REPO.wiki.git +$remote = `git remote get-url origin`; +$repo = 'unknown'; +if (preg_match('/github.com[:\/](\S*)\.git$/', $remote, $match)) { $repo = $match[1]; - $revs = $match[2]; + verbose( "remote url found: $repo", Level::INFO); +} +else { + verbose( "No remote url found!", Level::ERROR); + exit(3); +} + +// pull possible changes and analyze result +// be aware that git output is localized! +$pullResult = `git pull -v 2>&1`; +if (preg_match('/^\S* ([a-z0-9]{7}\.\.[a-z0-9]{7})\n/m', $pullResult, $match)) { + $revs = $match[1]; + verbose( "updated: $revs", Level::INFO); $wikiDiffUrl = 'https://github.com/' . str_replace('.wiki', '/wiki', $repo) . '/_compare/' . $revs; $changeLog = `git log --pretty=format:'%h - %s (%cr) <%an>' $revs`; @@ -86,7 +101,9 @@ class Level { mail($email, $subject, $body, "From: $from"); } else { + // this may be because the repo is up to date, what is okay verbose( "No match in pullResult!", Level::INFO); + // but can also be an issue with the regex, because of localization or git changes verbose( "\npullResult:"); verbose( $pullResult); } From 8e77b1c03e2923775c8dacdd4986bae51524672b Mon Sep 17 00:00:00 2001 From: Armin Stebich Date: Sat, 25 Jun 2022 13:36:02 +0200 Subject: [PATCH 5/5] update to version 1.0.2, add copyright --- github-wiki-notify.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github-wiki-notify.php b/github-wiki-notify.php index cedf215..c55e941 100755 --- a/github-wiki-notify.php +++ b/github-wiki-notify.php @@ -12,8 +12,9 @@ * --verbose=LEVEL LEVEL = 0..5, see class Level, 0 = quiet .. 5 = debugging, default = 0 * * @author Anthony Bush - * @version 1.0.1 + * @version 1.0.2 * @copyright Anthony Bush, 20 December, 2011 + * @copyright Armin Stebich, 25 June, 2022 * @license * @package default **/