Skip to content

Commit dec34fc

Browse files
authored
Merge pull request #151 from RayLabsHQ/148-release-order-fix-for-same-created-date
fix: Sort releases by published_at instead of created_at
2 parents 3857f2f + f5727da commit dec34fc

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/lib/gitea.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
import {
2-
and,
3-
eq,
4-
} from 'drizzle-orm';
5-
6-
import type { Config } from '@/types/config';
7-
import { membershipRoleEnum } from '@/types/organizations';
8-
import {
2+
repoStatusEnum,
93
type RepositoryVisibility,
104
type RepoStatus,
11-
repoStatusEnum,
12-
} from '@/types/Repository';
13-
import { Octokit } from '@octokit/rest';
14-
15-
import {
16-
db,
17-
organizations,
18-
repositories,
19-
} from './db';
20-
import type {
21-
Organization,
22-
Repository,
23-
} from './db/schema';
24-
import { createMirrorJob } from './helpers';
25-
import {
26-
httpDelete,
27-
httpGet,
28-
httpPatch,
29-
httpPost,
30-
httpPut,
31-
} from './http-client';
5+
} from "@/types/Repository";
6+
import { membershipRoleEnum } from "@/types/organizations";
7+
import { Octokit } from "@octokit/rest";
8+
import type { Config } from "@/types/config";
9+
import type { Organization, Repository } from "./db/schema";
10+
import { httpPost, httpGet, httpDelete, httpPut, httpPatch } from "./http-client";
11+
import { createMirrorJob } from "./helpers";
12+
import { db, organizations, repositories } from "./db";
13+
import { eq, and } from "drizzle-orm";
14+
import { decryptConfigTokens } from "./utils/config-encryption";
15+
import { formatDateShort } from "./utils";
3216
import {
3317
parseRepositoryMetadataState,
3418
serializeRepositoryMetadataState,
35-
} from './metadata-state';
36-
import { formatDateShort } from './utils';
37-
import { decryptConfigTokens } from './utils/config-encryption';
19+
} from "./metadata-state";
3820

3921
/**
4022
* Helper function to get organization configuration including destination override
@@ -2011,7 +1993,11 @@ export async function mirrorGitHubReleasesToGitea({
20111993
let skippedCount = 0;
20121994

20131995
const getReleaseTimestamp = (release: typeof releases.data[number]) => {
2014-
const sourceDate = release.created_at ?? release.published_at ?? "";
1996+
// Use published_at first (when the release was published on GitHub)
1997+
// Fall back to created_at (when the git tag was created) only if published_at is missing
1998+
// This matches GitHub's sorting behavior and handles cases where multiple tags
1999+
// point to the same commit but have different publish dates
2000+
const sourceDate = release.published_at ?? release.created_at ?? "";
20152001
const timestamp = sourceDate ? new Date(sourceDate).getTime() : 0;
20162002
return Number.isFinite(timestamp) ? timestamp : 0;
20172003
};
@@ -2023,10 +2009,14 @@ export async function mirrorGitHubReleasesToGitea({
20232009
.slice(0, releaseLimit)
20242010
.sort((a, b) => getReleaseTimestamp(a) - getReleaseTimestamp(b));
20252011

2026-
console.log(`[Releases] Processing ${releasesToProcess.length} releases in chronological order (oldest to newest)`);
2012+
console.log(`[Releases] Processing ${releasesToProcess.length} releases in chronological order (oldest to newest by published date)`);
20272013
releasesToProcess.forEach((rel, idx) => {
2028-
const date = new Date(rel.published_at || rel.created_at);
2029-
console.log(`[Releases] ${idx + 1}. ${rel.tag_name} - Originally published: ${date.toISOString()}`);
2014+
const publishedDate = new Date(rel.published_at || rel.created_at);
2015+
const createdDate = new Date(rel.created_at);
2016+
const dateInfo = rel.published_at !== rel.created_at
2017+
? `published ${publishedDate.toISOString()} (tag created ${createdDate.toISOString()})`
2018+
: `published ${publishedDate.toISOString()}`;
2019+
console.log(`[Releases] ${idx + 1}. ${rel.tag_name} - ${dateInfo}`);
20302020
});
20312021

20322022
// Check if existing releases in Gitea are in the wrong order
@@ -2116,9 +2106,21 @@ export async function mirrorGitHubReleasesToGitea({
21162106

21172107
// Prepare release body with GitHub original date header
21182108
const githubPublishedDate = release.published_at || release.created_at;
2119-
const githubDateHeader = githubPublishedDate
2120-
? `> 📅 **Originally published on GitHub:** ${new Date(githubPublishedDate).toUTCString()}\n\n`
2121-
: '';
2109+
const githubTagCreatedDate = release.created_at;
2110+
2111+
let githubDateHeader = '';
2112+
if (githubPublishedDate) {
2113+
githubDateHeader = `> 📅 **Originally published on GitHub:** ${new Date(githubPublishedDate).toUTCString()}`;
2114+
2115+
// If the tag was created on a different date than the release was published,
2116+
// show both dates (helps with repos that create multiple tags from the same commit)
2117+
if (release.published_at && release.created_at && release.published_at !== release.created_at) {
2118+
githubDateHeader += `\n> 🏷️ **Git tag created:** ${new Date(githubTagCreatedDate).toUTCString()}`;
2119+
}
2120+
2121+
githubDateHeader += '\n\n';
2122+
}
2123+
21222124
const originalReleaseNote = release.body || "";
21232125
const releaseNote = githubDateHeader + originalReleaseNote;
21242126

@@ -2894,4 +2896,4 @@ export async function archiveGiteaRepo(
28942896
console.log(`[Archive] Repository ${owner}/${repo} data is preserved but not marked as archived`);
28952897
// Don't throw - we want cleanup to continue for other repos
28962898
}
2897-
}
2899+
}

0 commit comments

Comments
 (0)