CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

tessl/factory

Skills that Tessl factory loops run: implement a Linear, GitHub or Jira issue as a pull request, shepherd that pull request through review and checks, wake the shepherd for pull requests that a push to their base branch left in conflict, and keep the work in step when a person stops, ends, answers or closes it.

Quality

86%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Medium

Suggest reviewing before use

Overview
Quality
Evals
Security
Files

download-github-images.mjsskills/implement-github-issue/scripts/

#!/usr/bin/env node

// Downloads the images GitHub serves in an issue's or pull request's text, in
// the current repository, and prints each file with where it came from.
//
// usage: node download-github-images.mjs <issue or pull request number>
//
// The repository is the clone the current directory is in. Outside a clone it
// is the clone this script is in, wherever the skill is installed.
//
// In a private repository an image link in the Markdown returns 404, with or
// without a token. The REST API's HTML rendering (`body_html`) carries a
// signed link for each image instead, which downloads with no login and
// expires 300 seconds after it is issued, so each response's images are
// downloaded as soon as it arrives. A pull request also has its reviews and
// review comments read. Images served from anywhere else are listed, not
// downloaded. A download that fails is printed with its reason; a read that
// fails exits 1 with nothing on stdout.

import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const PAGE_SIZE = 100;
const CALL_TIMEOUT_MS = 2 * 60_000;
const DOWNLOAD_TIMEOUT_MS = 60_000;

const GITHUB_HOSTED = [
  /^https:\/\/private-user-images\.githubusercontent\.com\//,
  /^https:\/\/user-images\.githubusercontent\.com\//,
  /^https:\/\/github\.com\/user-attachments\/assets\//,
  /^https:\/\/camo\.githubusercontent\.com\//,
];

const EXTENSIONS = {
  'image/png': 'png',
  'image/jpeg': 'jpg',
  'image/gif': 'gif',
  'image/webp': 'webp',
  'image/svg+xml': 'svg',
};

function decodeEntities(text) {
  return text.replace(
    /&(#x[0-9a-f]+|#[0-9]+|amp|lt|gt|quot|apos|#39);/gi,
    (_, entity) => {
      const lower = entity.toLowerCase();
      if (lower.startsWith('#x'))
        return String.fromCodePoint(parseInt(lower.slice(2), 16));
      if (lower.startsWith('#'))
        return String.fromCodePoint(parseInt(lower.slice(1), 10));
      return { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }[lower];
    },
  );
}

/**
 * A tag's attributes, read in order, so text inside one attribute's value is
 * never taken for another attribute. The first of a repeated name wins.
 */
function attributes(tag) {
  const found = Object.create(null);
  const pattern =
    /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
  for (const match of tag.replace(/^<img\b|\/?>$/gi, '').matchAll(pattern)) {
    const name = match[1].toLowerCase();
    if (name in found) continue;
    found[name] = decodeEntities(match[2] ?? match[3] ?? match[4] ?? '');
  }
  return found;
}

/** Alt text on one line: whitespace and control characters become one space. */
function oneLine(text) {
  return text.replace(/[\s\p{Cc}]+/gu, ' ').trim();
}

/** Every `<img>` in rendered HTML, once per link, in order: `{url, alt, hosted}`. */
export function imageLinks(html) {
  const seen = new Set();
  const images = [];
  for (const [tag] of (html ?? '').matchAll(/<img\b[^>]*>/gi)) {
    const { src: url, alt } = attributes(tag);
    if (!url || seen.has(url)) continue;
    seen.add(url);
    images.push({
      url,
      alt: oneLine(alt ?? ''),
      hosted: GITHUB_HOSTED.some((pattern) => pattern.test(url)),
    });
  }
  return images;
}

/**
 * The directory `gh` resolves `{owner}/{repo}` from: the current directory when
 * it is in a clone, otherwise this script's own directory.
 */
function repositoryDirectory() {
  const inClone = spawnSync('git', ['rev-parse', '--is-inside-work-tree'], {
    encoding: 'utf8',
    timeout: CALL_TIMEOUT_MS,
  });
  return inClone.status === 0 && inClone.stdout.trim() === 'true'
    ? process.cwd()
    : dirname(fileURLToPath(import.meta.url));
}

let repository;

function api(path) {
  repository ??= repositoryDirectory();
  const result = spawnSync(
    'gh',
    ['api', '-H', 'Accept: application/vnd.github.full+json', path],
    {
      cwd: repository,
      encoding: 'utf8',
      timeout: CALL_TIMEOUT_MS,
      maxBuffer: 256 * 1024 * 1024,
    },
  );
  if (result.status !== 0) {
    const reason =
      result.error?.message || result.stderr?.trim() || `exit ${result.status}`;
    throw new Error(`gh api ${path} failed: ${reason}`);
  }
  return JSON.parse(result.stdout);
}

function* pages(path) {
  for (let page = 1; ; page += 1) {
    const items = api(`${path}?per_page=${PAGE_SIZE}&page=${page}`);
    if (!Array.isArray(items))
      throw new Error(`gh api ${path} did not return a list`);
    yield items;
    if (items.length < PAGE_SIZE) return;
  }
}

function extension(contentType, url) {
  const type = (contentType ?? '').split(';')[0].trim().toLowerCase();
  if (EXTENSIONS[type]) return EXTENSIONS[type];
  const fromPath = new URL(url).pathname.match(/\.([a-z0-9]{1,5})$/i);
  return fromPath ? fromPath[1].toLowerCase() : 'bin';
}

async function download(url) {
  const response = await fetch(url, {
    redirect: 'follow',
    signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS),
  });
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return {
    bytes: Buffer.from(await response.arrayBuffer()),
    contentType: response.headers.get('content-type'),
  };
}

