Skip to content
Open
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
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ Sources: [GitHub action](.github/actions/commit/action.yml).

### Public read API and Atom feeds

The read-only JSON API at `/api/v1` requires no authentication or token, except for who follows whom. Read a public profile at `/api/v1/users/{handle}`, that user's commits at `/api/v1/users/{handle}/commits`, or one commit at `/api/v1/commits/{commit}`. `/api/v1/users/{handle}/followers` and `/following` list people the way the profile's people lists do, newest follow first, with each person's lane and whether you follow each other, and `/api/v1/users/{handle}/following/organizations` lists the organizations they follow the same way, with whether you follow each one; like on the web, they are for members only and need a [signed request](#signed-api-and-devices). Add `?replies=1` to the user-commits endpoint to read their replies instead of top-level commits. Lists use cursor pagination: 20 commits, or 30 people or organizations, per page. Only public approved, non-deleted content is returned, not private profile data such as email. Unsigned requests are limited to **60 requests per minute per IP** and cannot create posts, follow, or star; terminal posting and the [signed API](#signed-api-and-devices) are separate signed interfaces.
The read-only JSON API at `/api/v1` requires no authentication or token, except for who follows whom. Read a public profile at `/api/v1/users/{handle}`, that user's commits at `/api/v1/users/{handle}/commits`, or one commit at `/api/v1/commits/{commit}`. `/api/v1/users/{handle}/followers` and `/following` list people the way the profile's people lists do, newest follow first, with each person's lane and whether you follow each other, and `/api/v1/users/{handle}/following/organizations` lists the organizations they follow the same way, with whether you follow each one; like on the web, they are for members only and need a [signed request](#signed-api-and-devices). Add `?replies=1` to the user-commits endpoint to read their replies instead of top-level commits. Lists use cursor pagination: 20 commits, or 30 people or organizations, per page. `/api/v1/changelog` lists the commits on `production` three days at a time, like the Changelog page: days split in `?timezone=`, or a signed user's saved timezone, and `?before=` takes the `older_before` it returns; it answers 503 when GitHub can't be reached. Only public approved, non-deleted content is returned, not private profile data such as email. Unsigned requests are limited to **60 requests per minute per IP** and cannot create posts, follow, or star; terminal posting and the [signed API](#signed-api-and-devices) are separate signed interfaces.

Subscribe in a feed reader to `/@{handle}/feed.atom` for a profile's latest **50 approved top-level commits**, including image enclosures. Feeds are public and can be cached for five minutes.

Expand Down
3 changes: 2 additions & 1 deletion app/Enums/ApiErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum ApiErrorCode: string
case GitHubIssueRefused = 'github_issue_refused';
case GitHubIssueUnanswered = 'github_issue_unanswered';
case GuildPaused = 'guild_paused';
case ChangelogUnavailable = 'changelog_unavailable';

public function status(): int
{
Expand All @@ -37,7 +38,7 @@ public function status(): int
self::PasteisExhausted, self::GuildPaused => 422,
self::RateLimited => 429,
self::GitHubIssueRefused, self::GitHubIssueUnanswered => 502,
self::GitHubSyncFailed => 503,
self::GitHubSyncFailed, self::ChangelogUnavailable => 503,
};
}
}
8 changes: 8 additions & 0 deletions app/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public static function gitHubIssueUnanswered(): self
return new self(ApiErrorCode::GitHubIssueUnanswered, __('GitHub did not respond. Check the upstream issues before trying again. Your draft is safe.'));
}

/**
* GitHub could not be read, so the changelog has nothing to list until it can.
*/
public static function changelogUnavailable(): self
{
return new self(ApiErrorCode::ChangelogUnavailable, __("GitHub can't be reached right now, so the changelog is unavailable. Try again in a moment."));
}

/**
* The user is banned, so the only thing left for them is deleting their account.
*/
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Controllers/Api/ChangelogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Exceptions\ApiException;
use App\Http\Requests\ListApiChangelogRequest;
use App\Queries\ChangelogQuery;
use Illuminate\Http\JsonResponse;

final readonly class ChangelogController
{
/**
* What shipped to production, three days at a time, as the web's changelog lists it.
*/
public function index(ListApiChangelogRequest $request, ChangelogQuery $changelog): JsonResponse
{
$days = $changelog->get($request->timeZone(), $request->before());

if ($days === null) {
throw ApiException::changelogUnavailable();
}

return response()->json([
'data' => $days['commits'],
'older_before' => $days['olderBefore'],
'commits_url' => 'https://github.com/'.config()->string('services.github.repository').'/commits/'.ChangelogQuery::Branch,
]);
}
}
47 changes: 47 additions & 0 deletions app/Http/Requests/ListApiChangelogRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Http\FormRequest;

final class ListApiChangelogRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @return array<string, array<int, string>>
*/
public function rules(): array
{
return [
'timezone' => ['nullable', 'string', 'timezone:all'],
'before' => ['nullable', 'date'],
];
}

/**
* The time zone that decides where one day of merges ends: the one asked for, else the signed user's.
*/
public function timeZone(): string
{
$user = $this->user();
$saved = $user instanceof User ? $user->timezone ?? null : null;

return $this->string('timezone')->value() ?: $saved ?? config()->string('app.timezone');
}

