-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoperations.ts
More file actions
117 lines (101 loc) · 3.42 KB
/
Copy pathoperations.ts
File metadata and controls
117 lines (101 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { SimpleGit, simpleGit } from 'simple-git';
import { logger } from '../config/logger';
import { GitHubError } from '../utils/error';
import { envConfig } from '../config/env';
/**
* Create a new branch and switch to it
*/
export const createBranch = async (git: SimpleGit, branchName: string): Promise<void> => {
try {
logger.info({ branchName }, 'Creating new branch');
// Check if branch already exists
const branches = await git.branch();
if (branches.all.includes(branchName)) {
throw new GitHubError(`Branch ${branchName} already exists`);
}
// Create and checkout new branch
await git.checkoutLocalBranch(branchName);
logger.info({ branchName }, 'Branch created successfully');
} catch (error) {
logger.error({ branchName, error }, 'Failed to create branch');
throw new GitHubError(
`Failed to create branch: ${error instanceof Error ? error.message : String(error)}`,
);
}
};
/**
* Checkout a remote branch by name or SHA.
*/
export const checkoutRemoteBranch = async (
repoPath: string,
ref: string,
sha?: string,
): Promise<void> => {
const git = simpleGit(repoPath);
try {
logger.info({ repoPath, ref, sha }, 'Checking out remote branch/ref');
// Fetch all remote branches to ensure the ref is available locally
await git.fetch('origin');
// If a SHA is provided, try to checkout that specific commit
if (sha) {
await git.checkout(sha);
logger.info({ repoPath, sha }, 'Checked out specific commit SHA');
} else {
// Otherwise, checkout the branch directly
await git.checkout(ref);
logger.info({ repoPath, ref }, 'Checked out remote branch');
}
} catch (error) {
logger.error({ repoPath, ref, sha, error }, 'Failed to checkout remote branch/ref');
throw new GitHubError(
`Failed to checkout remote branch/ref ${ref}: ${error instanceof Error ? error.message : String(error)}`,
);
}
};
/**
* Stage and commit changes
*/
export const commitChanges = async (git: SimpleGit, message: string): Promise<void> => {
try {
logger.info('Committing changes');
// Configure Git identity for the GitHub App
await git.addConfig('user.name', envConfig.GIT_BOT_USERNAME);
await git.addConfig('user.email', envConfig.GIT_BOT_EMAIL);
// Stage all changes
await git.add('.');
// Check if there are any changes to commit
const status = await git.status();
if (
status.modified.length === 0 &&
status.created.length === 0 &&
status.deleted.length === 0
) {
logger.info('No changes to commit');
return;
}
// Commit changes
await git.commit(message);
logger.info('Changes committed successfully');
} catch (error) {
logger.error({ error }, 'Failed to commit changes');
throw new GitHubError(
`Failed to commit changes: ${error instanceof Error ? error.message : String(error)}`,
);
}
};
/**
* Push changes to remote repository
*/
export const pushChanges = async (git: SimpleGit, branchName: string): Promise<void> => {
try {
logger.info({ branchName }, 'Pushing changes');
// Push to remote
await git.push('origin', branchName);
logger.info({ branchName }, 'Changes pushed successfully');
} catch (error) {
logger.error({ branchName, error }, 'Failed to push changes');
throw new GitHubError(
`Failed to push changes: ${error instanceof Error ? error.message : String(error)}`,
);
}
};