@harvestr-api/integration-github
Advanced tools
Comparing version 4.7.9 to 4.7.10
@@ -1,1 +0,1 @@ | ||
!function(e,t){for(var i in t)e[i]=t[i]}(exports,function(e){var t={};function i(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=e,i.c=t,i.d=function(e,t,o){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)i.d(o,r,function(t){return e[t]}.bind(null,r));return o},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=5)}([function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.githubApiService=t.getGithubBearer=void 0;const n=i(1),u=i(2),a=s(i(3)),l=i(4),c=async e=>{var t;const i=await n.apiTokenService.getApiTokens({where:{source:"GITHUB"}},e);return i.length?null===(t=i[0])||void 0===t?void 0:t.token:void 0};async function g(e,i){return![void 0,"undefined",null,""].includes(e)?e:(await t.githubApiService.getUser(i)).login}t.getGithubBearer=async(e,t)=>{let i;if(i=t||await c(e),void 0===i)throw new Error("getGithubBearer() : Not authorized");return{bearer:i}},t.githubApiService={urlGet:async(e,i)=>a.get({url:e.url,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}}),async createIssue(e,i){const o=null!==e.harvestrIssue.githubOwnerName&&""!==e.harvestrIssue.githubOwnerName,r=o?{}:await t.githubApiService.getUser(i),s=await a.post({url:`https://api.github.com/repos/${o?e.harvestrIssue.githubOwnerName:r.login}/${e.harvestrIssue.githubRepositoryName}/issues`,auth:await t.getGithubBearer(i),body:t.githubApiService.toGithubIssue(e.harvestrIssue),json:!0,headers:{"user-agent":"node.js"}});return u.logger.info("githuApiService.createIssue() : before githubApiService.linkIssue"),await t.githubApiService.linkIssue({harvestrIssue:await t.githubApiService.toHarvestrGithubIssue(s,e.harvestrIssue,i),discovery:e.discovery,gqlMutationLinkIssue:e.gqlMutationLinkIssue},i),u.logger.info("githuApiService.createIssue() : after githubApiService.linkIssue"),"number"==typeof e.harvestrIssue.githubColumnId&&e.harvestrIssue.githubColumnId>-1&&await t.githubApiService.createCard({column_id:e.harvestrIssue.githubColumnId,issue_id:s.id},i),s},createCard:async(e,i)=>a.post({url:`https://api.github.com/projects/columns/${e.column_id}/cards`,auth:await t.getGithubBearer(i),body:{content_id:e.issue_id,content_type:"Issue"},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),async unlinkIssue(e){"function"==typeof e.gqlMutationUnlinkIssue?await e.gqlMutationUnlinkIssue():await n.githubStorageService.deleteIssueById(e.issueId)},async linkIssue(e,i){var o,r,s,a,c,g,h,d;const p=await this.getIssue({owner_name:(null==e?void 0:e.harvestrIssue.githubOwnerName)||"",repository_name:(null===(o=null==e?void 0:e.harvestrIssue)||void 0===o?void 0:o.githubRepositoryName)||"",issue_number:(null===(r=null==e?void 0:e.harvestrIssue)||void 0===r?void 0:r.githubNumber)||0},i);"function"==typeof e.gqlMutationLinkIssue?(u.logger.info("githubApiService.linkIssue() : before gqlMutationLinkIssue()"),await e.gqlMutationLinkIssue(await t.githubApiService.toHarvestrGithubIssue(p,e.harvestrIssue,i)),u.logger.info("githubApiService.linkIssue() : after gqlMutationLinkIssue()")):await n.githubStorageService.storeIssue(await t.githubApiService.toHarvestrGithubIssue(p,e.harvestrIssue,i),e.discovery),await t.githubApiService.createComment({owner_name:(null==e?void 0:e.harvestrIssue.githubOwnerName)||"",repository_name:(null===(s=null==e?void 0:e.harvestrIssue)||void 0===s?void 0:s.githubRepositoryName)||"",issue_number:(null===(a=null==e?void 0:e.harvestrIssue)||void 0===a?void 0:a.githubNumber)||0,comment:{body:`**Issue was linked to Harvestr Discovery:** [${(null===(c=null==e?void 0:e.discovery)||void 0===c?void 0:c.title)||""}](${process.env.HARVESTR_CLIENT_URL}/components/0/list/${null===(g=u.toClientId(null==e?void 0:e.discovery))||void 0===g?void 0:g.id})`}},i),u.logger.info("githubApiService.linkIssue() : before githubWebhookService.registerWebhook()"),await l.githubWebhookService.registerWebhook((null===(h=null==e?void 0:e.harvestrIssue)||void 0===h?void 0:h.githubOwnerName)||null,(null===(d=null==e?void 0:e.harvestrIssue)||void 0===d?void 0:d.githubRepositoryName)||"",i),u.logger.info("githubApiService.linkIssue() : after githubWebhookService.registerWebhook()")},hasIntegrationEnabled:async e=>void 0!==await c({project_id:e,type:"INTEGRATION_GITHUB"}),async syncIssues(e,i){console.log("github.syncIssues() --------------------"),console.log("github.syncIssues() : where.projectsIds : ",e.projectsIds);const o=e.projectsIds.map(async e=>{if(!1===await t.githubApiService.hasIntegrationEnabled(e))return[];{const t=`${i.devtools.BASE_REDIS_ISSUES_SYNC_KEY}${e}`;if(!1===await i.devtools.canSyncToday("github",t))return console.log(`github : sync OFF for projectId ${e} : last sync less than 24h`),[];{console.log("github : sync ON for projectId "+e);const t=(await n.githubStorageService.getIssuesOfProjectForSync({projectId:e})).filter(e=>"0"!==String(e.githubId)&&null!==e.githubId);return this.bulkSyncIssuesOfProjects(t,{project_id:e,type:"INTEGRATION_GITHUB"})}}});return console.log("github.syncIssues(): END"),o},async bulkSyncIssuesOfProjects(e,i){return u.parallelPromiseHelper.uploadData(e,async e=>e.map(async e=>{try{const o=await t.githubApiService.getIssue({owner_name:(null==e?void 0:e.githubOwnerName)||"",repository_name:(null==e?void 0:e.githubRepositoryName)||"",issue_number:(null==e?void 0:e.githubNumber)||-1},i),r=await this.toHarvestrGithubIssue(o,void 0);return o&&(null==o?void 0:o.id)&&(null==o?void 0:o.githubNumber)&&(null==o?void 0:o.githubNumber)>0&&"object"==typeof o?n.githubStorageService.updateIssue(o.id,r):Promise.resolve()}catch(t){if(401===t.statusCode||404===t.statusCode||429===t.statusCode)return console.log("issue not found or over-rate limit : githubNumber : ",e.githubNumber),!1}}))},async handleIssueOnGQLUpdateDiscoveryResolver(e,i,o,r){u.logger.info("githuApiService.handleIssueOnGQLUpdateDiscoveryResolver()");var s;await(async s=>{u.logger.info("queryType : ",s),void 0!==s&&await{UNLINK:async()=>{var o,r;try{await t.githubApiService.unlinkIssue({issueId:null===(r=null===(o=null==i?void 0:i.githubIssues)||void 0===o?void 0:o.delete[0])||void 0===r?void 0:r.id,gqlMutationUnlinkIssue:async()=>e(i)})}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > UNLINK : ERR : ",JSON.stringify(e))}},LINK:async()=>{try{await t.githubApiService.linkIssue({harvestrIssue:i.githubIssues.create[0],discovery:o,gqlMutationLinkIssue:async t=>(i.githubIssues.create[0]=t,e(i))},r)}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > LINK : ERR : ",JSON.stringify(e))}},CREATE:async()=>{var s;try{u.logger.info("githuApiService.execOperation : CREATE"),await t.githubApiService.createIssue({harvestrIssue:null===(s=null==i?void 0:i.githubIssues)||void 0===s?void 0:s.create[0],discovery:o,gqlMutationLinkIssue:async t=>(i.githubIssues.create[0]=t,e(i))},r)}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > CREATE : ERR : ",JSON.stringify(e))}}}[s]()})((void 0!==(null===(s=null==i?void 0:i.githubIssues)||void 0===s?void 0:s.delete)?"UNLINK":void 0)||(()=>{var e,t,o;return void 0!==(null===(e=null==i?void 0:i.githubIssues)||void 0===e?void 0:e.create)&&-1!==(null===(o=((null===(t=null==i?void 0:i.githubIssues)||void 0===t?void 0:t.create)||[])[0])||void 0===o?void 0:o.githubId)?"LINK":void 0})()||(()=>{var e,t,o;return void 0!==(null===(e=null==i?void 0:i.githubIssues)||void 0===e?void 0:e.create)&&-1===(null===(o=((null===(t=null==i?void 0:i.githubIssues)||void 0===t?void 0:t.create)||[])[0])||void 0===o?void 0:o.githubId)?"CREATE":void 0})())},async createComment(e,i){const o=e.owner_name&&null!==e.owner_name?{}:await t.githubApiService.getUser(i);return a.post({url:`https://api.github.com/repos/${e.owner_name&&null!==e.owner_name?e.owner_name:o.login}/${e.repository_name}/issues/${e.issue_number}/comments`,auth:await t.getGithubBearer(i),body:e.comment,json:!0,headers:{"user-agent":"node.js"}})},getUser:async function(e,i){return a.get({url:"https://api.github.com/user",auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserOrgs:async function(e,i){return a.get({url:"https://api.github.com/user/orgs",auth:await t.getGithubBearer(e),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserRepositories:async function(e,i){return a.get({url:"https://api.github.com/user/repos",auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserPermissionsOnRepo:async(e,i,o,r)=>a.get({url:`https://api.github.com/repos/${e.username}/${e.repoName}/collaborators/${e.username}/permission`,auth:await t.getGithubBearer(i,o),qs:{},json:!0,headers:{"user-agent":"node.js"}}),async hasUserRequiredPermissions(e,i,o){const r=["admin","maintainer","member"],s=(null==e?void 0:e.user)||await t.githubApiService.getUser(i,o);return(await t.githubApiService.getUserRepositories(i,o)||[]).reduce(async(e,n)=>{try{const u=await t.githubApiService.getUserPermissionsOnRepo({repoName:n.name,username:s.login},i,o);return e=e&&r.includes(u.permission)}catch(t){return e}},!0)},async getOrganizations(e,i){const o=await t.githubApiService.getUser(e,i);return a.get({url:o.organizations_url,auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getOrganizationUsers(e,i){if(null===e.organization_name||"null"===e.organization_name)return[await t.githubApiService.getUser(i)];return u.httpRequestPaginated({httpRequest:async(o,r)=>a.get({url:`https://api.github.com/orgs/${e.organization_name}/members?page=${o}&per_page=${r}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}}),pageStartsAt:1,itemsPerPage:100})},getRepositories:async(e,i,o)=>u.httpRequestPaginated({httpRequest:async(r,s)=>{let n="";if(void 0!==e.organization_name)n=`https://api.github.com/orgs/${e.organization_name}/repos?page=${r}&per_page=100`;else{n=`https://api.github.com/users/${e.user_name||(await t.githubApiService.getUser(i,o)).login}/repos?page=${r}&per_page=${s||100}`}return a.get({url:n,auth:await t.getGithubBearer(i,o),qs:{},json:!0,headers:{"user-agent":"node.js"}})},pageStartsAt:1,itemsPerPage:100}),async getRepository(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},searchIssues:async(e,i)=>u.httpRequestPaginated({httpRequest:async(o,r)=>{const s=await g(e.owner_name,i);return(await a.get({url:`https://api.github.com/search/issues?page=${o}&per_page=${r}&q=repo:${s}/${e.repository_name}+state:open+${e.query||""}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})).items},pageStartsAt:1,itemsPerPage:100}),async getIssue(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/issues/${e.issue_number}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getLabels(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/labels`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getMilestones(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/milestones`,auth:await t.getGithubBearer(i),qs:{state:"open"},json:!0,headers:{"user-agent":"node.js"}})},getRepositoryProjects:async(e,i)=>u.httpRequestPaginated({httpRequest:async(o,r)=>{const s=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${s}/${e.repository_name}/projects?page=${o}&per_page=${r}`,auth:await t.getGithubBearer(i),qs:{state:"open"},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}})},pageStartsAt:1,itemsPerPage:100}),getProjectColumns:async(e,i)=>a.get({url:`https://api.github.com/projects/${e.project_id}/columns`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),getProjectColumnCards:async(e,i)=>a.get({url:`https://api.github.com/projects/${e.project_id}/columns/${e.column_id}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),async getColumnPosition(e,i){if(null===e.project_id||null===e.column_id)return-1;console.log("continue fetch columns");return(await t.githubApiService.getProjectColumns({project_id:e.project_id},i)).findIndex(t=>t.id===e.column_id)},async toHarvestrGithubIssue(e,i,o){var r,s,n,u,a,l,c,g;return{id:null==i?void 0:i.id,githubId:parseInt(null==e?void 0:e.id),githubNumber:null==e?void 0:e.number,githubUrl:null==e?void 0:e.html_url,githubDescription:(null==e?void 0:e.body)||"",githubRepositoryName:(h=null==e?void 0:e.repository_url,h.split("/").pop()),githubRepositoryUrl:null==e?void 0:e.repository_url,githubOwnerName:(e=>{const t=e.split("/");return t.slice(t.length-2,t.length-1).shift()})(null==e?void 0:e.repository_url),githubProjectId:(null==i?void 0:i.githubProjectId)||-1,githubTitle:null==e?void 0:e.title,githubUserId:(null===(r=null==e?void 0:e.user)||void 0===r?void 0:r.id)||-1,githubUserLogin:(null===(s=null==e?void 0:e.user)||void 0===s?void 0:s.login)||"",githubLabels:((null==e?void 0:e.labels)||[]).filter(e=>null!==e).map(({name:e})=>e),githubStateName:(null==e?void 0:e.state)||"",githubColumnId:(null==i?void 0:i.githubColumnId)||0,githubColumnName:(null==i?void 0:i.githubColumnName)||"",githubColumnPosition:-1!==((null==i?void 0:i.githubProjectId)||-1)&&-1!==((null==i?void 0:i.githubColumnId)||-1)?await t.githubApiService.getColumnPosition({project_id:(null==i?void 0:i.githubProjectId)||null,column_id:(null==i?void 0:i.githubColumnId)||null},o):-1,githubLocked:(null==e?void 0:e.locked)||!1,githubAssigneeId:(null===(n=null==e?void 0:e.assignee)||void 0===n?void 0:n.id)||-1,githubAssigneeLogin:(null===(u=null==e?void 0:e.assignee)||void 0===u?void 0:u.login)||"",githubAssigneeAvatarUrl:(null===(a=null==e?void 0:e.assignee)||void 0===a?void 0:a.avatar_url)||"",githubMilestoneId:(null==e?void 0:e.milestone)?null===(l=null==e?void 0:e.milestone)||void 0===l?void 0:l.id:-1,githubMilestoneNumber:(null===(c=null==e?void 0:e.milestone)||void 0===c?void 0:c.number)||-1,githubMilestoneTitle:(null===(g=null==e?void 0:e.milestone)||void 0===g?void 0:g.title)||"",githubCommentsCount:(null==e?void 0:e.comments)||0};var h},toGithubIssue:e=>({owner_name:(null==e?void 0:e.githubOwnerName)||"",repository_name:null==e?void 0:e.githubRepositoryName,column_id:null==e?void 0:e.githubColumnId,assignee:null==e?void 0:e.githubAssigneeLogin,title:null==e?void 0:e.githubTitle,body:null==e?void 0:e.githubDescription,milestone:null==e?void 0:e.githubMilestoneNumber,labels:[...(null==e?void 0:e.githubLabels)||[]]})}},function(e,t){e.exports=require("@harvestr-api/common")},function(e,t){e.exports=require("@harvestr-api/utils")},function(e,t){e.exports=require("request-promise")},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.githubWebhookService=t.githubWebhookEvents=void 0;const u=i(1),a=i(2),l=n(i(7)),c=s(i(3)),g=i(0);var h;!function(e){e.ISSUES="issues",e.PROJECT_COLUMN="project_column"}(h=t.githubWebhookEvents||(t.githubWebhookEvents={})),t.githubWebhookService={getRepositoryWebhooks:async(e,t,i)=>c.get({url:`https://api.github.com/repos/${e.owner_name_or_userlogin}/${e.repository_name}/hooks`,auth:await g.getGithubBearer(t,i),qs:{},json:!0,headers:{"user-agent":"node.js"}}).then(e=>"404"===String(e.statusCode)?[]:e).catch(e=>{if("404"===String(e.statusCode))return[]}),async registerWebhook(e,i,o){a.logger.info("githubWebhookService.registerWebhook()");const r=await g.githubApiService.getUser(o),s=void 0!==e&&""!==e&&null!==e;if(!0===await t.githubWebhookService.isWebhookAlreadyBound(s?e:r.login,i,`${process.env.INTEGRATIONS_API_URL}/devtools/github/sync/issue?project_id=${o.project_id}`,o))return a.logger.info(`githubWebhookService.registerWebhook() : hook already bound to issue's repository '${e}/${i}'`),"already_registered";const n=await g.getGithubBearer(o);a.logger.info("githubWebhookService.registerWebhook() : getGithubBearer() : "+n);const u={url:`https://api.github.com/repos/${null!==e?e:r.login}/${i}/hooks`,auth:n,body:{name:"web",active:!0,events:[h.ISSUES,h.PROJECT_COLUMN],config:{url:`${process.env.INTEGRATIONS_API_URL}/devtools/github/sync/issue?project_id=${o.project_id}`,content_type:"json",insecure_ssl:"0",secret:process.env.GITHUB_WEBHOOK_SIGNATURE}},json:!0,headers:{"user-agent":"node.js"}};return a.logger.info("githubWebhookService.registerWebhook() : POST options : "+JSON.stringify(u)),a.logger.info("githubWebhookService.registerWebhook() : reqt.post"),c.post(u).then(e=>e.id).catch(t=>("404"===String(t.statusCode)&&console.log("Hook endpoint or repository does not exist for : ",`https://api.github.com/repos/${null!==e?e:r.login}/${i}/hooks`),Promise.resolve({statusCode:t.statusCode})))},async registerAllWebhooks(e){const i=new Set,o=await u.githubStorageService.getIssuesOfProjectWithoutWebhook(e);await a.parallelPromiseHelper.uploadData(o||[],async o=>Promise.all(o.reduce((o,r)=>(i.has(r.githubRepositoryName)||(o.push(t.githubWebhookService.registerWebhook(r.githubOwnerName,r.githubRepositoryName,{type:"INTEGRATION_JIRA",project_id:e.project_id}).catch(e=>null)),i.add(r.githubOwnerName)),o),[])),10)},async unregisterAllWebhooks(e,i){console.log("github.unregisterAllWebhooks()");const o=async o=>{let r=[],s=[];"org"===o?s=await g.githubApiService.getOrganizations(e,i):"user"===o&&(s=[await g.githubApiService.getUser(e,i)]);for(const n of s){const s=await g.githubApiService.getRepositories({["org"===o?"organization_name":"user_name"]:n.login},e,i);for(const o of s){const s=await t.githubWebhookService.getRepositoryWebhooks({owner_name_or_userlogin:n.login,repository_name:o.name},e,i);r=[...r,...s.filter(e=>{var t,i,o;return"repository"===(null===(t=null==e?void 0:e.type)||void 0===t?void 0:t.toLowerCase())&&(null===(o=null===(i=null==e?void 0:e.config)||void 0===i?void 0:i.url)||void 0===o?void 0:o.includes(process.env.INTEGRATIONS_API_URL))}).map(e=>({webhookId:e.id,accountLogin:n.login,repoName:o.name}))]}}r.length>0&&await a.parallelPromiseHelper.uploadData(r,async o=>Promise.all(o.map(o=>{t.githubWebhookService.unregisterWebhookOnRepo({owner_or_user_login:o.accountLogin,repoName:o.repoName,webhookId:o.webhookId},e,i)})),10)};return await o("org"),await o("user"),Promise.resolve()},async unregisterWebhookOnRepo(e,t,i){const o=await g.getGithubBearer(t,i);return c.delete({url:`https://api.github.com/repos/${e.owner_or_user_login}/${e.repoName}/hooks/${e.webhookId}`,auth:o,headers:{"user-agent":"node.js"}})},isWebhookSignatureValid(e){if(void 0===e.body||null===e.body||"object"==typeof e.body&&"object"===e.body.constructor.name)return!1;const t=l.default.createHmac("sha1",process.env.GITHUB_WEBHOOK_SIGNATURE);return t.update(e.body,"utf8"),e.headers["X-Hub-Signature"]==="sha1="+t.digest("hex")},async isWebhookAlreadyBound(e,i,o,r){const s=await t.githubWebhookService.getRepositoryWebhooks({owner_name_or_userlogin:e,repository_name:i},r);if(0===s.length)return!1;return void 0!==(s||[]).find(e=>{var t;return((null===(t=null==e?void 0:e.config)||void 0===t?void 0:t.url)||"").includes(o)&&!0===((null==e?void 0:e.active)||!1)&&((null==e?void 0:e.events)||[]).includes(h.ISSUES)})}}},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||t.hasOwnProperty(i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),r(i(6),t),r(i(0),t),r(i(8),t),r(i(4),t)},function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0})},function(e,t){e.exports=require("crypto")},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.githubOauthService=void 0;const u=i(1),a=i(2),l=n(i(9)),c=i(10),g=s(i(11)),h=s(i(12)),d=i(0),p=(process.env.HARVESTR_API_URL||"")+"/authorize/github/callback",b=process.env.GITHUB_OAUTH_CLIENT_ID,v=process.env.GITHUB_OAUTH_CLIENT_SECRET;t.githubOauthService={async handleGithubOauth(e,t){try{a.logger.info(b),a.logger.info(p);const i=e.query.subdomain,o=e.query.id;a.logger.info("subdomain :",i);const r=(await(async e=>new c.OAuth2(b,v,"https://github.com/","login/oauth/authorize","login/oauth/access_token",null))()).getAuthorizeUrl({response_type:"code",scope:"user public_repo repo repo_deployment repo:status read:repo_hook write:repo_hook read:org read:public_key read:gpg_key",redirect_uri:p,state:l.default(JSON.stringify({project_id:o,subdomain:i}))});a.logger.info(r),t.redirect(r)}catch(e){a.logger.error(e),t.status(400).send(e)}},async handleGithubOauthResponse(e,t){try{const i=e.query.state,o=(h.parse(e.url,!0).query||{}).code,{project_id:r,subdomain:s}=JSON.parse(l.default.decode(i));if(console.log(r,s),!r)throw new Error("No project id found");const n={type:"INTEGRATION_GITHUB",project_id:r};g.post({url:"https://github.com/login/oauth/access_token",form:{client_id:b,client_secret:v,code:o,grant_type:"authorization_code",redirect_uri:p}},async(e,i,o)=>{let r="";try{if(e)throw new Error(e.data);const i=JSON.parse('{"'+o.replace(/&/g,'","').replace(/=/g,'":"')+'"}',(function(e,t){return""===e?t:decodeURIComponent(t)}));if(!await d.githubApiService.hasUserRequiredPermissions({},n,i.access_token))throw r=i.access_token,new Error("not_enough_permissions");return await u.apiTokenService.createApiToken({source:"GITHUB",token:i.access_token,subdomain:s||void 0},n),void t.status(200).send("<h3>Success! Redirecting...</h3><h4>You can close this window.</h4>")}catch(e){if("not_enough_permissions"!==e.message)throw a.logger.error(e),new Error("error getting access token");{const e=await d.githubApiService.getUserOrgs(n,r);t.status(400).send(u.getTemplate("github_not_enough_permissions",{orgs:e}))}}})}catch(e){a.logger.error(e),t.status(400).send("<h3>Error: </h3>"+e)}}}},function(e,t){e.exports=require("base64url")},function(e,t){e.exports=require("oauth")},function(e,t){e.exports=require("request")},function(e,t){e.exports=require("url")}])); | ||
!function(e,t){for(var i in t)e[i]=t[i]}(exports,function(e){var t={};function i(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=e,i.c=t,i.d=function(e,t,o){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)i.d(o,r,function(t){return e[t]}.bind(null,r));return o},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=5)}([function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.githubApiService=t.getGithubBearer=void 0;const n=i(1),u=i(2),a=s(i(3)),l=i(4),c=async e=>{var t;const i=await n.apiTokenService.getApiTokens({where:{source:"GITHUB"}},e);return i.length?null===(t=i[0])||void 0===t?void 0:t.token:void 0};async function g(e,i){return![void 0,"undefined",null,""].includes(e)?e:(await t.githubApiService.getUser(i)).login}t.getGithubBearer=async(e,t)=>{let i;if(i=t||await c(e),void 0===i)throw new Error("getGithubBearer() : Not authorized");return{bearer:i}},t.githubApiService={urlGet:async(e,i)=>a.get({url:e.url,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}}),async createIssue(e,i){const o=null!==e.harvestrIssue.githubOwnerName&&""!==e.harvestrIssue.githubOwnerName,r=o?{}:await t.githubApiService.getUser(i),s=t.githubApiService.validateGithubIssue(e.harvestrIssue),n=await a.post({url:`https://api.github.com/repos/${o?s.githubOwnerName:r.login}/${s.githubRepositoryName}/issues`,auth:await t.getGithubBearer(i),body:t.githubApiService.toGithubIssue(e.harvestrIssue),json:!0,headers:{"user-agent":"node.js"}});return u.logger.info("githuApiService.createIssue() : before githubApiService.linkIssue"),await t.githubApiService.linkIssue({harvestrIssue:await t.githubApiService.toHarvestrGithubIssue(n,s,i),discovery:e.discovery,gqlMutationLinkIssue:e.gqlMutationLinkIssue},i),u.logger.info("githuApiService.createIssue() : after githubApiService.linkIssue"),"number"==typeof e.harvestrIssue.githubColumnId&&e.harvestrIssue.githubColumnId>-1&&await t.githubApiService.createCard({column_id:e.harvestrIssue.githubColumnId,issue_id:n.id},i),n},createCard:async(e,i)=>a.post({url:`https://api.github.com/projects/columns/${e.column_id}/cards`,auth:await t.getGithubBearer(i),body:{content_id:e.issue_id,content_type:"Issue"},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),async unlinkIssue(e){"function"==typeof e.gqlMutationUnlinkIssue?await e.gqlMutationUnlinkIssue():await n.githubStorageService.deleteIssueById(e.issueId)},async linkIssue(e,i){var o,r,s,a,c,g,h,d;const p=await this.getIssue({owner_name:(null==e?void 0:e.harvestrIssue.githubOwnerName)||"",repository_name:(null===(o=null==e?void 0:e.harvestrIssue)||void 0===o?void 0:o.githubRepositoryName)||"",issue_number:(null===(r=null==e?void 0:e.harvestrIssue)||void 0===r?void 0:r.githubNumber)||0},i);"function"==typeof e.gqlMutationLinkIssue?(u.logger.info("githubApiService.linkIssue() : before gqlMutationLinkIssue()"),await e.gqlMutationLinkIssue(await t.githubApiService.toHarvestrGithubIssue(p,e.harvestrIssue,i)),u.logger.info("githubApiService.linkIssue() : after gqlMutationLinkIssue()")):await n.githubStorageService.storeIssue(await t.githubApiService.toHarvestrGithubIssue(p,e.harvestrIssue,i),e.discovery),await t.githubApiService.createComment({owner_name:(null==e?void 0:e.harvestrIssue.githubOwnerName)||"",repository_name:(null===(s=null==e?void 0:e.harvestrIssue)||void 0===s?void 0:s.githubRepositoryName)||"",issue_number:(null===(a=null==e?void 0:e.harvestrIssue)||void 0===a?void 0:a.githubNumber)||0,comment:{body:`**Issue was linked to Harvestr Discovery:** [${(null===(c=null==e?void 0:e.discovery)||void 0===c?void 0:c.title)||""}](${process.env.HARVESTR_CLIENT_URL}/components/0/list/${null===(g=u.toClientId(null==e?void 0:e.discovery))||void 0===g?void 0:g.id})`}},i),u.logger.info("githubApiService.linkIssue() : before githubWebhookService.registerWebhook()"),await l.githubWebhookService.registerWebhook((null===(h=null==e?void 0:e.harvestrIssue)||void 0===h?void 0:h.githubOwnerName)||null,(null===(d=null==e?void 0:e.harvestrIssue)||void 0===d?void 0:d.githubRepositoryName)||"",i),u.logger.info("githubApiService.linkIssue() : after githubWebhookService.registerWebhook()")},hasIntegrationEnabled:async e=>void 0!==await c({project_id:e,type:"INTEGRATION_GITHUB"}),async syncIssues(e,i){console.log("github.syncIssues() --------------------"),console.log("github.syncIssues() : where.projectsIds : ",e.projectsIds);const o=e.projectsIds.map(async e=>{if(!1===await t.githubApiService.hasIntegrationEnabled(e))return[];{const t=`${i.devtools.BASE_REDIS_ISSUES_SYNC_KEY}${e}`;if(!1===await i.devtools.canSyncToday("github",t))return console.log(`github : sync OFF for projectId ${e} : last sync less than 24h`),[];{console.log("github : sync ON for projectId "+e);const t=(await n.githubStorageService.getIssuesOfProjectForSync({projectId:e})).filter(e=>"0"!==String(e.githubId)&&null!==e.githubId);return this.bulkSyncIssuesOfProjects(t,{project_id:e,type:"INTEGRATION_GITHUB"})}}});return console.log("github.syncIssues(): END"),o},async bulkSyncIssuesOfProjects(e,i){return u.parallelPromiseHelper.uploadData(e,async e=>e.map(async e=>{try{const o=await t.githubApiService.getIssue({owner_name:(null==e?void 0:e.githubOwnerName)||"",repository_name:(null==e?void 0:e.githubRepositoryName)||"",issue_number:(null==e?void 0:e.githubNumber)||-1},i),r=await this.toHarvestrGithubIssue(o,void 0);return o&&(null==o?void 0:o.id)&&(null==o?void 0:o.githubNumber)&&(null==o?void 0:o.githubNumber)>0&&"object"==typeof o?n.githubStorageService.updateIssue(o.id,r):Promise.resolve()}catch(t){if(401===t.statusCode||404===t.statusCode||429===t.statusCode)return console.log("issue not found or over-rate limit : githubNumber : ",e.githubNumber),!1}}))},async handleIssueOnGQLUpdateDiscoveryResolver(e,i,o,r){u.logger.info("githuApiService.handleIssueOnGQLUpdateDiscoveryResolver()");var s;await(async s=>{u.logger.info("queryType : ",s),void 0!==s&&await{UNLINK:async()=>{var o,r;try{await t.githubApiService.unlinkIssue({issueId:null===(r=null===(o=null==i?void 0:i.githubIssues)||void 0===o?void 0:o.delete[0])||void 0===r?void 0:r.id,gqlMutationUnlinkIssue:async()=>e(i)})}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > UNLINK : ERR : ",JSON.stringify(e))}},LINK:async()=>{try{await t.githubApiService.linkIssue({harvestrIssue:i.githubIssues.create[0],discovery:o,gqlMutationLinkIssue:async t=>(i.githubIssues.create[0]=t,e(i))},r)}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > LINK : ERR : ",JSON.stringify(e))}},CREATE:async()=>{var s;try{u.logger.info("githuApiService.execOperation : CREATE"),await t.githubApiService.createIssue({harvestrIssue:null===(s=null==i?void 0:i.githubIssues)||void 0===s?void 0:s.create[0],discovery:o,gqlMutationLinkIssue:async t=>(i.githubIssues.create[0]=t,e(i))},r)}catch(e){u.logger.info("handleIssueUponGQLUpdateDiscovery > CREATE : ERR : ",JSON.stringify(e))}}}[s]()})((void 0!==(null===(s=null==i?void 0:i.githubIssues)||void 0===s?void 0:s.delete)?"UNLINK":void 0)||(()=>{var e,t,o;return void 0!==(null===(e=null==i?void 0:i.githubIssues)||void 0===e?void 0:e.create)&&-1!==(null===(o=((null===(t=null==i?void 0:i.githubIssues)||void 0===t?void 0:t.create)||[])[0])||void 0===o?void 0:o.githubId)?"LINK":void 0})()||(()=>{var e,t,o;return void 0!==(null===(e=null==i?void 0:i.githubIssues)||void 0===e?void 0:e.create)&&-1===(null===(o=((null===(t=null==i?void 0:i.githubIssues)||void 0===t?void 0:t.create)||[])[0])||void 0===o?void 0:o.githubId)?"CREATE":void 0})())},async createComment(e,i){const o=e.owner_name&&null!==e.owner_name?{}:await t.githubApiService.getUser(i);return a.post({url:`https://api.github.com/repos/${e.owner_name&&null!==e.owner_name?e.owner_name:o.login}/${e.repository_name}/issues/${e.issue_number}/comments`,auth:await t.getGithubBearer(i),body:e.comment,json:!0,headers:{"user-agent":"node.js"}})},getUser:async function(e,i){return a.get({url:"https://api.github.com/user",auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserOrgs:async function(e,i){return a.get({url:"https://api.github.com/user/orgs",auth:await t.getGithubBearer(e),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserRepositories:async function(e,i){return a.get({url:"https://api.github.com/user/repos",auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},getUserPermissionsOnRepo:async(e,i,o,r)=>a.get({url:`https://api.github.com/repos/${e.username}/${e.repoName}/collaborators/${e.username}/permission`,auth:await t.getGithubBearer(i,o),qs:{},json:!0,headers:{"user-agent":"node.js"}}),async hasUserRequiredPermissions(e,i,o){const r=["admin","maintainer","member"],s=(null==e?void 0:e.user)||await t.githubApiService.getUser(i,o);return(await t.githubApiService.getUserRepositories(i,o)||[]).reduce(async(e,n)=>{try{const u=await t.githubApiService.getUserPermissionsOnRepo({repoName:n.name,username:s.login},i,o);return e=e&&r.includes(u.permission)}catch(t){return e}},!0)},async getOrganizations(e,i){const o=await t.githubApiService.getUser(e,i);return a.get({url:o.organizations_url,auth:await t.getGithubBearer(e,i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getOrganizationUsers(e,i){if(null===e.organization_name||"null"===e.organization_name)return[await t.githubApiService.getUser(i)];return u.httpRequestPaginated({httpRequest:async(o,r)=>a.get({url:`https://api.github.com/orgs/${e.organization_name}/members?page=${o}&per_page=${r}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}}),pageStartsAt:1,itemsPerPage:100})},getRepositories:async(e,i,o)=>u.httpRequestPaginated({httpRequest:async(r,s)=>{let n="";if(void 0!==e.organization_name)n=`https://api.github.com/orgs/${e.organization_name}/repos?page=${r}&per_page=100`;else{n=`https://api.github.com/users/${e.user_name||(await t.githubApiService.getUser(i,o)).login}/repos?page=${r}&per_page=${s||100}`}return a.get({url:n,auth:await t.getGithubBearer(i,o),qs:{},json:!0,headers:{"user-agent":"node.js"}})},pageStartsAt:1,itemsPerPage:100}),async getRepository(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},searchIssues:async(e,i)=>u.httpRequestPaginated({httpRequest:async(o,r)=>{const s=await g(e.owner_name,i);return(await a.get({url:`https://api.github.com/search/issues?page=${o}&per_page=${r}&q=repo:${s}/${e.repository_name}+state:open+${e.query||""}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})).items},pageStartsAt:1,itemsPerPage:100}),async getIssue(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/issues/${e.issue_number}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getLabels(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/labels`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js"}})},async getMilestones(e,i){const o=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${o}/${e.repository_name}/milestones`,auth:await t.getGithubBearer(i),qs:{state:"open"},json:!0,headers:{"user-agent":"node.js"}})},getRepositoryProjects:async(e,i)=>u.httpRequestPaginated({httpRequest:async(o,r)=>{const s=await g(e.owner_name,i);return a.get({url:`https://api.github.com/repos/${s}/${e.repository_name}/projects?page=${o}&per_page=${r}`,auth:await t.getGithubBearer(i),qs:{state:"open"},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}})},pageStartsAt:1,itemsPerPage:100}),getProjectColumns:async(e,i)=>a.get({url:`https://api.github.com/projects/${e.project_id}/columns`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),getProjectColumnCards:async(e,i)=>a.get({url:`https://api.github.com/projects/${e.project_id}/columns/${e.column_id}`,auth:await t.getGithubBearer(i),qs:{},json:!0,headers:{"user-agent":"node.js",Accept:"application/vnd.github.inertia-preview+json"}}),async getColumnPosition(e,i){if(null===e.project_id||null===e.column_id)return-1;console.log("continue fetch columns");return(await t.githubApiService.getProjectColumns({project_id:e.project_id},i)).findIndex(t=>t.id===e.column_id)},async toHarvestrGithubIssue(e,i,o){var r,s,n,u,a,l,c,g;return{id:null==i?void 0:i.id,githubId:parseInt(null==e?void 0:e.id),githubNumber:null==e?void 0:e.number,githubUrl:null==e?void 0:e.html_url,githubDescription:(null==e?void 0:e.body)||"",githubRepositoryName:(h=null==e?void 0:e.repository_url,h.split("/").pop()),githubRepositoryUrl:null==e?void 0:e.repository_url,githubOwnerName:(e=>{const t=e.split("/");return t.slice(t.length-2,t.length-1).shift()})(null==e?void 0:e.repository_url),githubProjectId:(null==i?void 0:i.githubProjectId)||-1,githubTitle:null==e?void 0:e.title,githubUserId:(null===(r=null==e?void 0:e.user)||void 0===r?void 0:r.id)||-1,githubUserLogin:(null===(s=null==e?void 0:e.user)||void 0===s?void 0:s.login)||"",githubLabels:((null==e?void 0:e.labels)||[]).filter(e=>null!==e).map(({name:e})=>e),githubStateName:(null==e?void 0:e.state)||"",githubColumnId:(null==i?void 0:i.githubColumnId)||0,githubColumnName:(null==i?void 0:i.githubColumnName)||"",githubColumnPosition:-1!==((null==i?void 0:i.githubProjectId)||-1)&&-1!==((null==i?void 0:i.githubColumnId)||-1)?await t.githubApiService.getColumnPosition({project_id:(null==i?void 0:i.githubProjectId)||null,column_id:(null==i?void 0:i.githubColumnId)||null},o):-1,githubLocked:(null==e?void 0:e.locked)||!1,githubAssigneeId:(null===(n=null==e?void 0:e.assignee)||void 0===n?void 0:n.id)||-1,githubAssigneeLogin:(null===(u=null==e?void 0:e.assignee)||void 0===u?void 0:u.login)||"",githubAssigneeAvatarUrl:(null===(a=null==e?void 0:e.assignee)||void 0===a?void 0:a.avatar_url)||"",githubMilestoneId:(null==e?void 0:e.milestone)?null===(l=null==e?void 0:e.milestone)||void 0===l?void 0:l.id:-1,githubMilestoneNumber:(null===(c=null==e?void 0:e.milestone)||void 0===c?void 0:c.number)||-1,githubMilestoneTitle:(null===(g=null==e?void 0:e.milestone)||void 0===g?void 0:g.title)||"",githubCommentsCount:(null==e?void 0:e.comments)||0};var h},validateGithubIssue(e){const t=u.htmlToMarkdown({rules:{weblink:{filter:["a"],replacement:(e,t)=>`[${e}](${t.getAttribute("href")})`},strikethrough:{filter:["s"],replacement:e=>`~~${e}~~`},bold:{filter:["strong","b"],replacement:e=>`**${e}**`},heading1:{filter:["h1"],replacement:e=>"# "+e},heading2:{filter:["h2"],replacement:e=>"## "+e}}});return{...e,githubDescription:t.htmlToMarkdown((null==e?void 0:e.githubDescription)||"")}},toGithubIssue:e=>({owner_name:(null==e?void 0:e.githubOwnerName)||"",repository_name:null==e?void 0:e.githubRepositoryName,column_id:null==e?void 0:e.githubColumnId,assignee:null==e?void 0:e.githubAssigneeLogin,title:null==e?void 0:e.githubTitle,body:null==e?void 0:e.githubDescription,milestone:null==e?void 0:e.githubMilestoneNumber,labels:[...(null==e?void 0:e.githubLabels)||[]]})}},function(e,t){e.exports=require("@harvestr-api/common")},function(e,t){e.exports=require("@harvestr-api/utils")},function(e,t){e.exports=require("request-promise")},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.githubWebhookService=t.githubWebhookEvents=void 0;const u=i(1),a=i(2),l=n(i(7)),c=s(i(3)),g=i(0);var h;!function(e){e.ISSUES="issues",e.PROJECT_COLUMN="project_column"}(h=t.githubWebhookEvents||(t.githubWebhookEvents={})),t.githubWebhookService={getRepositoryWebhooks:async(e,t,i)=>c.get({url:`https://api.github.com/repos/${e.owner_name_or_userlogin}/${e.repository_name}/hooks`,auth:await g.getGithubBearer(t,i),qs:{},json:!0,headers:{"user-agent":"node.js"}}).then(e=>"404"===String(e.statusCode)?[]:e).catch(e=>{if("404"===String(e.statusCode))return[]}),async registerWebhook(e,i,o){a.logger.info("githubWebhookService.registerWebhook()");const r=await g.githubApiService.getUser(o),s=void 0!==e&&""!==e&&null!==e;if(!0===await t.githubWebhookService.isWebhookAlreadyBound(s?e:r.login,i,`${process.env.INTEGRATIONS_API_URL}/devtools/github/sync/issue?project_id=${o.project_id}`,o))return a.logger.info(`githubWebhookService.registerWebhook() : hook already bound to issue's repository '${e}/${i}'`),"already_registered";const n=await g.getGithubBearer(o);a.logger.info("githubWebhookService.registerWebhook() : getGithubBearer() : "+n);const u={url:`https://api.github.com/repos/${null!==e?e:r.login}/${i}/hooks`,auth:n,body:{name:"web",active:!0,events:[h.ISSUES,h.PROJECT_COLUMN],config:{url:`${process.env.INTEGRATIONS_API_URL}/devtools/github/sync/issue?project_id=${o.project_id}`,content_type:"json",insecure_ssl:"0",secret:process.env.GITHUB_WEBHOOK_SIGNATURE}},json:!0,headers:{"user-agent":"node.js"}};return a.logger.info("githubWebhookService.registerWebhook() : POST options : "+JSON.stringify(u)),a.logger.info("githubWebhookService.registerWebhook() : reqt.post"),c.post(u).then(e=>e.id).catch(t=>("404"===String(t.statusCode)&&console.log("Hook endpoint or repository does not exist for : ",`https://api.github.com/repos/${null!==e?e:r.login}/${i}/hooks`),Promise.resolve({statusCode:t.statusCode})))},async registerAllWebhooks(e){const i=new Set,o=await u.githubStorageService.getIssuesOfProjectWithoutWebhook(e);await a.parallelPromiseHelper.uploadData(o||[],async o=>Promise.all(o.reduce((o,r)=>(i.has(r.githubRepositoryName)||(o.push(t.githubWebhookService.registerWebhook(r.githubOwnerName,r.githubRepositoryName,{type:"INTEGRATION_JIRA",project_id:e.project_id}).catch(e=>null)),i.add(r.githubOwnerName)),o),[])),10)},async unregisterAllWebhooks(e,i){console.log("github.unregisterAllWebhooks()");const o=async o=>{let r=[],s=[];"org"===o?s=await g.githubApiService.getOrganizations(e,i):"user"===o&&(s=[await g.githubApiService.getUser(e,i)]);for(const n of s){const s=await g.githubApiService.getRepositories({["org"===o?"organization_name":"user_name"]:n.login},e,i);for(const o of s){const s=await t.githubWebhookService.getRepositoryWebhooks({owner_name_or_userlogin:n.login,repository_name:o.name},e,i);r=[...r,...s.filter(e=>{var t,i,o;return"repository"===(null===(t=null==e?void 0:e.type)||void 0===t?void 0:t.toLowerCase())&&(null===(o=null===(i=null==e?void 0:e.config)||void 0===i?void 0:i.url)||void 0===o?void 0:o.includes(process.env.INTEGRATIONS_API_URL))}).map(e=>({webhookId:e.id,accountLogin:n.login,repoName:o.name}))]}}r.length>0&&await a.parallelPromiseHelper.uploadData(r,async o=>Promise.all(o.map(o=>{t.githubWebhookService.unregisterWebhookOnRepo({owner_or_user_login:o.accountLogin,repoName:o.repoName,webhookId:o.webhookId},e,i)})),10)};return await o("org"),await o("user"),Promise.resolve()},async unregisterWebhookOnRepo(e,t,i){const o=await g.getGithubBearer(t,i);return c.delete({url:`https://api.github.com/repos/${e.owner_or_user_login}/${e.repoName}/hooks/${e.webhookId}`,auth:o,headers:{"user-agent":"node.js"}})},isWebhookSignatureValid(e){if(void 0===e.body||null===e.body||"object"==typeof e.body&&"object"===e.body.constructor.name)return!1;const t=l.default.createHmac("sha1",process.env.GITHUB_WEBHOOK_SIGNATURE);return t.update(e.body,"utf8"),e.headers["X-Hub-Signature"]==="sha1="+t.digest("hex")},async isWebhookAlreadyBound(e,i,o,r){const s=await t.githubWebhookService.getRepositoryWebhooks({owner_name_or_userlogin:e,repository_name:i},r);if(0===s.length)return!1;return void 0!==(s||[]).find(e=>{var t;return((null===(t=null==e?void 0:e.config)||void 0===t?void 0:t.url)||"").includes(o)&&!0===((null==e?void 0:e.active)||!1)&&((null==e?void 0:e.events)||[]).includes(h.ISSUES)})}}},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||t.hasOwnProperty(i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),r(i(6),t),r(i(0),t),r(i(8),t),r(i(4),t)},function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0})},function(e,t){e.exports=require("crypto")},function(e,t,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.hasOwnProperty.call(e,i)&&o(t,e,i);return r(t,e),t},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.githubOauthService=void 0;const u=i(1),a=i(2),l=n(i(9)),c=i(10),g=s(i(11)),h=s(i(12)),d=i(0),p=(process.env.HARVESTR_API_URL||"")+"/authorize/github/callback",b=process.env.GITHUB_OAUTH_CLIENT_ID,v=process.env.GITHUB_OAUTH_CLIENT_SECRET;t.githubOauthService={async handleGithubOauth(e,t){try{a.logger.info(b),a.logger.info(p);const i=e.query.subdomain,o=e.query.id;a.logger.info("subdomain :",i);const r=(await(async e=>new c.OAuth2(b,v,"https://github.com/","login/oauth/authorize","login/oauth/access_token",null))()).getAuthorizeUrl({response_type:"code",scope:"user public_repo repo repo_deployment repo:status read:repo_hook write:repo_hook read:org read:public_key read:gpg_key",redirect_uri:p,state:l.default(JSON.stringify({project_id:o,subdomain:i}))});a.logger.info(r),t.redirect(r)}catch(e){a.logger.error(e),t.status(400).send(e)}},async handleGithubOauthResponse(e,t){try{const i=e.query.state,o=(h.parse(e.url,!0).query||{}).code,{project_id:r,subdomain:s}=JSON.parse(l.default.decode(i));if(console.log(r,s),!r)throw new Error("No project id found");const n={type:"INTEGRATION_GITHUB",project_id:r};g.post({url:"https://github.com/login/oauth/access_token",form:{client_id:b,client_secret:v,code:o,grant_type:"authorization_code",redirect_uri:p}},async(e,i,o)=>{let r="";try{if(e)throw new Error(e.data);const i=JSON.parse('{"'+o.replace(/&/g,'","').replace(/=/g,'":"')+'"}',(function(e,t){return""===e?t:decodeURIComponent(t)}));if(!await d.githubApiService.hasUserRequiredPermissions({},n,i.access_token))throw r=i.access_token,new Error("not_enough_permissions");return await u.apiTokenService.createApiToken({source:"GITHUB",token:i.access_token,subdomain:s||void 0},n),void t.status(200).send("<h3>Success! Redirecting...</h3><h4>You can close this window.</h4>")}catch(e){if("not_enough_permissions"!==e.message)throw a.logger.error(e),new Error("error getting access token");{const e=await d.githubApiService.getUserOrgs(n,r);t.status(400).send(u.getTemplate("github_not_enough_permissions",{orgs:e}))}}})}catch(e){a.logger.error(e),t.status(400).send("<h3>Error: </h3>"+e)}}}},function(e,t){e.exports=require("base64url")},function(e,t){e.exports=require("oauth")},function(e,t){e.exports=require("request")},function(e,t){e.exports=require("url")}])); |
@@ -104,2 +104,3 @@ import { AuthContext } from "@harvestr-api/common"; | ||
toHarvestrGithubIssue(githubIssue: any, originIssue?: gqlGithubIssueCreateWithoutDiscoveryInput | undefined, ctx?: AuthContext | undefined): Promise<gqlGithubIssueCreateWithoutDiscoveryInput>; | ||
validateGithubIssue(githubIssue: gqlGithubIssueCreateWithoutDiscoveryInput): gqlGithubIssueCreateWithoutDiscoveryInput; | ||
toGithubIssue(harvestrGithubIssue: gqlGithubIssueCreateWithoutDiscoveryInput): { | ||
@@ -106,0 +107,0 @@ owner_name: string; |
{ | ||
"name": "@harvestr-api/integration-github", | ||
"version": "4.7.9", | ||
"version": "4.7.10", | ||
"description": "> TODO: description", | ||
@@ -26,5 +26,5 @@ "author": "valentinbe <valentinbe@hotmail.fr>", | ||
"dependencies": { | ||
"@harvestr-api/common": "^4.7.9", | ||
"@harvestr-api/common": "^4.7.10", | ||
"@harvestr-api/models": "^4.7.6", | ||
"@harvestr-api/utils": "^4.7.6", | ||
"@harvestr-api/utils": "^4.7.10", | ||
"base64url": "^3.0.0", | ||
@@ -37,3 +37,3 @@ "express": "^4.15.2", | ||
}, | ||
"gitHead": "d33fc89d688694832c8e7e50734417d09921a765", | ||
"gitHead": "abcc427bba74ea9185047fa63f2a011a5e8ddbaf", | ||
"devDependencies": { | ||
@@ -40,0 +40,0 @@ "@types/request-promise": "^4.1.46", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120399
326
Updated@harvestr-api/common@^4.7.10
Updated@harvestr-api/utils@^4.7.10