Skip to content

Commit

Permalink
feat(bitbucket): add support for constructing pull_request payloads (
Browse files Browse the repository at this point in the history
…#23)

## 🧰 Changes

Add support for constructing `pull_request` payloads 

TODO
- [x]  Update action code samples on the README

## 🧬 QA & Testing

Do the tests pass? More thorough instructions incoming!
  • Loading branch information
domharrington authored Aug 24, 2023
1 parent 109acc3 commit d2e6543
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 50 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
push:
branches:
- main
pull_request:
types: [opened, synchronize]
jobs:
sync:
runs-on: ubuntu-latest
Expand All @@ -40,14 +42,20 @@ npx @readme/[email protected] './*{yaml,yml,json}' --key=$README_MICRO_SECRET
### As a Bitbucket Pipeline

```yaml
image: node:18

definitions:
steps:
- step: &readme-micro
name: ReadMe Micro
image: node:18
script:
- npx @readme/[email protected] '**/*.{yaml,yml,json}' --key=$README_MICRO_SECRET

# Run Pipeline to sync OpenAPI files for every push to the `main` branch
pipelines:
branches:
# Run Pipeline to sync OpenAPI files for every push to the `main` branch
main:
- step:
name: ReadMe Micro
script:
- npx @readme/[email protected] './*{yaml,yml,json}' --key=$README_MICRO_SECRET
- step: *readme-micro
pull-requests:
'**':
- step: *readme-micro
```
56 changes: 47 additions & 9 deletions __tests__/lib/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('getContext()', () => {
.reply(200, { display_name: 'Dom H' });

await expect(getContext()).resolves.toMatchObject({
eventName: 'push',
ref: `refs/heads/${process.env.BITBUCKET_BRANCH}`,
sha: process.env.BITBUCKET_COMMIT,
actor: 'Dom H',
runId: parseInt(process.env.BITBUCKET_BUILD_NUMBER, 10),
payload: {
commits: [
Expand All @@ -109,6 +109,9 @@ describe('getContext()', () => {
organization: process.env.BITBUCKET_WORKSPACE,
name: process.env.BITBUCKET_REPO_SLUG,
},
organization: {
login: process.env.BITBUCKET_WORKSPACE,
},
},
});

Expand Down Expand Up @@ -151,18 +154,53 @@ describe('getContext()', () => {
});

await expect(getContext()).resolves.toMatchObject({
actor: 'Unknown User',
payload: {
// commits: [
// {
// author: {
// username: 'Unknown User',
// },
// },
// ],
commits: [
{
author: {
name: 'Unknown User',
},
},
],
},
});
mock.done();
});

it('should work for `pull_requests`', async () => {
process.env.BITBUCKET_COMMIT = latestCommitSha;
process.env.BITBUCKET_STEP_TRIGGERER_UUID = '{636fcf11-a096-4b88-99ed-ce185e001fdb}';
process.env.BITBUCKET_BUILD_NUMBER = '1234';
process.env.BITBUCKET_WORKSPACE = 'workspace-name';
process.env.BITBUCKET_REPO_SLUG = 'repo-name';
process.env.BITBUCKET_BRANCH = 'main';

process.env.BITBUCKET_PR_ID = '999';

// eslint-disable-next-line global-require
const getContext = require('../../lib/context');

const mock = nock('https://api.bitbucket.org')
.get(`/2.0/users/${encodeURIComponent(process.env.BITBUCKET_STEP_TRIGGERER_UUID)}`)
.reply(200, { display_name: 'Dom H' });

await expect(getContext()).resolves.toMatchObject({
eventName: 'pull_request',
ref: `refs/heads/${process.env.BITBUCKET_BRANCH}`,
sha: process.env.BITBUCKET_COMMIT,
runId: parseInt(process.env.BITBUCKET_BUILD_NUMBER, 10),
payload: {
number: process.env.BITBUCKET_PR_ID,
repository: {
name: process.env.BITBUCKET_REPO_SLUG,
},
organization: {
login: process.env.BITBUCKET_WORKSPACE,
},
},
});

mock.done();
});
});
});
99 changes: 65 additions & 34 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ module.exports = async function getContext() {
case 'GitHub Actions':
return context;
case 'Bitbucket Pipelines': {
const baseContext = {
ref: process.env.BITBUCKET_TAG
? `refs/tags/${process.env.BITBUCKET_TAG}`
: `refs/heads/${process.env.BITBUCKET_BRANCH}`,
sha: process.env.BITBUCKET_COMMIT,
runId: parseInt(process.env.BITBUCKET_BUILD_NUMBER, 10),
};

// Pipelines dont give us the username of the user who made the action
// happen, so we have to make an extra request to fetch this
// https://jira.atlassian.com/browse/BCLOUD-16711
Expand All @@ -52,42 +60,65 @@ module.exports = async function getContext() {

const actor = response.data.display_name || 'Unknown User';

return {
ref: process.env.BITBUCKET_TAG
? `refs/tags/${process.env.BITBUCKET_TAG}`
: `refs/heads/${process.env.BITBUCKET_BRANCH}`,
sha: process.env.BITBUCKET_COMMIT,
actor,
runId: parseInt(process.env.BITBUCKET_BUILD_NUMBER, 10),
payload: {
repository: {
name: process.env.BITBUCKET_REPO_SLUG,
organization: process.env.BITBUCKET_WORKSPACE,
},
commits: [
{
message: execSync('git log --format=%B -n 1 $BITBUCKET_COMMIT', {
env: { BITBUCKET_COMMIT: process.env.BITBUCKET_COMMIT },
encoding: 'utf-8',
}).trim(),
// Get the unix timestamp of the commit then convert it to
// an ISO8601 UTC string
//
// https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emctem
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
timestamp: new Date(
execSync('git show -s --format=%ct $BITBUCKET_COMMIT', {
env: { BITBUCKET_COMMIT: process.env.BITBUCKET_COMMIT },
encoding: 'utf-8',
}).trim() * 1000
).toISOString(),
author: {
name: actor,
// BITBUCKET_PR_ID only set on pull request triggered builds
// this is how we determine if it's a `push` or `pull_request`
const eventName = process.env.BITBUCKET_PR_ID ? 'pull_request' : 'push';

switch (eventName) {
case 'push':
return {
...baseContext,
eventName,
payload: {
repository: {
name: process.env.BITBUCKET_REPO_SLUG,
// TODO we should remove this in a future release
organization: process.env.BITBUCKET_WORKSPACE,
},
organization: {
login: process.env.BITBUCKET_WORKSPACE,
},
commits: [
{
message: execSync('git log --format=%B -n 1 $BITBUCKET_COMMIT', {
env: { BITBUCKET_COMMIT: process.env.BITBUCKET_COMMIT },
encoding: 'utf-8',
}).trim(),
// Get the unix timestamp of the commit then convert it to
// an ISO8601 UTC string
//
// https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emctem
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
timestamp: new Date(
execSync('git show -s --format=%ct $BITBUCKET_COMMIT', {
env: { BITBUCKET_COMMIT: process.env.BITBUCKET_COMMIT },
encoding: 'utf-8',
}).trim() * 1000
).toISOString(),
author: {
name: actor,
},
},
],
},
],
},
};
};
case 'pull_request':
return {
...baseContext,
eventName,
payload: {
number: process.env.BITBUCKET_PR_ID,
repository: {
name: process.env.BITBUCKET_REPO_SLUG,
},
organization: {
login: process.env.BITBUCKET_WORKSPACE,
},
},
};
default:
return {};
}
}
default:
return {};
Expand Down

0 comments on commit d2e6543

Please sign in to comment.