Skip to content

Commit d6f7400

Browse files
committed
fix: map GitHub IDs for manual credit additions
1 parent c8db9e5 commit d6f7400

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎src/extensions/endpoints/admin-sponsors/src/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { EventContext } from '@directus/types';
44
import { isAxiosError } from 'axios';
55
import type { Request, RequestHandler, Router } from 'express';
66
import Joi from 'joi';
7+
import { redirectGithubId } from '../../../lib/src/add-credits.js';
78
import { asyncWrapper } from '../../../lib/src/async-wrapper.js';
89
import { getGithubApiClient } from '../../../lib/src/github-api-client.js';
910
import { validate } from '../../../lib/src/middlewares/validate.js';
@@ -177,10 +178,11 @@ export const createAdminSponsorsEndpoint = (queryService: QueryService, githubLo
177178
const accountability = (req as Request & { accountability: EventContext['accountability'] }).accountability;
178179
const input = req.body as ManualAdditionInput;
179180
const isPayment = input.type === 'payment';
181+
const githubId = redirectGithubId(input.githubId);
180182
let githubLogin: string;
181183

182184
try {
183-
githubLogin = await githubLoginResolver(input.githubId, context);
185+
githubLogin = await githubLoginResolver(githubId, context);
184186
} catch (error) {
185187
const status = isAxiosError(error) ? error.response?.status : undefined;
186188

@@ -189,13 +191,13 @@ export const createAdminSponsorsEndpoint = (queryService: QueryService, githubLo
189191
return;
190192
}
191193

192-
context.logger.error({ githubId: input.githubId, status }, 'GitHub account lookup failed.');
194+
context.logger.error({ githubId, status }, 'GitHub account lookup failed.');
193195
res.status(503).send('GitHub account lookup is temporarily unavailable.');
194196
return;
195197
}
196198

197199
await context.database('gp_credits_additions').insert({
198-
github_id: input.githubId,
200+
github_id: githubId,
199201
amount: input.credits,
200202
reason: isPayment ? 'one_time_sponsorship' : 'other',
201203
meta: JSON.stringify(isPayment

‎src/extensions/endpoints/admin-sponsors/src/queries.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Knex } from 'knex';
2-
import { SOURCE_ID_TO_TARGET_ID } from '../../../lib/src/add-credits.js';
2+
import { redirectGithubId, SOURCE_ID_TO_TARGET_ID } from '../../../lib/src/add-credits.js';
33
import type {
44
AccountsQuery,
55
EventsQuery,
@@ -112,7 +112,7 @@ export const applySearch = (query: Knex.QueryBuilder, search: string | undefined
112112
const value = `%${search}%`;
113113
query.where((builder) => {
114114
if (/^\d+$/.test(search)) {
115-
builder.where('additions.github_id', search);
115+
builder.where('additions.github_id', redirectGithubId(search));
116116
} else {
117117
builder.where('additions.github_id', 'like', value);
118118
}
@@ -131,7 +131,7 @@ export const applyManualSearch = (query: Knex.QueryBuilder, search: string | und
131131
const value = `%${search}%`;
132132
query.where((builder) => {
133133
if (/^\d+$/.test(search)) {
134-
builder.where('additions.github_id', search);
134+
builder.where('additions.github_id', redirectGithubId(search));
135135
} else {
136136
builder.where('additions.github_id', 'like', value);
137137
}

‎src/extensions/endpoints/admin-sponsors/test/queries.test.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sinon from 'sinon';
44
import { applyManualSearch, applySearch, fillChartPoints, normalizeManualAddition, normalizeSponsorAccount, normalizeSponsorshipEvent, utcMonthSql } from '../src/queries.js';
55

66
describe('sponsor query normalization', () => {
7-
it('matches numeric GitHub IDs exactly while searching event usernames', () => {
7+
it('matches mapped numeric GitHub IDs exactly while searching event usernames', () => {
88
const builder = {
99
where: sinon.stub().returnsThis(),
1010
orWhere: sinon.stub().returnsThis(),
@@ -13,15 +13,15 @@ describe('sponsor query normalization', () => {
1313

1414
const where = sinon.spy((callback: (builder: Knex.QueryBuilder) => void) => callback(builder as unknown as Knex.QueryBuilder));
1515

16-
applySearch({ where } as unknown as Knex.QueryBuilder, '123');
16+
applySearch({ where } as unknown as Knex.QueryBuilder, '66716858');
1717

18-
expect(builder.where.calledOnceWithExactly('additions.github_id', '123')).to.equal(true);
19-
expect(builder.orWhere.calledWithExactly('current_sponsors.github_login', 'like', '%123%')).to.equal(true);
20-
expect(builder.orWhere.calledWithExactly('directus_users.github_username', 'like', '%123%')).to.equal(true);
21-
expect(builder.orWhereRaw.calledWithExactly(`NULLIF(JSON_UNQUOTE(JSON_EXTRACT(additions.meta, '$.githubLogin')), '') LIKE ?`, [ '%123%' ])).to.equal(true);
18+
expect(builder.where.calledOnceWithExactly('additions.github_id', '6209808')).to.equal(true);
19+
expect(builder.orWhere.calledWithExactly('current_sponsors.github_login', 'like', '%66716858%')).to.equal(true);
20+
expect(builder.orWhere.calledWithExactly('directus_users.github_username', 'like', '%66716858%')).to.equal(true);
21+
expect(builder.orWhereRaw.calledWithExactly(`NULLIF(JSON_UNQUOTE(JSON_EXTRACT(additions.meta, '$.githubLogin')), '') LIKE ?`, [ '%66716858%' ])).to.equal(true);
2222
});
2323

24-
it('matches numeric GitHub IDs exactly while searching manual-addition usernames', () => {
24+
it('matches mapped numeric GitHub IDs exactly while searching manual-addition usernames', () => {
2525
const builder = {
2626
where: sinon.stub().returnsThis(),
2727
orWhere: sinon.stub().returnsThis(),
@@ -30,12 +30,12 @@ describe('sponsor query normalization', () => {
3030

3131
const where = sinon.spy((callback: (builder: Knex.QueryBuilder) => void) => callback(builder as unknown as Knex.QueryBuilder));
3232

33-
applyManualSearch({ where } as unknown as Knex.QueryBuilder, '123', 'added-by-expression');
33+
applyManualSearch({ where } as unknown as Knex.QueryBuilder, '66716858', 'added-by-expression');
3434

35-
expect(builder.where.calledOnceWithExactly('additions.github_id', '123')).to.equal(true);
36-
expect(builder.orWhere.calledWithExactly('current_sponsors.github_login', 'like', '%123%')).to.equal(true);
37-
expect(builder.orWhere.calledWithExactly('dashboard_users.github_username', 'like', '%123%')).to.equal(true);
38-
expect(builder.orWhereRaw.calledWithExactly(`NULLIF(JSON_UNQUOTE(JSON_EXTRACT(additions.meta, '$.githubLogin')), '') LIKE ?`, [ '%123%' ])).to.equal(true);
35+
expect(builder.where.calledOnceWithExactly('additions.github_id', '6209808')).to.equal(true);
36+
expect(builder.orWhere.calledWithExactly('current_sponsors.github_login', 'like', '%66716858%')).to.equal(true);
37+
expect(builder.orWhere.calledWithExactly('dashboard_users.github_username', 'like', '%66716858%')).to.equal(true);
38+
expect(builder.orWhereRaw.calledWithExactly(`NULLIF(JSON_UNQUOTE(JSON_EXTRACT(additions.meta, '$.githubLogin')), '') LIKE ?`, [ '%66716858%' ])).to.equal(true);
3939
});
4040

4141
it('groups monthly chart timestamps in UTC', () => {

0 commit comments

Comments
 (0)