import { Connection, ConnectionManager, Connector } from '@things-factory/integration-base' import simpleGit from 'simple-git' export class GitConnector implements Connector { async ready(connectionConfigs) { await Promise.all(connectionConfigs.map(this.connect.bind(this))) ConnectionManager.logger.info('Git connections are ready') } // async checkConnectionInstance(domain, connectionName): Promise { // try { // const connection = await ConnectionManager.getConnectionInstanceByName(domain, connectionName) // return !!connection // } catch (e) { // return false // } // } async connect(connection) { var { endpoint, // Git 저장소 URL (ex: https://git.com/user/repo.git, https://gitlab.com/user/repo.git, ssh://git@bitbucket.org/user/repo.git) params: { branch = 'main', token, sshKeyPath } } = connection try { // ✅ Git 클라이언트 생성 const client = simpleGit() // ✅ Git 인증 처리 (토큰 방식 vs SSH 방식) let remoteUrl = endpoint if (token) { // HTTP 인증 방식 (예: GitHub, GitLab의 PAT) remoteUrl = endpoint.replace('https://', `https://${token}@`) } else if (sshKeyPath) { // SSH 방식 인증 (예: Bitbucket, 사내 Git 서버) client.env({ GIT_SSH_COMMAND: `ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no` }) } // ✅ ConnectionManager에 저장할 때 `remoteUrl` 추가 ConnectionManager.addConnectionInstance(connection, { client, branch, endpoint, remoteUrl, // ✅ remoteUrl 추가 token, sshKeyPath }) ConnectionManager.logger.info(`Git connection(${connection.name}:${endpoint}) is connected`) } catch (ex) { ConnectionManager.logger.error(`Git connection(${connection.name}:${endpoint}) failed to connect`, ex) throw ex } } async disconnect(connection: Connection) { ConnectionManager.removeConnectionInstance(connection) ConnectionManager.logger.info(`Git connection(${connection.name}) is disconnected`) } get parameterSpec() { return [ { type: 'string', name: 'branch', label: 'git.branch' }, { type: 'string', name: 'token', label: 'git.token' }, { type: 'string', name: 'sshKeyPath', label: 'git.ssh-key-path' } ] } get taskPrefixes() { return ['git'] } get help() { return 'integration/connector/git-connector' } } ConnectionManager.registerConnector('git-connector', new GitConnector())