Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
feat: delete previous comments before posting
Browse files Browse the repository at this point in the history
  • Loading branch information
Shesekino committed Mar 18, 2020
1 parent 6e98f86 commit a17510c
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 32 deletions.
34 changes: 27 additions & 7 deletions dist/comment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/comment.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion dist/config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/config.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions dist/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -338,7 +338,7 @@
"signature": "a4926a5d72f5b3053cccf6dd6ba3f1f1d70431910de3e5890fc367554b98a7ed"
},
"../src/index.ts": {
"version": "5e001caa32d02f184a957893145e4ccf6522322ac75c8c3ddc88917fc9aea448",
"version": "a0a040197410f7ff237c18484130c2407eeb82c5739085ecb0c485ca62c56bed",
"signature": "8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881"
}
},
Expand Down Expand Up @@ -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"
Expand Down
38 changes: 31 additions & 7 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<void> {
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);
}
23 changes: 22 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ 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) {
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);
}

Expand Down

0 comments on commit a17510c

Please sign in to comment.