Skip to content

Instantly share code, notes, and snippets.

@laprasdrum
Created March 16, 2021 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laprasdrum/b9e3afa643cdad4d379c5159a3e14854 to your computer and use it in GitHub Desktop.
Save laprasdrum/b9e3afa643cdad4d379c5159a3e14854 to your computer and use it in GitHub Desktop.
Zapier Code: fetch pipeline issues from ZenHub & fill each issues' description from GitHub API
// generate pipeline issue message for Slack
// e.g. (all issue messages are linked)
//
// 📝 *TODO*
// prepare for dinner
// write blog
//
// 📝 *In Review*
// buy a new desk
// ZenHub URL format: https://github.com/ZenHubIO/API#notes-1
// https://github.com/${org}/${repo}/issues#workspaces/${workspaceName}-${workspaceId}/board?repos=${repoId}
const inputData = {
'zenhubApiRoot', 'https://api.zenhub.com',
'zenhubToken': 'xxx',
'repoId': 'xxx',
'workspaceId': 'xxx',
'githubApiRoot', 'https://api.github.com/graphql',
'githubToken', 'xxx'
}
// as you like :)
const pipelines = ['TODO', 'In Review']
// generate Slack mrkdwn message.
async function getIssueMessage(i) {
const githubHeader = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${inputData.githubToken}`
}
const query = `{
repository(owner:"org", name:"repo") {
issue(number:${i.issue_number}) {
title
url
}
}
}`
const body = { "query": query }
const res = await fetch(inputData.githubApiRoot, {
method: 'POST',
body: JSON.stringify(body),
headers: githubHeader
})
const json = await res.json()
const title = await json.data.repository.issue.title
const url = await json.data.repository.issue.url
return `<${url}|${title}>`
}
const zenhubHeader = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Authentication-Token': inputData.zenhubToken
}
const boardUrl = `${inputData.zenhubApiRoot}/p2/workspaces/${inputData.workspaceId}/repositories/${inputData.repoId}/board`
const res = await fetch(boardUrl, {
method: 'GET',
headers: zenhubHeader
})
const json = await res.json()
const issues = await json.pipelines
.filter(p => pipelines.includes(p.name))
.map(async p => {
const messages = await p.issues.map(i => getIssueMessage(i))
let ret = await Promise.all(messages)
return `:memo: *${p.name}*\n` + ret.join('\n')
})
const ret = await Promise.all(issues)
const message = await ret.join('\n\n')
return { issueMessage: message }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment