File size: 2,091 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { execSync } from "child_process";
import fs from "fs";
import path from "path";

const REPO = "diegosouzapw/OmniRoute";
const artifactsDir =
  process.env.ARTIFACTS_DIR ||
  path.join(process.cwd(), "artifacts");

async function main() {
  try {
    // 1. Get PR numbers
    console.log("Fetching open PR numbers...");
    const prNumbersOutput = execSync(
      `gh pr list --repo ${REPO} --state open --limit 500 --json number --jq '.[].number'`,
      { encoding: "utf-8" }
    );
    const prNumbers = prNumbersOutput.trim().split("\n").map(Number).filter(Boolean);
    console.log(`Found ${prNumbers.length} open PRs:`, prNumbers);

    if (!fs.existsSync(artifactsDir)) {
      fs.mkdirSync(artifactsDir, { recursive: true });
    }

    // 2. Fetch metadata and diff for each PR
    for (const prNum of prNumbers) {
      console.log(`\n--- Fetching PR #${prNum} ---`);

      // Metadata
      try {
        const metadataCmd = `gh pr view ${prNum} --repo ${REPO} --json number,title,author,headRefName,baseRefName,body,createdAt,additions,deletions,files`;
        const metadataJson = execSync(metadataCmd, { encoding: "utf-8" });
        const metadataPath = path.join(artifactsDir, `pr_${prNum}_meta.json`);
        fs.writeFileSync(metadataPath, metadataJson);
        console.log(`Saved metadata to ${metadataPath}`);
      } catch (err) {
        console.error(`Failed to fetch metadata for PR #${prNum}:`, err.message);
      }

      // Diff
      try {
        const diffCmd = `gh pr diff ${prNum} --repo ${REPO}`;
        const diffText = execSync(diffCmd, { encoding: "utf-8", maxBuffer: 100 * 1024 * 1024 });
        const diffPath = path.join("/tmp", `pr${prNum}.diff`);
        fs.writeFileSync(diffPath, diffText);
        console.log(`Saved diff to ${diffPath} (Size: ${diffText.length} bytes)`);
      } catch (err) {
        console.error(`Failed to fetch diff for PR #${prNum}:`, err.message);
      }
    }

    console.log("\nAll PR data fetched successfully!");
  } catch (error) {
    console.error("Error during PR fetching:", error);
  }
}

main();