UNPKG

@btag/react-trumbowyg

Version:
212 lines (181 loc) 7.59 kB
const axios = require('axios'); const confluenceAuth = process.env.BASIC_AUTH_CONFLUENCE; const githubAuth = process.env.GITHUB_OAUTH_TOKEN; const projectName = process.env.PROJECT_NAME; const repoName = process.env.REPO_NAME; const getRequestConfluence = (_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 getRequestGithub = (_url, auth) => axios({ method: 'get', url: _url, headers: { Authorization: `Bearer ${auth}`, 'user-agent': 'node.js', }, }) .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: ${projectName}`); console.log(`REPO NAME: ${repoName}`); console.log('----------------------------------------------------------------'); if (process.env.BUILDKITE_BRANCH === 'main') { const commitHash = process.env.BUILDKITE_COMMIT; const prList = await getRequestGithub( `https://api.github.com/repos/Staffbase/${repoName}/commits/${commitHash}/pulls`, githubAuth ); if (prList.length !== 0) { prId = prList[0].number; const prDetail = getRequestGithub( `https://api.github.com/repos/Staffbase/${repoName}/pulls/${prId}`, githubAuth ); const userDetail = await getRequestGithub( `https://api.github.com/users/${githubUsername}`, githubAuth ); author = `${userDetail.name}`; const prDetailBody = prDetail.body; const ticketSummaryArr = prDetailBody.split(/ |\r|\n/); for (const element of ticketSummaryArr) { if (element.includes('atlassian.net/browse/')) { ticketLink = element; break; } } prLink = prDetail.url; prTitle = prDetail.title; dateAdded = new Date(prDetail.updated_at).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}"`); const commitDetails = await getRequestGithub( `https://api.github.com/repos/Staffbase/${repoName}/commits/${commitHash}`, githubAuth ); author = `${commitDetails.commit.committer.name}`; prLink = commitDetails.html_url; prTitle = commitDetails.commit.message; dateAdded = new Date(commitDetails.commit.committer.date).toLocaleString('en-CA', { timeZone: 'America/Vancouver', }); } const releaseNotesPage = await getRequestConfluence( `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 getRequestConfluence( `https://mitarbeiterapp.atlassian.net/wiki/rest/api/content/?id=${confluencePageId}&expand=version`, confluenceAuth, ); const newConfluenceVersion = (curConfluenceVersion.results[0].version.number += 1); const curConfluencePage = await getRequestConfluence( `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();