feat: improve logging
This commit is contained in:
parent
fda2ce9a00
commit
d069e01c7c
|
@ -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}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
35
utils.ts
35
utils.ts
|
@ -1,15 +1,36 @@
|
|||
export function sanitize(str: string): string {
|
||||
return str.replaceAll(' ', '_')
|
||||
return str.replaceAll(' ', '_')
|
||||
}
|
||||
|
||||
export function capitalize(str: string): string {
|
||||
return str
|
||||
.split(' ')
|
||||
.map(word => word.split(''))
|
||||
.map(([firstLetter, ...tail]) => `${firstLetter.toLocaleUpperCase()}${tail.join().toLocaleLowerCase()}`)
|
||||
.join(' ')
|
||||
return str
|
||||
.split(' ')
|
||||
.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',
|
||||
)
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue