removed if statements checking length, removed try catch preventing error from breaking execution

Signed-off-by: José Costa <jose.j.costa@shell.com>
This commit is contained in:
José Costa
2024-01-16 14:26:14 +00:00
parent 1a37e86b9d
commit 7647d8d870
4 changed files with 78 additions and 98 deletions
@@ -248,75 +248,67 @@ export class GithubMultiOrgEntityProvider implements EntityProvider {
: await this.getAllOrgs(this.options.gitHubConfig);
for (const org of orgsToProcess) {
try {
const { headers, type: tokenType } =
await this.options.githubCredentialsProvider.getCredentials({
url: `${this.options.githubUrl}/${org}`,
});
const client = graphql.defaults({
baseUrl: this.options.gitHubConfig.apiBaseUrl,
headers,
const { headers, type: tokenType } =
await this.options.githubCredentialsProvider.getCredentials({
url: `${this.options.githubUrl}/${org}`,
});
const client = graphql.defaults({
baseUrl: this.options.gitHubConfig.apiBaseUrl,
headers,
});
logger.info(`Reading GitHub users and teams for org: ${org}`);
logger.info(`Reading GitHub users and teams for org: ${org}`);
const { users } = await getOrganizationUsers(
client,
org,
tokenType,
this.options.userTransformer,
);
const { users } = await getOrganizationUsers(
client,
org,
tokenType,
this.options.userTransformer,
);
const { teams } = await getOrganizationTeams(
client,
org,
this.defaultMultiOrgTeamTransformer.bind(this),
);
const { teams } = await getOrganizationTeams(
client,
org,
this.defaultMultiOrgTeamTransformer.bind(this),
);
// Grab current users from `allUsersMap` if they already exist in our
// pending users so we can append to their group membership relations
const pendingUsers = users.map(u => {
const userRef = stringifyEntityRef(u);
if (!allUsersMap.has(userRef)) {
allUsersMap.set(userRef, u);
}
return allUsersMap.get(userRef);
});
if (areGroupEntities(teams)) {
buildOrgHierarchy(teams);
if (areUserEntities(pendingUsers)) {
assignGroupsToUsers(pendingUsers, teams);
}
// Grab current users from `allUsersMap` if they already exist in our
// pending users so we can append to their group membership relations
const pendingUsers = users.map(u => {
const userRef = stringifyEntityRef(u);
if (!allUsersMap.has(userRef)) {
allUsersMap.set(userRef, u);
}
allTeams.push(...teams);
} catch (e) {
logger.error(`Failed to read GitHub org data for ${org}: ${e}`);
return allUsersMap.get(userRef);
});
if (areGroupEntities(teams)) {
buildOrgHierarchy(teams);
if (areUserEntities(pendingUsers)) {
assignGroupsToUsers(pendingUsers, teams);
}
}
allTeams.push(...teams);
}
const allUsers = Array.from(allUsersMap.values());
if (allUsers.length > 0 || allTeams.length > 0) {
const { markCommitComplete } = markReadComplete({ allUsers, allTeams });
const { markCommitComplete } = markReadComplete({ allUsers, allTeams });
await this.connection.applyMutation({
type: 'full',
entities: [...allUsers, ...allTeams].map(entity => ({
locationKey: `github-multi-org-provider:${this.options.id}`,
entity: withLocations(
`https://${this.options.gitHubConfig.host}`,
entity,
),
})),
});
await this.connection.applyMutation({
type: 'full',
entities: [...allUsers, ...allTeams].map(entity => ({
locationKey: `github-multi-org-provider:${this.options.id}`,
entity: withLocations(
`https://${this.options.gitHubConfig.host}`,
entity,
),
})),
});
markCommitComplete();
} else {
logger.info('No users or teams to process');
}
markCommitComplete();
}
private supportsEventTopics(): string[] {
@@ -229,25 +229,21 @@ export class GithubOrgEntityProvider
}
}
if (users.length > 0 || teams.length > 0) {
const { markCommitComplete } = markReadComplete({ users, teams });
const { markCommitComplete } = markReadComplete({ users, teams });
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...teams].map(entity => ({
locationKey: `github-org-provider:${this.options.id}`,
entity: withLocations(
`https://${this.options.gitHubConfig.host}`,
org,
entity,
),
})),
});
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...teams].map(entity => ({
locationKey: `github-org-provider:${this.options.id}`,
entity: withLocations(
`https://${this.options.gitHubConfig.host}`,
org,
entity,
),
})),
});
markCommitComplete();
} else {
logger.info('No users or teams to process');
}
markCommitComplete();
}
/** {@inheritdoc @backstage/plugin-events-node#EventSubscriber.onEvent} */
@@ -192,21 +192,17 @@ export class LdapOrgEntityProvider implements EntityProvider {
logger,
},
);
if (users.length > 0 || groups.length > 0) {
const { markCommitComplete } = markReadComplete({ users, groups });
const { markCommitComplete } = markReadComplete({ users, groups });
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...groups].map(entity => ({
locationKey: `ldap-org-provider:${this.options.id}`,
entity: withLocations(this.options.id, entity),
})),
});
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...groups].map(entity => ({
locationKey: `ldap-org-provider:${this.options.id}`,
entity: withLocations(this.options.id, entity),
})),
});
markCommitComplete();
} else {
logger.info('No users or teams to process');
}
markCommitComplete();
}
private schedule(schedule: LdapOrgEntityProviderOptions['schedule']) {
@@ -323,21 +323,17 @@ export class MicrosoftGraphOrgEntityProvider implements EntityProvider {
logger: logger,
},
);
if (users.length > 0 || groups.length > 0) {
const { markCommitComplete } = markReadComplete({ users, groups });
const { markCommitComplete } = markReadComplete({ users, groups });
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...groups].map(entity => ({
locationKey: `msgraph-org-provider:${this.options.id}`,
entity: withLocations(this.options.id, entity),
})),
});
await this.connection.applyMutation({
type: 'full',
entities: [...users, ...groups].map(entity => ({
locationKey: `msgraph-org-provider:${this.options.id}`,
entity: withLocations(this.options.id, entity),
})),
});
markCommitComplete();
} else {
logger.info('No users or teams to process');
}
markCommitComplete();
}
private schedule(taskRunner: TaskRunner) {