refactor: extract subscripts in functions

This commit is contained in:
Julien Oculi 2024-01-15 21:01:07 +01:00
parent 65bba68feb
commit c43a64033b
4 changed files with 80 additions and 79 deletions

2
.env Normal file
View file

@ -0,0 +1,2 @@
API_KEY=""
ENDPOINT="https://projets.cohabit.fr/redmine"

View file

@ -1,47 +1,37 @@
import * as CSV from 'std:csv';
import { Redmine } from 'bluemine'; import { Redmine } from 'bluemine';
const endpoint = 'https://projets.cohabit.fr/redmine';
const apiKey = '';
const redmine = new Redmine({ endpoint, apiKey });
type CsvEntry = { type CsvEntry = {
firstname: string; firstname: string;
lastname: string; lastname: string;
mail: string; mail: string;
}; };
const file = await Deno.readTextFile( async function createUsers(redmine: Redmine, csv: CsvEntry[]) {
'C:/Users/Julien/Documents/Stage/FabLab/entrepreneur_full.csv', const userIds: number[] = [];
);
const csv = CSV.parse(file.replaceAll(';', ','), {
skipFirstRow: true,
}) as CsvEntry[];
const userIds: number[] = []; function capitalize(str: string): string {
const [first, ...tail] = str.split('');
return `${first.toLocaleUpperCase()}${tail.join('').toLocaleLowerCase()}`;
}
function capitalize(str: string): string { for (const { firstname, lastname, mail } of csv) {
const [first, ...tail] = str.split(''); const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`;
return `${first.toLocaleUpperCase()}${tail.join('').toLocaleLowerCase()}`;
try {
const { user } = await redmine.users.create({
firstname: capitalize(firstname),
lastname: capitalize(lastname),
mail: mail,
login,
sendCreationMail: true,
generatePassword: true,
});
userIds.push(user.id);
} catch {
console.error(login);
}
}
await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds });
} }
for (const { firstname, lastname, mail } of csv) {
const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`;
try {
const { user } = await redmine.users.create({
firstname: capitalize(firstname),
lastname: capitalize(lastname),
mail: mail,
login,
sendCreationMail: true,
generatePassword: true,
});
userIds.push(user.id);
} catch {
console.error(login);
}
}
await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds });

View file

@ -1,50 +1,40 @@
import * as CSV from 'std:csv';
import { Redmine } from 'bluemine'; import { Redmine } from 'bluemine';
const endpoint = 'https://projets.cohabit.fr/redmine';
const apiKey = '';
const redmine = new Redmine({ endpoint, apiKey });
type CsvEntry = { type CsvEntry = {
firstname: string; firstname: string;
lastname: string; lastname: string;
mail: string; mail: string;
}; };
const file = await Deno.readTextFile( async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) {
'C:/Users/Julien/Documents/Stage/FabLab/entrepreneur_full.csv', const users: { id: number; login: string }[] = [];
);
const csv = CSV.parse(file.replaceAll(';', ','), {
skipFirstRow: true,
}) as CsvEntry[];
const users: { id: number; login: string }[] = []; for (const { firstname, lastname } of csv) {
const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`;
for (const { firstname, lastname } of csv) { try {
const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`; const list = await redmine.users.list({ limit: 1, name: login });
const user = list.users[0];
try { console.log(`get user: ${user.login}#${user.id}`);
const list = await redmine.users.list({ limit: 1, name: login }); users.push(user);
const user = list.users[0]; } catch {
console.error(`error list: ${login}`);
}
}
console.log(`get user: ${user.login}#${user.id}`); const { groups } = await redmine.unstableGroups.list() as unknown as {
users.push(user); groups: { id: number; name: string }[];
} catch { };
console.error(`error list: ${login}`);
} const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0];
}
for (const user of users) {
const { groups } = await redmine.unstableGroups.list() as unknown as { try {
groups: { id: number; name: string }[]; console.error(`ok: ${user.login}#${user.id}`);
}; await redmine.unstableGroups.addUser(id, user);
} catch {
const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0]; console.error(`error: ${user.login}`);
}
for (const user of users) { }
try {
console.error(`ok: ${user.login}#${user.id}`);
await redmine.unstableGroups.addUser(id, user);
} catch {
console.error(`error: ${user.login}`);
}
} }

19
mod.ts Normal file
View file

@ -0,0 +1,19 @@
import { Redmine } from 'bluemine';
import * as CSV from 'std:csv';
if (import.meta.main) {
const endpoint = Deno.env.get('ENDPOINT')
const apiKey = Deno.env.get('API_KEY')
if (apiKey === undefined) throw new Error('Redmine REST API key is missing, please define API_KEY')
if (endpoint === undefined) throw new Error('Redmine REST endpoint is missing, please define ENDPOINT')
const redmine = new Redmine({ endpoint, apiKey });
const file = await Deno.readTextFile(
'path',
);
const csv = CSV.parse(file.replaceAll(';', ','), {
skipFirstRow: true,
}) as CsvEntry[];
}