Skip to content

Commit a754f94

Browse files
committed
Add GitHub repository cloning functionality
- Create new script (src/clone-repo.ts) to clone GitHub repositories using GitHub App installation tokens - Add "clone" npm script to package.json - Update README.md with detailed instructions on how the clone script works - Update .gitignore to exclude the cloned-repo directory The script utilizes the x-access-token authentication method for secure GitHub repository cloning with optimized options (shallow clone, single branch).
1 parent 32ecad1 commit a754f94

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
.LSOverride
2424

2525
# Icon must end with two \r
26-
Icon
26+
Icon
27+
2728

2829
# Thumbnails
2930
._*
@@ -215,3 +216,6 @@ $RECYCLE.BIN/
215216
*.lnk
216217

217218
# End of https://www.toptal.com/developers/gitignore/api/node,linux,macos,windows
219+
220+
# For https://github.com/codenote-net/github-rag-examples
221+
cloned-repo

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ This executes the `src/index.ts` file directly using Node.js.
3838

3939
This project includes a script to clone a GitHub repository using a GitHub App installation token. The script uses the x-access-token authentication method as described in the [GitHub documentation](https://docs.github.com/ja/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation).
4040

41+
### How the Clone Script Works
42+
43+
The `src/clone-repo.ts` script:
44+
45+
1. Uses the GitHub App installation token with the `x-access-token` authentication method
46+
2. Validates environment variables for token and repository information
47+
3. Constructs a secure clone URL with the token
48+
4. Removes any existing clone directory to ensure a clean clone
49+
5. Clones the repository with optimized options:
50+
- `--single-branch`: Clones only the default branch
51+
- `--no-tags`: Skips downloading tags
52+
- `--depth 1`: Creates a shallow clone with only the latest commit
53+
54+
The script is configured in `package.json` to run with the `--experimental-strip-types` flag, which enables TypeScript execution without transpilation:
55+
56+
```json
57+
{
58+
"scripts": {
59+
"clone": "node --experimental-strip-types src/clone-repo.ts"
60+
}
61+
}
62+
```
63+
4164
### Usage
4265

4366
1. Set the required environment variables:
@@ -49,13 +72,13 @@ export REPO_NAME="repository_name"
4972
export CLONE_DIR="./cloned-repo" # Optional, defaults to './cloned-repo'
5073
```
5174

52-
2. Run the clone script:
75+
2. Run the clone script using npm:
5376

5477
```bash
5578
npm run clone
5679
```
5780

58-
This will clone the specified repository using the GitHub App installation token with the specified branch or tag.
81+
This command executes the TypeScript file directly using Node.js with type stripping, eliminating the need for a separate build step before running the script.
5982

6083
## How It Works
6184

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"start": "node --experimental-strip-types src/index.ts",
88
"test": "echo \"Error: no test specified\" && exit 1",
9-
"dev": "node --experimental-strip-types --watch src/index.ts"
9+
"dev": "node --experimental-strip-types --watch src/index.ts",
10+
"clone": "node --experimental-strip-types src/clone-repo.ts"
1011
},
1112
"keywords": [
1213
"github",

src/clone-repo.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* GitHub Repository Clone Script
3+
*
4+
* This script clones a GitHub repository using a GitHub App installation token.
5+
* It uses the x-access-token authentication method as described in:
6+
* https://docs.github.com/ja/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
7+
*/
8+
9+
import { execSync } from 'node:child_process';
10+
import path from 'node:path';
11+
import fs from 'node:fs';
12+
13+
// Configuration - Replace these with your actual values
14+
const TOKEN = process.env.GITHUB_TOKEN;
15+
const REPO_OWNER = process.env.REPO_OWNER;
16+
const REPO_NAME = process.env.REPO_NAME;
17+
const CLONE_DIR = process.env.CLONE_DIR || './cloned-repo';
18+
19+
// Validate token
20+
if (!TOKEN) {
21+
console.error('Error: GitHub token is required. Please set the GITHUB_TOKEN environment variable.');
22+
process.exit(1);
23+
}
24+
25+
// Validate repository information
26+
if (REPO_OWNER === 'owner' || REPO_NAME === 'repo') {
27+
console.error('Error: Please configure REPO_OWNER and REPO_NAME environment variables.');
28+
process.exit(1);
29+
}
30+
31+
// Construct the clone URL with the token
32+
const cloneUrl = `https://x-access-token:${TOKEN}@github.com/${REPO_OWNER}/${REPO_NAME}.git`;
33+
34+
console.log(`\nCloning repository: ${REPO_OWNER}/${REPO_NAME}`);
35+
console.log(`Clone directory: ${path.resolve(CLONE_DIR)}`);
36+
37+
try {
38+
// Make sure the target directory doesn't exist
39+
if (fs.existsSync(CLONE_DIR)) {
40+
console.log(`\nRemoving existing directory: ${CLONE_DIR}`);
41+
fs.rmSync(CLONE_DIR, { recursive: true, force: true });
42+
}
43+
44+
// Clone the repository with the specified branch or tag
45+
console.log('\nCloning repository...');
46+
execSync(
47+
`git clone --single-branch --no-tags --depth 1 ${cloneUrl} ${CLONE_DIR}`,
48+
{ stdio: 'inherit' }
49+
);
50+
51+
console.log('\nRepository cloned successfully! 🎉');
52+
} catch (error) {
53+
console.error('\nError cloning repository:', error);
54+
process.exit(1);
55+
}

0 commit comments

Comments
 (0)