diff --git a/dist/comment.js b/dist/comment.js index 12af0e8..f7aa0d9 100644 --- a/dist/comment.js +++ b/dist/comment.js @@ -2,19 +2,39 @@ Object.defineProperty(exports, "__esModule", { value: true }); const needle = require("needle"); const config = require("./config"); +const options = { + json: true, + compressed: true, + headers: { + Authorization: 'token ' + config.GITHUB_TOKEN, + }, +}; async function postComment(issueUrl, commentText) { const url = `${issueUrl}/comments`; const payload = { body: commentText, }; - const options = { - json: true, - compressed: true, - headers: { - Authorization: 'token ' + config.GITHUB_TOKEN, - }, - }; await needle('post', url, payload, options); } exports.postComment = postComment; +async function deleteExistingComments(issueUrl) { + const listCommentsUrl = `${issueUrl}/comments`; + const payload = null; + const commentsResponse = await needle('get', listCommentsUrl, payload, options); + const comments = commentsResponse.body; + const deleteRequests = []; + for (const comment of comments) { + if (comment.user.id !== config.GITHUB_POSTER_ID) { + continue; + } + const deleteUrl = comment.url; + console.log('deleting comment', deleteUrl); + const deleteRequest = needle('delete', deleteUrl, payload, options); + deleteRequests.push(deleteRequest); + } + // TODO: throttle if needed + // right now it is expected that no more than 1 request will be present + await Promise.all(deleteRequests); +} +exports.deleteExistingComments = deleteExistingComments; //# sourceMappingURL=comment.js.map \ No newline at end of file diff --git a/dist/comment.js.map b/dist/comment.js.map index 960f5d3..ddcb404 100644 --- a/dist/comment.js.map +++ b/dist/comment.js.map @@ -1 +1 @@ -{"version":3,"file":"comment.js","sourceRoot":"","sources":["../src/comment.ts"],"names":[],"mappings":";;AAAA,iCAAiC;AAEjC,mCAAmC;AAE5B,KAAK,UAAU,WAAW,CAAC,QAAgB,EAAE,WAAmB;IACrE,MAAM,GAAG,GAAG,GAAG,QAAQ,WAAW,CAAC;IACnC,MAAM,OAAO,GAAG;QACd,IAAI,EAAE,WAAW;KAClB,CAAC;IACF,MAAM,OAAO,GAAG;QACd,IAAI,EAAE,IAAI;QACV,UAAU,EAAE,IAAI;QAChB,OAAO,EAAE;YACP,aAAa,EAAE,QAAQ,GAAG,MAAM,CAAC,Y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GAAG,MAAM,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QACpE,cAAc,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;KACpC;IAED,2BAA2B;IAC3B,uEAAuE;IACvE,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;AACpC,CAAC;AArBD,wDAqBC"} \ No newline at end of file diff --git a/dist/config.js b/dist/config.js index d0e8e70..10c609a 100644 --- a/dist/config.js +++ b/dist/config.js @@ -1,9 +1,25 @@ "use strict"; -const config = { +const initialConfig = { GITHUB_TOKEN: process.env.GITHUB_TOKEN, RELEASE_BRANCH: process.env.INPUT_RELEASEBRANCH, GITHUB_POSTER_ID: process.env.INPUT_GITHUBPOSTERID, GITHUB_PR_USERNAME: process.env.GITHUB_PR_USERNAME, }; +if (!initialConfig.GITHUB_TOKEN) { + throw new Error('missing GITHUB_TOKEN'); +} +if (!initialConfig.RELEASE_BRANCH) { + throw new Error('missing input: releaseBranch'); +} +if (!initialConfig.GITHUB_POSTER_ID) { + throw new Error('missing input: githubPosterId'); +} +const posterId = parseInt(initialConfig.GITHUB_POSTER_ID); +const config = { + GITHUB_TOKEN: process.env.GITHUB_TOKEN, + RELEASE_BRANCH: process.env.INPUT_RELEASEBRANCH, + GITHUB_POSTER_ID: posterId, + GITHUB_PR_USERNAME: process.env.GITHUB_PR_USERNAME, +}; module.exports = config; //# sourceMappingURL=config.js.map \ No newline at end of file diff --git a/dist/config.js.map b/dist/config.js.map index ca6af1b..e9e66d4 100644 --- a/dist/config.js.map +++ b/dist/config.js.map @@ -1 +1 @@ -{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,GAAG;IACb,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;IACtC,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB;IAC/C,gBAAgB,EAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB;IAClD,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB;CACnD,CAAC;AAEF,iBAAS,MAAM,CAAC"} \ No newline at end of file +{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";AAAA,MAAM,aAAa,GAAG;IACpB,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;IACtC,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB;IAC/C,gBAAgB,EAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB;IAClD,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB;CACnD,CAAC;AAEF,IAAI,CAAC,aAAa,CAAC,YAAY,EAAE;IAC/B,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;CACzC;AAED,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE;IACjC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;CACjD;AAED,IAAI,CAAC,aAAa,CAAC,gBAAgB,EAAE;IACnC,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;CAClD;AAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,gBAAgB,CAAC,CAAC;AAE1D,MAAM,MAAM,GAAG;IACb,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;IACtC,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB;IAC/C,gBAAgB,EAAE,QAAQ;IAC1B,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB;CACnD,CAAA;AAED,iBAAS,MAAM,CAAC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index a2646fe..30d139f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -19,15 +19,14 @@ async function main() { const eventData = fs.readFileSync(eventPath, 'utf8'); const eventObj = JSON.parse(eventData); // console.log(eventObj); + const issueUrl = eventObj.pull_request.issue_url; + await comment.deleteExistingComments(issueUrl); const commitsData = await commit.getCommits(); const message = compose.previewFromCommits(commitsData); if (!message) { console.log('no relevant changes detected, exiting gracefully'); process.exit(0); } - // TODO: list all comments - // TODO: delete all relevant comments - const issueUrl = eventObj.pull_request.issue_url; comment.postComment(issueUrl, message); } main(); diff --git a/dist/index.js.map b/dist/index.js.map index 45aa5df..4628e7b 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,yBAAyB;AAEzB,qCAAqC;AACrC,qCAAqC;AACrC,mCAAmC;AACnC,qCAAqC;AAErC,KAAK,UAAU,IAAI;IACjB,oCAAoC;IACpC,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IAChD,IAAI,SAAS,KAAK,cAAc,EAAE;QAChC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;IAED,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IAChD,IAAI,CAAC,SAAS,EAAE;QACd,OAAO,CAAC,GAAG,CAAC,qCAAqC,CAAC,CAAC;QACnD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;IAED,MAAM,SAAS,GAAG,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACrD,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACvC,yBAAyB;IAEzB,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,UAAU,EAAE,CAAC;IAC9C,MAAM,OAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC;IACxD,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,CAAC,GAAG,CAAC,kDAAkD,CAAC,CAAC;QAChE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;IAED,0BAA0B;IAC1B,qCAAq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KACjB;IAED,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACzC,CAAC;AAED,IAAI,EAAE,CAAC"} \ No newline at end of file diff --git a/dist/tsconfig.tsbuildinfo b/dist/tsconfig.tsbuildinfo index 0eb8988..9183b61 100644 --- a/dist/tsconfig.tsbuildinfo +++ b/dist/tsconfig.tsbuildinfo @@ -314,12 +314,12 @@ "signature": "3b7ee66f0fc447e9c6db2b8824a6cf9d90fe98d2623eee33e30c14f3e22ceda5" }, "../src/config.ts": { - "version": "0ddbe0b3965f0b64809a6bc70f47ff189dc424cda05ae58e21d212435e5b464a", - "signature": "2782e63ac260c8378d1cd37a3c766b5e89e7e588155eee71a3d3b22f4bdd8859" + "version": "40f321c21193ebd72166f4a2d302273c6f65b7230ff868fc3fcc93b0c4814e62", + "signature": "8c4b7f94ddf84526f305c41e27f82c06a3230c6dc0e6c75763129e11129c21df" }, "../src/comment.ts": { - "version": "116fdb624201b58cd716a335516f851648bac031b8c2814b911e7a620226184b", - "signature": "049479754c13a674b3ec824a3a16d251f5b6dd9b8f28f4bd8ae3fdf937c57cd9" + "version": "aa5ecb8c6f659b20e42a23b874531adc9c8a837c9a424bd6ece9eb7ba5b5fedb", + "signature": "0cb8d03e55c9dea9f3bc8589000f32a46bd418148f02d0b4d7b4084f0f7c00aa" }, "../node_modules/@types/child-process-promise/index.d.ts": { "version": "57a8223e715ae455cbd8324b54458c1da1065471b73d0278ba9d5b29541d602b", @@ -338,7 +338,7 @@ "signature": "a4926a5d72f5b3053cccf6dd6ba3f1f1d70431910de3e5890fc367554b98a7ed" }, "../src/index.ts": { - "version": "5e001caa32d02f184a957893145e4ccf6522322ac75c8c3ddc88917fc9aea448", + "version": "a0a040197410f7ff237c18484130c2407eeb82c5739085ecb0c485ca62c56bed", "signature": "8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881" } }, @@ -1453,12 +1453,12 @@ "../node_modules/@types/node/ts3.2/index.d.ts", "../node_modules/@types/needle/index.d.ts", "../src/config.ts", - "../src/comment.ts", "../node_modules/@types/child-process-promise/index.d.ts", "../src/types.ts", + "../src/commit.ts", "../src/compose.ts", "../src/index.ts", - "../src/commit.ts" + "../src/comment.ts" ] }, "version": "3.8.3" diff --git a/src/comment.ts b/src/comment.ts index 4b50e22..bb112de 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -2,18 +2,42 @@ import * as needle from 'needle'; import * as config from './config'; +const options = { + json: true, + compressed: true, + headers: { + Authorization: 'token ' + config.GITHUB_TOKEN, + }, +}; + export async function postComment(issueUrl: string, commentText: string): Promise { const url = `${issueUrl}/comments`; const payload = { body: commentText, }; - const options = { - json: true, - compressed: true, - headers: { - Authorization: 'token ' + config.GITHUB_TOKEN, - }, - }; await needle('post', url, payload, options); } + +export async function deleteExistingComments(issueUrl: string): Promise { + const listCommentsUrl = `${issueUrl}/comments`; + const payload = null; + + const commentsResponse = await needle('get', listCommentsUrl, payload, options); + const comments = commentsResponse.body; + + const deleteRequests: any[] = []; + for (const comment of comments) { + if (comment.user.id !== config.GITHUB_POSTER_ID) { + continue; + } + const deleteUrl = comment.url; + console.log('deleting comment', deleteUrl); + const deleteRequest = needle('delete', deleteUrl, payload, options); + deleteRequests.push(deleteRequest); + } + + // TODO: throttle if needed + // right now it is expected that no more than 1 request will be present + await Promise.all(deleteRequests); +} diff --git a/src/config.ts b/src/config.ts index 326a61e..77e2870 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,8 +1,29 @@ -const config = { +const initialConfig = { GITHUB_TOKEN: process.env.GITHUB_TOKEN, RELEASE_BRANCH: process.env.INPUT_RELEASEBRANCH, GITHUB_POSTER_ID: process.env.INPUT_GITHUBPOSTERID, GITHUB_PR_USERNAME: process.env.GITHUB_PR_USERNAME, }; +if (!initialConfig.GITHUB_TOKEN) { + throw new Error('missing GITHUB_TOKEN'); +} + +if (!initialConfig.RELEASE_BRANCH) { + throw new Error('missing input: releaseBranch'); +} + +if (!initialConfig.GITHUB_POSTER_ID) { + throw new Error('missing input: githubPosterId'); +} + +const posterId = parseInt(initialConfig.GITHUB_POSTER_ID); + +const config = { + GITHUB_TOKEN: process.env.GITHUB_TOKEN, + RELEASE_BRANCH: process.env.INPUT_RELEASEBRANCH, + GITHUB_POSTER_ID: posterId, + GITHUB_PR_USERNAME: process.env.GITHUB_PR_USERNAME, +} + export = config; diff --git a/src/index.ts b/src/index.ts index e63ace6..d7ead44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,9 @@ async function main() { const eventObj = JSON.parse(eventData); // console.log(eventObj); + const issueUrl = eventObj.pull_request.issue_url; + await comment.deleteExistingComments(issueUrl); + const commitsData = await commit.getCommits(); const message = compose.previewFromCommits(commitsData); if (!message) { @@ -29,9 +32,6 @@ async function main() { process.exit(0); } - // TODO: list all comments - // TODO: delete all relevant comments - const issueUrl = eventObj.pull_request.issue_url; comment.postComment(issueUrl, message); }