Skip to content

Commit

Permalink
ARCH-2011 - Adding tests for PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danielle-casella-adams committed Feb 22, 2024
1 parent a924fe7 commit b316dff
Show file tree
Hide file tree
Showing 9 changed files with 4,600 additions and 13 deletions.
240 changes: 234 additions & 6 deletions .github/workflows/build-and-review-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
# action needs to be compiled. For composite-run-steps actions this is typically empty.
build-command: 'npm run build'

test:
test-unit:
runs-on: ubuntu-latest
if: 1 == 2
env:
Expand Down Expand Up @@ -641,7 +641,7 @@ jobs:

test-status-checks:
runs-on: ubuntu-latest

if: 1 == 2
env:
NO_FAILURES_DIR: './test/files/multiple-trx'
FAILURES_DIR: './test/files/failing-test'
Expand Down Expand Up @@ -832,7 +832,12 @@ jobs:

test-pr-comments:
runs-on: ubuntu-latest
if: 1 == 2
env:
NO_FAILURES_DIR: './test/files/multiple-trx'
TRUNCATE_DIR: './test/files/truncate'
EXISTING_COMMENT_ID: ''
COMMENT_IDENTIFIER: 'existing-comment-${{ github.run_id }}'

steps:
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
Expand All @@ -849,20 +854,243 @@ jobs:
- name: Setup - Checkout the action
uses: actions/checkout@v4

- name: Setup - Create a comment that can be updated
if: always()
uses: actions/github-script@v6
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '<!-- im-open/process-dotnet-test-results ${{ env.COMMENT_IDENTIFIER }} -->\nThis comment will be replaced soon.'
})
.then(response => {
core.exportVariable('EXISTING_COMMENT_ID', response.data.id);
})
.catch(error => {
core.setFailed(`An error occurred in the setup step while creating a comment: ${error.message}`);
});
await new Promise(r => setTimeout(r, 5 * 1000));
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 1 - UPDATE COMMENT '
- name: ' TEST 18 - PR COMMENT - UPDATE W/ MATCHING PREFIX '
run: echo ""

- name: 18 - When process-dotnet-test-results is called with updateComment=true and there is a comment with matching prefix
if: always()
id: update-with-matching-prefix
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
base-directory: ${{ env.NO_FAILURES_DIR }}
create-results-file: true # Keep this so we can compare the comment contents to the expected contents
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: ${{ env.COMMENT_IDENTIFIER }}

- name: 18 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.update-with-matching-prefix.outcome }}"

- name: 18 - And the pr-comment-id output should match the existing comment id
if: always()
run: ./test/assert-values-match.sh --name "pr-comment-id output" --expected "${{ env.EXISTING_COMMENT_ID }}" --actual "${{ steps.update-with-matching-prefix.outputs.pr-comment-id }}"

- name: 18 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.update-with-matching-prefix.outputs.test-results-truncated }}"

- name: 18 - And the pr-comment should match the match the expected values
if: always()
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.update-with-matching-prefix.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, core, commentId);
const expectedBody = fs.readFileSync('./test-results.md', 'utf8');
const expectedPrefix = '<!-- im-open/process-dotnet-test-results ${{ env.COMMENT_IDENTIFIER }} -->';
const expectedComment = {
prefixAndBody: `${expectedPrefix}\n${expectedBody}`,
action: 'updated'
};
assertCommentMatchesExpectations(core, actualComment, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 2 - DO NOT UPDATE COMMENT '
- name: ' TEST 19 - PR COMMENT - UPDATE W/O MATCHING PREFIX '
run: echo ""

- name: 19 - When process-dotnet-test-results is called with updateComment=true but there is no comment with matching prefix
if: always()
id: update-without-matching-prefix
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
base-directory: ${{ env.NO_FAILURES_DIR }}
create-results-file: true # Keep this so we can compare the comment contents to the expected contents
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: 'different-identifier-${{ github.run_id }}'

- name: 19 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.update-without-matching-prefix.outcome }}"

