11import * as child_process from 'child_process'
22import * as fs from 'fs' ;
33import * as path from 'path' ;
4+ import { homedir } from 'os' ;
45import { promisify } from 'util' ;
56import * as core from '@actions/core' ;
67import Octokit from '@octokit/rest' ;
@@ -14,6 +15,7 @@ const access = promisify(fs.access);
1415const execFile = promisify ( child_process . execFile ) ;
1516const mkdir = promisify ( fs . mkdir ) ;
1617const readFile = promisify ( fs . readFile ) ;
18+ const writeFile = promisify ( fs . writeFile ) ;
1719
1820/**
1921 * Must be "true" if all queries should be run (and not just changed queries)
@@ -136,19 +138,13 @@ function isConfig(config: any): config is Config {
136138
137139 /*
138140 * Before we can run `git fetch` in the CWD,
139- * we need to add a new remote that we're authenticated with
141+ * we need to add the authentication details for the remote for https
140142 */
141- /**
142- * The name we'll use for our new remote,
143- * something that's reasonably unique.
144- * Though we will delete this remote later.
145- */
146- const remoteName = event . after ;
147- console . log ( `Adding remote ${ remoteName } ` ) ;
148- await execFile ( 'git' , [
149- 'remote' , 'add' , remoteName ,
150- `https://x-access-token:${ GITHUB_TOKEN } @github.com/${ event . repository . full_name } .git`
151- ] ) ;
143+ await execFile ( 'git' , [ 'config' , '--global' , 'credential.helper' , 'store' ] ) ;
144+ // Write the required information to ~/.git-credentials
145+ const credentials = `https://x-access-token:${ GITHUB_TOKEN } @github.com` ;
146+ const credentialsPath = path . join ( homedir ( ) , '.git-credentials' ) ;
147+ await writeFile ( credentialsPath , credentials ) ;
152148
153149 /**
154150 * The output from a successful call to `git diff --name-only`
@@ -176,9 +172,9 @@ function isConfig(config: any): config is Config {
176172 const pr = pulls . data [ 0 ] ;
177173 const baseBranch = pr . base . ref ;
178174 // Ensure we have the commits from that ref
179- await execFile ( 'git' , [ 'fetch' , remoteName , baseBranch ] ) ;
175+ await execFile ( 'git' , [ 'fetch' , 'origin' , baseBranch ] ) ;
180176 const baseSha = await ( await execFile (
181- 'git' , [ 'rev-parse' , `refs/remotes/${ remoteName } /${ baseBranch } ` ]
177+ 'git' , [ 'rev-parse' , `refs/remotes/origin /${ baseBranch } ` ]
182178 ) ) . stdout . trim ( ) ;
183179 diff = {
184180 baseSha,
@@ -221,9 +217,9 @@ function isConfig(config: any): config is Config {
221217
222218 if ( ! diff ) {
223219 try {
224- await execFile ( 'git' , [ 'fetch' , remoteName , 'HEAD' ] ) ;
220+ await execFile ( 'git' , [ 'fetch' , 'origin' , 'HEAD' ] ) ;
225221 const defaultBranchSha = await ( await execFile (
226- 'git' , [ 'rev-parse' , `refs/remotes/${ remoteName } /HEAD` ]
222+ 'git' , [ 'rev-parse' , `refs/remotes/origin /HEAD` ]
227223 ) ) . stdout . trim ( ) ;
228224 const result = await execFile (
229225 'git' , [ 'diff' , '--name-only' , `${ defaultBranchSha } ..${ event . after } ` ]
@@ -239,9 +235,6 @@ function isConfig(config: any): config is Config {
239235 }
240236 }
241237
242- console . log ( `Removing remote ${ remoteName } ` ) ;
243- await execFile ( 'git' , [ 'remote' , 'remove' , remoteName ] ) ;
244-
245238 if ( ! diff ) {
246239 unableToGetChangedQueries = true ;
247240 } else {
0 commit comments