|
| 1 | +import retry from 'async-retry'; |
| 2 | +import { defaultLogger } from '../../../log/logger'; |
1 | 3 | import { BitbucketHttpClient } from '../http/bitbucket-http.client'; |
2 | | -import { BitbucketRepository, BitbucketBranch, BitbucketCommit, BitbucketPaginatedResponse } from '../types/bitbucket.types'; |
| 4 | +import { BitbucketRepository, BitbucketBranch, BitbucketCommit, BitbucketPaginatedResponse, BitbucketDirectoryEntry } from '../types/bitbucket.types'; |
3 | 5 |
|
4 | 6 | export class BitbucketRepositoryService { |
5 | 7 | constructor(private readonly httpClient: BitbucketHttpClient) {} |
@@ -47,4 +49,122 @@ export class BitbucketRepositoryService { |
47 | 49 | public async getFileContent(workspace: string, repoSlug: string, filePath: string, ref: string = 'main'): Promise<string> { |
48 | 50 | return this.httpClient.get<string>(`/repositories/${workspace}/${repoSlug}/src/${ref}/${filePath}`); |
49 | 51 | } |
| 52 | + |
| 53 | + public async getDirectoryContent(workspace: string, repoSlug: string, path: string, ref: string = 'main'): Promise<BitbucketDirectoryEntry[]> { |
| 54 | + // Input validation |
| 55 | + if (!workspace || workspace.trim() === '') { |
| 56 | + throw new Error('Workspace is required and cannot be empty'); |
| 57 | + } |
| 58 | + if (!repoSlug || repoSlug.trim() === '') { |
| 59 | + throw new Error('Repository slug is required and cannot be empty'); |
| 60 | + } |
| 61 | + if (!ref || ref.trim() === '') { |
| 62 | + throw new Error('Reference is required and cannot be empty'); |
| 63 | + } |
| 64 | + |
| 65 | + const trimmedWorkspace = workspace.trim(); |
| 66 | + const trimmedRepoSlug = repoSlug.trim(); |
| 67 | + const trimmedPath = path.trim(); |
| 68 | + const trimmedRef = ref.trim(); |
| 69 | + |
| 70 | + try { |
| 71 | + const response = await retry( |
| 72 | + async () => { |
| 73 | + return await this.httpClient.get<BitbucketPaginatedResponse<BitbucketDirectoryEntry>>( |
| 74 | + `/repositories/${trimmedWorkspace}/${trimmedRepoSlug}/src/${trimmedRef}/${trimmedPath}` |
| 75 | + ); |
| 76 | + }, |
| 77 | + { |
| 78 | + retries: 3, |
| 79 | + minTimeout: 1000, |
| 80 | + maxTimeout: 5000, |
| 81 | + factor: 2, |
| 82 | + onRetry: (error: Error, attempt: number) => { |
| 83 | + defaultLogger.warn({ |
| 84 | + operation: 'getDirectoryContent', |
| 85 | + workspace: trimmedWorkspace, |
| 86 | + repoSlug: trimmedRepoSlug, |
| 87 | + path: trimmedPath, |
| 88 | + ref: trimmedRef, |
| 89 | + attempt, |
| 90 | + error: error.message |
| 91 | + }, `Retrying directory content retrieval (attempt ${attempt}/3)`); |
| 92 | + } |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + // Defensive validation of response |
| 97 | + if (!response || !Array.isArray(response.values)) { |
| 98 | + defaultLogger.warn({ |
| 99 | + operation: 'getDirectoryContent', |
| 100 | + workspace: trimmedWorkspace, |
| 101 | + repoSlug: trimmedRepoSlug, |
| 102 | + path: trimmedPath, |
| 103 | + ref: trimmedRef, |
| 104 | + responseType: typeof response, |
| 105 | + hasValues: !!response?.values |
| 106 | + }, `Invalid response structure for directory content, returning empty array`); |
| 107 | + return []; |
| 108 | + } |
| 109 | + |
| 110 | + defaultLogger.info({ |
| 111 | + operation: 'getDirectoryContent', |
| 112 | + workspace: trimmedWorkspace, |
| 113 | + repoSlug: trimmedRepoSlug, |
| 114 | + path: trimmedPath, |
| 115 | + ref: trimmedRef, |
| 116 | + itemCount: response.values.length |
| 117 | + }, `Successfully retrieved directory content for ${trimmedWorkspace}/${trimmedRepoSlug}/${trimmedPath}`); |
| 118 | + |
| 119 | + return response.values; |
| 120 | + } catch (error: any) { |
| 121 | + // Handle 404 errors gracefully for idempotent operations |
| 122 | + if (error.response?.status === 404 || error.status === 404 || error.message?.includes('404')) { |
| 123 | + defaultLogger.info({ |
| 124 | + operation: 'getDirectoryContent', |
| 125 | + workspace: trimmedWorkspace, |
| 126 | + repoSlug: trimmedRepoSlug, |
| 127 | + path: trimmedPath, |
| 128 | + ref: trimmedRef, |
| 129 | + status: 'not_found' |
| 130 | + }, `Directory content not found for ${trimmedWorkspace}/${trimmedRepoSlug}/${trimmedPath} (404 Not Found)`); |
| 131 | + return []; |
| 132 | + } |
| 133 | + |
| 134 | + defaultLogger.error({ |
| 135 | + operation: 'getDirectoryContent', |
| 136 | + workspace: trimmedWorkspace, |
| 137 | + repoSlug: trimmedRepoSlug, |
| 138 | + path: trimmedPath, |
| 139 | + ref: trimmedRef, |
| 140 | + error: error.message, |
| 141 | + status: error.response?.status || error.status |
| 142 | + }, `Failed to get directory content for ${trimmedWorkspace}/${trimmedRepoSlug}/${trimmedPath}`); |
| 143 | + throw error; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public async deleteFile(workspace: string, repoSlug: string, filePath: string, branch: string = 'main', commitMessage: string = 'Delete file'): Promise<void> { |
| 148 | + try { |
| 149 | + // Bitbucket API requires a commit with file deletion |
| 150 | + const commitData = { |
| 151 | + message: commitMessage, |
| 152 | + branch: branch, |
| 153 | + files: { |
| 154 | + [filePath]: null // null value indicates file deletion |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + await this.httpClient.post(`/repositories/${workspace}/${repoSlug}/src`, commitData, { |
| 159 | + headers: { |
| 160 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 161 | + }, |
| 162 | + }); |
| 163 | + |
| 164 | + console.log(`Successfully deleted file ${filePath} from ${workspace}/${repoSlug}`); |
| 165 | + } catch (error) { |
| 166 | + console.error(`Failed to delete file ${filePath} from ${workspace}/${repoSlug}:`, error); |
| 167 | + throw error; |
| 168 | + } |
| 169 | + } |
50 | 170 | } |
0 commit comments