- name: 19 - And the pr-comment-id output should be different than the existing comment id
if: always()
run: ./test/assert-values-do-not-match.sh --name "pr-comment-id output" --value1 "${{ env.EXISTING_COMMENT_ID }}" --value2 "${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}"

- name: 19 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.update-without-matching-prefix.outputs.test-results-truncated }}"

- name: 19 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, core, commentId);
const expectedBody = fs.readFileSync('./test-results.md', 'utf8');
const expectedPrefix = '<!-- im-open/process-dotnet-test-results different-identifier-${{ github.run_id }} -->';
const expectedComment = {
prefixAndBody: `${expectedPrefix}\n${expectedBody}`,
action: 'created'
};
assertCommentMatchesExpectations(core, actualComment, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 3 - TRUNCATE COMMENT '
- name: ' TEST 20 - PR COMMENT - NO UPDATE '
run: echo ""

- name: 20 - When process-dotnet-test-results is called with updateComment=false
if: always()
id: matching-prefix-no-update
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
base-directory: ${{ env.NO_FAILURES_DIR }}
create-results-file: true # Keep this so we can compare the comment contents to the expected contents
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: false

- name: 20 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.matching-prefix-no-update.outcome }}"

- name: 20 - And the pr-comment-id output should be different than the existing comment id
if: always()
run: ./test/assert-values-do-not-match.sh --name "pr-comment-id output" --value1 "${{ env.EXISTING_COMMENT_ID }}" --value2 "${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}"

- name: 20 - And the test-results-truncated output should be false
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "false" --actual "${{ steps.matching-prefix-no-update.outputs.test-results-truncated }}"

