import{logger as e}from"../logger/index.mjs";import{createHmac as t}from"node:crypto";import{Octokit as n}from"@octokit/rest";const r=async t=>{let n=[];if(t.repository&&t.webhooks?.autoTriggerBuilds)try{await a(t),n.push({target:t.repository.provider,success:!0})}catch(r){e.error(`Failed to trigger ${t.repository.provider}`,r),n.push({target:t.repository.provider,success:!1,message:r.message||String(r)})}let r=t.webhooks?.webhooks||[];for(let t of r)if(t.enabled)try{await l(t),n.push({target:t.name,success:!0})}catch(r){e.error(`Failed to trigger webhook ${t.name}`,r),n.push({target:t.name,success:!1,message:r.message||String(r)})}return n},i=async(t,n)=>{let r=t.webhooks?.webhooks||[];if(n<0||n>=r.length)throw Error(`Webhook index ${n} is out of range`);let i=r[n];if(!i.enabled)throw Error(`Webhook "${i.name}" is disabled`);try{return await l(i),{target:i.name,success:!0}}catch(t){return e.error(`Failed to trigger webhook ${i.name}`,t),{target:i.name,success:!1,message:t.message||String(t)}}},a=async e=>{let{repository:t,oAuth2Access:n}=e;if(!t)throw Error(`No repository configured`);let r=n?.[0]?.accessToken?.[0];if(!r)throw Error(`No valid OAuth token found`);let{provider:i}=t;switch(i){case`github`:return o(t,r);case`gitlab`:return s(t,r);case`bitbucket`:return c(t,r);default:throw Error(`Unknown provider: ${i}`)}},o=async(t,r)=>{await new n({auth:r}).repos.createDispatchEvent({owner:t.owner,repo:t.repository,event_type:`intlayer_cms_update`,client_payload:{timestamp:new Date().toISOString(),source:`intlayer-cms`}}),e.info(`Successfully triggered GitHub Action for ${t.owner}/${t.repository}`)},s=async(t,n)=>{let r=encodeURIComponent(`${t.owner}/${t.repository}`),i=t.branch||`main`,a=`${t.instanceUrl||`https://gitlab.com`}/api/v4/projects/${r}/trigger/pipeline`,o=new FormData;o.append(`token`,n),o.append(`ref`,i),o.append(`variables[INTLAYER_UPDATE]`,`true`);let s=await fetch(a,{method:`POST`,body:o});if(!s.ok){let e=await s.text();throw Error(`GitLab error: ${s.status} - ${e}`)}e.info(`Successfully triggered GitLab pipeline for ${t.owner}/${t.repository}`)},c=async(t,n)=>{let r=t.workspace||t.owner,i=t.branch||`main`,a=`https://api.bitbucket.org/2.0/repositories/${r}/${t.repository}/pipelines/`,o={target:{ref_type:`branch`,type:`pipeline_ref_target`,ref_name:i},variables:[{key:`INTLAYER_UPDATE`,value:`true`,secured:!1}]},s=await fetch(a,{method:`POST`,headers:{Authorization:`Bearer ${n}`,"Content-Type":`application/json`},body:JSON.stringify(o)});if(!s.ok){let e=await s.text();throw Error(`Bitbucket error: ${s.status} - ${e}`)}e.info(`Successfully triggered Bitbucket pipeline for ${r}/${t.repository}`)},l=async n=>{let r={"Content-Type":`application/json`};if(n.secret){let e=JSON.stringify({event:`intlayer_cms_update`});r[`X-Intlayer-Signature`]=t(`sha256`,n.secret).update(e).digest(`hex`)}let i=await fetch(n.url,{method:`POST`,headers:r,body:JSON.stringify({event:`intlayer_cms_update`,timestamp:new Date().toISOString()})});if(!i.ok){let e=await i.text();throw Error(`Webhook ${n.name} failed: ${i.status} - ${e}`)}e.info(`Successfully triggered webhook: ${n.name}`)};export{r as triggerAll,i as triggerSingleWebhook};
//# sourceMappingURL=webhook.service.mjs.map