import{logger as e}from"../logger/index.mjs";import{checkPipelineFileExists as t}from"./bitbucket.service.mjs";import{checkWorkflowFileExists as n,createWorkflowFile as r}from"./github.service.mjs";import{checkPipelineFileExists as i,createPipelineFile as a}from"./gitlab.service.mjs";const o=`.github/workflows/intlayer-cms.yml`,s=`.gitlab-ci.yml`,c=`bitbucket-pipelines.yml`,l=`name: Intlayer CMS Update
on:
  repository_dispatch:
    types: [intlayer_cms_update]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm run build
`,u=`stages:
  - build

intlayer_cms_update:
  stage: build
  only:
    - triggers
  script:
    - npm ci
    - npm run build
`,d=`pipelines:
  custom:
    intlayer-cms-update:
      - step:
          name: Build
          script:
            - npm ci
            - npm run build
`,f=e=>(e.oAuth2Access?.[0])?.accessToken?.[0]||null,p=async r=>{let{repository:a}=r;if(!a)throw Error(`Project is not connected to a repository.`);let p=f(r);if(!p)throw Error(`No valid OAuth2 access token found.`);let m=a.branch||`main`;try{switch(a.provider){case`github`:{let{owner:e,repository:t}=a,r=o;return{exists:await n(p,e,t,r,m),content:l,path:r,fileUrl:`https://github.com/${e}/${t}/blob/${m}/${r}`,allowAutoPush:!0}}case`gitlab`:{let{projectId:e,instanceUrl:t}=a;if(!e)throw Error(`GitLab project ID is required.`);let n=s;return{exists:await i(p,e,n,m,t),content:u,path:n,fileUrl:`${t||`https://gitlab.com`}/${a.owner}/${a.repository}/-/blob/${m}/${n}`,allowAutoPush:!0}}case`bitbucket`:{let{workspace:e,repository:n}=a,r=c;return{exists:await t(p,e,n,r,m),content:d,path:r,fileUrl:`https://bitbucket.org/${e}/${n}/src/${m}/${r}`,allowAutoPush:!1}}default:throw Error(`Unsupported repository provider: ${a.provider}`)}}catch(t){throw e.error(`Error getting CI status:`,t),t}},m=async t=>{let{repository:n}=t;if(!n)throw Error(`Project is not connected to a repository.`);let i=f(t);if(!i)throw Error(`No valid OAuth2 access token found.`);let c=n.branch||`main`;try{switch(n.provider){case`github`:{let{owner:e,repository:t}=n;await r(i,e,t,o,l,c,`Add Intlayer CMS workflow`);break}case`gitlab`:{let{projectId:e,instanceUrl:t}=n;if(!e)throw Error(`GitLab project ID is required.`);await a(i,e,s,u,c,t,`Add Intlayer CMS pipeline`);break}case`bitbucket`:throw Error(`Bitbucket does not support automatic CI file installation. Please add the file manually.`);default:throw Error(`Unsupported repository provider: ${n.provider}`)}e.info(`Successfully installed CI configuration for project ${t.id}`)}catch(t){throw e.error(`Error installing CI configuration:`,t),t}};export{c as BITBUCKET_PIPELINE_FILENAME,d as BITBUCKET_TEMPLATE,l as GITHUB_TEMPLATE,o as GITHUB_WORKFLOW_FILENAME,s as GITLAB_PIPELINE_FILENAME,u as GITLAB_TEMPLATE,p as getCIStatus,m as installCI};
//# sourceMappingURL=ci.service.mjs.map