UNPKG

@btag/react-trumbowyg

Version:
205 lines (175 loc) 7.84 kB
const axios = require('axios'); const bitbucketAuth = process.env.BASIC_AUTH_BITBUCKET; const confluenceAuth = process.env.BASIC_AUTH_CONFLUENCE; const projectName = process.env.PROJECT_NAME; const repoName = process.env.REPO_NAME; const getRequest = (_url, auth) => axios({ method: 'get', url: _url, headers: { Authorization: `Basic ${auth}`, }, }) .then((response) => { return response.data; }) .catch((error) => { console.log(error); process.exit(1); }); const putRequest = (_url, auth, _newBody) => axios({ method: 'put', url: _url, headers: { Authorization: `Basic ${auth}`, 'Content-type': 'application/json', Accept: 'application/json', }, data: _newBody, }) .then((response) => { console.log(`Response: ${response.status}`); console.log(response.status) }) .catch((error) => { console.log(error); process.exit(1); }); async function main() { let author; let confluencePageId; let dateAdded; let prId; let prLink; let prTitle; let ticketLink; let changelog; let featureFlag; console.log('----------------------------------------------------------------'); console.log(`PROJECT NAME: ${process.env.PROJECT_NAME}`); console.log(`REPO NAME: ${process.env.REPO_NAME}`); console.log('----------------------------------------------------------------'); if (process.env.BUILDKITE_BRANCH === 'main') { const commitHash = process.env.BUILDKITE_COMMIT; const commitDetails = await getRequest( `https://api.bitbucket.org/2.0/repositories/bananatag/${repoName}/commit/${commitHash}`, bitbucketAuth, ); const prList = await getRequest( `https://api.bitbucket.org/2.0/repositories/bananatag/${repoName}/commit/${commitHash}/pullrequests`, bitbucketAuth, ); if (prList.values.length !== 0) { prId = prList.values[0].id; const prDetail = await getRequest( `https://api.bitbucket.org/2.0/repositories/bananatag/${repoName}/pullrequests/${prId}`, bitbucketAuth, ); author = typeof commitDetails.author.user !== 'undefined' ? commitDetails.author.user.display_name : commitDetails.author.raw .replace('<', '(') .replace('>', ')'); const ticketSummary = commitDetails.summary.html; const ticketSummaryArr = ticketSummary.split(' '); for (const element of ticketSummaryArr) { if (element.includes('atlassian.net/browse/')) { ticketLink = element; break; } } // Transform 'href="https://mitarbeiterapp.atlassian.net/browse/XX-123"' to 'https://mitarbeiterapp.atlassian.net/browse/XX-123' if (ticketLink !== undefined) { ticketLink = ticketLink.substring(ticketLink.search('http'), ticketLink.length - 1); } prLink = prDetail.links.html.href; prTitle = prDetail.title; dateAdded = new Date(prDetail.updated_on).toLocaleString('en-CA', { timeZone: 'America/Vancouver', }); const issueID = ticketLink.substring(ticketLink.lastIndexOf('/') + 1); const issueDetail = await getRequest( `https://mitarbeiterapp.atlassian.net/rest/api/2/issue/${issueID}`, confluenceAuth, ); changelog = (issueDetail.fields.customfield_11582 !== null) ? issueDetail.fields.customfield_11582[0].value : ""; featureFlag = (issueDetail.fields.customfield_11588 !== null) ? issueDetail.fields.customfield_11588[0].value : ""; } else { console.error(`Unable to find a PR associated with commit hash "${commitHash}"`); console.log(`Using the description for commit hash "${commitHash}"`); prLink = commitDetails.links.html.href; prTitle = commitDetails.message; author = typeof commitDetails.author.user !== 'undefined' ? commitDetails.author.user.display_name : commitDetails.author.raw .replace('<', '(') .replace('>', ')'); dateAdded = new Date(commitDetails.date).toLocaleString('en-CA', { timeZone: 'America/Vancouver', }); } const releaseNotesPage = await getRequest( `https://mitarbeiterapp.atlassian.net/wiki/rest/api/content?title=Latest%20Release%20Notes&spaceKey=ENGINEERING`, confluenceAuth, ); if (releaseNotesPage.results.length !== 0) { confluencePageId = releaseNotesPage.results[0].id; } else { console.error(`Unable to find the Confluence Page "Latest Release Notes"`); process.exit(1); } const curConfluenceVersion = await getRequest( `https://mitarbeiterapp.atlassian.net/wiki/rest/api/content/?id=${confluencePageId}&expand=version`, confluenceAuth, ); const newConfluenceVersion = (curConfluenceVersion.results[0].version.number += 1); const curConfluencePage = await getRequest( `https://mitarbeiterapp.atlassian.net/wiki/rest/api/content/?id=${confluencePageId}&expand=body.storage`, confluenceAuth, ); const curConfluenceBodyValue = curConfluencePage.results[0].body.storage.value; const projectNameIndex = curConfluencePage.results[0].body.storage.value.indexOf( `<h2><strong>${projectName}</strong></h2>`, ); const endOfTableIndex = curConfluencePage.results[0].body.storage.value.indexOf( '</tbody>', projectNameIndex, ); const newReleaseNoteRow = `<tr><td class="numberingColumn">1</td><td><p><a href="${prLink}" data-card-appearance="inline">${prLink}</a></p></td><td><p><a href="${ticketLink}" data-card-appearance="inline">${ticketLink}</a></p></td><td><p>${prTitle}</p></td><td><p></p></td><td><p>${changelog}</p></td><td><p>${featureFlag}</p></td><td><p>${author}</p></td><td><p>${dateAdded}</p></td></tr>`; console.log('\n\n\n============================== New Row Data =============================='); console.dir(newReleaseNoteRow, {depth: null, colors: true, maxArrayLength: null}); console.log('========================== End of New Row Data ===========================\n\n\n'); const newConfluenceBodyValue = [ curConfluenceBodyValue.slice(0, endOfTableIndex), newReleaseNoteRow, curConfluenceBodyValue.slice(endOfTableIndex), ].join(''); const newConfluenceBodyValueStringified = JSON.stringify(newConfluenceBodyValue); const newConflueceBody = `{ "version": { "number": ${newConfluenceVersion} }, "title": "Latest Release Notes", "type": "page", "status": "current", "body": { "storage": { "value": ${newConfluenceBodyValueStringified}, "representation": "storage" } } }`; putRequest( `https://mitarbeiterapp.atlassian.net/wiki/rest/api/content/${confluencePageId}`, confluenceAuth, newConflueceBody, ); } else { console.log(`Current branch is not "main". No Release note is required.`); } } main();