Skip to content

Commit

Permalink
Merge pull request #22 from Staffbase/fix-bugs
Browse files Browse the repository at this point in the history
🐛 Fix some bugs
  • Loading branch information
jreimone authored Jul 12, 2023
2 parents 8ac3977 + 580a2d2 commit bdbeedc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
18 changes: 13 additions & 5 deletions src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class Util {
}
const jsonContents = matches[expectedIndexOfObject];
if (!jsonContents) throw new Error("Provided input seems to be empty between ```json and ```");
const parsedObject = JSON.parse(jsonContents);
const cleanedJson = jsonContents.replace(/[\u0009]/g, " ");
const parsedObject = JSON.parse(cleanedJson);
return parsedObject;
}

Expand All @@ -41,13 +42,13 @@ export class Util {
}

public static convertPrepareObjectToTestIOPayload(prepareObject: any, repo: string, owner: string, pr: number, prTitle: string): any {
// 80 is restriction from TestIO
const titleBase = `[${owner}/${repo}/${pr}]${prTitle}`;
const testioPayload = {
exploratory_test: {
test_title: titleBase,
test_environment: {
title: Util.truncateString(titleBase, 80, "[test environment]", false),
// 80 is restriction from TestIO
title: Util.truncateString(titleBase, 80, "[test environment]", true),
url: prepareObject.test_environment.url,
access: prepareObject.test_environment.access,
},
Expand Down Expand Up @@ -119,10 +120,17 @@ export class Util {
}

public static truncateString(string: string, maxLength: number, suffix: string, forceAddSuffix: boolean) {
if (string.length <= maxLength) {
if (string.length <= maxLength && !forceAddSuffix) {
return string;
}

return string.slice(0, maxLength - suffix.length - 1) + suffix;
return string.slice(0, maxLength - suffix.length) + suffix;
}

public static retrievePrepareObjectFromComment(comment: string): any | undefined {
const jsonRegex = /.*```json\s(.+)\s```.*/sm; // everything between ```json and ``` so that we can parse it
let preparation: any;
preparation = Util.getJsonObjectFromComment(jsonRegex, comment, 1);
return preparation;
}
}
13 changes: 9 additions & 4 deletions src/reportFailure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ async function reportFailure() {
const errorMessageFilePath = `${process.env.TESTIO_SCRIPTS_DIR}/resources/${errorFileName}`;
const createCommentUrl = `${process.env.TESTIO_CREATE_COMMENT_URL}`;

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');
commentErrorMessage = "🚨 Failure 🚨 :bangbang: ⛔️ Please check the following error ⛔️ :bangbang: \n\n```" + errorMessageToReport + "```";
} else {
commentErrorMessage = "🚨 Failed to trigger a test on TestIO 🚨 Please revise your steps";
}

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 payloadFile = `${process.env.TESTIO_SCRIPTS_DIR}/resources/testio_payload.json`;
let payloadString = "";
if (fs.existsSync(payloadFile)) {
payloadString = JSON.stringify(JSON.parse(fs.readFileSync(payloadFile, 'utf8')), null, 2);
}

const failureCommentBody = commentFailureTemplate.replace(commentErrorMessagePlaceholder, commentErrorMessage).replace(sentPayloadPlaceholder, payloadString).replace(createCommentUrlPlaceholder, createCommentUrl);

const octokit = new Octokit({
Expand Down
9 changes: 6 additions & 3 deletions src/retrievePayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ async function createPayload() {

const commentContents = `${retrievedComment.data.body}`;
if (!commentContents) Util.throwErrorAndPrepareErrorMessage(`Comment ${commentUrl} seems to be empty`, errorFileName);

const triggerCommentUrl = Util.getUrlFromComment(commentContents);
if (triggerCommentUrl != undefined) {
core.setOutput("testio-create-comment-url", triggerCommentUrl);
} else {
core.setOutput("testio-create-comment-url", "");
}

const jsonRegex = /```json\s(.+)\s```/sm; // everything between ```json and ``` so that we can parse it
let preparation: any;
try {
preparation = Util.getJsonObjectFromComment(jsonRegex, commentContents, 1);
preparation = Util.retrievePrepareObjectFromComment(commentContents);
} catch (error) {
Util.throwErrorAndPrepareErrorMessage(JSON.stringify(error), errorFileName);
if (error instanceof Error) {
Util.throwErrorAndPrepareErrorMessage(error.message, errorFileName);
}
Util.throwErrorAndPrepareErrorMessage(String(error), errorFileName);
}

const prepareTestSchemaFile = `${process.env.TESTIO_SCRIPTS_DIR}/resources/exploratory_test_comment_prepare_schema.json`;
Expand Down
18 changes: 14 additions & 4 deletions test/Util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("TestIO Trigger-from-PR logic", () => {
const testioPayload = Util.convertPrepareObjectToTestIOPayload(prepareObject, repo, owner, pr, prTitle);
const testName = `[${owner}/${repo}/${pr}]${prTitle}`;
expect(testioPayload.exploratory_test.test_title).toBe(testName);
expect(testioPayload.exploratory_test.test_environment.title).toBe(testName + " [test environment]");
expect(testioPayload.exploratory_test.test_environment.title).toBe(testName + "[test environment]");
expect(testioPayload.exploratory_test.test_environment.url).toBe(prepareObject.test_environment.url);
expect(testioPayload.exploratory_test.test_environment.access).toBe(prepareObject.test_environment.access);
expect(testioPayload.exploratory_test.features[0].title).toBe(prepareObject.feature.title);
Expand All @@ -79,19 +79,29 @@ describe("TestIO Trigger-from-PR logic", () => {
expect(testioPayload.exploratory_test.features[0].user_stories).toBe(prepareObject.feature.user_stories);
});

it('should truncate looooooong PR titles with ellipsis', () => {
it('should truncate looooooong PR titles and add suffix', () => {
const maxLength= 80;
let prTitle = "This is my short title";
let truncatedString = Util.truncateString(prTitle, maxLength, "...", false);
expect(truncatedString).toBe(prTitle);

prTitle = "This is my veryyyyyyyy loooooooooooooooooooooooooooooooooooooooooooooooooong PR title";
truncatedString = Util.truncateString(prTitle, maxLength, "...", false);
expect(truncatedString).toBe("This is my veryyyyyyyy loooooooooooooooooooooooooooooooooooooooooooooooooong...");
expect(truncatedString).toBe("This is my veryyyyyyyy loooooooooooooooooooooooooooooooooooooooooooooooooong ...");

prTitle = "This is my veryyyyyyyy loooooooooooooooooooooooooooooooooooooooooooooooooooooooong PR title";
truncatedString = Util.truncateString(prTitle, maxLength, "[test environment]", true);
expect(truncatedString).toBe("This is my veryyyyyyyy looooooooooooooooooooooooooooooooooooo[test environment]");
expect(truncatedString).toBe("This is my veryyyyyyyy loooooooooooooooooooooooooooooooooooooo[test environment]");
});

// digging into failing action run https://github.com/Staffbase/testio-management/actions/runs/5527851778/jobs/10084029267
it('should parse a failing prepare comment', () => {
const failingCommentFile = "testResources/testio-management-pull-65-issuecomment-1630429841.md";
const failingComment = fs.readFileSync(failingCommentFile, 'utf8');

expect(() => Util.retrievePrepareObjectFromComment(failingComment)).not.toThrowError();
const prepareObject: any = Util.retrievePrepareObjectFromComment(failingComment);
expect(prepareObject).not.toBeUndefined();
});

});
20 changes: 20 additions & 0 deletions testResources/testio-management-pull-65-issuecomment-1630429841.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@bot-testio exploratory-test submit
```json
{
"test_environment": {
"url": "[your URL of preview deployment or built bundle from bot-the-builder](https://testio.staffbase.rocks/?_preview_version=10114)",
"access": "user: staffbaseuser1 password: posting-earshot-vertices"
},
"feature": {
"title": "Authentication",
"description": "Users need to be authenticated successfully",
"howtofind": "Open the app",
"user_stories": [
"Ensure that the user can log in successfully with the given credentials"
]
}
}


```
As response to [test creation trigger](https://github.com/Staffbase/testio-management/pull/65#issuecomment-1630428945).

0 comments on commit bdbeedc

Please sign in to comment.