Select Git revision
issues.php
Jan-Hendrik Willms authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
issues.php 3.75 KiB
<?php
/**
* @property Parsedown $parsedown
*/
final class IssuesController extends \TracToGitlab\Controller
{
const SECRET_ISSUE_OPEN = 'N]<d5V/6tn/sYNMy';
const LABEL_MAPPING = [
'TIC' => ['1927f2b86d6b185aa6c6697810ad42f1', 'a65dd807699b7b3b3d182b674042b70e', 'TIC #%u: %s'],
'StEP' => ['1927f2b86d6b185aa6c6697810ad42f1', '93c84607e687030b249a69cd30ca7bd9', 'StEP%05u: %s'],
];
public function create_action()
{
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
if ($_SERVER['HTTP_X_GITLAB_TOKEN'] !== self::SECRET_ISSUE_OPEN) {
throw new AccessDeniedException();
}
$input = file_get_contents('php://input');
$payload = json_decode($input, true);
if ($payload['object_attributes']['action'] === 'open') {
$user = User::findOneByUsername($payload['user']['username'])
?? User::findOneByEmail($payload['user']['email'])
?? User::findOneByUsername('gitlab-bot');
if ($user) {
foreach (self::LABEL_MAPPING as $label => $definition) {
if (!$this->hasLabel($payload, $label)) {
continue;
}
$title = sprintf(
$definition[2],
$payload['object_attributes']['iid'],
$payload['object_attributes']['title']
);
$body = Studip\Markup::markAsHtml(
$this->parsedown->text(
sprintf(
"%s\n\n----\nZum [gitlab-Issue #%u](%s)",
trim($payload['object_attributes']['description']),
$payload['object_attributes']['iid'],
$payload['object_attributes']['url']
)
)
);
$url = $this->createForumEntry(
$user,
$title,
$body,
$definition[0],
$definition[1]
);
$this->gitlab->issues()->addNote(
$payload['project']['id'],
$payload['object_attributes']['iid'],
"[Zum Forenbeitrag auf dem Entwicklungsserver]({$url})"
); }
}
}
$this->render_nothing();
}
private function hasLabel(array $change, string $needle): bool
{
foreach ($change['labels'] as $label) {
if ($label['title'] === $needle) {
return true;
}
}
return false;
}
/**
* @return string Absolute URL to forum posting
*/
private function createForumEntry(\User $user, string $subject, string $body, string $course_id, string $parent_id)
{
require_once "{$GLOBALS['STUDIP_BASE_PATH']}/public/plugins_packages/core/Forum/models/ForumEntry.php";
$id = md5(uniqid('forum-entry', true));
\ForumEntry::insert([
'topic_id' => $id,
'seminar_id' => $course_id,
'user_id' => $user->id,
'name' => $subject,
'content' => $body,
'author' => $user->getFullName(),
'author_host' => '',
], $parent_id);
$old_base = \URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
$url = \PluginEngine::getURL(
'CoreForum',
['cid' => $course_id],
"index/index/{$id}#{$id}"
);
\URLHelper::setBaseURL($old_base);
return $url;
}
}