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:
Dandelion Mané 2019-09-18 14:50:52 +02:00 committed by GitHub
parent ac8ac7051f
commit 1449935651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -2,6 +2,7 @@
import deepFreeze from "deep-freeze";
import {NodeAddress, type NodeAddressT} from "../../core/graph";
import {botSet} from "./bots";
export opaque type RawAddress: NodeAddressT = NodeAddressT;
@ -36,6 +37,26 @@ export const Prefix = deepFreeze({
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 = {|
+type: typeof REPO_TYPE,
+owner: string,

View File

@ -2,7 +2,7 @@
import {NodeAddress} from "../../core/graph";
import * as GN from "./nodes";
import {fromRaw, toRaw} from "./nodes";
import {fromRaw, toRaw, type UserlikeAddress, loginAddress} from "./nodes";
describe("plugins/github/nodes", () => {
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);
});
});
});