Skip to content

Commit

Permalink
Update project update endpoint to use repo name
Browse files Browse the repository at this point in the history
  • Loading branch information
Programie committed Oct 5, 2023
1 parent 4018a25 commit 916c6df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ services:
- 8080:80
environment:
USE_CACHE: 'false'
GITLAB_TOKEN: test
PROJECT_UPDATE_TOKEN: test
2 changes: 1 addition & 1 deletion httpdocs/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$router->map("GET", "/projects/[applications|minecraft-plugins|php-libraries:category]", [ProjectsController::class, "listProjectsOfCategory"]);
$router->map("GET", "/projects/[noslash:name]", [ProjectsController::class, "redirectToRepoReadme"]);
$router->map("GET", "/projects/[noslash:name]/cover-image.jpg", [ProjectsController::class, "getCoverImage"]);
$router->map("POST", "/projects/[noslash:name]/update", [ProjectsController::class, "update"]);
$router->map("POST", "/update-project", [ProjectsController::class, "update"]);

$match = $router->match();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,35 @@ public function getCoverImage($params): void
readfile($path);
}

public function update($params): string
public function update(): string
{
$headers = getallheaders();
if ($headers === false) {
throw new ForbiddenException;
}

if (!isset($headers["X-Gitlab-Token"])) {
if (!isset($headers["X-Update-Token"])) {
throw new ForbiddenException;
}

$requiredToken = getenv("GITLAB_TOKEN");
$requiredToken = getenv("PROJECT_UPDATE_TOKEN");
if (!$requiredToken) {
// $requiredToken is null, false, empty string, ...
throw new ForbiddenException;
}

if ($headers["X-Gitlab-Token"] !== $requiredToken) {
if ($headers["X-Update-Token"] !== $requiredToken) {
throw new ForbiddenException;
}

$project = $this->projects->byName($params["name"]);
$repository = $_POST["repository"] ?? null;
if (!$repository) {
throw new ForbiddenException;
}

$repository = explode("/", $repository)[1];

$project = $this->projects->byRepository($repository);
if ($project === null) {
throw new NotFoundException;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/php/com/selfcoders/website/model/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ public function byName(string $name): ?Project
return null;
}

/**
* @param string $repository
* @return Project|null
*/
public function byRepository(string $repository): ?Project
{
/**
* @var $project Project
*/
foreach ($this as $project) {
if ($project->repoName === $repository) {
return $project;
}
}

return null;
}

public function latest(int $limit): self
{
$projects = clone $this;
Expand Down

0 comments on commit 916c6df

Please sign in to comment.