Skip to content

Commit

Permalink
bug(github): blank content, unsubscribe, delete repo, fix #375, #455 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX authored Feb 16, 2022
1 parent 15a1248 commit fc15dec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
30 changes: 23 additions & 7 deletions plugins/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export function apply(ctx: Context, config: Config) {

ctx.plugin(GitHub, config)

ctx.command('github', 'GitHub 相关功能').alias('gh')
.action(({ session }) => session.execute('help github', true))

const tokens: Dict<string> = {}

ctx.router.get(config.path + '/authorize', async (_ctx) => {
Expand Down Expand Up @@ -112,10 +109,26 @@ export function apply(ctx: Context, config: Config) {
await ctx.github.request('DELETE', `${url}/${repo.id}`, session)
} catch (err) {
if (!axios.isAxiosError(err)) throw err
logger.warn(err)
return '移除仓库失败。'
if (err.response.status !== 404) {
logger.warn(err)
return '移除仓库失败。'
}
}
await ctx.database.remove('github', [name])

async function updateChannels() {
const channels = await ctx.database.get('channel', {}, ['id', 'platform', 'githubWebhooks'])
return ctx.database.upsert('channel', channels.filter(({ githubWebhooks }) => {
const shouldUpdate = githubWebhooks[name]
delete githubWebhooks[name]
return shouldUpdate
}))
}

unsubscribe(name)
await Promise.all([
updateChannels(),
ctx.database.remove('github', [name]),
])
return '移除仓库成功!'
}
}
Expand All @@ -129,7 +142,8 @@ export function apply(ctx: Context, config: Config) {
(subscriptions[repo] ||= {})[cid] = meta
}

function unsubscribe(repo: string, id: string) {
function unsubscribe(repo: string, id?: string) {
if (!id) return delete subscriptions[repo]
delete subscriptions[repo][id]
if (!Object.keys(subscriptions[repo]).length) {
delete subscriptions[repo]
Expand Down Expand Up @@ -186,6 +200,8 @@ export function apply(ctx: Context, config: Config) {
return '移除订阅成功!'
}
}

return session.execute('help github')
})

async function request(method: Method, url: string, session: ReplySession, body: any, message: string) {
Expand Down
1 change: 1 addition & 0 deletions plugins/github/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function render(tokens: marked.Token[]) {
}

export function transform(source: string) {
if (!source) return ''
source = source.replace(/^<!--(.*)-->$/gm, '')
return render(marked.lexer(source)).trim().replace(/\n\s*\n/g, '\n')
}

0 comments on commit fc15dec

Please sign in to comment.