Github

Example Javascript code for importing issues from Github

You can use Import Scripts to import issues from your Github account directly into an Equals workbook.

Create a Github personal access token

Follow the instructions here to create a Github personal access token. Your token needs the repo scope.

Add the key to a secret group

Copy the access token from the prior step. Then create a new secret group and paste the key into a new secret named github_pk. Create a second secret in the same group called github_username with the username that created the key.

Example code

Once you've completed the prior steps, create a new workbook, add a new import script to a sheet and copy + paste the script below. Once pasted select your secret group in the toolbar (make sure the key in the secret group is called github_pk). Update the REPO constant to the Github repo you want to pull issues from – it should include the name of the organization or user that owns the repo, followed by a slash, and the name of the repo.

const equals = require("equals");
const axios = require("axios");

const REPO = "ExampleOrg/example_app";
const FIRST_PAGE = `https://api.github.com/repos/${REPO}/issues?state=all&per_page=100`;
const MAX_PAGES = 10;
const LINK_REGEX = /<(.*)>; rel="(\w+)"/;

const parseLinkHeader = (linkHeader) => {
  const parsed = {};
  const links = linkHeader.split(",");
  links.forEach((link) => {
    const [,url,name] = link.match(LINK_REGEX);
    parsed[name] = url;
  })
  return parsed;
}

const getIssues = async () => {
  let issues = [];
  let nextPage = FIRST_PAGE;
  let i = 0;
  while(nextPage && i < MAX_PAGES) {
    const resp = await axios({
      method: "get",
      url: FIRST_PAGE,
      auth: {
        username: equals.getSecret("github_username"),
        password: equals.getSecret("github_pk")
      }
    });
    nextPage = parseLinkHeader(resp.headers.link).next;
    issues = issues.concat(resp.data);
    i += 1;
  }
  return issues;
}

const issues = await getIssues();

equals.addHeaders(["number", "title", "user", "state", "closed at", "created at"]);

for(const issue of issues) {
  if(issue.pull_request) {
    continue;
  }
  equals.addRow([
    issue.number,
    issue.title,
    issue.user.login,
    issue.state,
    issue.closed_at,
    issue.created_at
  ])
}