Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit bbcb514

Browse files
committed
Add HTTP auth rather than adding a remote for fetching
1 parent c5f6040 commit bbcb514

File tree

1 file changed

+12
-19
lines changed
  • codeql-learninglab-check/package/src

1 file changed

+12
-19
lines changed

codeql-learninglab-check/package/src/index.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as child_process from 'child_process'
22
import * as fs from 'fs';
33
import * as path from 'path';
4+
import {homedir} from 'os';
45
import { promisify } from 'util';
56
import * as core from '@actions/core';
67
import Octokit from '@octokit/rest';
@@ -14,6 +15,7 @@ const access = promisify(fs.access);
1415
const execFile = promisify(child_process.execFile);
1516
const mkdir = promisify(fs.mkdir);
1617
const 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

Comments
 (0)