@@ -6,41 +6,20 @@ import {
66import type {
77 CommitFilesFromBase64Args ,
88 CommitFilesResult ,
9- GitBase ,
109} from "./interface.ts" ;
11- import { normalizeCommitMessage } from "./utils.ts" ;
10+ import { normalizeCommitMessage , resolveGitRef } from "./utils.ts" ;
1211
13- const getBaseRef = ( base : GitBase ) : string => {
14- if ( "branch" in base ) {
15- return `refs/heads/${ base . branch } ` ;
16- } else if ( "tag" in base ) {
17- return `refs/tags/${ base . tag } ` ;
18- } else {
19- // For explicit commit bases we don't resolve the base oid from a ref,
20- // but the shared metadata query still expects a valid qualified ref name.
21- return "HEAD" ;
22- }
23- } ;
12+ function getBaseRefSha (
13+ baseRef : NonNullable < GetRepositoryMetadataQuery [ "repository" ] > [ "baseRef" ] ,
14+ ) {
15+ if ( ! baseRef ?. target ) return null ;
2416
25- const getOidFromRef = (
26- base : GitBase ,
27- ref : ( GetRepositoryMetadataQuery [ "repository" ] &
28- Record < never , never > ) [ "baseRef" ] ,
29- ) => {
30- if ( "commit" in base ) {
31- return base . commit ;
17+ if ( "target" in baseRef . target ) {
18+ return baseRef . target . target . oid ;
3219 }
3320
34- if ( ! ref ?. target ) {
35- throw new Error ( `Could not determine oid from ref: ${ JSON . stringify ( ref ) } ` ) ;
36- }
37-
38- if ( "target" in ref . target ) {
39- return ref . target . target . oid ;
40- }
41-
42- return ref . target . oid ;
43- } ;
21+ return baseRef . target . oid ;
22+ }
4423
4524const isAlreadyExistingRefError = ( error : unknown ) =>
4625 typeof error === "object" &&
@@ -55,20 +34,20 @@ const isAlreadyExistingRefError = (error: unknown) =>
5534const createCommit = async ( {
5635 octokit,
5736 refId,
58- baseOid ,
37+ baseSha ,
5938 message,
6039 fileChanges,
6140} : Pick < CommitFilesFromBase64Args , "octokit" | "message" | "fileChanges" > & {
6241 refId : string ;
63- baseOid : string ;
42+ baseSha : string ;
6443} ) => {
6544 // we have to stick to GraphQL here as with REST, each file change would become a separate API call
6645 return createCommitOnBranchQuery ( octokit , {
6746 input : {
6847 branch : {
6948 id : refId ,
7049 } ,
71- expectedHeadOid : baseOid ,
50+ expectedHeadOid : baseSha ,
7251 message : normalizeCommitMessage ( message ) ,
7352 fileChanges,
7453 } ,
@@ -85,8 +64,7 @@ export const commitFilesFromBase64 = async ({
8564 message,
8665 fileChanges,
8766} : CommitFilesFromBase64Args ) : Promise < CommitFilesResult > => {
88- const repositoryNameWithOwner = `${ owner } /${ repo } ` ;
89- const baseRef = getBaseRef ( base ) ;
67+ const baseRef = resolveGitRef ( base ) ;
9068 const targetRef = `refs/heads/${ branch } ` ;
9169
9270 const info = await getRepositoryMetadata ( octokit , {
@@ -97,40 +75,36 @@ export const commitFilesFromBase64 = async ({
9775 } ) ;
9876
9977 if ( ! info ) {
100- throw new Error (
101- `Repository ${ JSON . stringify ( repositoryNameWithOwner ) } not found` ,
102- ) ;
78+ throw new Error ( `Repository "${ owner } /${ repo } " not found` ) ;
10379 }
104- if ( ! ( "commit" in base ) && ! info . baseRef ) {
105- throw new Error ( `Ref ${ JSON . stringify ( baseRef ) } not found` ) ;
106- }
107-
108- const resolvedBaseRef = info . baseRef ;
10980
11081 /**
111- * The commit oid to base the new commit on.
82+ * The commit sha to base the new commit on.
11283 *
11384 * Used both to create the new commit,
11485 * and to determine whether an existing branch can be updated.
11586 */
116- const baseOid = getOidFromRef ( base , info . baseRef ) ;
117- const targetOid = info . targetBranch ?. target ?. oid ?? null ;
87+ const baseSha = "commit" in base ? base . commit : getBaseRefSha ( info . baseRef ) ;
88+ if ( ! baseSha ) {
89+ throw new Error ( `Could not determine sha for base ref "${ baseRef } "` ) ;
90+ }
91+ const targetSha = info . targetBranch ?. target ?. oid ?? null ;
11892 const sameBranchBase = "branch" in base && base . branch === branch ;
11993
12094 let mode : "create" | "update" | "force-update" ;
12195
12296 if ( sameBranchBase ) {
12397 mode = force ? "force-update" : "update" ;
124- } else if ( targetOid === null ) {
98+ } else if ( targetSha === null ) {
12599 // TODO: legit *creation* failure should be retried if `force === true`
126100 mode = "create" ;
127101 } else if ( force ) {
128102 mode = "force-update" ;
129- } else if ( targetOid === baseOid ) {
103+ } else if ( targetSha === baseSha ) {
130104 mode = "update" ;
131105 } else {
132106 throw new Error (
133- `Branch ${ branch } exists already and does not match base ${ baseOid } , force is set to false` ,
107+ `Branch ${ branch } exists already and does not match base ${ baseSha } , force is set to false` ,
134108 ) ;
135109 }
136110
@@ -146,7 +120,7 @@ export const commitFilesFromBase64 = async ({
146120 owner,
147121 repo,
148122 ref : `refs/heads/${ tempBranch } ` ,
149- sha : baseOid ,
123+ sha : baseSha ,
150124 } ) ;
151125
152126 const refIdStr = createdTempRef . data . node_id ;
@@ -165,7 +139,7 @@ export const commitFilesFromBase64 = async ({
165139 owner,
166140 repo,
167141 ref : `heads/${ tempBranch } ` ,
168- sha : baseOid ,
142+ sha : baseSha ,
169143 force : true ,
170144 } ) ;
171145
@@ -181,14 +155,14 @@ export const commitFilesFromBase64 = async ({
181155 const tempCommit = await createCommit ( {
182156 octokit,
183157 refId : tempRefId ,
184- baseOid ,
158+ baseSha ,
185159 message,
186160 fileChanges,
187161 } ) ;
188162
189- const tempHeadOid = tempCommit . createCommitOnBranch ?. commit ?. oid ;
163+ const tempHeadSha = tempCommit . createCommitOnBranch ?. commit ?. oid ;
190164
191- if ( ! tempHeadOid ) {
165+ if ( ! tempHeadSha ) {
192166 throw new Error (
193167 `Failed to determine head commit of temporary branch ${ tempBranch } ` ,
194168 ) ;
@@ -198,7 +172,7 @@ export const commitFilesFromBase64 = async ({
198172 owner,
199173 repo,
200174 ref : `heads/${ branch } ` ,
201- sha : tempHeadOid ,
175+ sha : tempHeadSha ,
202176 force : true ,
203177 } ) ;
204178
@@ -226,7 +200,7 @@ export const commitFilesFromBase64 = async ({
226200 owner,
227201 repo,
228202 ref : `refs/heads/${ branch } ` ,
229- sha : baseOid ,
203+ sha : baseSha ,
230204 } ) ;
231205
232206 const refIdStr = createdRef . data . node_id ;
@@ -237,13 +211,13 @@ export const commitFilesFromBase64 = async ({
237211
238212 refId = refIdStr ;
239213 } else {
240- refId = sameBranchBase ? resolvedBaseRef ! . id : info . targetBranch ! . id ;
214+ refId = sameBranchBase ? info . baseRef ! . id : info . targetBranch ! . id ;
241215 }
242216
243217 const newCommit = await createCommit ( {
244218 octokit,
245219 refId,
246- baseOid ,
220+ baseSha ,
247221 message,
248222 fileChanges,
249223 } ) ;
0 commit comments