Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getPatch: avoid failing on more than 300 files #210

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 72 additions & 13 deletions src/getPatch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { promises as fs } from 'fs'
import path from 'path'
import os from 'os'
import { execSync } from 'child_process'

export default async function getPatch ({
owner, repo, prnum,
githubToken = null,
Expand All @@ -24,17 +29,73 @@ export default async function getPatch ({

patchBody = await patchResponse.text()
} else {
const { data: pBody } = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
pull_number: prnum,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
mediaType: {
format: 'diff'
}
})
try {
const { data: pBody } = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
pull_number: prnum,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
mediaType: {
format: 'diff'
}
})

patchBody = pBody
} catch (err) {
console.log(err)

// since we failed to get the diff, we will try to get the PR body through cloning the source code
const { data: repoResponse } = await github.request('GET /repos/{owner}/{repo}', {
owner,
repo,
pull_number: prnum,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})

const { data: prResponse } = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
pull_number: prnum,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
mediaType: {
format: 'json'
}
})

// clone the repo
const cloneUrl = repoResponse.clone_url
const clonePath = path.join(os.tmpdir(), `pr-${prnum}`)
thypon marked this conversation as resolved.
Show resolved Hide resolved

console.log(`Cloning ${cloneUrl} to ${clonePath}, manually`)
execSync(`git clone ${cloneUrl} ${clonePath}`)
thypon marked this conversation as resolved.
Show resolved Hide resolved

// save current directory
const currentDir = process.cwd()

// change dir to the new created repo
process.chdir(clonePath)

// fetch branch associated with the PR
const targetBranch = prResponse.base.ref
const sourceBranch = prResponse.head.ref
execSync(`git fetch origin ${targetBranch}`)
thypon marked this conversation as resolved.
Show resolved Hide resolved
execSync(`git fetch origin ${sourceBranch}`)
thypon marked this conversation as resolved.
Show resolved Hide resolved

// generate the PR diff
execSync(`git diff origin/${sourceBranch} origin/${targetBranch} > pr.diff`)
thypon marked this conversation as resolved.
Show resolved Hide resolved

patchBody = await fs.readFile(path.join(clonePath, 'pr.diff'), 'utf8')
thypon marked this conversation as resolved.
Show resolved Hide resolved

// get back to previous directory and delete the cloned repo
process.chdir(currentDir)
await fs.rm(clonePath, { recursive: true })
}

const { data: repoResponse } = await github.request('GET /repos/{owner}/{repo}', {
owner,
Expand All @@ -48,8 +109,6 @@ export default async function getPatch ({
if (!runIfPrivate && (repoResponse.private || repoResponse.visibility === 'private')) {
throw new Error('This repo is private, and you have not enabled runIfPrivate')
}

patchBody = pBody
}

return {
Expand Down