feat(core): Added retrieval class for Gitea based assets.
This commit is contained in:
		| @@ -1,5 +1,12 @@ | ||||
| # Changelog | ||||
|  | ||||
| ## 2025-01-01 - 1.8.0 - feat(core) | ||||
| Added GiteaAssets class for managing files in Gitea repositories | ||||
|  | ||||
| - Introduced GiteaAssets class to handle file retrieval from Gitea repositories. | ||||
| - Added tests for GiteaAssets implementation. | ||||
| - Updated plugins module to include smartrequest for HTTP requests. | ||||
|  | ||||
| ## 2024-11-05 - 1.7.7 - fix(core) | ||||
| Fix dependency resolution in package initialization | ||||
|  | ||||
|   | ||||
| @@ -54,6 +54,7 @@ | ||||
|     "@push.rocks/smartlog": "^3.0.7", | ||||
|     "@push.rocks/smartnpm": "^2.0.4", | ||||
|     "@push.rocks/smartpath": "^5.0.18", | ||||
|     "@push.rocks/smartrequest": "^2.0.23", | ||||
|     "@push.rocks/smartshell": "^3.0.6" | ||||
|   }, | ||||
|   "keywords": [ | ||||
|   | ||||
							
								
								
									
										7400
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7400
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										39
									
								
								test/test.giteaassets.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								test/test.giteaassets.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| import { expect, tap } from '@push.rocks/tapbundle'; | ||||
| import * as giteaAssets from '../ts/classes.giteaassets.js'; | ||||
|  | ||||
| let giteaAssetsInstance: giteaAssets.GiteaAssets; | ||||
|  | ||||
| tap.test('should create a GiteaAssets instance', async () => { | ||||
|   giteaAssetsInstance = new giteaAssets.GiteaAssets({ | ||||
|     giteaBaseUrl: 'https://code.foss.global', | ||||
|   }); | ||||
|   expect(giteaAssetsInstance).toBeInstanceOf(giteaAssets.GiteaAssets); | ||||
| }); | ||||
|  | ||||
| tap.test('should get files from a repository', async () => { | ||||
|   const files = await giteaAssetsInstance.getFiles('git.zone', 'cli', 'assets/templates/cli'); | ||||
|   console.log(files); | ||||
|  | ||||
|   for (const file of files) { | ||||
|     if (file.name.endsWith('cli.js')) { | ||||
|       console.log(atob(file.base64Content)); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   expect(files).toBeTruthy(); | ||||
| }); | ||||
|  | ||||
| tap.test('should get files from a repository', async () => { | ||||
|   const files = await giteaAssetsInstance.getFiles('git.zone', 'cli', 'assets/templates/cli/cli.js'); | ||||
|   console.log(files); | ||||
|  | ||||
|   for (const file of files) { | ||||
|     if (file.name.endsWith('cli.js')) { | ||||
|       console.log(atob(file.base64Content)); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   expect(files).toBeTruthy(); | ||||
| }); | ||||
|  | ||||
| tap.start(); | ||||
| @@ -3,6 +3,6 @@ | ||||
|  */ | ||||
| export const commitinfo = { | ||||
|   name: '@git.zone/tspublish', | ||||
|   version: '1.7.7', | ||||
|   version: '1.8.0', | ||||
|   description: 'A tool to publish multiple, concise, and small packages from monorepos, specifically for TypeScript projects within a git environment.' | ||||
| } | ||||
|   | ||||
							
								
								
									
										103
									
								
								ts/classes.giteaassets.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								ts/classes.giteaassets.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,103 @@ | ||||
| import * as plugins from './plugins.js'; | ||||
|  | ||||
| interface IRepoFile { | ||||
|   name: string; | ||||
|   path: string; | ||||
|   type: 'file' | 'dir'; | ||||
|   download_url: string | null; | ||||
|   base64Content: string | null; | ||||
|   encoding: string | null; | ||||
| } | ||||
|  | ||||
| interface IGiteaAssetsOptions { | ||||
|   giteaBaseUrl: string; // Base URL of your Gitea instance | ||||
|   token?: string; // Optional token for private repositories | ||||
| } | ||||
|  | ||||
| export class GiteaAssets { | ||||
|   private baseUrl: string; | ||||
|   private headers: {[key: string]: string} = {}; | ||||
|  | ||||
|   constructor(options: IGiteaAssetsOptions) { | ||||
|     this.baseUrl = options.giteaBaseUrl | ||||
|     if (this.baseUrl.endsWith('/')) { | ||||
|       this.baseUrl = this.baseUrl.slice(0, -1); | ||||
|     } | ||||
|     this.baseUrl += '/api/v1'; | ||||
|     this.headers = options.token | ||||
|         ? { ...this.headers, 'Authorization': `token ${options.token}` } | ||||
|         : this.headers; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Get all files in a directory of a repository | ||||
|    * @param owner - Repository owner | ||||
|    * @param repo - Repository name | ||||
|    * @param directory - Directory path ('' for root) | ||||
|    * @param branch - Branch name (optional) | ||||
|    * @returns A list of files in the directory | ||||
|    */ | ||||
|   async getFiles( | ||||
|     owner: string, | ||||
|     repo: string, | ||||
|     directory: string, | ||||
|     branch?: string | ||||
|   ): Promise<IRepoFile[]> { | ||||
|     try { | ||||
|       const response = await plugins.smartrequest.request( | ||||
|         this.baseUrl + `/repos/${owner}/${repo}/contents/${directory}`, | ||||
|         { | ||||
|           headers: this.headers, | ||||
|           method: 'GET', | ||||
|           params: branch ? { ref: branch } : {}, | ||||
|         } | ||||
|       ) | ||||
|       if (!Array.isArray(response.body) && typeof response.body === 'object') { | ||||
|         response.body = [response.body]; | ||||
|       } else if (Array.isArray(response.body)) { | ||||
|         for (const entry of response.body) { | ||||
|           if (entry.type === 'dir') { | ||||
|             continue; | ||||
|           } else if (entry.type === 'file') { | ||||
|             const response2 = await plugins.smartrequest.request( | ||||
|               this.baseUrl + `/repos/${owner}/${repo}/contents/${entry.path}`, | ||||
|               { | ||||
|                 headers: this.headers, | ||||
|                 method: 'GET', | ||||
|                 params: branch ? { ref: branch } : {}, | ||||
|               } | ||||
|             ); | ||||
|             entry.encoding = response2.body.encoding; | ||||
|             entry.content = response2.body.content; | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       // lets map to the IRepoFile interface | ||||
|       response.body = response.body.map((entry: any) => { | ||||
|         return { | ||||
|           name: entry.name, | ||||
|           path: entry.path, | ||||
|           type: entry.type, | ||||
|           download_url: entry.download_url, | ||||
|           base64Content: entry.content, | ||||
|           encoding: entry.encoding, | ||||
|         }; | ||||
|       }); | ||||
|  | ||||
|       return response.body; | ||||
|     } catch (error) { | ||||
|       console.error('Error fetching repository files:', error); | ||||
|       throw error; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * gets the current cli entry file from the code.foss.global/git.zone/cli repository | ||||
|    * @returns | ||||
|    */ | ||||
|   public async getBinCliEntryFile() { | ||||
|     const files = await this.getFiles('git.zone', 'cli', 'assets/templates/cli/cli.js'); | ||||
|     return files[0]; | ||||
|   } | ||||
| } | ||||
| @@ -6,4 +6,8 @@ export interface ITsPublishJson { | ||||
|   name: string; | ||||
|   dependencies: string[]; | ||||
|   registries: string[]; | ||||
|   /** | ||||
|    * allows the sepcification of bin names that invoke cli scripts | ||||
|    */ | ||||
|   bin: string[]; | ||||
| } | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import * as smartdelay from '@push.rocks/smartdelay'; | ||||
| import * as smartlog from '@push.rocks/smartlog'; | ||||
| import * as smartnpm from '@push.rocks/smartnpm'; | ||||
| import * as smartpath from '@push.rocks/smartpath'; | ||||
| import * as smartrequest from '@push.rocks/smartrequest'; | ||||
| import * as smartshell from '@push.rocks/smartshell'; | ||||
|  | ||||
| export { smartfile, smartcli, smartdelay, smartlog, smartnpm, smartpath, smartshell }; | ||||
| export { smartfile, smartcli, smartdelay, smartlog, smartnpm, smartpath, smartrequest, smartshell }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user