actions-ding/src/index.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-06-19 15:57:43 +08:00
import * as core from '@actions/core'
2020-06-19 17:25:50 +08:00
import {DingBot} from '@zcong/ding-bot'
2020-06-19 15:57:43 +08:00
const endpoint = 'https://oapi.dingtalk.com/robot/send'
async function run(): Promise<void> {
try {
2020-06-19 17:25:50 +08:00
const token = core.getInput('dingToken')
const body = core.getInput('body')
const secretStr = core.getInput('secret')
const ignoreError = core.getInput('ignoreError') === 'true'
2020-06-19 17:25:50 +08:00
const secret = secretStr === '' ? undefined : secretStr
if (secret) {
core.info('get secret, sign mode')
}
2020-06-19 16:00:51 +08:00
core.info(`Send body: ${body}`)
2020-06-19 17:25:50 +08:00
const dingBot = new DingBot({
endpoint,
accessToken: token,
signKey: secret
})
2020-06-19 15:57:43 +08:00
try {
const resp = await dingBot.rawSend(body)
2020-06-19 15:57:43 +08:00
2020-06-19 17:25:50 +08:00
if (resp?.errcode !== 0) {
if (ignoreError) {
core.warning(resp?.errmsg)
return
} else {
core.setFailed(resp?.errmsg)
}
2020-06-19 15:57:43 +08:00
}
2021-11-08 10:55:31 +08:00
} catch (requestErr: any) {
2020-06-19 15:57:43 +08:00
core.error(
`send request error, status: ${requestErr.response?.status}, data: ${requestErr.response?.data}`
)
if (ignoreError) {
core.warning(requestErr.message)
return
} else {
core.setFailed(requestErr.message)
}
2020-06-19 15:57:43 +08:00
}
2021-11-08 10:55:31 +08:00
} catch (error: any) {
2020-06-19 15:57:43 +08:00
core.setFailed(error.message)
}
}
run()