feat: improve logging

This commit is contained in:
Julien Oculi 2024-01-15 21:59:22 +01:00
parent fda2ce9a00
commit d069e01c7c
3 changed files with 62 additions and 21 deletions

View file

@ -1,8 +1,12 @@
import { Redmine } from 'bluemine';
import { CsvEntry } from '../type.ts';
import { toLogin } from '../utils.ts';
import { Redmine } from 'bluemine'
import { CsvEntry } from '../type.ts'
import { log, toLogin } from '../utils.ts'
async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) {
async function addUsersToGroup(
redmine: Redmine,
csv: CsvEntry[],
groupName: string,
) {
const users: Redmine['users']['List']['Result']['users'] = []
for (const { firstname, lastname } of csv) {
@ -12,10 +16,11 @@ async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) {
const list = await redmine.users.list({ limit: 1, name: login })
const user = list.users[0]
console.log(`get user: ${user.login}#${user.id}`)
log.info(`get id of ${user.login}#${user.id}`)
users.push(user)
} catch {
console.error(`error list: ${login}`)
} catch (error) {
log.error(`get id of ${login}`)
console.error(String(error))
}
}
@ -25,10 +30,10 @@ async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) {
for (const user of users) {
try {
console.error(`ok: ${user.login}#${user.id}`)
log.info(`add ${user.login}#${user.id} to "${groupName}"`)
await redmine.unstableGroups.addUser(id, user)
} catch {
console.error(`error: ${user.login}`)
log.error(`add ${user.login}#${user.id} to "${groupName}"`)
}
}
}

View file

@ -1,8 +1,12 @@
import { Redmine } from 'bluemine'
import { CsvEntry } from '../type.ts'
import { capitalize, sanitize, toLogin } from '../utils.ts'
import { capitalize, log, sanitize, toLogin } from '../utils.ts'
async function createUsers(redmine: Redmine, csv: CsvEntry[]) {
async function createUsers(
redmine: Redmine,
csv: CsvEntry[],
groupName: string,
) {
const userIds: number[] = []
for (const { firstname, lastname, mail } of csv) {
@ -18,11 +22,22 @@ async function createUsers(redmine: Redmine, csv: CsvEntry[]) {
generatePassword: true,
})
log.info(`create user ${login}#${user.id}`)
userIds.push(user.id)
} catch {
console.error(login)
} catch (error) {
log.error(`create user ${login}`)
console.error(error)
}
}
await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds })
try {
log.info(`add to "${groupName}" users [${userIds.join(', ')}]`)
await redmine.unstableGroups.create({
name: 'entrepreneur-01-24',
userIds,
})
} catch (error) {
log.error(`add to "${groupName}" users [${userIds.join(', ')}]`)
console.error(error)
}
}

View file

@ -5,11 +5,32 @@ export function sanitize(str: string): string {
export function capitalize(str: string): string {
return str
.split(' ')
.map(word => word.split(''))
.map(([firstLetter, ...tail]) => `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`)
.map((word) => word.split(''))
.map(([firstLetter, ...tail]) =>
`${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`
)
.join(' ')
}
export function toLogin(firstname: string, lastname: string): string {
return `${sanitize(firstname.toLocaleLowerCase())}.${sanitize(lastname.toLocaleLowerCase())}`
return `${sanitize(firstname.toLocaleLowerCase())}.${
sanitize(lastname.toLocaleLowerCase())
}`
}
export const log = {
info(message: string) {
console.log(
`%c[info]%c ${message}`,
'color: cyan; font-weight: bold',
'color: white',
)
},
error(message: string) {
console.error(
`%c[error]%c ${message}`,
'color: red; font-weight: bold',
'color: white',
)
},
}