Skip to content

Commit

Permalink
Moved getGhHandle outside of fetchAllFellows to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantcornholio committed Jul 29, 2024
1 parent dcec5ef commit 74b0f71
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions src/polkadot/fellows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collectives, people } from "@polkadot-api/descriptors";
import { createClient, SS58String } from "polkadot-api";
import { createClient, SS58String, TypedApi } from "polkadot-api";
import { chainSpec as polkadotChainSpec } from "polkadot-api/chains/polkadot";
import { chainSpec as collectivesChainSpec } from "polkadot-api/chains/polkadot_collectives";
import { chainSpec as peopleChainSpec } from "polkadot-api/chains/polkadot_people";
Expand All @@ -15,6 +15,48 @@ export class PolkadotFellows implements TeamApi {

constructor(private readonly logger: ActionLogger) {}

private async getGhHandle(
address: SS58String,
peopleApi: TypedApi<typeof people>,
logger: ActionLogger,
): Promise<string | undefined> {
logger.debug(`Fetching identity of '${address}'`);

const identityOf = await peopleApi.query.Identity.IdentityOf.getValue(address);

if (identityOf) {
const [identity] = identityOf;
const github = identity.info.github.value;

if (!github) {
logger.debug(`'${address}' does not have an additional field named 'github'`);
return;
}

const handle = github.asText().replace("@", "") as string;

if (handle) {
logger.info(`Found github handle for '${address}': '${handle}'`);
} else {
logger.debug(`'${address}' does not have a GitHub handle`);
return;
}
return handle;
}

logger.debug(`Identity of '${address}' is null. Checking for super identity`);

const superIdentityAddress = (await peopleApi.query.Identity.SuperOf.getValue(address))?.[0];

if (superIdentityAddress) {
logger.debug(`'${address}' has a super identity: '${superIdentityAddress}'. Fetching that identity`);
return await this.getGhHandle(superIdentityAddress, peopleApi, logger);
} else {
logger.debug(`No superidentity for ${address} found.`);
return undefined;
}
}

private async fetchAllFellows(logger: ActionLogger): Promise<Map<string, number>> {
logger.info("Initializing smoldot");
const smoldot = start();
Expand All @@ -26,56 +68,19 @@ export class PolkadotFellows implements TeamApi {
});

// Add the people chain to smoldot
const peopleRelayChain = await smoldot.addChain({
const peopleParachain = await smoldot.addChain({
chainSpec: peopleChainSpec,
potentialRelayChains: [smoldotRelayChain],
});

// Initialize the smoldot provider
const jsonRpcProvider = getSmProvider(peopleRelayChain);
const jsonRpcProvider = getSmProvider(peopleParachain);
logger.info("Initializing the people client");
const peopleClient = createClient(jsonRpcProvider);

// Get the types for the people client
const peopleApi = peopleClient.getTypedApi(people);

const getGhHandle = async (address: SS58String): Promise<string | undefined> => {
logger.debug(`Fetching identity of '${address}'`);
const identityOf = await peopleApi.query.Identity.IdentityOf.getValue(address);

if (identityOf) {
const [identity] = identityOf;
const github = identity.info.github.value;

if (!github) {
logger.debug(`'${address}' does not have an additional field named 'github'`);
return;
}

const handle = github.asText().replace("@", "") as string;

if (handle) {
logger.info(`Found github handle for '${address}': '${handle}'`);
} else {
logger.debug(`'${address}' does not have a GitHub handle`);
return;
}
return handle;
}

logger.debug(`Identity of '${address}' is null. Checking for super identity`);

const superIdentityAddress = (await peopleApi.query.Identity.SuperOf.getValue(address))?.[0];

if (superIdentityAddress) {
logger.debug(`'${address}' has a super identity: '${superIdentityAddress}'. Fetching that identity`);
return await getGhHandle(superIdentityAddress);
} else {
logger.debug(`No superidentity for ${address} found.`);
return undefined;
}
};

logger.info("Initializing the collectives client");

const collectiveRelayChain = await smoldot.addChain({
Expand Down Expand Up @@ -107,7 +112,7 @@ export class PolkadotFellows implements TeamApi {
return {
address,
rank,
githubHandle: await getGhHandle(address),
githubHandle: await this.getGhHandle(address, peopleApi, logger),
};
}),
);
Expand Down

0 comments on commit 74b0f71

Please sign in to comment.