@@ -12,6 +12,7 @@ import { getOctokit } from "@actions/github";
1212import { commitChangesFromRepo } from "../../git" ;
1313import { getRefTreeQuery } from "../../github/graphql/queries" ;
1414import { deleteBranches } from "./util" ;
15+ import git from "isomorphic-git" ;
1516
1617const octokit = getOctokit ( ENV . GITHUB_TOKEN ) ;
1718
@@ -57,6 +58,96 @@ const expectBranchHasFile = async ({
5758 }
5859} ;
5960
61+ const expectParentHasOid = async ( {
62+ branch,
63+ oid,
64+ } : {
65+ branch : string ;
66+ oid : string ;
67+ } ) => {
68+ const commit = (
69+ await getRefTreeQuery ( octokit , {
70+ owner : REPO . owner ,
71+ name : REPO . repository ,
72+ ref : `refs/heads/${ branch } ` ,
73+ path : "README.md" ,
74+ } )
75+ ) . repository ?. ref ?. target ;
76+
77+ if ( ! commit || ! ( "parents" in commit ) ) {
78+ throw new Error ( "Expected commit to have a parent" ) ;
79+ }
80+
81+ expect ( commit . parents . nodes ) . toEqual ( [ { oid } ] ) ;
82+ } ;
83+
84+ const makeFileChanges = async ( repoDirectory : string ) => {
85+ // Update an existing file
86+ await fs . promises . writeFile (
87+ path . join ( repoDirectory , "LICENSE" ) ,
88+ "This is a new license" ,
89+ ) ;
90+ // Remove a file
91+ await fs . promises . rm ( path . join ( repoDirectory , "package.json" ) ) ;
92+ // Remove a file nested in a directory
93+ await fs . promises . rm ( path . join ( repoDirectory , "src" , "index.ts" ) ) ;
94+ // Add a new file
95+ await fs . promises . writeFile (
96+ path . join ( repoDirectory , "new-file.txt" ) ,
97+ "This is a new file" ,
98+ ) ;
99+ // Add a new file nested in a directory
100+ await fs . promises . mkdir ( path . join ( repoDirectory , "nested" ) , {
101+ recursive : true ,
102+ } ) ;
103+ await fs . promises . writeFile (
104+ path . join ( repoDirectory , "nested" , "nested-file.txt" ) ,
105+ "This is a nested file" ,
106+ ) ;
107+ // Add files that should be ignored
108+ await fs . promises . writeFile (
109+ path . join ( repoDirectory , ".env" ) ,
110+ "This file should be ignored" ,
111+ ) ;
112+ await fs . promises . mkdir ( path . join ( repoDirectory , "coverage" , "foo" ) , {
113+ recursive : true ,
114+ } ) ;
115+ await fs . promises . writeFile (
116+ path . join ( repoDirectory , "coverage" , "foo" , "bar" ) ,
117+ "This file should be ignored" ,
118+ ) ;
119+ } ;
120+
121+ const makeFileChangeAssertions = async ( branch : string ) => {
122+ // Expect the deleted files to not exist
123+ await expectBranchHasFile ( { branch, path : "package.json" , oid : null } ) ;
124+ await expectBranchHasFile ( { branch, path : "src/index.ts" , oid : null } ) ;
125+ // Expect updated file to have new oid
126+ await expectBranchHasFile ( {
127+ branch,
128+ path : "LICENSE" ,
129+ oid : "8dd03bb8a1d83212f3667bd2eb8b92746120ab8f" ,
130+ } ) ;
131+ // Expect new files to have correct oid
132+ await expectBranchHasFile ( {
133+ branch,
134+ path : "new-file.txt" ,
135+ oid : "be5b944ff55ca7569cc2ae34c35b5bda8cd5d37e" ,
136+ } ) ;
137+ await expectBranchHasFile ( {
138+ branch,
139+ path : "nested/nested-file.txt" ,
140+ oid : "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3" ,
141+ } ) ;
142+ // Expect ignored files to not exist
143+ await expectBranchHasFile ( { branch, path : ".env" , oid : null } ) ;
144+ await expectBranchHasFile ( {
145+ branch,
146+ path : "coverage/foo/bar" ,
147+ oid : null ,
148+ } ) ;
149+ } ;
150+
60151describe ( "git" , ( ) => {
61152 const branches : string [ ] = [ ] ;
62153
@@ -72,7 +163,7 @@ describe("git", () => {
72163 await fs . promises . mkdir ( testDir , { recursive : true } ) ;
73164 const repoDirectory = path . join ( testDir , "repo-1" ) ;
74165
75- // Clone the git repo locally usig the git cli and child-process
166+ // Clone the git repo locally using the git cli and child-process
76167 await new Promise < void > ( ( resolve , reject ) => {
77168 const p = exec (
78169 `git clone ${ process . cwd ( ) } repo-1` ,
@@ -89,40 +180,7 @@ describe("git", () => {
89180 p . stderr ?. pipe ( process . stderr ) ;
90181 } ) ;
91182
92- // Update an existing file
93- await fs . promises . writeFile (
94- path . join ( repoDirectory , "LICENSE" ) ,
95- "This is a new license" ,
96- ) ;
97- // Remove a file
98- await fs . promises . rm ( path . join ( repoDirectory , "package.json" ) ) ;
99- // Remove a file nested in a directory
100- await fs . promises . rm ( path . join ( repoDirectory , "src" , "index.ts" ) ) ;
101- // Add a new file
102- await fs . promises . writeFile (
103- path . join ( repoDirectory , "new-file.txt" ) ,
104- "This is a new file" ,
105- ) ;
106- // Add a new file nested in a directory
107- await fs . promises . mkdir ( path . join ( repoDirectory , "nested" ) , {
108- recursive : true ,
109- } ) ;
110- await fs . promises . writeFile (
111- path . join ( repoDirectory , "nested" , "nested-file.txt" ) ,
112- "This is a nested file" ,
113- ) ;
114- // Add files that should be ignored
115- await fs . promises . writeFile (
116- path . join ( repoDirectory , ".env" ) ,
117- "This file should be ignored" ,
118- ) ;
119- await fs . promises . mkdir ( path . join ( repoDirectory , "coverage" , "foo" ) , {
120- recursive : true ,
121- } ) ;
122- await fs . promises . writeFile (
123- path . join ( repoDirectory , "coverage" , "foo" , "bar" ) ,
124- "This file should be ignored" ,
125- ) ;
183+ await makeFileChanges ( repoDirectory ) ;
126184
127185 // Push the changes
128186 await commitChangesFromRepo ( {
@@ -137,33 +195,76 @@ describe("git", () => {
137195 log,
138196 } ) ;
139197
140- // Expect the deleted files to not exist
141- await expectBranchHasFile ( { branch, path : "package.json" , oid : null } ) ;
142- await expectBranchHasFile ( { branch, path : "src/index.ts" , oid : null } ) ;
143- // Expect updated file to have new oid
144- await expectBranchHasFile ( {
145- branch,
146- path : "LICENSE" ,
147- oid : "8dd03bb8a1d83212f3667bd2eb8b92746120ab8f" ,
148- } ) ;
149- // Expect new files to have correct oid
150- await expectBranchHasFile ( {
151- branch,
152- path : "new-file.txt" ,
153- oid : "be5b944ff55ca7569cc2ae34c35b5bda8cd5d37e" ,
198+ await makeFileChangeAssertions ( branch ) ;
199+
200+ // Expect the OID to be the HEAD commit
201+ const oid =
202+ (
203+ await git . log ( {
204+ fs,
205+ dir : repoDirectory ,
206+ ref : "HEAD" ,
207+ depth : 1 ,
208+ } )
209+ ) [ 0 ] ?. oid ?? "NO_OID" ;
210+
211+ await expectParentHasOid ( { branch, oid } ) ;
212+ } ) ;
213+
214+ it ( "should correctly be able to base changes off specific commit" , async ( ) => {
215+ const branch = `${ TEST_BRANCH_PREFIX } -specific-base` ;
216+
217+ await fs . promises . mkdir ( testDir , { recursive : true } ) ;
218+ const repoDirectory = path . join ( testDir , "repo-2" ) ;
219+
220+ // Clone the git repo locally usig the git cli and child-process
221+ await new Promise < void > ( ( resolve , reject ) => {
222+ const p = exec (
223+ `git clone ${ process . cwd ( ) } repo-2` ,
224+ { cwd : testDir } ,
225+ ( error ) => {
226+ if ( error ) {
227+ reject ( error ) ;
228+ } else {
229+ resolve ( ) ;
230+ }
231+ } ,
232+ ) ;
233+ p . stdout ?. pipe ( process . stdout ) ;
234+ p . stderr ?. pipe ( process . stderr ) ;
154235 } ) ;
155- await expectBranchHasFile ( {
156- branch,
157- path : "nested/nested-file.txt" ,
158- oid : "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3" ,
236+
237+ makeFileChanges ( repoDirectory ) ;
238+
239+ // Determine the previous commit hash
240+ const gitLog = await git . log ( {
241+ fs,
242+ dir : repoDirectory ,
243+ ref : "HEAD" ,
244+ depth : 2 ,
159245 } ) ;
160- // Expect ignored files to not exist
161- await expectBranchHasFile ( { branch, path : ".env" , oid : null } ) ;
162- await expectBranchHasFile ( {
246+
247+ const oid = gitLog [ 1 ] ?. oid ?? "" ;
248+
249+ // Push the changes
250+ await commitChangesFromRepo ( {
251+ octokit,
252+ ...REPO ,
163253 branch,
164- path : "coverage/foo/bar" ,
165- oid : null ,
254+ message : {
255+ headline : "Test commit" ,
256+ body : "This is a test commit" ,
257+ } ,
258+ repoDirectory,
259+ log,
260+ base : {
261+ commit : oid ,
262+ } ,
166263 } ) ;
264+
265+ await makeFileChangeAssertions ( branch ) ;
266+
267+ await expectParentHasOid ( { branch, oid } ) ;
167268 } ) ;
168269 } ) ;
169270
0 commit comments