import{logger as e}from"../logger/index.mjs";import{getDBClient as t}from"../utils/mongoDB/connectDB.mjs";import{configurationFilesCandidates as n}from"@intlayer/config/node";import{ObjectId as r}from"mongodb";const i=`https://api.bitbucket.org/2.0`,a=`https://bitbucket.org/site/oauth2`,o=e=>{let t=process.env.ATLASSIAN_CLIENT_ID;if(!t)throw Error(`Bitbucket/Atlassian Client ID is not configured`);return`${a}/authorize?${new URLSearchParams({client_id:t,response_type:`code`,state:`bitbucket_oauth`}).toString()}`},s=async t=>{let n=process.env.ATLASSIAN_CLIENT_ID,r=process.env.ATLASSIAN_CLIENT_SECRET;if(!n||!r)throw Error(`Bitbucket OAuth credentials are not configured`);try{let e=Buffer.from(`${n}:${r}`).toString(`base64`),i=await fetch(`${a}/access_token`,{method:`POST`,headers:{"Content-Type":`application/x-www-form-urlencoded`,Authorization:`Basic ${e}`},body:new URLSearchParams({grant_type:`authorization_code`,code:t})});if(!i.ok)throw Error(`Bitbucket token exchange failed: ${i.statusText}`);let o=await i.json();if(o.error)throw Error(`Bitbucket token error: ${o.error_description}`);return o.access_token}catch(t){throw e.error(`Error exchanging Bitbucket code for token:`,t),t}},c=async t=>{try{let e=await fetch(`${i}/user`,{headers:{Authorization:`Bearer ${t}`,Accept:`application/json`}});if(!e.ok)throw Error(`Failed to fetch Bitbucket user: ${e.statusText}`);let n=await fetch(`${i}/repositories?role=member&sort=-updated_on&pagelen=100`,{headers:{Authorization:`Bearer ${t}`,Accept:`application/json`}});if(!n.ok)throw Error(`Failed to fetch Bitbucket repositories: ${n.statusText}`);return(await n.json()).values||[]}catch(t){throw e.error(`Error fetching Bitbucket repositories:`,t),t}},l=async(t,r,a,o=`main`)=>{try{let e=await fetch(`${i}/repositories/${r}/${a}/src/${encodeURIComponent(o)}/?max_depth=10&pagelen=10000`,{headers:{Authorization:`Bearer ${t}`,Accept:`application/json`}});if(!e.ok){if(e.status===404)return[];throw Error(`Failed to fetch repository tree: ${e.statusText}`)}return((await e.json()).values||[]).filter(e=>e.type===`commit_file`?n.some(t=>e.path.endsWith(t)):!1).map(e=>e.path)}catch(t){return t.status===404||e.error(`Error checking intlayer configuration on Bitbucket:`,t),[]}},u=async(t,n,r,a,o=`main`)=>{try{let e=await fetch(`${i}/repositories/${n}/${r}/src/${encodeURIComponent(o)}/${encodeURIComponent(a)}`,{headers:{Authorization:`Bearer ${t}`,Accept:`application/json`}});if(!e.ok){if(e.status===404)return null;throw Error(`Failed to fetch file contents: ${e.statusText}`)}return await e.text()}catch(t){if(t.status===404)return null;throw e.error(`Error fetching Bitbucket file contents:`,t),t}},d=async n=>{try{let e=t().db(),i=await e.collection(`account`).findOne({userId:n,providerId:`atlassian`});return!i&&r.isValid(n)&&(i=await e.collection(`account`).findOne({userId:new r(n),providerId:`atlassian`})),i||=await e.collection(`accounts`).findOne({userId:n,providerId:`atlassian`}),!i&&r.isValid(n)&&(i=await e.collection(`accounts`).findOne({userId:new r(n),providerId:`atlassian`})),i&&(i.accessToken||i.access_token)||null}catch(t){return e.error(`Error retrieving Bitbucket token from DB:`,t),null}},f=async(t,n,r,a=`bitbucket-pipelines.yml`,o=`main`)=>{try{let e=await fetch(`${i}/repositories/${n}/${r}/src/${encodeURIComponent(o)}/${encodeURIComponent(a)}`,{headers:{Authorization:`Bearer ${t}`,Accept:`application/json`}});if(e.status===404)return!1;if(!e.ok)throw Error(`Failed to check file existence: ${e.statusText}`);return!0}catch(t){if(t.status===404)return!1;throw e.error(`Error checking pipeline file existence:`,t),t}},p=async(e,t,n,r=`bitbucket-pipelines.yml`,i,a=`main`,o=`Add Intlayer CI pipeline`)=>{throw Error(`Bitbucket API does not support automatic file creation. Please manually add the pipeline file to your repository.`)};export{l as checkIntlayerConfig,f as checkPipelineFileExists,p as createPipelineFile,s as exchangeCodeForToken,o as getAuthorizationUrl,d as getBitbucketTokenFromUser,u as getRepositoryFileContents,c as getUserRepositories};
//# sourceMappingURL=bitbucket.service.mjs.map