/**
* Where the reader got to, to list the days merged before it.
*/
public function before(): ?CarbonImmutable
{
return $this->date('before')?->toImmutable();
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\ApiException;
use App\Http\Controllers\Api\BroadcastAuthorizationController;
use App\Http\Controllers\Api\CallController;
use App\Http\Controllers\Api\ChangelogController;
use App\Http\Controllers\Api\CommitController as ApiCommitController;
use App\Http\Controllers\Api\CommitPastelController;
use App\Http\Controllers\Api\CommitPinController;
Expand Down Expand Up @@ -140,6 +141,8 @@
->name('api-organization-commit.index');
Route::get('organizations/{handle}/mentions', [OrganizationMentionController::class, 'index'])
->name('api-organization-mention.index');
Route::get('changelog', [ChangelogController::class, 'index'])
->name('api-changelog.index');

// Mentioned profiles: the user or organization a commit's or message's mention captured, by the id it carries,
// as the web's mention links open them, so they still lead there once they rename...
Expand Down
99 changes: 99 additions & 0 deletions tests/Feature/Controllers/Api/ChangelogControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

use App\Contracts\Services\GitHubProvider;
use App\Models\User;
use Illuminate\Http\Client\ConnectionException;

/**
* @return array<string, mixed>
*/
function apiChangelogCommit(int $number, string $committedAt, ?int $parent): array
{
return [
'sha' => "c897f867{$number}",
'message' => "feat: turn emoji shortcodes into emoji in remotes (#{$number})",
'url' => "https://github.com/dev-null-sh/dev-null.sh/commit/c897f867{$number}",
'committed_at' => $committedAt,
'parents' => $parent === null ? [] : ["c897f867{$parent}"],
'author' => ['id' => 14284867, 'login' => 'xHeaven', 'avatar_url' => 'https://avatars.githubusercontent.com/u/1?v=4'],
];
}

function mockApiChangelog(): void
{
test()->mock(GitHubProvider::class)->shouldReceive('commits')
->andReturnUsing(fn (string $repository, string $branch, int $page): array => $page === 1 ? [
apiChangelogCommit(377, '2026-10-17T23:30:00Z', 376),
apiChangelogCommit(376, '2026-10-16T12:00:00Z', 375),
apiChangelogCommit(375, '2026-10-15T12:00:00Z', 374),
apiChangelogCommit(374, '2026-10-14T12:00:00Z', null),
] : []);
}

it('lists the latest days of commits on production, signed or not, with a link to every commit on production', function (bool $isSigned): void {
mockApiChangelog();
$uri = route('api-changelog.index', ['timezone' => 'UTC']);

$response = $isSigned ? signedApiRequest(apiKeyPairFor(User::factory()->create()), 'GET', $uri) : $this->getJson($uri);

$response->assertOk()
->assertJsonCount(3, 'data')
->assertJsonPath('data.0.sha', 'c897f867377')
->assertJsonPath('data.0.pull_request.number', 377)
->assertJsonPath('data.0.type', 'feat')
->assertJsonPath('data.0.title', 'Turn emoji shortcodes into emoji in remotes')
->assertJsonPath('data.0.author.login', 'xHeaven')
->assertJsonPath('older_before', '2026-10-15T00:00:00+00:00')
->assertJsonPath('commits_url', 'https://github.com/dev-null-sh/dev-null.sh/commits/production');
})->with(['unsigned' => false, 'signed' => true]);

it('groups days in the reader’s time zone, and carries on from where the reader is', function (): void {
mockApiChangelog();

$this->getJson(route('api-changelog.index', ['timezone' => 'Europe/Budapest']))
->assertJsonCount(3, 'data')
->assertJsonPath('data.2.pull_request.number', 375);

$this->getJson(route('api-changelog.index', ['timezone' => 'UTC', 'before' => '2026-10-15T00:00:00+00:00']))
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.pull_request.number', 374)
->assertJsonPath('older_before', null);
});

it('uses the signed user’s time zone when the request names none', function (): void {
mockApiChangelog();
$keyPair = apiKeyPairFor(User::factory()->create(['timezone' => 'Europe/Budapest']));

signedApiRequest($keyPair, 'GET', route('api-changelog.index'))
->assertJsonPath('older_before', '2026-10-14T22:00:00+00:00');

signedApiRequest($keyPair, 'GET', route('api-changelog.index', ['timezone' => 'UTC']))
->assertJsonPath('older_before', '2026-10-15T00:00:00+00:00');
});

it('falls back to the app’s time zone for a signed user who saved none', function (): void {
mockApiChangelog();
config(['app.timezone' => 'Europe/Budapest']);

signedApiRequest(apiKeyPairFor(User::factory()->create(['timezone' => null])), 'GET', route('api-changelog.index'))
->assertJsonPath('older_before', '2026-10-14T22:00:00+00:00');
});

it('rejects a time zone or a starting point it cannot read', function (array $query, string $field): void {
$this->getJson(route('api-changelog.index', $query))
->assertUnprocessable()
->assertJsonValidationErrors($field);
})->with([
'unknown time zone' => [['timezone' => 'Mars/Olympus'], 'timezone'],
'not a date' => [['timezone' => 'UTC', 'before' => 'yesterday-ish'], 'before'],
]);

it('says the changelog is unavailable when GitHub cannot be reached', function (): void {
$this->mock(GitHubProvider::class)->shouldReceive('commits')->andThrow(new ConnectionException('timed out'));

$this->getJson(route('api-changelog.index', ['timezone' => 'UTC']))
->assertServiceUnavailable()
->assertJsonPath('code', 'changelog_unavailable');
});