Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 158 additions & 15 deletions action/editcommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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() {
Expand All @@ -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()));
Expand Down Expand Up @@ -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;
}

}

Expand Down
2 changes: 2 additions & 0 deletions conf/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
$conf['gitPath'] = '';
$conf['addParams'] = '';
$conf['ignorePaths'] = '';
$conf['emailAddressOnError'] = '';
$conf['notifyByMailOnSuccess'] = 0;
2 changes: 2 additions & 0 deletions conf/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
$meta['gitPath'] = array('string');
$meta['addParams'] = array('string');
$meta['ignorePaths'] = array('string');
$meta['emailAddressOnError'] = array('string');
$meta['notifyByMailOnSuccess'] = array('onoff');
6 changes: 6 additions & 0 deletions lang/de/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$lang['mail_create_new_error_subject'] = 'gitbacked plugin: FEHLER beim Anlegen eines neuen Git Repositories';
$lang['mail_repo_path_error_subject'] = 'gitbacked plugin: Ein FEHLER mit dem konfigurierten Pfad zum Git Repository ist aufgetreten';
$lang['mail_command_error_subject'] = 'gitbacked plugin: Ein Git Befehl ist FEHLGESCHLAGEN';
$lang['mail_command_success_subject'] = 'gitbacked plugin: Ein Git Befehl wurde ausgeführt';
19 changes: 19 additions & 0 deletions lang/de/mail_command_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
==== Der folgende Git Befehl ist FEHLGESCHLAGEN auf @DOKUWIKIURL@ ====

* **Repo Pfad:** <code>@GIT_REPO_PATH@</code>

* **Aktuelles Arbeitsverzeichnis:** <code>@GIT_CWD@</code>

* **Befehl:** <code>@GIT_COMMAND@</code>

* **Exitcode:** <code>@GIT_COMMAND_EXITCODE@</code>

* **Fehler:** <code>@GIT_ERROR_MESSAGE@</code>

* **Anwender:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
15 changes: 15 additions & 0 deletions lang/de/mail_command_success.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
==== Der folgende Git Befehl wurde ausgeführt auf @DOKUWIKIURL@ ====

* **Repo Pfad:** <code>@GIT_REPO_PATH@</code>

* **Aktuelles Arbeitsverzeichnis:** <code>@GIT_CWD@</code>

* **Befehl:** <code>@GIT_COMMAND@</code>

* **Anwender:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
15 changes: 15 additions & 0 deletions lang/de/mail_create_new_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
==== FEHLER beim Anlegen eines neuen Git Repositories auf @DOKUWIKIURL@ ====

* **Repo Pfad:** <code>@GIT_REPO_PATH@</code>

* **Referenz:** <code>@GIT_REFERENCE@</code>

* **Fehler:** <code>@GIT_ERROR_MESSAGE@</code>

* **Anwender:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
13 changes: 13 additions & 0 deletions lang/de/mail_repo_path_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
==== Ein FEHLER mit dem konfigurierten Git Repository Pfad ist aufgetreten auf @DOKUWIKIURL@ ====

* **Repo Pfad:** <code>@GIT_REPO_PATH@</code>

* **Fehler:** <code>@GIT_ERROR_MESSAGE@</code>

* **Anwender:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
22 changes: 22 additions & 0 deletions lang/de/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* German strings for the gitbacked plugin
*
* @author Markus Hoffrogge <mhoffrogge@gmail.com>
*/

$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 <code>savedir</code> '.$GLOBALS['conf']['savedir'].')';
$lang['repoWorkDir'] = 'Pfad des git working tree. Dieser muss die "pages" and "media" Verzeichnisse enthalten (z.B. das <code>savedir</code> '.$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(n) gesendet, anstatt den aktuellen Endanwender mit einer Exception zu verunsichern. Mehrere Adressen können durch Kommata getrennt konfiguriert werden';
$lang['notifyByMailOnSuccess'] = 'Wenn <code>emailAddressOnError</code> definiert ist, dann wird bei jedem Commit eine eMail gesendet. Diese Einstellung sollte nur zum Testen der eMail Benachrichtigung aktiviert werden';
6 changes: 6 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$lang['mail_create_new_error_subject'] = 'gitbacked plugin: The creation of a new Git repo FAILED';
$lang['mail_repo_path_error_subject'] = 'gitbacked plugin: A FAILURE with the Git repo path occurred';
$lang['mail_command_error_subject'] = 'gitbacked plugin: A Git command FAILED';
$lang['mail_command_success_subject'] = 'gitbacked plugin: A Git command was performed';
19 changes: 19 additions & 0 deletions lang/en/mail_command_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
==== The following Git command FAILED on @DOKUWIKIURL@ ====

* **Repo path:** <code>@GIT_REPO_PATH@</code>

* **Current working dir:** <code>@GIT_CWD@</code>

* **Command:** <code>@GIT_COMMAND@</code>

* **Exitcode:** <code>@GIT_COMMAND_EXITCODE@</code>

* **Error:** <code>@GIT_ERROR_MESSAGE@</code>

* **User:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
15 changes: 15 additions & 0 deletions lang/en/mail_command_success.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
==== The following Git command was performed on @DOKUWIKIURL@ ====

* **Repo path:** <code>@GIT_REPO_PATH@</code>

* **Current working dir:** <code>@GIT_CWD@</code>

* **Command:** <code>@GIT_COMMAND@</code>

* **User:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
15 changes: 15 additions & 0 deletions lang/en/mail_create_new_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
==== The creation of a new Git repo FAILED on @DOKUWIKIURL@ ====

* **Repo path:** <code>@GIT_REPO_PATH@</code>

* **Reference:** <code>@GIT_REFERENCE@</code>

* **Error:** <code>@GIT_ERROR_MESSAGE@</code>

* **User:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
13 changes: 13 additions & 0 deletions lang/en/mail_repo_path_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
==== An FAILURE with the Git repo path occurred on @DOKUWIKIURL@ ====

* **Repo path:** <code>@GIT_REPO_PATH@</code>

* **Error:** <code>@GIT_ERROR_MESSAGE@</code>

* **User:** <code>@NAME@</code>

* **eMail:** <code>@MAIL@</code>

* **Browser:** <code>@BROWSER@</code>

* **Wiki:** <code>@TITLE@ @DOKUWIKIURL@</code>
4 changes: 3 additions & 1 deletion lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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 <code>emailAddressOnError</code> is defined, an eMail will be sent on any git commit. This is supposed to be used for eMail notification test purposes only';
Loading