Skip to content
Snippets Groups Projects
Commit a3228846 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

fixes #20

parent bb253b68
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** final class IssuesController extends TracToGitlab\EventController
* @property Parsedown $parsedown
*/
final class IssuesController extends \TracToGitlab\Controller
{ {
const SECRET_ISSUE_OPEN = 'N]<d5V/6tn/sYNMy'; const SECRET_ISSUE_OPEN = 'N]<d5V/6tn/sYNMy';
...@@ -17,35 +14,36 @@ final class IssuesController extends \TracToGitlab\Controller ...@@ -17,35 +14,36 @@ final class IssuesController extends \TracToGitlab\Controller
throw new MethodNotAllowedException(); throw new MethodNotAllowedException();
} }
if ($_SERVER['HTTP_X_GITLAB_TOKEN'] !== self::SECRET_ISSUE_OPEN) { if (!$this->verifySecret(self::SECRET_ISSUE_OPEN)) {
throw new AccessDeniedException(); throw new AccessDeniedException();
} }
$input = file_get_contents('php://input'); if ($this->getFromPayload('object_attributes', 'action') === 'open') {
$payload = json_decode($input, true); $username = $this->getFromPayload('user', 'username');
$email = $this->getFromPayload('user', 'email');
if ($payload['object_attributes']['action'] === 'open') { $user = User::findOneByUsername($username)
$user = User::findOneByUsername($payload['user']['username']) ?? User::findOneByEmail($email)
?? User::findOneByEmail($payload['user']['email'])
?? User::findOneByUsername('gitlab-bot'); ?? User::findOneByUsername('gitlab-bot');
if ($user) { if ($user) {
$labels = $this->getFromPayload('labels');
foreach (self::LABEL_MAPPING as $label => $definition) { foreach (self::LABEL_MAPPING as $label => $definition) {
if (!$this->hasLabel($payload, $label)) { if (!$this->hasLabel($labels, $label)) {
continue; continue;
} }
$title = sprintf( $title = sprintf(
$definition[2], $definition[2],
$payload['object_attributes']['iid'], $this->getFromPayload('object_attributes', 'iid'),
$payload['object_attributes']['title'] $this->getFromPayload('object_attributes', 'title')
); );
$body = Studip\Markup::markAsHtml( $body = Studip\Markup::markAsHtml(
$this->parsedown->text( $this->parsedown->text(
sprintf( sprintf(
"%s\n\n----\nZum [gitlab-Issue #%u](%s)", "%s\n\n----\nZum [gitlab-Issue #%u](%s)",
trim($payload['object_attributes']['description']), $this->getFromPayload('object_attributes', 'description'),
$payload['object_attributes']['iid'], $this->getFromPayload('object_attributes', 'iid'),
$payload['object_attributes']['url'] $this->getFromPayload('object_attributes', 'url')
) )
) )
); );
...@@ -59,18 +57,19 @@ final class IssuesController extends \TracToGitlab\Controller ...@@ -59,18 +57,19 @@ final class IssuesController extends \TracToGitlab\Controller
); );
$this->gitlab->issues()->addNote( $this->gitlab->issues()->addNote(
$payload['project']['id'], $this->getFromPayload('project', 'id'),
$payload['object_attributes']['iid'], $this->getFromPayload('object_attributes', 'iid'),
"[Zum Forenbeitrag auf dem Entwicklungsserver]({$url})" "[Zum Forenbeitrag auf dem Entwicklungsserver]({$url})"
); } );
}
} }
} }
$this->render_nothing(); $this->render_nothing();
} }
private function hasLabel(array $change, string $needle): bool private function hasLabel(array $labels, string $needle): bool
{ {
foreach ($change['labels'] as $label) { foreach ($labels as $label) {
if ($label['title'] === $needle) { if ($label['title'] === $needle) {
return true; return true;
} }
......
<?php
final class UsersController extends TracToGitlab\EventController
{
const SECRET_USER_CREATED = 'q<-%Y7Ys>h(@LHgg';
public function created_action()
{
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
if (
!$this->verifySecret(self::SECRET_USER_CREATED)
|| !$this->verifyEventType('System Hook')
) {
throw new AccessDeniedException();
}
if ($this->getFromPayload('event_name') === 'user_create') {
$this->gitlab->projects()->addMember(
$this->gitlabProjectId,
$this->getFromPayload('user_id'),
10
);
}
$this->render_nothing();
}
}
<?php
namespace TracToGitlab;
abstract class EventController extends Controller
{
private $payload = null;
protected function verifySecret(string $secret): bool
{
return $_SERVER['HTTP_X_GITLAB_TOKEN'] === $secret;
}
protected function verifyEventType(string $type): bool
{
return $_SERVER['HTTP_X_GITLAB_EVENT'] === $type;
}
protected function getFromPayload(...$keys)
{
if ($this->payload === null) {
$this->payload = $this->getPayloadFromRequest() ?? false;
}
if ($this->payload === false) {
throw new \Exception('No payload detected');
}
$result = $this->payload;
foreach ($keys as $key) {
if (!isset($result[$key])) {
throw new \Exception("Invalid payload key {$key}");
}
$result = $result[$key];
}
return $result;
}
private function getPayloadFromRequest(): ?array
{
$input =file_get_contents('php://input');
if (!$input) {
return null;
}
$payload = json_decode($input, true);
if (!$payload || !is_array($payload)) {
return null;
}
return $payload;
}
}
pluginname=Trac to gitlab converter pluginname=Trac to gitlab converter
pluginclassname=TracToGitlabPlugin pluginclassname=TracToGitlabPlugin
origin=UOL origin=UOL
version=1.1 version=1.1.1
studipMinVersion=5.0 studipMinVersion=5.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment