Created
July 26, 2019 11:03
-
-
Save talhaHavadar/e4da95d1c63fdaacae9694d0b034e988 to your computer and use it in GitHub Desktop.
Sourcebox for "Using Javascript Async Iterator to Fetch Data From Paginated REST API" article in my linkedin(https://www.linkedin.com/in/talha-can-havadar-536b76b7/).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let axios = require('axios'); | |
const api = { | |
async version() { | |
let response = await axios.get("http://localhost:5454/rest/api/1.0/application-properties") | |
return response.data; | |
}, | |
async branches(projectKey, repositorySlug, params) { | |
try { | |
let response = await axios.get(`http://localhost:5454/rest/api/1.0/projects/${projectKey}/repos/${repositorySlug}/branches`, { | |
params: params | |
}); | |
return response.data; | |
} catch (error) { | |
return error; | |
} | |
} | |
}; | |
const utils = { | |
async getLinkedJiraIssuesOfBranch(branch) { | |
let pageIterator = { | |
[Symbol.asyncIterator]: () => { | |
return { | |
offset: 0, | |
finished: false, | |
async next() { | |
if (!this.finished) { | |
try { | |
let response = await api.branches(branch.repository.project.key, branch.repository.slug, { start: this.offset, filterText: branch.displayId }); | |
this.finished = response.isLastPage; | |
this.offset += response.limit; | |
return Promise.resolve({ | |
value: response, | |
done: false | |
}); | |
} catch (error) { | |
console.error([ | |
`[bitbucket.utils] Error occured while querying the branches with projectKey=${branch.repository.project.key}`, | |
`repositorySlug=${branch.repository.slug} ?filterText=${branch.displayId}. ${JSON.stringify(error)}` | |
].join("")); | |
return Promise.reject({ | |
done: true, | |
error: error | |
}); | |
} | |
} else { | |
this.offset = 0; | |
this.finished = false; | |
return Promise.resolve({ | |
done: true | |
}); | |
} | |
} | |
} | |
} | |
}; | |
try { | |
for await (const data of pageIterator) { | |
let { values } = data; | |
let branchDetails = values.find(item => item.id === branch.id); | |
if (branchDetails) { | |
return branchDetails.metadata["com.atlassian.bitbucket.server.bitbucket-jira:branch-list-jira-issues"]; | |
} | |
} | |
} catch (error) { | |
console.error(`[bitbucket.utils] Error occured while iterating through page iterator. ${JSON.stringify(error)}`); | |
} | |
return []; | |
} | |
}; | |
(async() => { | |
console.log(`BitBucket API ==> ${JSON.stringify(await api.version())}`); | |
console.log("Linked Issues:", await utils.getLinkedJiraIssuesOfBranch({ | |
id: "refs/heads/deneme", | |
displayId: "deneme", | |
repository: { | |
slug: "test-repo", | |
project: { | |
key: "test-project" | |
} | |
} | |
})); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "mockoon_export", | |
"appVersion": "1.4.0", | |
"checksum": "926609023232e43001705b219864a08a", | |
"subject": "environment", | |
"data": { | |
"uuid": "2f855330-aed3-11e9-afdf-5fe4e2ff0c33", | |
"name": "Bitbucket Server Mock", | |
"endpointPrefix": "", | |
"latency": 0, | |
"port": 5454, | |
"routes": [ | |
{ | |
"uuid": "3a1e2f60-aed3-11e9-afdf-5fe4e2ff0c33", | |
"documentation": "", | |
"method": "get", | |
"endpoint": "rest/api/1.0/application-properties", | |
"body": "{\n \"version\": \"2.1.0\",\n \"buildNumber\": \"20130123103656677\",\n \"buildDate\": \"1358897885952000\",\n \"displayName\": \"Example.com Bitbucket\"\n}", | |
"latency": 0, | |
"statusCode": "200", | |
"headers": [ | |
{ | |
"key": "", | |
"value": "" | |
} | |
], | |
"filePath": "", | |
"sendFileAsBody": false | |
}, | |
{ | |
"uuid": "0a49a130-aed7-11e9-afdf-5fe4e2ff0c33", | |
"documentation": "", | |
"method": "get", | |
"endpoint": "rest/api/1.0/projects/:projectKey/repos/:repositorySlug/branches", | |
"body": "{\n \"size\": 4,\n \"limit\": 25,\n \"isLastPage\": {{#switch (queryParam 'start')}}\n {{#case \"100\"}}true,{{/case}}\n {{#default}}false,{{/default}}\n {{/switch}}\n \"values\": [\n {\n \"id\": \"refs/heads/dev\",\n \"displayId\": \"dev\",\n \"type\": \"BRANCH\",\n \"latestCommit\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"latestChangeset\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"isDefault\": false,\n \"metadata\": {\n \"com.atlassian.bitbucket.server.bitbucket-jira:branch-list-jira-issues\": [],\n \"com.atlassian.bitbucket.server.bitbucket-branch:ahead-behind-metadata-provider\": {\n \"ahead\": 2,\n \"behind\": 4\n },\n \"com.atlassian.bitbucket.server.bitbucket-branch:latest-commit-metadata\": {\n \"id\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"displayId\": \"cb3cf2e4d15\",\n \"author\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"authorTimestamp\": 1563791286000,\n \"committer\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"committerTimestamp\": 1563791286000,\n \"message\": \"Merge pull request #4 in TCHBBT/test_webhooks from deneme to dev\\n\\n* commit 'e7e09ef1d63e39657aa2aabadc604774ecc3a065':\\n readme.md edited online with Bitbucket\",\n \"parents\": [\n {\n \"id\": \"bd376834da94f253842cec85e9e8387b60aff86d\",\n \"displayId\": \"bd376834da9\"\n },\n {\n \"id\": \"e7e09ef1d63e39657aa2aabadc604774ecc3a065\",\n \"displayId\": \"e7e09ef1d63\"\n }\n ]\n },\n \"com.atlassian.bitbucket.server.bitbucket-ref-metadata:outgoing-pull-request-metadata\": {\n \"open\": 0,\n \"merged\": 2,\n \"declined\": 0\n }\n }\n },\n {\n \"id\": \"refs/heads/deneme\",\n \"displayId\": \"deneme\",\n \"type\": \"BRANCH\",\n \"latestCommit\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"latestChangeset\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"isDefault\": false,\n \"metadata\": {\n \"com.atlassian.bitbucket.server.bitbucket-jira:branch-list-jira-issues\": [{\n \"key\": \"TCHJT-1\",\n \"url\": \"http://tjira01:8080/browse/TCHJT-1\"\n }],\n \"com.atlassian.bitbucket.server.bitbucket-branch:ahead-behind-metadata-provider\": {\n \"ahead\": 2,\n \"behind\": 4\n },\n \"com.atlassian.bitbucket.server.bitbucket-branch:latest-commit-metadata\": {\n \"id\": \"cb3cf2e4d1517c83e720d2585b9402dbef71f992\",\n \"displayId\": \"cb3cf2e4d15\",\n \"author\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"authorTimestamp\": 1563791286000,\n \"committer\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"committerTimestamp\": 1563791286000,\n \"message\": \"Merge pull request #4 in TCHBBT/test_webhooks from deneme to dev\\n\\n* commit 'e7e09ef1d63e39657aa2aabadc604774ecc3a065':\\n readme.md edited online with Bitbucket\",\n \"parents\": [\n {\n \"id\": \"bd376834da94f253842cec85e9e8387b60aff86d\",\n \"displayId\": \"bd376834da9\"\n },\n {\n \"id\": \"e7e09ef1d63e39657aa2aabadc604774ecc3a065\",\n \"displayId\": \"e7e09ef1d63\"\n }\n ]\n },\n \"com.atlassian.bitbucket.server.bitbucket-ref-metadata:outgoing-pull-request-metadata\": {\n \"pullRequest\": {\n \"id\": 4,\n \"version\": 2,\n \"title\": \"readme.md edited online with Bitbucket\",\n \"state\": \"MERGED\",\n \"open\": false,\n \"closed\": true,\n \"createdDate\": 1563791279047,\n \"updatedDate\": 1563791286297,\n \"closedDate\": 1563791286297,\n \"fromRef\": {\n \"id\": \"refs/heads/deneme\",\n \"displayId\": \"deneme\",\n \"latestCommit\": \"e7e09ef1d63e39657aa2aabadc604774ecc3a065\",\n \"repository\": {\n \"slug\": \"test_webhooks\",\n \"id\": 275,\n \"name\": \"test_webhooks\",\n \"scmId\": \"git\",\n \"state\": \"AVAILABLE\",\n \"statusMessage\": \"Available\",\n \"forkable\": true,\n \"project\": {\n \"key\": \"TCHBBT\",\n \"id\": 306,\n \"name\": \"TCH_BB_TEST\",\n \"description\": \"Talha Can HAVADAR - Bitbucket Test Project\",\n \"public\": false,\n \"type\": \"NORMAL\"\n },\n \"public\": false\n }\n },\n \"toRef\": {\n \"id\": \"refs/heads/dev\",\n \"displayId\": \"dev\",\n \"latestCommit\": \"bd376834da94f253842cec85e9e8387b60aff86d\",\n \"repository\": {\n \"slug\": \"test_webhooks\",\n \"id\": 275,\n \"name\": \"test_webhooks\",\n \"scmId\": \"git\",\n \"state\": \"AVAILABLE\",\n \"statusMessage\": \"Available\",\n \"forkable\": true,\n \"project\": {\n \"key\": \"TCHBBT\",\n \"id\": 306,\n \"name\": \"TCH_BB_TEST\",\n \"description\": \"Talha Can HAVADAR - Bitbucket Test Project\",\n \"public\": false,\n \"type\": \"NORMAL\"\n },\n \"public\": false\n }\n },\n \"locked\": false,\n \"author\": {\n \"user\": {\n \"name\": \"tchavadar\",\n \"emailAddress\": \"[email protected]\",\n \"id\": 3957,\n \"displayName\": \"Talha Can HAVADAR\",\n \"active\": true,\n \"slug\": \"tchavadar\",\n \"type\": \"NORMAL\"\n },\n \"role\": \"AUTHOR\",\n \"approved\": false,\n \"status\": \"UNAPPROVED\"\n },\n \"reviewers\": [],\n \"participants\": [],\n \"properties\": {\n \"resolvedTaskCount\": 0,\n \"openTaskCount\": 0\n }\n }\n }\n }\n },\n {\n \"id\": \"refs/heads/master\",\n \"displayId\": \"master\",\n \"type\": \"BRANCH\",\n \"latestCommit\": \"7ef9c59c07a08d542c35fb64dab023d2b1a7807d\",\n \"latestChangeset\": \"7ef9c59c07a08d542c35fb64dab023d2b1a7807d\",\n \"isDefault\": true,\n \"metadata\": {\n \"com.atlassian.bitbucket.server.bitbucket-jira:branch-list-jira-issues\": [],\n \"com.atlassian.bitbucket.server.bitbucket-branch:latest-commit-metadata\": {\n \"id\": \"7ef9c59c07a08d542c35fb64dab023d2b1a7807d\",\n \"displayId\": \"7ef9c59c07a\",\n \"author\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"authorTimestamp\": 1563791183000,\n \"committer\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"committerTimestamp\": 1563791183000,\n \"message\": \"Merge pull request #3 in TCHBBT/test_webhooks from feature/TCHJT-1-test-story-1 to master\\n\\n* commit '3c00a3654835a0c4ecc9d01252f0a7326e6e3a37':\\n readme.md edited online with Bitbucket\",\n \"parents\": [\n {\n \"id\": \"206425bc52376d2cc9868e5ecef5588b110ce720\",\n \"displayId\": \"206425bc523\"\n },\n {\n \"id\": \"3c00a3654835a0c4ecc9d01252f0a7326e6e3a37\",\n \"displayId\": \"3c00a365483\"\n }\n ]\n }\n }\n },\n {\n \"id\": \"refs/heads/feature/TCHJT-1-test-story-1\",\n \"displayId\": \"feature/TCHJT-1-test-story-1\",\n \"type\": \"BRANCH\",\n \"latestCommit\": \"3c00a3654835a0c4ecc9d01252f0a7326e6e3a37\",\n \"latestChangeset\": \"3c00a3654835a0c4ecc9d01252f0a7326e6e3a37\",\n \"isDefault\": false,\n \"metadata\": {\n \"com.atlassian.bitbucket.server.bitbucket-jira:branch-list-jira-issues\": [\n {\n \"key\": \"TCHJT-1\",\n \"url\": \"http://tjira01:8080/browse/TCHJT-1\"\n }\n ],\n \"com.atlassian.bitbucket.server.bitbucket-branch:ahead-behind-metadata-provider\": {\n \"ahead\": 0,\n \"behind\": 3\n },\n \"com.atlassian.bitbucket.server.bitbucket-branch:latest-commit-metadata\": {\n \"id\": \"3c00a3654835a0c4ecc9d01252f0a7326e6e3a37\",\n \"displayId\": \"3c00a365483\",\n \"author\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"authorTimestamp\": 1553580950000,\n \"committer\": {\n \"name\": \"Talha Can HAVADAR\",\n \"emailAddress\": \"[email protected]\",\n \"avatarUrl\": \"http://www.gravatar.com/avatar/8b418d821af4248059cb5187edfb5ead.jpg?s=64&d=mm\"\n },\n \"committerTimestamp\": 1553580950000,\n \"message\": \"readme.md edited online with Bitbucket\",\n \"parents\": [\n {\n \"id\": \"bd376834da94f253842cec85e9e8387b60aff86d\",\n \"displayId\": \"bd376834da9\"\n }\n ]\n },\n \"com.atlassian.bitbucket.server.bitbucket-ref-metadata:outgoing-pull-request-metadata\": {\n \"pullRequest\": {\n \"id\": 3,\n \"version\": 2,\n \"title\": \"readme.md edited online with Bitbucket\",\n \"state\": \"MERGED\",\n \"open\": false,\n \"closed\": true,\n \"createdDate\": 1563791107753,\n \"updatedDate\": 1563791183863,\n \"closedDate\": 1563791183863,\n \"fromRef\": {\n \"id\": \"refs/heads/feature/TCHJT-1-test-story-1\",\n \"displayId\": \"feature/TCHJT-1-test-story-1\",\n \"latestCommit\": \"3c00a3654835a0c4ecc9d01252f0a7326e6e3a37\",\n \"repository\": {\n \"slug\": \"test_webhooks\",\n \"id\": 275,\n \"name\": \"test_webhooks\",\n \"scmId\": \"git\",\n \"state\": \"AVAILABLE\",\n \"statusMessage\": \"Available\",\n \"forkable\": true,\n \"project\": {\n \"key\": \"TCHBBT\",\n \"id\": 306,\n \"name\": \"TCH_BB_TEST\",\n \"description\": \"Talha Can HAVADAR - Bitbucket Test Project\",\n \"public\": false,\n \"type\": \"NORMAL\"\n },\n \"public\": false\n }\n },\n \"toRef\": {\n \"id\": \"refs/heads/master\",\n \"displayId\": \"master\",\n \"latestCommit\": \"206425bc52376d2cc9868e5ecef5588b110ce720\",\n \"repository\": {\n \"slug\": \"test_webhooks\",\n \"id\": 275,\n \"name\": \"test_webhooks\",\n \"scmId\": \"git\",\n \"state\": \"AVAILABLE\",\n \"statusMessage\": \"Available\",\n \"forkable\": true,\n \"project\": {\n \"key\": \"TCHBBT\",\n \"id\": 306,\n \"name\": \"TCH_BB_TEST\",\n \"description\": \"Talha Can HAVADAR - Bitbucket Test Project\",\n \"public\": false,\n \"type\": \"NORMAL\"\n },\n \"public\": false\n }\n },\n \"locked\": false,\n \"author\": {\n \"user\": {\n \"name\": \"tchavadar\",\n \"emailAddress\": \"[email protected]\",\n \"id\": 3957,\n \"displayName\": \"Talha Can HAVADAR\",\n \"active\": true,\n \"slug\": \"tchavadar\",\n \"type\": \"NORMAL\"\n },\n \"role\": \"AUTHOR\",\n \"approved\": false,\n \"status\": \"UNAPPROVED\"\n },\n \"reviewers\": [],\n \"participants\": [],\n \"properties\": {\n \"resolvedTaskCount\": 0,\n \"openTaskCount\": 0\n }\n }\n }\n }\n }\n ],\n \"start\": {{queryParam 'start' 0}}\n}", | |
"latency": 0, | |
"statusCode": "200", | |
"headers": [ | |
{ | |
"key": "", | |
"value": "" | |
} | |
], | |
"filePath": "", | |
"sendFileAsBody": false | |
} | |
], | |
"proxyMode": false, | |
"proxyHost": "", | |
"https": false, | |
"cors": true, | |
"headers": [ | |
{ | |
"key": "Content-Type", | |
"value": "application/json" | |
} | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "iterators", | |
"version": "1.0.0", | |
"description": "Iterators Demo", | |
"main": "demo.js", | |
"dependencies": { | |
"axios": "^0.19.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"start": "node --inspect demo.js" | |
}, | |
"author": "Talha Can Havadar <[email protected]>", | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment