Skip to content

Instantly share code, notes, and snippets.

@amichuda
Created March 16, 2022 16:46
Show Gist options
  • Save amichuda/bb5405031c302f657d0bfb950fa98a11 to your computer and use it in GitHub Desktop.
Save amichuda/bb5405031c302f657d0bfb950fa98a11 to your computer and use it in GitHub Desktop.
A Obsidian QuickAdd Macro to output issues by a user and organizations
// To use this with QuickAdd, you need to also create a capture in QuickAdd that reads in the output of this macro
// In the bottom of the capture, in the "Format" box, add {{MACRO:github-issue-macro}} (or however you register the macro.
module.exports = async (params) => {
const { app } = params;
let file = app.workspace.getMostRecentLeaf()?.view?.file;
if (!file) {
return;
}
const username = "<YOUR USERNAME>";
const gh_headers = {
"Authorization": "token <GITHUB PAT>", //Go to https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
"Accept": "application/vnd.github.v3+json"
};
const orgs = [
"<input>",
"<orgs>"
];
// First we get my username and org urls for querying repos
const user = `https://api.github.com/users/${username}/repos`;
const org_func = (org) => `https://api.github.com/orgs/${org}/repos`;
let all_org_urls = orgs.map(org_func);
// Now we query them all to get all my repos
const user_repos = await request({
url: user,
method: "GET",
headers: gh_headers
});
let all_repos = [];
JSON.parse(user_repos).map((repo) => {
all_repos.push({"name" : repo.full_name, "url" : repo.url})
})
await Promise.all(all_org_urls.map(
async (u) => {
const r = await request({ url: u, method: "GET", headers: gh_headers })
json = JSON.parse(r)
return json.map((repo) => all_repos.push({"name" : repo.full_name, "url" : repo.url}))
}
)
)
const date = new Date().toUTCString();
// Now query each repo and get the issues and throw into a list
const final_list = Promise.all(
all_repos.map(
async (repo) => {
if (!repo) {
console.log("Could not find repo")
return;
}
gh_headers["If-Modified-Since"] = date
const text = await request({url : `${repo['url']}/issues?state=open`, method: "GET", headers: gh_headers });
let issues = null;
try {
issues = JSON.parse(text)
} catch (e) {
console.log(`Error in parsing ${repo['name']}`)
return;
}
let fileContent = await app.vault.cachedRead(file)
const marker = (number) => `gh${number}`
const repo_title = `## ${repo['name']}`
const issue_list = issues
.filter(issue => fileContent.indexOf(marker(issue.number)) === -1)
.map(issue => `- [ ] ([${marker(issue.number)}](${issue.html_url})) ${issue.title}`)
.join('\n');
return repo_title + '\n\n' + issue_list
}
)
)
return final_list.then((l) => l.join('\n\n')).then((l) => l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment