refactor graphql code into github helper class

This commit is contained in:
Peter Evans 2024-08-02 16:24:55 +00:00
parent 9c7a97affd
commit 6c5dc224d1
5 changed files with 343 additions and 1153 deletions

View file

@ -1,11 +1,4 @@
import * as core from '@actions/core'
import {graphql} from '@octokit/graphql'
import type {
Repository,
Ref,
Commit,
FileChanges
} from '@octokit/graphql-schema'
import {
createOrUpdateBranch,
getWorkingBaseAndType,
@ -203,203 +196,13 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
`Pushing pull request branch to '${branchRemoteName}/${inputs.branch}'`
)
if (inputs.signCommit) {
core.info(`Use API to push a signed commit`)
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: 'token ' + inputs.token
}
})
const [repoOwner, repoName] = branchRepository.split('/')
core.debug(`repoOwner: '${repoOwner}', repoName: '${repoName}'`)
const refQuery = `
query GetRefId($repoName: String!, $repoOwner: String!, $branchName: String!) {
repository(owner: $repoOwner, name: $repoName){
id
ref(qualifiedName: $branchName){
id
name
prefix
target{
id
oid
commitUrl
commitResourcePath
abbreviatedOid
}
}
},
}
`
let branchRef = await graphqlWithAuth<{repository: Repository}>(
refQuery,
{
repoOwner: repoOwner,
repoName: repoName,
branchName: inputs.branch
}
await githubHelper.pushSignedCommit(
branchRepository,
inputs.branch,
inputs.base,
inputs.commitMessage,
result.branchFileChanges
)
core.debug(
`Fetched information for branch '${inputs.branch}' - '${JSON.stringify(branchRef)}'`
)
// if the branch does not exist, then first we need to create the branch from base
if (branchRef.repository.ref == null) {
core.debug(`Branch does not exist - '${inputs.branch}'`)
branchRef = await graphqlWithAuth<{repository: Repository}>(
refQuery,
{
repoOwner: repoOwner,
repoName: repoName,
branchName: inputs.base
}
)
core.debug(
`Fetched information for base branch '${inputs.base}' - '${JSON.stringify(branchRef)}'`
)
core.info(
`Creating new branch '${inputs.branch}' from '${inputs.base}', with ref '${JSON.stringify(branchRef.repository.ref!.target!.oid)}'`
)
if (branchRef.repository.ref != null) {
core.debug(`Send request for creating new branch`)
const newBranchMutation = `
mutation CreateNewBranch($branchName: String!, $oid: GitObjectID!, $repoId: ID!) {
createRef(input: {
name: $branchName,
oid: $oid,
repositoryId: $repoId
}) {
ref {
id
name
prefix
}
}
}
`
const newBranch = await graphqlWithAuth<{createRef: {ref: Ref}}>(
newBranchMutation,
{
repoId: branchRef.repository.id,
oid: branchRef.repository.ref.target!.oid,
branchName: 'refs/heads/' + inputs.branch
}
)
core.debug(
`Created new branch '${inputs.branch}': '${JSON.stringify(newBranch.createRef.ref)}'`
)
}
}
core.info(
`Hash ref of branch '${inputs.branch}' is '${JSON.stringify(branchRef.repository.ref!.target!.oid)}'`
)
// // switch to input-branch for reading updated file contents
// await git.checkout(inputs.branch)
// const changedFiles = await git.getChangedFiles(
// branchRef.repository.ref!.target!.oid,
// ['--diff-filter=M']
// )
// const deletedFiles = await git.getChangedFiles(
// branchRef.repository.ref!.target!.oid,
// ['--diff-filter=D']
// )
// const fileChanges = <FileChanges>{additions: [], deletions: []}
// core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`)
// core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`)
// for (const file of changedFiles) {
// core.debug(`Reading contents of file: '${file}'`)
// fileChanges.additions!.push({
// path: file,
// contents: utils.readFileBase64([repoPath, file])
// })
// }
// for (const file of deletedFiles) {
// core.debug(`Marking file as deleted: '${file}'`)
// fileChanges.deletions!.push({
// path: file
// })
// }
const fileChanges = <FileChanges>{
additions: result.fileChanges!.additions,
deletions: result.fileChanges!.deletions
}
const pushCommitMutation = `
mutation PushCommit(
$repoNameWithOwner: String!,
$branchName: String!,
$headOid: GitObjectID!,
$commitMessage: String!,
$fileChanges: FileChanges
) {
createCommitOnBranch(input: {
branch: {
repositoryNameWithOwner: $repoNameWithOwner,
branchName: $branchName,
}
fileChanges: $fileChanges
message: {
headline: $commitMessage
}
expectedHeadOid: $headOid
}){
clientMutationId
ref{
id
name
prefix
}
commit{
id
abbreviatedOid
oid
}
}
}
`
const pushCommitVars = {
branchName: inputs.branch,
repoNameWithOwner: repoOwner + '/' + repoName,
headOid: branchRef.repository.ref!.target!.oid,
commitMessage: inputs.commitMessage,
fileChanges: fileChanges
}
const pushCommitVarsWithoutContents = {
...pushCommitVars,
fileChanges: {
...pushCommitVars.fileChanges,
additions: pushCommitVars.fileChanges.additions?.map(addition => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {contents, ...rest} = addition
return rest
})
}
}
core.debug(
`Push commit with payload: '${JSON.stringify(pushCommitVarsWithoutContents)}'`
)
const commit = await graphqlWithAuth<{
createCommitOnBranch: {ref: Ref; commit: Commit}
}>(pushCommitMutation, pushCommitVars)
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`)
core.info(
`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`
)
// // switch back to previous branch/state since we are done with reading the changed file contents
// await git.checkout('-')
} else {
await git.push([
'--force-with-lease',