- name: 20 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, core, commentId);
const expectedBody = fs.readFileSync('./test-results.md', 'utf8');
const expectedPrefix = '<!-- im-open/process-dotnet-test-results -->';
const expectedComment = {
prefixAndBody: `${expectedPrefix}\n${expectedBody}`,
action: 'created'
};
assertCommentMatchesExpectations(core, actualComment, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEST 21 - PR COMMENT - TRUNCATE '
run: echo ""

- name: 21 - When process-dotnet-test-results is called with a large comment that needs to be truncated
if: always()
id: truncate
uses: ./
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
base-directory: ${{ env.TRUNCATE_DIR }}
create-results-file: true # Keep this so we can compare the comment contents to the expected contents
create-status-check: false
create-pr-comment: true
update-comment-if-one-exists: true
comment-identifier: ${{ env.COMMENT_IDENTIFIER }}

- name: 21 - Then the action outcome should be success
if: always()
run: ./test/assert-values-match.sh --name "step outcome" --expected "success" --actual "${{ steps.truncate.outcome }}"

- name: 21 - And the pr-comment-id output should match the existing comment id
if: always()
run: ./test/assert-values-match.sh --name "pr-comment-id output" --expected "${{ env.EXISTING_COMMENT_ID }}" --actual "${{ steps.truncate.outputs.pr-comment-id }}"

- name: 21 - And the test-results-truncated output should be true
if: always()
run: ./test/assert-values-match.sh --name "test-results-truncated output" --expected "true" --actual "${{ steps.truncate.outputs.test-results-truncated }}"

- name: 21 - And the pr-comment should match the expected values
if: always()
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const assertCommentExists = require('./test/assert-pr-comment-exists.js');
const assertCommentMatchesExpectations = require('./test/assert-pr-comment-matches-expectations.js');
const commentId = '${{ steps.truncate.outputs.pr-comment-id }}';
const actualComment = await assertCommentExists(github, core, commentId);
const expectedBody = fs.readFileSync('./test-results.md', 'utf8');
const expectedPrefix = '<!-- im-open/process-dotnet-test-results ${{ env.COMMENT_IDENTIFIER }} -->';
const expectedComment = {
prefixAndBody: `${expectedPrefix}\nTest results truncated due to character limit. See full report in output. \n${expectedBody}`,
action: 'updated'
};
assertCommentMatchesExpectations(core, actualComment, expectedComment);
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
- name: ' TEARDOWN '
run: echo ""

- name: Teardown - Delete PR Comments
if: always()
uses: actions/github-script@v6
with:
github-token: ${{ secrets.PIPELINE_BOT_PAT }}
script: |
const fs = require('fs');
const deletePrComment = require('./test/delete-pr-comment.js');
await deletePrComment(github, core, '${{ secrets.PIPELINE_BOT_PAT }}', '${{ env.EXISTING_COMMENT_ID }}');
await deletePrComment(github, core, '${{ secrets.PIPELINE_BOT_PAT }}', '${{ steps.matching-prefix-no-update.outputs.pr-comment-id }}');
await deletePrComment(github, core, '${{ secrets.PIPELINE_BOT_PAT }}', '${{ steps.update-without-matching-prefix.outputs.pr-comment-id }}');
- name: '-------------------------------------------------------------------------------------------------------------'
run: echo ""
6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27898,6 +27898,8 @@ async function createPRCommentIfRequested(testResultsMarkup) {
return;
}
let markup = testResultsMarkup;
core.info(`
Creating a PR comment with length ${markup.length}...`);
const charLimit = 65535;
let truncated = false;
if (markup.length > charLimit) {
Expand All @@ -27909,9 +27911,7 @@ async function createPRCommentIfRequested(testResultsMarkup) {
}
core.setOutput('test-results-truncated', truncated);
const commentId = await createPrComment(token, markup, updateCommentIfOneExists, commentIdentifier);
if (commentId && commentId.length > 0) {
core.setOutput('pr-comment-id', commentId);
}
core.setOutput('pr-comment-id', commentId);
}
async function getMarkupAndCreateStatusCheckForEachTrxFile(trxToJson) {
let markupForResults = [];
Expand Down
7 changes: 3 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function createPRCommentIfRequested(testResultsMarkup) {
// The README.md indicates only one per comment per run
// so all the trx markup will be combined into one comment
let markup = testResultsMarkup;
core.info(`\nCreating a PR comment with length ${markup.length}...`);

// GitHub API has a limit of 65535 characters for a comment so truncate the markup if we need to
const charLimit = 65535;
Expand All @@ -56,9 +57,7 @@ async function createPRCommentIfRequested(testResultsMarkup) {

// TODO: implement steve's change for cypress
const commentId = await createPrComment(token, markup, updateCommentIfOneExists, commentIdentifier);
if (commentId && commentId.length > 0) {
core.setOutput('pr-comment-id', commentId); // This is mainly for testing purposes
}
core.setOutput('pr-comment-id', commentId); // This is mainly for testing purposes
}

async function getMarkupAndCreateStatusCheckForEachTrxFile(trxToJson) {
Expand All @@ -82,7 +81,7 @@ async function getMarkupAndCreateStatusCheckForEachTrxFile(trxToJson) {

if (shouldCreateStatusCheck && statusCheckIds.length > 0) {
core.info(`\nThe following status check ids were created: ${statusCheckIds.join(',')}`);
core.setOutput('status-check-ids', statusCheckIds.join(','));
core.setOutput('status-check-ids', statusCheckIds.join(',')); // This is mainly for testing purposes
}
return markupForResults.join('\n');
}
Expand Down
34 changes: 34 additions & 0 deletions test/assert-pr-comment-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = async (github, core, commentId) => {
core.info(`\nAsserting that PR Comment with the following id exists: '${commentId}'`);

let actualComment;

if (!commentId || commentId.trim() === '') {
core.setFailed(`The comment id provided was empty.`);
}

const commentResponse = await github.rest.issues.getComment({
owner: 'im-open',
repo: 'process-dotnet-test-results',
comment_id: commentId.trim()
});

if (!commentResponse && !commentResponse.data) {
core.setFailed(`Comment ${commentId} does not appear to exist.`);
} else {
core.info(`Comment ${commentId} exists.`);
let rawComment = commentResponse.data;

actualComment = {
id: rawComment.id,
body: rawComment.body,
createdAt: rawComment.created_at,
updatedAt: rawComment.updated_at,
issueUrl: rawComment.issue_url
};
core.info(`Comment ${actualComment.id} details:`);
console.log(actualComment);
}

return actualComment;
};
Loading

0 comments on commit b316dff

Please sign in to comment.