function by(item) {
  return item.user?.login ?? '(unknown)';
}

/**
 * Reads the text of issue or pull request `number` and downloads its images
 * into `dir`. Returns the lines to print.
 */
export async function downloadImages(number, dir) {
  const lines = [];
  let downloaded = 0;
  let failed = 0;
  let index = 0;

  async function take(source, slug, html) {
    const images = imageLinks(html);
    if (images.length === 0) return;
    lines.push('', `## ${source}`);
    for (const image of images) {
      const alt = image.alt ? ` "${image.alt}"` : '';
      if (!image.hosted) {
        lines.push(
          `- not served by GitHub, not downloaded${alt}: ${image.url}`,
        );
        continue;
      }
      index += 1;
      const name = `${String(index).padStart(2, '0')}-${slug}`;
      try {
        const { bytes, contentType } = await download(image.url);
        const file = join(dir, `${name}.${extension(contentType, image.url)}`);
        writeFileSync(file, bytes);
        downloaded += 1;
        lines.push(`- ${file}${alt}`);
      } catch (error) {
        failed += 1;
        lines.push(
          `- download failed${alt}: ${error.message}: ${image.url.split('?')[0]}`,
        );
      }
    }
  }

  const issue = api(`repos/{owner}/{repo}/issues/${number}`);
  const isPullRequest = Boolean(issue.pull_request);
  await take(`body by ${by(issue)}`, 'body', issue.body_html);
  for (const page of pages(`repos/{owner}/{repo}/issues/${number}/comments`)) {
    for (const comment of page) {
      await take(
        `comment ${comment.id} by ${by(comment)} at ${comment.created_at}`,
        `comment-${comment.id}`,
        comment.body_html,
      );
    }
  }
  if (isPullRequest) {
    for (const page of pages(`repos/{owner}/{repo}/pulls/${number}/reviews`)) {
      for (const review of page) {
        await take(
          `review ${review.id} by ${by(review)} at ${review.submitted_at}`,
          `review-${review.id}`,
          review.body_html,
        );
      }
    }
    for (const page of pages(`repos/{owner}/{repo}/pulls/${number}/comments`)) {
      for (const comment of page) {
        await take(
          `review comment ${comment.id} by ${by(comment)} at ${comment.created_at} on ${comment.path}`,
          `review-comment-${comment.id}`,
          comment.body_html,
        );
      }
    }
  }

  const header = [
    `# Images in ${issue.html_url}`,
    '',
    `- Collected at: ${new Date().toISOString().replace(/\.\d+Z$/, 'Z')} (UTC)`,
    `- Kind: ${isPullRequest ? 'pull request' : 'issue'}`,
    `- Directory: ${dir}`,
    `- Downloaded: ${downloaded}`,
    `- Failed: ${failed}`,
  ];
  if (lines.length === 0) lines.push('', 'No images in the text.');
  return [...header, ...lines];
}

async function main() {
  const number = process.argv[2];
  if (!/^[1-9][0-9]*$/.test(number ?? '') || process.argv.length > 3) {
    console.error(
      'usage: node download-github-images.mjs <issue or pull request number>',
    );
    process.exit(2);
  }
  const dir = mkdtempSync(join(tmpdir(), `github-images-${number}-`));
  try {
    console.log((await downloadImages(number, dir)).join('\n'));
  } catch (error) {
    rmSync(dir, { recursive: true, force: true });
    console.error(`download-github-images: ${error.message}`);
    process.exit(1);
  }
}

if (
  process.argv[1] &&
  resolve(process.argv[1]) === fileURLToPath(import.meta.url)
) {
  await main();
}

README.md

tile.json