Skip to content

Commit

Permalink
Require the Pull Request Commit Links API to work
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2b3bfa0 authored Jun 18, 2021
1 parent 68cb5a2 commit 94cc7d4
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/drivers/bitbucket_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,15 @@ class BitBucketCloud {
const { projectPath } = this;
const { commitSha, report, update, watermark } = opts;

const commitEndpoint = `/repositories/${projectPath}/commit/${commitSha}/comments/`;

const existingCommmit = (
await this.paginatedRequest({ endpoint: commitEndpoint, method: 'GET' })
)
.filter((comment) => {
const { content: { raw = '' } = {} } = comment;
return raw.endsWith(watermark);
})
.sort((first, second) => first.id < second.id)
.pop();

const commitOutput = (
await this.request({
endpoint:
commitEndpoint +
(update && existingCommmit ? existingCommmit.id : ''),
method: update && existingCommmit ? 'PUT' : 'POST',
body: JSON.stringify({ content: { raw: report } })
})
).links.html.href;

// Check for a corresponding PR. If it exists, also put the comment there.
let prs;
try {
const getPrEndpoint = `/repositories/${projectPath}/commit/${commitSha}/pullrequests`;
prs = await this.paginatedRequest({ endpoint: getPrEndpoint });
} catch (err) {
if (err.message === 'Not Found Resource not found')
console.warn(
"Can't create a pull request comment: the Pull Request Commit Links application has not been installed."
);
else throw err;
err.message = 'Click \'Go to pull request\' on any commit details page to enable this API'
throw err;
}

if (prs && prs.length) {
Expand Down Expand Up @@ -85,7 +61,27 @@ class BitBucketCloud {
}
}

return commitOutput;
const commitEndpoint = `/repositories/${projectPath}/commit/${commitSha}/comments/`;

const existingCommmit = (
await this.paginatedRequest({ endpoint: commitEndpoint, method: 'GET' })
)
.filter((comment) => {
const { content: { raw = '' } = {} } = comment;
return raw.endsWith(watermark);
})
.sort((first, second) => first.id < second.id)
.pop();

return (
await this.request({
endpoint:
commitEndpoint +
(update && existingCommmit ? existingCommmit.id : ''),
method: update && existingCommmit ? 'PUT' : 'POST',
body: JSON.stringify({ content: { raw: report } })
})
).links.html.href;
}

async checkCreate() {
Expand Down

1 comment on commit 94cc7d4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment

CML watermark

Please sign in to comment.