11import type { GetRepositoryMetadataQuery } from "./github/graphql/generated/operations.js" ;
22import {
33 createCommitOnBranchQuery ,
4+ createRefMutation ,
5+ deleteRefMutation ,
46 getRepositoryMetadata ,
7+ updateRefMutation ,
58} from "./github/graphql/queries.ts" ;
69import type {
710 CommitFilesFromBase64Args ,
@@ -145,14 +148,15 @@ export const commitFilesFromBase64 = async ({
145148 let tempRefId : string ;
146149
147150 try {
148- const createdTempRef = await octokit . rest . git . createRef ( {
149- owner,
150- repo,
151- ref : `refs/heads/${ tempBranch } ` ,
152- sha : baseOid ,
151+ const createdTempRef = await createRefMutation ( octokit , {
152+ input : {
153+ repositoryId : info . id ,
154+ name : `refs/heads/${ tempBranch } ` ,
155+ oid : baseOid ,
156+ } ,
153157 } ) ;
154158
155- const refIdStr = createdTempRef . data . node_id ;
159+ const refIdStr = createdTempRef . createRef ?. ref ?. id ;
156160
157161 if ( ! refIdStr ) {
158162 throw new Error ( `Failed to create temporary branch ${ tempBranch } ` ) ;
@@ -164,15 +168,27 @@ export const commitFilesFromBase64 = async ({
164168 throw error ;
165169 }
166170
167- const updatedTempRef = await octokit . rest . git . updateRef ( {
171+ const tempRefInfo = await getRepositoryMetadata ( octokit , {
168172 owner,
169173 repo,
170- ref : `heads/${ tempBranch } ` ,
171- sha : baseOid ,
172- force : true ,
174+ baseRef : `refs/heads/${ tempBranch } ` ,
175+ targetRef : `refs/heads/${ tempBranch } ` ,
173176 } ) ;
177+ const existingTempRefId = tempRefInfo ?. targetBranch ?. id ;
174178
175- const refIdStr = updatedTempRef . data . node_id ;
179+ if ( ! existingTempRefId ) {
180+ throw new Error ( `Failed to resolve temporary branch ${ tempBranch } ` ) ;
181+ }
182+
183+ const updatedTempRef = await updateRefMutation ( octokit , {
184+ input : {
185+ refId : existingTempRefId ,
186+ oid : baseOid ,
187+ force : true ,
188+ } ,
189+ } ) ;
190+
191+ const refIdStr = updatedTempRef . updateRef ?. ref ?. id ;
176192
177193 if ( ! refIdStr ) {
178194 throw new Error ( `Failed to update temporary branch ${ tempBranch } ` ) ;
@@ -198,24 +214,24 @@ export const commitFilesFromBase64 = async ({
198214 ) ;
199215 }
200216
201- const updatedTargetRef = await octokit . rest . git . updateRef ( {
202- owner ,
203- repo ,
204- ref : `heads/ ${ branch } ` ,
205- sha : tempHeadOid ,
206- force : true ,
217+ const updatedTargetRef = await updateRefMutation ( octokit , {
218+ input : {
219+ refId : info . targetBranch ! . id ,
220+ oid : tempHeadOid ,
221+ force : true ,
222+ } ,
207223 } ) ;
208224
209- const updatedTargetRefId = updatedTargetRef . data . node_id ;
225+ const updatedTargetRefId = updatedTargetRef . updateRef ?. ref ?. id ;
210226
211227 if ( ! updatedTargetRefId ) {
212228 throw new Error ( `Failed to update branch ${ branch } ` ) ;
213229 }
214230
215- await octokit . rest . git . deleteRef ( {
216- owner ,
217- repo ,
218- ref : `heads/ ${ tempBranch } ` ,
231+ await deleteRefMutation ( octokit , {
232+ input : {
233+ refId : tempRefId ,
234+ } ,
219235 } ) ;
220236
221237 return {
@@ -226,14 +242,15 @@ export const commitFilesFromBase64 = async ({
226242 let refId : string ;
227243
228244 if ( mode === "create" ) {
229- const createdRef = await octokit . rest . git . createRef ( {
230- owner,
231- repo,
232- ref : `refs/heads/${ branch } ` ,
233- sha : baseOid ,
245+ const createdRef = await createRefMutation ( octokit , {
246+ input : {
247+ repositoryId : info . id ,
248+ name : `refs/heads/${ branch } ` ,
249+ oid : baseOid ,
250+ } ,
234251 } ) ;
235252
236- const refIdStr = createdRef . data . node_id ;
253+ const refIdStr = createdRef . createRef ?. ref ?. id ;
237254
238255 if ( ! refIdStr ) {
239256 throw new Error ( `Failed to create branch ${ branch } ` ) ;
0 commit comments