feat: allow push-to-fork to push to sibling repos (#2414)

Fixes #2412.
This commit is contained in:
Benjamin Gilbert 2023-09-29 10:24:09 -05:00 committed by Peter Evans
parent be547fcbbb
commit bc1906950d
2 changed files with 16 additions and 7 deletions

View file

@ -84,11 +84,22 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
core.info( core.info(
`Checking if '${branchRepository}' is a fork of '${baseRemote.repository}'` `Checking if '${branchRepository}' is a fork of '${baseRemote.repository}'`
) )
const parentRepository = const baseParentRepository = await githubHelper.getRepositoryParent(
baseRemote.repository
)
const branchParentRepository =
await githubHelper.getRepositoryParent(branchRepository) await githubHelper.getRepositoryParent(branchRepository)
if (parentRepository != baseRemote.repository) { if (branchParentRepository == null) {
throw new Error( throw new Error(
`Repository '${branchRepository}' is not a fork of '${baseRemote.repository}'. Unable to continue.` `Repository '${branchRepository}' is not a fork. Unable to continue.`
)
}
if (
branchParentRepository != baseRemote.repository &&
baseParentRepository != branchParentRepository
) {
throw new Error(
`Repository '${branchRepository}' is not a fork of '${baseRemote.repository}', nor are they siblings. Unable to continue.`
) )
} }
// Add a remote for the fork // Add a remote for the fork

View file

@ -101,14 +101,12 @@ export class GitHubHelper {
} }
} }
async getRepositoryParent(headRepository: string): Promise<string> { async getRepositoryParent(headRepository: string): Promise<string | null> {
const {data: headRepo} = await this.octokit.rest.repos.get({ const {data: headRepo} = await this.octokit.rest.repos.get({
...this.parseRepository(headRepository) ...this.parseRepository(headRepository)
}) })
if (!headRepo.parent) { if (!headRepo.parent) {
throw new Error( return null
`Repository '${headRepository}' is not a fork. Unable to continue.`
)
} }
return headRepo.parent.full_name return headRepo.parent.full_name
} }