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 { Redmine } from 'bluemine'
import { CsvEntry } from '../type.ts'; import { CsvEntry } from '../type.ts'
import { toLogin } from '../utils.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'] = [] const users: Redmine['users']['List']['Result']['users'] = []
for (const { firstname, lastname } of csv) { 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 list = await redmine.users.list({ limit: 1, name: login })
const user = list.users[0] 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) users.push(user)
} catch { } catch (error) {
console.error(`error list: ${login}`) 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) { for (const user of users) {
try { try {
console.error(`ok: ${user.login}#${user.id}`) log.info(`add ${user.login}#${user.id} to "${groupName}"`)
await redmine.unstableGroups.addUser(id, user) await redmine.unstableGroups.addUser(id, user)
} catch { } 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 { Redmine } from 'bluemine'
import { CsvEntry } from '../type.ts' 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[] = [] const userIds: number[] = []
for (const { firstname, lastname, mail } of csv) { for (const { firstname, lastname, mail } of csv) {
@ -18,11 +22,22 @@ async function createUsers(redmine: Redmine, csv: CsvEntry[]) {
generatePassword: true, generatePassword: true,
}) })
log.info(`create user ${login}#${user.id}`)
userIds.push(user.id) userIds.push(user.id)
} catch { } catch (error) {
console.error(login) 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

@ -1,15 +1,36 @@
export function sanitize(str: string): string { export function sanitize(str: string): string {
return str.replaceAll(' ', '_') return str.replaceAll(' ', '_')
} }
export function capitalize(str: string): string { export function capitalize(str: string): string {
return str return str
.split(' ') .split(' ')
.map(word => word.split('')) .map((word) => word.split(''))
.map(([firstLetter, ...tail]) => `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`) .map(([firstLetter, ...tail]) =>
.join(' ') `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`
)
.join(' ')
} }
export function toLogin(firstname: string, lastname: string): string { 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',
)
},
} }