Skip to content
Snippets Groups Projects
Commit 6d531441 authored by Stefan Galinski's avatar Stefan Galinski :video_game:
Browse files

[FEATURE] Update app to use the latest libs incl. the Symfony 6 mailer. At...

[FEATURE] Update app to use the latest libs incl. the Symfony 6 mailer. At least 4 years of missing updates.
parent ade1ed65
No related branches found
No related tags found
No related merge requests found
......@@ -181,7 +181,9 @@ class GeneralUtility {
$users = array_unique($users);
foreach ($users as $user) {
$userData = $this->getUser($user);
$recipients[$userData['email']] = $userData['name'];
if ($userData['state'] === 'active') {
$recipients[$userData['email']] = $userData['name'];
}
}
return (!count($recipients)) ? $fallbackRecipient : $recipients;
}
......@@ -198,7 +200,7 @@ class GeneralUtility {
try {
/** @var Projects $projects */
$projects = $this->client->api('projects');
$projects = $this->client->projects();
/** @var array $members */
$members = $projects->members($id);
} catch (RuntimeException $e) {
......@@ -218,7 +220,7 @@ class GeneralUtility {
try {
/** @var Users $users */
$users = $this->client->api('users');
$users = $this->client->users();
/**
* Since commit 039883d the gitlab api php framework
* uses pagination. And there is no flag to fetch all. So
......@@ -245,7 +247,7 @@ class GeneralUtility {
try {
/** @var Users $users */
$users = $this->client->api('users');
$users = $this->client->users();
$userData = $users->show($id);
} catch (RuntimeException $e) {
error_log('Unable to get user data:' . $e->getMessage());
......@@ -265,7 +267,7 @@ class GeneralUtility {
try {
/** @var Users $users */
$users = $this->client->api('users');
$users = $this->client->users();
$userData = $users->emails($id);
} catch (RuntimeException $e) {
error_log('Unable to get user email: ' . $e->getMessage());
......@@ -286,7 +288,7 @@ class GeneralUtility {
try {
/** @var Groups $groups */
$groups = $this->client->api('groups');
$groups = $this->client->groups();
/** @var array $groupData */
$groupData = $groups->members($groupId);
} catch (RuntimeException $e) {
......@@ -333,4 +335,4 @@ class GeneralUtility {
public function setClient(Client $client) {
$this->client = $client;
}
}
\ No newline at end of file
}
......@@ -31,11 +31,12 @@ use Gitlab\Client;
use Gitlab\Exception\RuntimeException;
use Gitlab\HttpClient\Listener\AuthListener;
use SGalinski\Utility\GeneralUtility;
use Swift_Attachment;
use Swift_Mailer;
use Swift_Message;
use Swift_SendmailTransport;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Mime\Address;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
use Twig_Environment;
use Twig_Loader_Filesystem;
......@@ -101,8 +102,9 @@ class Webhook {
$authenticationToken = ($configuration['authToken'] ? $configuration['authToken'] : NULL);
$sudo = ($configuration['sudo'] ? $configuration['sudo'] : NULL);
$this->apiClient = $apiClient = \Gitlab\Client::create($this->settings['Configuration']['Gitlab']['apiUrl'])
->authenticate($authenticationToken, \Gitlab\Client::AUTH_URL_TOKEN, $sudo);
$this->apiClient = $apiClient = new \Gitlab\Client();
$this->apiClient->setUrl($this->settings['Configuration']['Gitlab']['apiUrl']);
$this->apiClient->authenticate($authenticationToken, \Gitlab\Client::AUTH_HTTP_TOKEN, $sudo);
$utility = new GeneralUtility($this->settings, $apiClient);
$branchName = str_replace('refs/heads/', '', $this->request['ref']);
......@@ -112,7 +114,7 @@ class Webhook {
try {
/** @var Projects $projects */
$projects = $apiClient->api('projects');
$projects = $apiClient->projects();
/** @var array $members */
$projectData = $projects->show($projectId);
} catch (RuntimeException $e) {
......@@ -140,37 +142,30 @@ class Webhook {
$mailBody = $this->includeStyleSheets($mailBody, 'Resources/StyleSheets/Mail.css');
// Mail Handling
$transport = new Swift_SendmailTransport('/usr/sbin/sendmail -bs');
$mailer = new Swift_Mailer($transport);
$transport = Transport::fromDsn('native://default');
//$transport = new SendmailTransport(' -t -f' . $this->settings['Configuration']['MailConfiguration']['from']);
//$transport = new SendmailTransport(' -t');
$mailer = new Mailer($transport);
$commitMessageAsArray = explode(chr(10), $commit['message']);
$subject = 'Commit: ' . $projectData['name_with_namespace'] . ' / ' .
$branchName . ' ' . $commitMessageAsArray[0];
/** @var Swift_Message $message */
$message = (new Swift_Message($subject))
->setFrom($this->settings['Configuration']['MailConfiguration']['from'])
->setBody($mailBody, 'text/html')
->setCharset('utf-8');
$email = (new Email())
->from($this->settings['Configuration']['MailConfiguration']['from'])
->subject($subject)
->html($mailBody);
// Send the message
$failedRecipients = [];
$recipients = $utility->getRecipientsForProject($projectData);
$debugRecipient = [$this->settings['Configuration']['Debug']['debugMailAdresse']];
$recipients = $this->debugMode ? $debugRecipient : $recipients;
// attach diffs if it is configured
$attachtmentConfiguration = $this->settings['Configuration']['MailConfiguration']['attachment'];
if ($attachtmentConfiguration['diffAsHtml'] === TRUE) {
$htmlAttatchment = $this->crawlCommitDiffAsHtml($commit);
$message->attach($htmlAttatchment);
}
if ($attachtmentConfiguration['diffAsFile'] === TRUE) {
$diffAttatchment = $this->crawlCommitDiffAsFile($commit);
$message->attach($diffAttatchment);
}
foreach ($recipients as $mail => $name) {
filter_var($name, FILTER_VALIDATE_EMAIL) ? $message->setTo($name) : $message->setTo([$mail => $name]);
$mailer->send($message, $failedRecipients);
if (filter_var($name, FILTER_VALIDATE_EMAIL)) {
$email->to(new Address($name));
} else {
$email->to(new Address($mail, $name));
}
$mailer->send($email);
}
// log commit id to prevent sending mails twice
......@@ -178,23 +173,6 @@ class Webhook {
}
}
/**
* The method downloads the diff file of the given commit and the diff as an Swift_Attachment instance.
*
* @param array $commit
* @return Swift_Attachment
*/
protected function crawlCommitDiffAsFile($commit) {
$diff = $this->crawlCommitDiffContent($commit);
$attachment = (new Swift_Attachment())
->setFilename($commit['id'] . '.diff')
->setContentType('text/plain')
->setBody(($diff !== '') ? $diff : '');
return $attachment;
}
/**
* The method downloads the diff file of the given commit and return the content as string.
*
......@@ -236,39 +214,6 @@ class Webhook {
return (self::$diffCache[$commit['id']] !== '') ? self::$diffCache[$commit['id']] : '';
}
/**
* The method crawl the DOM of the commit page for the file diffs and concatenate the markup for creating an
* HTML file. The HTML file gets inline stylesheets and will be returned as an Swift_Attachment instance.
*
* @param array $commit
* @return Swift_Attachment
*/
protected function crawlCommitDiffAsHtml($commit) {
error_log('Feature not working anymore');
$html = '<html><body>';
$content = '';
$commitUrl = $commit['url'] . '?private_token=' . $this->settings['Configuration']['Gitlab']['authToken'];
$content = file_get_contents($commitUrl);
/** @var Crawler $crawler */
$crawler = new Crawler();
$crawler->addContent($content);
foreach ($crawler->filter('.files') as $node) {
$html .= $node->ownerDocument->saveHTML($node);
}
$html .= '</body></html>';
$message = $this->includeStyleSheets($html, 'Resources/StyleSheets/Diff.css');
$attachment = (new Swift_Attachment())
->setFilename($commit['id'] . '.html')
->setContentType('text/html')
->setBody($message);
return $attachment;
}
/**
* Method includes stylesheets as inline css to the given markup.
*
......@@ -325,4 +270,4 @@ class Webhook {
public function getRequest() {
return $this->request;
}
}
\ No newline at end of file
}
{
"name": "sgalinski/webhooks",
"description": "Webhook library for Gitlab",
"config": {
"optimize-autoloader": true,
"platform": {
"php": "7.4.99"
}
},
"require": {
"m4tthumphrey/php-gitlab-api": "^9.9",
"m4tthumphrey/php-gitlab-api": "^11.8",
"guzzlehttp/guzzle": "^7.4",
"http-interop/http-factory-guzzle": "^1.2",
"symfony/dom-crawler": "^4.2",
"symfony/yaml": "^4.2",
"symfony/css-selector": "^4.2",
"swiftmailer/swiftmailer": "^6.0",
"twig/twig": "^2.5",
"tijsverkoyen/css-to-inline-styles": "^2.2"
"tijsverkoyen/css-to-inline-styles": "^2.2",
"symfony/mailer": "^5.4"
},
"keywords": ["gitlab"],
"license": "MIT",
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment