From 9cb7661e8315dde345457572126a46b953b3db1a Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Sat, 28 Nov 2026 12:37:36 +0100 Subject: [PATCH 1/5] feat: compare mailing-list mirror refs The mirror workflow needs to know whether lore has published a newer tip before it can be triggered on demand. Compare the refs advertised by lore.kernel.org and the configured GitHub mirror so a caller can make that decision without fetching the mail archive. --- GitGitGadget/mailing-list-mirror.js | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 GitGitGadget/mailing-list-mirror.js diff --git a/GitGitGadget/mailing-list-mirror.js b/GitGitGadget/mailing-list-mirror.js new file mode 100644 index 0000000..4f82b50 --- /dev/null +++ b/GitGitGadget/mailing-list-mirror.js @@ -0,0 +1,65 @@ +const https = require('https') + +const getRef = async (url, ref) => { + const { hostname, pathname } = new URL(url) + const requestPath = `${pathname.replace(/\/$/, '')}/info/refs?service=git-upload-pack` + const body = await new Promise((resolve, reject) => { + const request = https.request({ + hostname, + method: 'GET', + path: requestPath, + headers: { + 'Git-Protocol': 'version=1', + 'User-Agent': 'GitGitGadget/1.0' + } + }, response => { + const chunks = [] + response.on('data', chunk => chunks.push(chunk)) + response.on('error', reject) + response.on('end', () => { + if (response.statusCode !== 200) { + reject(new Error(`Could not read ${url}: HTTP ${response.statusCode}`)) + return + } + resolve(Buffer.concat(chunks).toString('utf8')) + }) + }) + request.on('error', reject) + request.setTimeout(30_000, () => request.destroy(new Error(`Timed out reading ${url}`))) + request.end() + }) + + let offset = 0 + while (offset + 4 <= body.length) { + const length = Number.parseInt(body.slice(offset, offset + 4), 16) + if (length === 0) { + offset += 4 + continue + } + if (!Number.isInteger(length) || length < 4 || offset + length > body.length) break + + const line = body.slice(offset + 4, offset + length).split('\0', 1)[0].trim() + const match = line.match(/^([0-9a-f]{40,64})\s+(.+)$/) + if (match && match[2] === ref) return match[1] + offset += length + } + + throw new Error(`Could not find ${ref} in ${url}`) +} + +const isUpToDate = async config => { + if (!config.mailrepo.mirrorURL) return true + + const epoch = config.mailrepo.public_inbox_epoch || 1 + const sourceURL = new URL(`${epoch}/`, config.mailrepo.url).href + const mirrorURL = new URL(config.mailrepo.mirrorURL) + if (!mirrorURL.pathname.endsWith('.git')) mirrorURL.pathname += '.git' + + const [sourceOID, mirrorOID] = await Promise.all([ + getRef(sourceURL, `refs/heads/${config.mailrepo.branch}`), + getRef(mirrorURL.href, config.mailrepo.mirrorRef || `refs/heads/lore-${epoch}`) + ]) + return sourceOID === mirrorOID +} + +module.exports = { isUpToDate } From 9ab3e2253c2023c203a73f8897f0d1e241f4042b Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Sat, 28 Nov 2026 12:37:46 +0100 Subject: [PATCH 2/5] feat: poll lore for mailing-list updates The GitHub Actions mirror schedule is best-effort and can start much later than its five-minute interval. Check lore and the GitHub mirror once a minute, and dispatch the existing sync workflow only when their refs differ. Skip a dispatch while a sync is queued or running so repeated timer ticks do not create redundant work. --- GitGitGadget/sync-mailing-list-mirror.js | 34 ++++++++++++++++++++++++ sync-mailing-list-mirror/function.json | 10 +++++++ sync-mailing-list-mirror/index.js | 3 +++ 3 files changed, 47 insertions(+) create mode 100644 GitGitGadget/sync-mailing-list-mirror.js create mode 100644 sync-mailing-list-mirror/function.json create mode 100644 sync-mailing-list-mirror/index.js diff --git a/GitGitGadget/sync-mailing-list-mirror.js b/GitGitGadget/sync-mailing-list-mirror.js new file mode 100644 index 0000000..43d0bcd --- /dev/null +++ b/GitGitGadget/sync-mailing-list-mirror.js @@ -0,0 +1,34 @@ +const { isUpToDate } = require('./mailing-list-mirror') + +module.exports = async context => { + try { + const { readFileSync } = require('fs') + const config = JSON.parse(readFileSync(`${__dirname}/gitgitgadget-config.json`)) + if (await isUpToDate(config)) { + context.log('Mailing-list mirror is up to date') + return + } + + const { owner, name } = config.workflowsRepo + const workflow = 'sync-mailing-list-mirror.yml' + const { getInstallationIdForRepo } = require('./get-installation-id-for-repo') + const { getInstallationAccessToken } = require('./get-installation-access-token') + const installationID = await getInstallationIdForRepo(context, owner, name) + const token = await getInstallationAccessToken(context, installationID) + const { listWorkflowRuns, triggerWorkflowDispatch } = require('./trigger-workflow-dispatch') + + for (const status of ['queued', 'in_progress']) { + const runs = await listWorkflowRuns(context, token, owner, name, workflow, 'main', status) + if (runs.length) { + context.log(`Skipping mirror sync; a run is already ${status}`) + return + } + } + + const run = await triggerWorkflowDispatch(context, token, owner, name, workflow, 'main') + context.log(`Triggered mailing-list mirror sync: ${run.html_url}`) + } catch (e) { + context.log('Caught exception ', e) + throw e + } +} diff --git a/sync-mailing-list-mirror/function.json b/sync-mailing-list-mirror/function.json new file mode 100644 index 0000000..d257407 --- /dev/null +++ b/sync-mailing-list-mirror/function.json @@ -0,0 +1,10 @@ +{ + "bindings": [ + { + "name": "timer", + "type": "timerTrigger", + "direction": "in", + "schedule": "0 */1 * * * *" + } + ] +} diff --git a/sync-mailing-list-mirror/index.js b/sync-mailing-list-mirror/index.js new file mode 100644 index 0000000..635dced --- /dev/null +++ b/sync-mailing-list-mirror/index.js @@ -0,0 +1,3 @@ +const syncMailingListMirror = require('../GitGitGadget/sync-mailing-list-mirror') + +module.exports = async context => syncMailingListMirror(context) From d09c3a0ed583fd72b45fa5786c56c68c8dcbae14 Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Sat, 28 Nov 2026 12:37:46 +0100 Subject: [PATCH 3/5] docs: explain low-latency mirror polling Describe the timer, its one-minute polling interval, and the existing scheduled fallback so operators know how mirror updates are triggered and what still affects delivery time. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e4f54a8..7af7662 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ Finally, [run the Function locally](https://learn.microsoft.com/en-us/azure/azur You can also run/debug it via VS Code, there is a default configuration called "Attach to Node Functions". +### Keeping the mailing-list mirror current + +The `sync-mailing-list-mirror` timer checks lore.kernel.org and the GitHub mirror once a minute. It dispatches the existing mirror workflow only when their advertised refs differ and skips the dispatch while another mirror run is queued or running. The workflow's five-minute schedule remains as a fallback. + +The timer uses the existing `AzureWebJobsStorage` setting for its schedule and lock. Delivery time still depends on lore.kernel.org publishing the message and Azure running the timer. + ## How to set up this GitHub App This process looks a bit complex, the main reason for that being that three things have to be set up essentially simultaneously: an Azure Function, a GitHub repository and a GitHub App. From 1434822864228716135d42d6af140054070e8770 Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Sat, 28 Nov 2026 12:52:24 +0100 Subject: [PATCH 4/5] fix: validate mailing-list mirror refs The source and mirror may be configured with or without trailing slashes, and neither ref should be read over an unencrypted connection. Normalize their URLs before requesting the Git protocol advertisement, and isolate its packet-line parser so malformed or missing refs fail clearly. --- GitGitGadget/mailing-list-mirror.js | 52 ++++++++++++++++++----------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/GitGitGadget/mailing-list-mirror.js b/GitGitGadget/mailing-list-mirror.js index 4f82b50..047471d 100644 --- a/GitGitGadget/mailing-list-mirror.js +++ b/GitGitGadget/mailing-list-mirror.js @@ -1,5 +1,27 @@ const https = require('https') +const parseRefAdvertisement = (body, ref, url) => { + let offset = 0 + while (offset + 4 <= body.length) { + const packetHeader = body.slice(offset, offset + 4) + if (!/^[0-9a-f]{4}$/i.test(packetHeader)) break + + const length = Number.parseInt(packetHeader, 16) + if (length === 0 || length === 1 || length === 2) { + offset += 4 + continue + } + if (length < 4 || offset + length > body.length) break + + const line = body.slice(offset + 4, offset + length).split('\0', 1)[0].trim() + const match = line.match(/^([0-9a-f]{40,64})\s+(.+)$/) + if (match && match[2] === ref) return match[1] + offset += length + } + + throw new Error(`Could not find ${ref} in ${url}`) +} + const getRef = async (url, ref) => { const { hostname, pathname } = new URL(url) const requestPath = `${pathname.replace(/\/$/, '')}/info/refs?service=git-upload-pack` @@ -29,37 +51,29 @@ const getRef = async (url, ref) => { request.end() }) - let offset = 0 - while (offset + 4 <= body.length) { - const length = Number.parseInt(body.slice(offset, offset + 4), 16) - if (length === 0) { - offset += 4 - continue - } - if (!Number.isInteger(length) || length < 4 || offset + length > body.length) break - - const line = body.slice(offset + 4, offset + length).split('\0', 1)[0].trim() - const match = line.match(/^([0-9a-f]{40,64})\s+(.+)$/) - if (match && match[2] === ref) return match[1] - offset += length - } - - throw new Error(`Could not find ${ref} in ${url}`) + return parseRefAdvertisement(body, ref, url) } const isUpToDate = async config => { if (!config.mailrepo.mirrorURL) return true const epoch = config.mailrepo.public_inbox_epoch || 1 - const sourceURL = new URL(`${epoch}/`, config.mailrepo.url).href + const sourceURL = new URL(config.mailrepo.url) + sourceURL.pathname = `${sourceURL.pathname.replace(/\/+$/, '')}/${epoch}/` + sourceURL.search = '' + sourceURL.hash = '' const mirrorURL = new URL(config.mailrepo.mirrorURL) + if (sourceURL.protocol !== 'https:' || mirrorURL.protocol !== 'https:') { + throw new Error('Mailing-list source and mirror URLs must use HTTPS') + } + mirrorURL.pathname = mirrorURL.pathname.replace(/\/+$/, '') if (!mirrorURL.pathname.endsWith('.git')) mirrorURL.pathname += '.git' const [sourceOID, mirrorOID] = await Promise.all([ - getRef(sourceURL, `refs/heads/${config.mailrepo.branch}`), + getRef(sourceURL.href, `refs/heads/${config.mailrepo.branch}`), getRef(mirrorURL.href, config.mailrepo.mirrorRef || `refs/heads/lore-${epoch}`) ]) return sourceOID === mirrorOID } -module.exports = { isUpToDate } +module.exports = { isUpToDate, parseRefAdvertisement } From 406183d139f228761350e261b78d41e2a2dfd349 Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Sat, 28 Nov 2026 12:52:24 +0100 Subject: [PATCH 5/5] test: cover mailing-list mirror polling The timer must avoid unnecessary GitHub API calls when the mirror is current and avoid duplicate sync runs while work is pending. Cover ref parsing and comparison, transport failures, and the dispatch decisions so these paths remain reviewable as the polling behavior changes. --- __tests__/mailing-list-mirror.test.js | 125 +++++++++++++++++++++ __tests__/sync-mailing-list-mirror.test.js | 82 ++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 __tests__/mailing-list-mirror.test.js create mode 100644 __tests__/sync-mailing-list-mirror.test.js diff --git a/__tests__/mailing-list-mirror.test.js b/__tests__/mailing-list-mirror.test.js new file mode 100644 index 0000000..e2162ae --- /dev/null +++ b/__tests__/mailing-list-mirror.test.js @@ -0,0 +1,125 @@ +jest.mock('https') +const https = require('https') +const { isUpToDate, parseRefAdvertisement } = require('../GitGitGadget/mailing-list-mirror') +const EventEmitter = require('events') + +let responses + +https.request = jest.fn((options, callback) => { + const { body, statusCode = 200 } = responses.shift() + const request = new EventEmitter() + request.setTimeout = jest.fn() + request.end = () => { + const response = new EventEmitter() + response.statusCode = statusCode + callback(response) + response.emit('data', Buffer.from(body)) + response.emit('end') + } + return request +}) + +const packet = line => `${(Buffer.byteLength(line) + 4).toString(16).padStart(4, '0')}${line}` + +const advertisement = (refs, capabilities = 'multi_ack side-band') => [ + packet('# service=git-upload-pack\n'), + '0000', + packet('version 1\n'), + ...refs.map(([oid, ref], index) => packet(`${oid} ${ref}${index === 0 ? `\0${capabilities}` : ''}\n`)), + '0000' +].join('') + +const makeConfig = () => ({ + mailrepo: { + url: 'https://lore.kernel.org/git', + branch: 'master', + public_inbox_epoch: 1, + mirrorURL: 'https://github.com/gitgitgadget/git-mailing-list-mirror/', + mirrorRef: 'refs/heads/lore-1' + } +}) + +beforeEach(() => { + jest.clearAllMocks() + responses = [] +}) + +test('read a ref from a Git protocol v1 advertisement', () => { + const head = 'a'.repeat(40) + const master = 'b'.repeat(40) + const mirror = 'c'.repeat(40) + const body = advertisement([ + [head, 'HEAD'], + [master, 'refs/heads/master'], + [mirror, 'refs/heads/lore-1'] + ]) + + expect(parseRefAdvertisement(body, 'refs/heads/lore-1', 'https://example.com/repo.git')).toBe(mirror) +}) + +test('reject an advertisement without the requested ref', () => { + const body = advertisement([['a'.repeat(40), 'refs/heads/master']]) + + expect(() => parseRefAdvertisement(body, 'refs/heads/lore-1', 'https://example.com/repo.git')) + .toThrow('Could not find refs/heads/lore-1') +}) + +test('reject a malformed packet advertisement', () => { + expect(() => parseRefAdvertisement('zzzz', 'refs/heads/master', 'https://example.com/repo.git')) + .toThrow('Could not find refs/heads/master') +}) + +test('compare the lore and mirror refs when URLs lack or have trailing slashes', async () => { + const source = 'a'.repeat(40) + responses = [ + { body: advertisement([[source, 'refs/heads/master']]) }, + { body: advertisement([[source, 'refs/heads/lore-1']]) } + ] + + await expect(isUpToDate(makeConfig())).resolves.toBe(true) + + expect(https.request.mock.calls.map(([options]) => [options.hostname, options.path])).toEqual([ + ['lore.kernel.org', '/git/1/info/refs?service=git-upload-pack'], + ['github.com', '/gitgitgadget/git-mailing-list-mirror.git/info/refs?service=git-upload-pack'] + ]) + for (const [options] of https.request.mock.calls) { + expect(options.headers['Git-Protocol']).toBe('version=1') + expect(options.headers['User-Agent']).toBe('GitGitGadget/1.0') + } +}) + +test('report when the GitHub mirror is behind lore', async () => { + responses = [ + { body: advertisement([['a'.repeat(40), 'refs/heads/master']]) }, + { body: advertisement([['b'.repeat(40), 'refs/heads/lore-1']]) } + ] + + await expect(isUpToDate(makeConfig())).resolves.toBe(false) +}) + +test('fail when either ref advertisement cannot be fetched', async () => { + responses = [ + { body: 'Forbidden', statusCode: 403 }, + { body: advertisement([['a'.repeat(40), 'refs/heads/lore-1']]) } + ] + + await expect(isUpToDate(makeConfig())).rejects.toThrow('HTTP 403') +}) + +test('reject non-HTTPS source and mirror URLs', async () => { + const config = makeConfig() + config.mailrepo.mirrorURL = 'http://github.com/gitgitgadget/git-mailing-list-mirror' + + await expect(isUpToDate(config)).rejects.toThrow('must use HTTPS') + + expect(https.request).not.toHaveBeenCalled() +}) + +test('skip ref polling when no GitHub mirror is configured', async () => { + const config = makeConfig() + delete config.mailrepo.mirrorURL + + await expect(isUpToDate(config)).resolves.toBe(true) + + expect(https.request).not.toHaveBeenCalled() +}) diff --git a/__tests__/sync-mailing-list-mirror.test.js b/__tests__/sync-mailing-list-mirror.test.js new file mode 100644 index 0000000..691d74f --- /dev/null +++ b/__tests__/sync-mailing-list-mirror.test.js @@ -0,0 +1,82 @@ +const mockIsUpToDate = jest.fn() +const mockGetInstallationIdForRepo = jest.fn() +const mockGetInstallationAccessToken = jest.fn() +const mockListWorkflowRuns = jest.fn() +const mockTriggerWorkflowDispatch = jest.fn() + +jest.mock('../GitGitGadget/mailing-list-mirror', () => ({ isUpToDate: mockIsUpToDate })) +jest.mock('../GitGitGadget/get-installation-id-for-repo', () => ({ + getInstallationIdForRepo: mockGetInstallationIdForRepo +})) +jest.mock('../GitGitGadget/get-installation-access-token', () => ({ + getInstallationAccessToken: mockGetInstallationAccessToken +})) +jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({ + listWorkflowRuns: mockListWorkflowRuns, + triggerWorkflowDispatch: mockTriggerWorkflowDispatch +})) + +const syncMailingListMirror = require('../GitGitGadget/sync-mailing-list-mirror') + +const makeContext = () => ({ log: jest.fn() }) + +beforeEach(() => { + jest.clearAllMocks() + mockIsUpToDate.mockResolvedValue(true) + mockGetInstallationIdForRepo.mockResolvedValue(17) + mockGetInstallationAccessToken.mockResolvedValue('installation-token') + mockListWorkflowRuns.mockResolvedValue([]) + mockTriggerWorkflowDispatch.mockResolvedValue({ html_url: 'https://github.com/run/1' }) +}) + +test('does not dispatch a sync when the mirror is current', async () => { + const context = makeContext() + + await syncMailingListMirror(context) + + expect(mockGetInstallationIdForRepo).not.toHaveBeenCalled() + expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled() + expect(context.log).toHaveBeenCalledWith('Mailing-list mirror is up to date') +}) + +test('dispatches a sync when lore is ahead and no run is active', async () => { + const context = makeContext() + mockIsUpToDate.mockResolvedValue(false) + + await syncMailingListMirror(context) + + expect(mockListWorkflowRuns).toHaveBeenCalledTimes(2) + expect(mockTriggerWorkflowDispatch).toHaveBeenCalledWith( + context, + 'installation-token', + 'gitgitgadget-workflows', + 'gitgitgadget-workflows', + 'sync-mailing-list-mirror.yml', + 'main' + ) +}) + +test.each(['queued', 'in_progress'])('does not dispatch while a sync is %s', async status => { + const context = makeContext() + mockIsUpToDate.mockResolvedValue(false) + mockListWorkflowRuns.mockImplementation(async (_context, _token, _owner, _repo, _workflow, _branch, queriedStatus) => { + return queriedStatus === status ? [{ status }] : [] + }) + + await syncMailingListMirror(context) + + expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled() + expect(context.log).toHaveBeenCalledWith(`Skipping mirror sync; a run is already ${status}`) +}) + +test('propagates a failure to read the mailing-list refs', async () => { + const context = makeContext() + const error = new Error('lore is unavailable') + mockIsUpToDate.mockRejectedValue(error) + + await expect(syncMailingListMirror(context)).rejects.toThrow('lore is unavailable') + + expect(mockGetInstallationIdForRepo).not.toHaveBeenCalled() + expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled() + expect(context.log).toHaveBeenCalledWith('Caught exception ', error) +})