From e8224fc2adab60c230d2e234fb6752e82d78cfc3 Mon Sep 17 00:00:00 2001 From: Markus Hoffrogge Date: Thu, 25 Mar 2021 22:31:06 +0100 Subject: [PATCH 1/3] Extension to send error messages to a configurable eMail address - plugin.info.txt: - added myself to the list of authors - updated date to 19-Mar-2021 - editcommit.php: - pass plugin instance to GitRepo cTor - added error notification helper methods - catch Exceptions from underlying GitRepo or Git methods to hide errors from end user rahter than sending error message by eMail - Git.php: - Git: - added plugin instance to methods as paramater - GitRepo: - added plugin instance to cTor as parameter - default.php, metadata.php, settings.php: - added config 'emailAddressOnError' - added config 'notifyByMailOnSuccess' - lang/en: - added localizations for error messages - added eMail templates for mail notifocations Signed-off-by: Markus Hoffrogge --- action/editcommit.php | 173 +++++++++++++++++++++++++++--- conf/default.php | 2 + conf/metadata.php | 2 + lang/en/lang.php | 6 ++ lang/en/mail_command_error.txt | 19 ++++ lang/en/mail_command_success.txt | 15 +++ lang/en/mail_create_new_error.txt | 15 +++ lang/en/mail_repo_path_error.txt | 13 +++ lang/en/settings.php | 4 +- lib/Git.php | 127 ++++++++++++++++++---- plugin.info.txt | 4 +- 11 files changed, 343 insertions(+), 37 deletions(-) create mode 100644 lang/en/lang.php create mode 100644 lang/en/mail_command_error.txt create mode 100644 lang/en/mail_command_success.txt create mode 100644 lang/en/mail_create_new_error.txt create mode 100644 lang/en/mail_repo_path_error.txt diff --git a/action/editcommit.php b/action/editcommit.php index 311c6ef..9990936 100644 --- a/action/editcommit.php +++ b/action/editcommit.php @@ -46,7 +46,7 @@ private function initRepo() { } //init the repo and create a new one if it is not present io_mkdir_p($repoPath); - $repo = new GitRepo($repoPath, true, true); + $repo = new GitRepo($repoPath, $this, true, true); //set git working directory (by default DokuWiki's savedir) $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir)); @@ -76,20 +76,25 @@ private function isIgnored($filePath) { } private function commitFile($filePath,$message) { - if (!$this->isIgnored($filePath)) { - $repo = $this->initRepo(); + try { + $repo = $this->initRepo(); - //add the changed file and set the commit message - $repo->add($filePath); - $repo->commit($message); + //add the changed file and set the commit message + $repo->add($filePath); + $repo->commit($message); - //if the push after Commit option is set we push the active branch to origin - if ($this->getConf('pushAfterCommit')) { - $repo->push('origin',$repo->active_branch()); + //if the push after Commit option is set we push the active branch to origin + if ($this->getConf('pushAfterCommit')) { + $repo->push('origin',$repo->active_branch()); + } + } catch (Exception $e) { + if (!$this->isNotifyByEmailOnGitCommandError()) { + throw new Exception('Git committing or pushing failed: '.$e->getMessage(), 1, $e); + } + return; } } - } private function getAuthor() { @@ -115,10 +120,17 @@ public function handle_periodic_pull(Doku_Event &$event, $param) { //if it is time to run a pull request if ($lastPull+$timeToWait < $now) { - $repo = $this->initRepo(); - - //execute the pull request - $repo->pull('origin',$repo->active_branch()); + try { + $repo = $this->initRepo(); + + //execute the pull request + $repo->pull('origin',$repo->active_branch()); + } catch (Exception $e) { + if (!$this->isNotifyByEmailOnGitCommandError()) { + throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e); + } + return; + } //save the current time to the file to track the last pull execution file_put_contents($lastPullFile,serialize(time())); @@ -197,8 +209,139 @@ public function handle_io_wikipage_write(Doku_Event &$event, $param) { $this->commitFile($pagePath,$message); } - } + + // ====== Error notification helpers ====== + /** + * Notifies error on create_new + * + * @access public + * @param string repository path + * @param string reference path / remote reference + * @param string error message + * @return bool + */ + public function notify_create_new_error($repo_path, $reference, $error_message) { + $template_replacements = array( + 'GIT_REPO_PATH' => $repo_path, + 'GIT_REFERENCE' => (empty($reference) ? 'n/a' : $reference), + 'GIT_ERROR_MESSAGE' => $error_message + ); + return $this->notifyByMail('mail_create_new_error_subject', 'mail_create_new_error', $template_replacements); + } + + /** + * Notifies error on setting repo path + * + * @access public + * @param string repository path + * @param string error message + * @return bool + */ + public function notify_repo_path_error($repo_path, $error_message) { + $template_replacements = array( + 'GIT_REPO_PATH' => $repo_path, + 'GIT_ERROR_MESSAGE' => $error_message + ); + return $this->notifyByMail('mail_repo_path_error_subject', 'mail_repo_path_error', $template_replacements); + } + + /** + * Notifies error on git command + * + * @access public + * @param string repository path + * @param string current working dir + * @param string command line + * @param int exit code of command (status) + * @param string error message + * @return bool + */ + public function notify_command_error($repo_path, $cwd, $command, $status, $error_message) { + $template_replacements = array( + 'GIT_REPO_PATH' => $repo_path, + 'GIT_CWD' => $cwd, + 'GIT_COMMAND' => $command, + 'GIT_COMMAND_EXITCODE' => $status, + 'GIT_ERROR_MESSAGE' => $error_message + ); + return $this->notifyByMail('mail_command_error_subject', 'mail_command_error', $template_replacements); + } + + /** + * Notifies success on git command + * + * @access public + * @param string repository path + * @param string current working dir + * @param string command line + * @return bool + */ + public function notify_command_success($repo_path, $cwd, $command) { + if (!$this->getConf('notifyByMailOnSuccess')) { + return false; + } + $template_replacements = array( + 'GIT_REPO_PATH' => $repo_path, + 'GIT_CWD' => $cwd, + 'GIT_COMMAND' => $command + ); + return $this->notifyByMail('mail_command_success_subject', 'mail_command_success', $template_replacements); + } + + /** + * Send an eMail, if eMail address is configured + * + * @access public + * @param string lang id for the subject + * @param string lang id for the template(.txt) + * @param array array of replacements + * @return bool + */ + public function notifyByMail($subject_id, $template_id, $template_replacements) { + $ret = false; + dbglog("GitBacked - notifyByMail: [subject_id=".$subject_id.", template_id=".$template_id.", template_replacements=".$template_replacements."]"); + if (!$this->isNotifyByEmailOnGitCommandError()) { + return $ret; + } + //$template_text = rawLocale($template_id); // this works for core artifacts only - not for plugins + $template_filename = $this->localFN($template_id); + $template_text = file_get_contents($template_filename); + $template_html = $this->render_text($template_text); + + $mailer = new \Mailer(); + $mailer->to($this->getEmailAddressOnErrorConfigured()); + dbglog("GitBacked - lang check['".$subject_id."']: ".$this->getLang($subject_id)); + dbglog("GitBacked - template text['".$template_id."']: ".$template_text); + dbglog("GitBacked - template html['".$template_id."']: ".$template_html); + $mailer->subject($this->getLang($subject_id)); + $mailer->setBody($template_text, $template_replacements, null, $template_html); + $ret = $mailer->send(); + + return $ret; + } + + /** + * Check, if eMail is to be sent on a Git command error. + * + * @access public + * @return bool + */ + public function isNotifyByEmailOnGitCommandError() { + $emailAddressOnError = $this->getEmailAddressOnErrorConfigured(); + return !empty($emailAddressOnError); + } + + /** + * Get the eMail address configured for notifications. + * + * @access public + * @return string + */ + public function getEmailAddressOnErrorConfigured() { + $emailAddressOnError = trim($this->getConf('emailAddressOnError')); + return $emailAddressOnError; + } } diff --git a/conf/default.php b/conf/default.php index b01c327..8b642e3 100644 --- a/conf/default.php +++ b/conf/default.php @@ -17,3 +17,5 @@ $conf['gitPath'] = ''; $conf['addParams'] = ''; $conf['ignorePaths'] = ''; +$conf['emailAddressOnError'] = ''; +$conf['notifyByMailOnSuccess'] = 0; diff --git a/conf/metadata.php b/conf/metadata.php index b71a89f..8ff5d3a 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -17,3 +17,5 @@ $meta['gitPath'] = array('string'); $meta['addParams'] = array('string'); $meta['ignorePaths'] = array('string'); +$meta['emailAddressOnError'] = array('string'); +$meta['notifyByMailOnSuccess'] = array('onoff'); diff --git a/lang/en/lang.php b/lang/en/lang.php new file mode 100644 index 0000000..6ea8620 --- /dev/null +++ b/lang/en/lang.php @@ -0,0 +1,6 @@ +@GIT_REPO_PATH@ + + * **Current working dir:** @GIT_CWD@ + + * **Command:** @GIT_COMMAND@ + + * **Exitcode:** @GIT_COMMAND_EXITCODE@ + + * **Error:** @GIT_ERROR_MESSAGE@ + + * **User:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/en/mail_command_success.txt b/lang/en/mail_command_success.txt new file mode 100644 index 0000000..6c19a0c --- /dev/null +++ b/lang/en/mail_command_success.txt @@ -0,0 +1,15 @@ +==== The following Git command was performed on @DOKUWIKIURL@ ==== + + * **Repo path:** @GIT_REPO_PATH@ + + * **Current working dir:** @GIT_CWD@ + + * **Command:** @GIT_COMMAND@ + + * **User:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/en/mail_create_new_error.txt b/lang/en/mail_create_new_error.txt new file mode 100644 index 0000000..52590f8 --- /dev/null +++ b/lang/en/mail_create_new_error.txt @@ -0,0 +1,15 @@ +==== The creation of a new Git repo FAILED on @DOKUWIKIURL@ ==== + + * **Repo path:** @GIT_REPO_PATH@ + + * **Reference:** @GIT_REFERENCE@ + + * **Error:** @GIT_ERROR_MESSAGE@ + + * **User:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/en/mail_repo_path_error.txt b/lang/en/mail_repo_path_error.txt new file mode 100644 index 0000000..d44fecb --- /dev/null +++ b/lang/en/mail_repo_path_error.txt @@ -0,0 +1,13 @@ +==== An FAILURE with the Git repo path occurred on @DOKUWIKIURL@ ==== + + * **Repo path:** @GIT_REPO_PATH@ + + * **Error:** @GIT_ERROR_MESSAGE@ + + * **User:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/en/settings.php b/lang/en/settings.php index 4308950..b2bd670 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -13,8 +13,10 @@ $lang['commitPageMsgDel'] = 'Commit message for deleted pages (%user%,%summary%,%page% are replaced by the corresponding values)'; $lang['commitMediaMsg'] = 'Commit message for media files (%user%,%media% are replaced by the corresponding values)'; $lang['commitMediaMsgDel'] = 'Commit message for deleted media files (%user%,%media% are replaced by the corresponding values)'; -$lang['repoPath'] = 'Path of the git repo (e.g. the savedir '.$GLOBALS['conf']['savedir'].')'; +$lang['repoPath'] = 'Path of the git repo(s) (e.g. the savedir '.$GLOBALS['conf']['savedir'].')'; $lang['repoWorkDir'] = 'Path of the git working tree, must contain "pages" and "media" directories (e.g. the savedir '.$GLOBALS['conf']['savedir'].')'; $lang['gitPath'] = 'Path to the git binary (if empty, the default "/usr/bin/git" will be used)'; $lang['addParams'] = 'Additional git parameters (added to the git execution command) (%user% and %mail% are replaced by the corresponding values)'; $lang['ignorePaths'] = 'Paths/files which are ignored and not added to git (comma-separated)'; +$lang['emailAddressOnError'] = 'If set, an eMail will be sent to this address with context details and error message rather than confusing the end user by the Exception raised'; +$lang['notifyByMailOnSuccess'] = 'If emailAddressOnError is defined, an eMail will be sent on any git commit. This is supposed to be used for eMail notification test purposes only'; diff --git a/lib/Git.php b/lib/Git.php index be2f0b6..52c57f7 100644 --- a/lib/Git.php +++ b/lib/Git.php @@ -64,10 +64,11 @@ public static function windows_mode() { * @access public * @param string repository path * @param string directory to source + * @param \action_plugin_gitbacked_editcommit plugin * @return GitRepo */ - public static function &create($repo_path, $source = null) { - return GitRepo::create_new($repo_path, $source); + public static function &create($repo_path, $source = null, \action_plugin_gitbacked_editcommit $plugin = null) { + return GitRepo::create_new($repo_path, $source, $plugin); } /** @@ -77,10 +78,11 @@ public static function &create($repo_path, $source = null) { * * @access public * @param string repository path + * @param \action_plugin_gitbacked_editcommit plugin * @return GitRepo */ - public static function open($repo_path) { - return new GitRepo($repo_path); + public static function open($repo_path, \action_plugin_gitbacked_editcommit $plugin = null) { + return new GitRepo($repo_path, $plugin); } /** @@ -93,10 +95,11 @@ public static function open($repo_path) { * @param string repository path * @param string remote source * @param string reference path + * @param \action_plugin_gitbacked_editcommit plugin * @return GitRepo **/ - public static function &clone_remote($repo_path, $remote, $reference = null) { - return GitRepo::create_new($repo_path, $remote, true, $reference); + public static function &clone_remote($repo_path, $remote, $reference = null, \action_plugin_gitbacked_editcommit $plugin = null) { + return GitRepo::create_new($repo_path, $plugin, $remote, true, $reference); } /** @@ -109,7 +112,7 @@ public static function &clone_remote($repo_path, $remote, $reference = null) { * @return bool */ public static function is_repo($var) { - return (get_class($var) == 'GitRepo'); + return ($var instanceof GitRepo); } } @@ -126,9 +129,16 @@ public static function is_repo($var) { */ class GitRepo { + // This regex will filter a probable password from any string containing a Git URL. + // Limitation: it will work for the first git URL occurrence in a string. + // Used https://regex101.com/ for evaluating! + const REGEX_GIT_URL_FILTER_PWD = "/^(.*)((http:)|(https:))([^:]+)(:[^@]*)?(.*)/im"; + const REGEX_GIT_URL_FILTER_PWD_REPLACE_PATTERN = "$1$2$5$7"; + protected $repo_path = null; protected $bare = false; protected $envopts = array(); + protected ?\action_plugin_gitbacked_editcommit $plugin = null; /** * Create a new git repository @@ -137,19 +147,22 @@ class GitRepo { * * @access public * @param string repository path + * @param \action_plugin_gitbacked_editcommit plugin * @param string directory to source * @param string reference path - * @return GitRepo + * @return GitRepo or null in case of an error */ - public static function &create_new($repo_path, $source = null, $remote_source = false, $reference = null) { + public static function &create_new($repo_path, \action_plugin_gitbacked_editcommit $plugin = null, $source = null, $remote_source = false, $reference = null) { if (is_dir($repo_path) && file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) { - throw new Exception('"'.$repo_path.'" is already a git repository'); + self::handle_create_new_error($repo_path, $reference, '"'.$repo_path.'" is already a git repository', $plugin); + return null; } else { - $repo = new self($repo_path, true, false); + $repo = new self($repo_path, $plugin, true, false); if (is_string($source)) { if ($remote_source) { if (!is_dir($reference) || !is_dir($reference.'/.git')) { - throw new Exception('"'.$reference.'" is not a git repository. Cannot use as reference.'); + self::handle_create_new_error($repo_path, $reference, '"'.$reference.'" is not a git repository. Cannot use as reference.', $plugin); + return null; } else if (strlen($reference)) { $reference = realpath($reference); $reference = "--reference $reference"; @@ -172,10 +185,12 @@ public static function &create_new($repo_path, $source = null, $remote_source = * * @access public * @param string repository path + * @param \action_plugin_gitbacked_editcommit plugin * @param bool create if not exists? * @return void */ - public function __construct($repo_path = null, $create_new = false, $_init = true) { + public function __construct($repo_path = null, \action_plugin_gitbacked_editcommit $plugin = null, $create_new = false, $_init = true) { + $this->plugin = $plugin; if (is_string($repo_path)) { $this->set_repo_path($repo_path, $create_new, $_init); } @@ -215,11 +230,11 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) { $this->run('init'); } } else { - throw new Exception('"'.$repo_path.'" is not a git repository'); + $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a git repository'); } } } else { - throw new Exception('"'.$repo_path.'" is not a directory'); + $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a directory'); } } else { if ($create_new) { @@ -228,10 +243,10 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) { $this->repo_path = $repo_path; if ($_init) $this->run('init'); } else { - throw new Exception('cannot create repository in non-existent directory'); + $this->handle_repo_path_error($repo_path, 'cannot create repository in non-existent directory'); } } else { - throw new Exception('"'.$repo_path.'" does not exist'); + $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" does not exist'); } } } @@ -278,9 +293,10 @@ public function test_git() { * * @access protected * @param string command to run - * @return string + * @return string or null in case of an error */ protected function run_command($command) { + //dbglog("Git->run_command(command=[".$command."])"); $descriptorspec = array( 1 => array('pipe', 'w'), 2 => array('pipe', 'w'), @@ -305,6 +321,7 @@ protected function run_command($command) { $env = array_merge($_ENV, $this->envopts); } $cwd = $this->repo_path; + //dbglog("GitBacked - cwd: [".$cwd."]"); $resource = proc_open($command, $descriptorspec, $pipes, $cwd, $env); $stdout = stream_get_contents($pipes[1]); @@ -314,7 +331,16 @@ protected function run_command($command) { } $status = trim(proc_close($resource)); - if ($status) throw new Exception($stderr); + //dbglog("GitBacked: run_command status: ".$status); + if ($status) { + //dbglog("GitBacked - stderr: [".$stderr."]"); + // Remove a probable password from the Git URL, if the URL is contained in the error message + $error_message = preg_replace($this::REGEX_GIT_URL_FILTER_PWD, $this::REGEX_GIT_URL_FILTER_PWD_REPLACE_PATTERN, $stderr); + //dbglog("GitBacked - error_message: [".$error_message."]"); + $this->handle_command_error($this->repo_path, $cwd, $command, $status, $error_message); + } else { + $this->handle_command_success($this->repo_path, $cwd, $command); + } return $stdout; } @@ -332,6 +358,69 @@ public function run($command) { return $this->run_command(Git::get_bin()." ".$command); } + /** + * Handles error on crate_new + * + * @access protected + * @param string repository path + * @param string error message + * @return void + */ + protected static function handle_create_new_error($repo_path, $reference, $error_message, $plugin) { + if ($plugin instanceof \action_plugin_gitbacked_editcommit) { + $plugin->notify_create_new_error($repo_path, $reference, $error_message); + } + throw new Exception($error_message); + } + + /** + * Handles error on setting the repo path + * + * @access protected + * @param string repository path + * @param string error message + * @return void + */ + protected function handle_repo_path_error($repo_path, $error_message) { + if ($this->plugin instanceof \action_plugin_gitbacked_editcommit) { + $this->plugin->notify_repo_path_error($repo_path, $error_message); + } + throw new Exception($error_message); + } + + /** + * Handles error on git command + * + * @access protected + * @param string repository path + * @param string current working dir + * @param string command line + * @param int exit code of command (status) + * @param string error message + * @return void + */ + protected function handle_command_error($repo_path, $cwd, $command, $status, $error_message) { + if ($this->plugin instanceof \action_plugin_gitbacked_editcommit) { + $this->plugin->notify_command_error($repo_path, $cwd, $command, $status, $error_message); + } + throw new Exception($error_message); + } + + /** + * Handles success on git command + * + * @access protected + * @param string repository path + * @param string current working dir + * @param string command line + * @return void + */ + protected function handle_command_success($repo_path, $cwd, $command) { + if ($this->plugin instanceof \action_plugin_gitbacked_editcommit) { + $this->plugin->notify_command_success($repo_path, $cwd, $command); + } + } + /** * Runs a 'git status' call * diff --git a/plugin.info.txt b/plugin.info.txt index f3ac1ee..08e56b4 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,7 +1,7 @@ base gitbacked -author Wolfgang Gassler (@woolfg), Carsten Teibes (@carstene1ns) +author Wolfgang Gassler (@woolfg), Carsten Teibes (@carstene1ns), Markus Hoffrogge (@mhoffrog) email wolfgang@gassler.org -date 2016-08-14 +date 2021-03-19 name gitbacked plugin desc Pages and Media are stored in Git url https://github.com/woolfg/dokuwiki-plugin-gitbacked From 00aa0913d094e5025aac5d2a8dca95f0c1da2eb9 Mon Sep 17 00:00:00 2001 From: Markus Hoffrogge Date: Thu, 25 Mar 2021 22:37:59 +0100 Subject: [PATCH 2/3] Extension to send error messages to a configurable eMail address - German translations added Signed-off-by: Markus Hoffrogge --- lang/de/lang.php | 6 ++++++ lang/de/mail_command_error.txt | 19 +++++++++++++++++++ lang/de/mail_command_success.txt | 15 +++++++++++++++ lang/de/mail_create_new_error.txt | 15 +++++++++++++++ lang/de/mail_repo_path_error.txt | 13 +++++++++++++ lang/de/settings.php | 22 ++++++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 lang/de/lang.php create mode 100644 lang/de/mail_command_error.txt create mode 100644 lang/de/mail_command_success.txt create mode 100644 lang/de/mail_create_new_error.txt create mode 100644 lang/de/mail_repo_path_error.txt create mode 100644 lang/de/settings.php diff --git a/lang/de/lang.php b/lang/de/lang.php new file mode 100644 index 0000000..cda517a --- /dev/null +++ b/lang/de/lang.php @@ -0,0 +1,6 @@ +@GIT_REPO_PATH@ + + * **Aktuelles Arbeitsverzeichnis:** @GIT_CWD@ + + * **Befehl:** @GIT_COMMAND@ + + * **Exitcode:** @GIT_COMMAND_EXITCODE@ + + * **Fehler:** @GIT_ERROR_MESSAGE@ + + * **Anwender:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/de/mail_command_success.txt b/lang/de/mail_command_success.txt new file mode 100644 index 0000000..65c02d2 --- /dev/null +++ b/lang/de/mail_command_success.txt @@ -0,0 +1,15 @@ +==== Der folgende Git Befehl wurde ausgeführt auf @DOKUWIKIURL@ ==== + + * **Repo Pfad:** @GIT_REPO_PATH@ + + * **Aktuelles Arbeitsverzeichnis:** @GIT_CWD@ + + * **Befehl:** @GIT_COMMAND@ + + * **Anwender:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/de/mail_create_new_error.txt b/lang/de/mail_create_new_error.txt new file mode 100644 index 0000000..d8ae142 --- /dev/null +++ b/lang/de/mail_create_new_error.txt @@ -0,0 +1,15 @@ +==== FEHLER beim Anlegen eines neuen Git Repositories auf @DOKUWIKIURL@ ==== + + * **Repo Pfad:** @GIT_REPO_PATH@ + + * **Referenz:** @GIT_REFERENCE@ + + * **Fehler:** @GIT_ERROR_MESSAGE@ + + * **Anwender:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/de/mail_repo_path_error.txt b/lang/de/mail_repo_path_error.txt new file mode 100644 index 0000000..36387e1 --- /dev/null +++ b/lang/de/mail_repo_path_error.txt @@ -0,0 +1,13 @@ +==== Ein FEHLER mit dem konfigurierten Git Repository Pfad ist aufgetreten auf @DOKUWIKIURL@ ==== + + * **Repo Pfad:** @GIT_REPO_PATH@ + + * **Fehler:** @GIT_ERROR_MESSAGE@ + + * **Anwender:** @NAME@ + + * **eMail:** @MAIL@ + + * **Browser:** @BROWSER@ + + * **Wiki:** @TITLE@ @DOKUWIKIURL@ diff --git a/lang/de/settings.php b/lang/de/settings.php new file mode 100644 index 0000000..a55bc60 --- /dev/null +++ b/lang/de/settings.php @@ -0,0 +1,22 @@ + + */ + +$lang['pushAfterCommit'] = 'Push des aktiven Branch zum remote origin nach jedem commit'; +$lang['periodicPull'] = 'Pull des remote git Repositories alle "periodicMinutes", getriggert von einem http Page Request'; +$lang['periodicMinutes'] = 'Zeitraum (in Minuten) zwischen den periodischen pull requests'; +$lang['commitPageMsg'] = 'Commit Kommentar für Seitenänderungen (%user%,%summary%,%page% werden durch die tatsächlichen Werte ersetzt)'; +$lang['commitPageMsgDel'] = 'Commit Kommentar für gelöschte Seiten (%user%,%summary%,%page% werden durch die tatsächlichen Werte ersetzt)'; +$lang['commitMediaMsg'] = 'Commit Kommentar for media Dateien (%user%,%media% werden durch die tatsächlichen Werte ersetzt)'; +$lang['commitMediaMsgDel'] = 'Commit Kommentar für gelöschte media Dateien (%user%,%media% werden durch die tatsächlichen Werte ersetzt)'; +$lang['repoPath'] = 'Pfad des git repo (z.B. das savedir '.$GLOBALS['conf']['savedir'].')'; +$lang['repoWorkDir'] = 'Pfad des git working tree. Dieser muss die "pages" and "media" Verzeichnisse enthalten (z.B. das savedir '.$GLOBALS['conf']['savedir'].')'; +$lang['gitPath'] = 'Pfad zum git binary (Wenn leer, dann wird der Standard "/usr/bin/git" verwendet)'; +$lang['addParams'] = 'Zusätzliche git Parameter (diese werden dem git Kommando zugefügt) (%user% und %mail% werden durch die tatsächlichen Werte ersetzt)'; +$lang['ignorePaths'] = 'Pfade/Dateien die ignoriert werden und nicht von git archiviert werden sollen (durch Kommata getrennt)'; +$lang['emailAddressOnError'] = 'Wenn definiert, dann wird bei einem Fehler eine eMail an diese Adresse gesendet mit detaillierter Kontext Information und der git Fehlermeldung, anstatt den aktuellen Endanwender mit einer Exception zu verunsichern'; +$lang['notifyByMailOnSuccess'] = 'Wenn emailAddressOnError definiert ist, dann wird bei jedem Commit eine eMail gesendet. Diese Einstellung sollte nur zum Testen der eMail Benachrichtigung aktiviert werden'; From 2e4b7233fb3c84af9c4759ec3e4ac01f98099154 Mon Sep 17 00:00:00 2001 From: Markus Hoffrogge Date: Fri, 26 Mar 2021 23:18:58 +0100 Subject: [PATCH 3/3] Extension to send error messages to a configurable eMail address - Git.php: - improved error handling for more consistent and obvious Exception throwing - lang/en/settings.php, lang/de/settings.php: - improved settings description Signed-off-by: Markus Hoffrogge --- lang/de/settings.php | 2 +- lang/en/settings.php | 2 +- lib/Git.php | 30 ++++++++++++++---------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lang/de/settings.php b/lang/de/settings.php index a55bc60..a0b6c17 100644 --- a/lang/de/settings.php +++ b/lang/de/settings.php @@ -18,5 +18,5 @@ $lang['gitPath'] = 'Pfad zum git binary (Wenn leer, dann wird der Standard "/usr/bin/git" verwendet)'; $lang['addParams'] = 'Zusätzliche git Parameter (diese werden dem git Kommando zugefügt) (%user% und %mail% werden durch die tatsächlichen Werte ersetzt)'; $lang['ignorePaths'] = 'Pfade/Dateien die ignoriert werden und nicht von git archiviert werden sollen (durch Kommata getrennt)'; -$lang['emailAddressOnError'] = 'Wenn definiert, dann wird bei einem Fehler eine eMail an diese Adresse gesendet mit detaillierter Kontext Information und der git Fehlermeldung, anstatt den aktuellen Endanwender mit einer Exception zu verunsichern'; +$lang['emailAddressOnError'] = 'Wenn definiert, dann wird bei einem Fehler eine eMail an diese Adresse(n) gesendet, anstatt den aktuellen Endanwender mit einer Exception zu verunsichern. Mehrere Adressen können durch Kommata getrennt konfiguriert werden'; $lang['notifyByMailOnSuccess'] = 'Wenn emailAddressOnError definiert ist, dann wird bei jedem Commit eine eMail gesendet. Diese Einstellung sollte nur zum Testen der eMail Benachrichtigung aktiviert werden'; diff --git a/lang/en/settings.php b/lang/en/settings.php index b2bd670..3701ec0 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -18,5 +18,5 @@ $lang['gitPath'] = 'Path to the git binary (if empty, the default "/usr/bin/git" will be used)'; $lang['addParams'] = 'Additional git parameters (added to the git execution command) (%user% and %mail% are replaced by the corresponding values)'; $lang['ignorePaths'] = 'Paths/files which are ignored and not added to git (comma-separated)'; -$lang['emailAddressOnError'] = 'If set, an eMail will be sent to this address with context details and error message rather than confusing the end user by the Exception raised'; +$lang['emailAddressOnError'] = 'If set, in case of a git error an eMail will be sent to this address rather than confusing the end user by the Exception raised. Multiple mail addresses can be configured comma separated'; $lang['notifyByMailOnSuccess'] = 'If emailAddressOnError is defined, an eMail will be sent on any git commit. This is supposed to be used for eMail notification test purposes only'; diff --git a/lib/Git.php b/lib/Git.php index 52c57f7..d09535f 100644 --- a/lib/Git.php +++ b/lib/Git.php @@ -154,15 +154,13 @@ class GitRepo { */ public static function &create_new($repo_path, \action_plugin_gitbacked_editcommit $plugin = null, $source = null, $remote_source = false, $reference = null) { if (is_dir($repo_path) && file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) { - self::handle_create_new_error($repo_path, $reference, '"'.$repo_path.'" is already a git repository', $plugin); - return null; + throw new Exception(self::handle_create_new_error($repo_path, $reference, '"'.$repo_path.'" is already a git repository', $plugin)); } else { $repo = new self($repo_path, $plugin, true, false); if (is_string($source)) { if ($remote_source) { if (!is_dir($reference) || !is_dir($reference.'/.git')) { - self::handle_create_new_error($repo_path, $reference, '"'.$reference.'" is not a git repository. Cannot use as reference.', $plugin); - return null; + throw new Exception(self::handle_create_new_error($repo_path, $reference, '"'.$reference.'" is not a git repository. Cannot use as reference.', $plugin)); } else if (strlen($reference)) { $reference = realpath($reference); $reference = "--reference $reference"; @@ -230,11 +228,11 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) { $this->run('init'); } } else { - $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a git repository'); + throw new Exception($this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a git repository')); } } } else { - $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a directory'); + throw new Exception($this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a directory')); } } else { if ($create_new) { @@ -243,10 +241,10 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) { $this->repo_path = $repo_path; if ($_init) $this->run('init'); } else { - $this->handle_repo_path_error($repo_path, 'cannot create repository in non-existent directory'); + throw new Exception($this->handle_repo_path_error($repo_path, 'cannot create repository in non-existent directory')); } } else { - $this->handle_repo_path_error($repo_path, '"'.$repo_path.'" does not exist'); + throw new Exception($this->handle_repo_path_error($repo_path, '"'.$repo_path.'" does not exist')); } } } @@ -337,7 +335,7 @@ protected function run_command($command) { // Remove a probable password from the Git URL, if the URL is contained in the error message $error_message = preg_replace($this::REGEX_GIT_URL_FILTER_PWD, $this::REGEX_GIT_URL_FILTER_PWD_REPLACE_PATTERN, $stderr); //dbglog("GitBacked - error_message: [".$error_message."]"); - $this->handle_command_error($this->repo_path, $cwd, $command, $status, $error_message); + throw new Exception($this->handle_command_error($this->repo_path, $cwd, $command, $status, $error_message)); } else { $this->handle_command_success($this->repo_path, $cwd, $command); } @@ -359,18 +357,18 @@ public function run($command) { } /** - * Handles error on crate_new + * Handles error on create_new * * @access protected * @param string repository path * @param string error message - * @return void + * @return string error message */ protected static function handle_create_new_error($repo_path, $reference, $error_message, $plugin) { if ($plugin instanceof \action_plugin_gitbacked_editcommit) { $plugin->notify_create_new_error($repo_path, $reference, $error_message); } - throw new Exception($error_message); + return $error_message; } /** @@ -379,13 +377,13 @@ protected static function handle_create_new_error($repo_path, $reference, $error * @access protected * @param string repository path * @param string error message - * @return void + * @return string error message */ protected function handle_repo_path_error($repo_path, $error_message) { if ($this->plugin instanceof \action_plugin_gitbacked_editcommit) { $this->plugin->notify_repo_path_error($repo_path, $error_message); } - throw new Exception($error_message); + return $error_message; } /** @@ -397,13 +395,13 @@ protected function handle_repo_path_error($repo_path, $error_message) { * @param string command line * @param int exit code of command (status) * @param string error message - * @return void + * @return string error message */ protected function handle_command_error($repo_path, $cwd, $command, $status, $error_message) { if ($this->plugin instanceof \action_plugin_gitbacked_editcommit) { $this->plugin->notify_command_error($repo_path, $cwd, $command, $status, $error_message); } - throw new Exception($error_message); + return $error_message; } /**