Created
March 16, 2021 06:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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