GitHub plugin: Expose user addresses (#1382)
Allow getting the node address for a user, given the user's login. This will be needed by the upcoming identity plugin. If the login in question corresponds to a bot, then a bot address will be returned. When we make the bot-set configuration (rather than hardcoded), we'll need to change the signature of this function; I think that's fine. Test plan: Unit tests added. (Also, it's really simple.)
This commit is contained in:
parent
ac8ac7051f
commit
1449935651
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import deepFreeze from "deep-freeze";
|
import deepFreeze from "deep-freeze";
|
||||||
import {NodeAddress, type NodeAddressT} from "../../core/graph";
|
import {NodeAddress, type NodeAddressT} from "../../core/graph";
|
||||||
|
import {botSet} from "./bots";
|
||||||
|
|
||||||
export opaque type RawAddress: NodeAddressT = NodeAddressT;
|
export opaque type RawAddress: NodeAddressT = NodeAddressT;
|
||||||
|
|
||||||
|
@ -36,6 +37,26 @@ export const Prefix = deepFreeze({
|
||||||
pullComment: _githubAddress(COMMENT_TYPE, PULL_TYPE),
|
pullComment: _githubAddress(COMMENT_TYPE, PULL_TYPE),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the address corresponding to a GitHub login.
|
||||||
|
*
|
||||||
|
* If the login is considered a bot, then a bot address is returned. Otherwise,
|
||||||
|
* a regular user address is returned. The method does not attempt to find out
|
||||||
|
* whether the address should actually be an organization address, as we don't
|
||||||
|
* yet handle organization addresses.
|
||||||
|
*
|
||||||
|
* Note: The signature will need to be refactored when we make the list of bots
|
||||||
|
* a configuration option rather than a hardcoded constant.
|
||||||
|
*/
|
||||||
|
export function loginAddress(username: string): RawAddress {
|
||||||
|
const bots = botSet();
|
||||||
|
if (bots.has(username)) {
|
||||||
|
return NodeAddress.append(Prefix.bot, username);
|
||||||
|
} else {
|
||||||
|
return NodeAddress.append(Prefix.user, username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export type RepoAddress = {|
|
export type RepoAddress = {|
|
||||||
+type: typeof REPO_TYPE,
|
+type: typeof REPO_TYPE,
|
||||||
+owner: string,
|
+owner: string,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import {NodeAddress} from "../../core/graph";
|
import {NodeAddress} from "../../core/graph";
|
||||||
import * as GN from "./nodes";
|
import * as GN from "./nodes";
|
||||||
import {fromRaw, toRaw} from "./nodes";
|
import {fromRaw, toRaw, type UserlikeAddress, loginAddress} from "./nodes";
|
||||||
|
|
||||||
describe("plugins/github/nodes", () => {
|
describe("plugins/github/nodes", () => {
|
||||||
const repo = (): GN.RepoAddress => ({
|
const repo = (): GN.RepoAddress => ({
|
||||||
|
@ -243,4 +243,29 @@ describe("plugins/github/nodes", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("loginAddress", () => {
|
||||||
|
it("works for a regular user", () => {
|
||||||
|
const username = "foo";
|
||||||
|
const structured: UserlikeAddress = {
|
||||||
|
type: "USERLIKE",
|
||||||
|
subtype: "USER",
|
||||||
|
login: username,
|
||||||
|
};
|
||||||
|
const actual = loginAddress(username);
|
||||||
|
const expected = toRaw(structured);
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
it("works for a bot", () => {
|
||||||
|
const username = "credbot";
|
||||||
|
const structured: UserlikeAddress = {
|
||||||
|
type: "USERLIKE",
|
||||||
|
subtype: "BOT",
|
||||||
|
login: username,
|
||||||
|
};
|
||||||
|
const actual = loginAddress(username);
|
||||||
|
const expected = toRaw(structured);
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue