Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better error handling #16

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions resources/exploratory_test_comment_failure_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$$ERROR_MESSAGE$$

<details>
<summary>Details 👇</summary>
The following payload has been sent to trigger the test on TestIO:

```json
$$SENT_PAYLOAD$$
```

</details>

As response to [test creation trigger]($$CREATE_COMMENT_URL$$).
19 changes: 14 additions & 5 deletions src/reportFailure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ async function reportFailure() {
const errorMessageFilePath = `${process.env.TESTIO_SCRIPTS_DIR}/resources/${errorFileName}`;
const createCommentUrl = `${process.env.TESTIO_CREATE_COMMENT_URL}`;

let commentFailureBody = "";
const payloadFile = `${process.env.TESTIO_SCRIPTS_DIR}/resources/testio_payload.json`;
const payloadString = JSON.stringify(JSON.parse(fs.readFileSync(payloadFile, 'utf8')), null, 2);

let commentErrorMessage = "";
if (fs.existsSync(errorMessageFilePath)) {
const errorMessageToReport = fs.readFileSync(errorMessageFilePath, 'utf8');
commentFailureBody = "🚨 Failure 🚨 :bangbang: ⛔️ Please check the following error ⛔️ :bangbang: \n```" + errorMessageToReport + "```";
commentErrorMessage = "🚨 Failure 🚨 :bangbang: ⛔️ Please check the following error ⛔️ :bangbang: \n\n```" + errorMessageToReport + "```";
} else {
commentFailureBody = "🚨 Failed to trigger a test on TestIO 🚨 Please revise your steps";
commentErrorMessage = "🚨 Failed to trigger a test on TestIO 🚨 Please revise your steps";
}
commentFailureBody = commentFailureBody.concat(`\n\nAs response to [test creation trigger](${createCommentUrl}).`);

const commentFailureTemplateFile = `${process.env.TESTIO_SCRIPTS_DIR}/resources/exploratory_test_comment_failure_template.md`;
const commentFailureTemplate = fs.readFileSync(commentFailureTemplateFile, 'utf8');
const commentErrorMessagePlaceholder = "$$ERROR_MESSAGE$$";
const sentPayloadPlaceholder = "$$SENT_PAYLOAD$$";
const createCommentUrlPlaceholder = "$$CREATE_COMMENT_URL$$";
const failureCommentBody = commentFailureTemplate.replace(commentErrorMessagePlaceholder, commentErrorMessage).replace(sentPayloadPlaceholder, payloadString).replace(createCommentUrlPlaceholder, createCommentUrl);

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
Expand All @@ -40,7 +49,7 @@ async function reportFailure() {
repo: github.context.repo.repo,
owner: github.context.repo.owner,
issue_number: github.context.issue.number,
body: commentFailureBody
body: failureCommentBody
});

}
Expand Down