Created
February 9, 2020 15:39
-
-
Save timucingelici/59feabcd3c5515a294529e135aa455f0 to your computer and use it in GitHub Desktop.
Postman Collection to fill TickSpot timesheets programmatically
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
{ | |
"info": { | |
"_postman_id": "4c2e044f-488c-4f18-968e-c070b36e1364", | |
"name": "TICKSPOT", | |
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" | |
}, | |
"item": [ | |
{ | |
"name": "Project ID", | |
"event": [ | |
{ | |
"listen": "test", | |
"script": { | |
"id": "6bbd5b5a-a839-4650-9eca-5a24f38ef409", | |
"exec": [ | |
"postman.setNextRequest(null)", | |
"", | |
"if(pm.response.code == 200) {", | |
" let projects = JSON.parse(responseBody)", | |
" ", | |
" if(projects) {", | |
" pm.environment.set(\"PROJECT_ID\", projects[0].id)", | |
" console.log(\"Project ID: \", pm.environment.get(\"PROJECT_ID\"))", | |
" ", | |
" postman.setNextRequest(\"Task ID\")", | |
" }", | |
"}" | |
], | |
"type": "text/javascript" | |
} | |
}, | |
{ | |
"listen": "prerequest", | |
"script": { | |
"id": "206a01d4-8b60-49c8-a6f8-fb720280a4cc", | |
"exec": [ | |
"" | |
], | |
"type": "text/javascript" | |
} | |
} | |
], | |
"request": { | |
"method": "GET", | |
"header": [ | |
{ | |
"key": "User-Agent", | |
"value": "{{USER_AGENT}}", | |
"type": "text" | |
} | |
], | |
"url": { | |
"raw": "{{API_URL}}/projects.json", | |
"host": [ | |
"{{API_URL}}" | |
], | |
"path": [ | |
"projects.json" | |
] | |
}, | |
"description": "Get project ID" | |
}, | |
"response": [] | |
}, | |
{ | |
"name": "Task ID", | |
"event": [ | |
{ | |
"listen": "test", | |
"script": { | |
"id": "73be930d-c945-4e65-bf64-bcf475df8fa4", | |
"exec": [ | |
"postman.setNextRequest(null)", | |
"", | |
"if(pm.response.code == 200) {", | |
" let tasks = JSON.parse(responseBody)", | |
" ", | |
" if(tasks) {", | |
" tasks.forEach((task) => { ", | |
" if(task.name == \"Development\") {", | |
" pm.environment.set(\"TASK_ID\", task.id)", | |
" ", | |
" console.log(\"Task ID:\", pm.environment.get(\"TASK_ID\"))", | |
" ", | |
" postman.setNextRequest(\"Entries\")", | |
" }", | |
" })", | |
" }", | |
"}" | |
], | |
"type": "text/javascript" | |
} | |
} | |
], | |
"request": { | |
"method": "GET", | |
"header": [ | |
{ | |
"key": "User-Agent", | |
"value": "{{USER_AGENT}}", | |
"type": "text" | |
} | |
], | |
"url": { | |
"raw": "{{API_URL}}/projects/{{PROJECT_ID}}/tasks.json", | |
"host": [ | |
"{{API_URL}}" | |
], | |
"path": [ | |
"projects", | |
"{{PROJECT_ID}}", | |
"tasks.json" | |
] | |
} | |
}, | |
"response": [] | |
}, | |
{ | |
"name": "Entries", | |
"event": [ | |
{ | |
"listen": "test", | |
"script": { | |
"id": "73be930d-c945-4e65-bf64-bcf475df8fa4", | |
"exec": [ | |
"postman.setNextRequest(null)", | |
"pm.environment.set(\"ENTRIES\", [])", | |
"", | |
"if(pm.response.code == 200) {", | |
" let entries = JSON.parse(responseBody)", | |
" ", | |
" if(entries && entries.length > 0) {", | |
" let newEntries = []", | |
" lastEntry = entries[0]", | |
" now = new Date()", | |
" last = new Date(lastEntry.date)", | |
" current = new Date(lastEntry.date)", | |
"", | |
" if (last.getTime() < now.getTime()){", | |
" ", | |
" console.log(last, current)", | |
" ", | |
" while (current.getTime() < now.getTime()){", | |
" current.setDate( current.getDate() + 1)", | |
" ", | |
" if (current.getDay() > 0 && current.getDay() < 6) {", | |
" let payload = {", | |
" date: [current.getFullYear(), current.getMonth() + 1, current.getDate()].join(\"-\"),", | |
" hours: 7.5,", | |
" notes: \"\",", | |
" task_id: pm.environment.get(\"TASK_ID\")", | |
" }", | |
" ", | |
" newEntries.push(payload)", | |
" console.log(JSON.stringify(payload))", | |
" }", | |
" }", | |
" }", | |
" ", | |
" pm.environment.set(\"ENTRIES\", JSON.stringify(newEntries))", | |
" ", | |
" postman.setNextRequest(\"Add Missing Days\")", | |
" } else {", | |
" console.log(\"No new entries...\")", | |
" }", | |
"} else {", | |
" console.error(\"Server returned error:\", pm.response)", | |
"}" | |
], | |
"type": "text/javascript" | |
} | |
}, | |
{ | |
"listen": "prerequest", | |
"script": { | |
"id": "d060309d-9861-4510-9ac6-6dfe98670de4", | |
"exec": [ | |
"let now = new Date()", | |
"let start = new Date()", | |
"start.setMonth( now.getMonth() - 1)", | |
"", | |
"pm.environment.set(\"TODAY\", [now.getFullYear(), (now.getMonth() + 1), now.getDate()].join(\"/\"))", | |
"pm.environment.set(\"START_DATE\", [start.getFullYear(), (start.getMonth() + 1), start.getDate()].join(\"/\"))", | |
"", | |
"console.log(pm.environment.get(\"START_DATE\"))", | |
"console.log(pm.environment.get(\"TODAY\"))" | |
], | |
"type": "text/javascript" | |
} | |
} | |
], | |
"request": { | |
"method": "GET", | |
"header": [ | |
{ | |
"key": "User-Agent", | |
"type": "text", | |
"value": "{{USER_AGENT}}" | |
} | |
], | |
"url": { | |
"raw": "{{API_URL}}/tasks/{{TASK_ID}}/entries.json?start_date={{START_DATE}}&end_date={{TODAY}}", | |
"host": [ | |
"{{API_URL}}" | |
], | |
"path": [ | |
"tasks", | |
"{{TASK_ID}}", | |
"entries.json" | |
], | |
"query": [ | |
{ | |
"key": "start_date", | |
"value": "{{START_DATE}}" | |
}, | |
{ | |
"key": "end_date", | |
"value": "{{TODAY}}" | |
} | |
] | |
} | |
}, | |
"response": [] | |
}, | |
{ | |
"name": "Add Missing Days", | |
"event": [ | |
{ | |
"listen": "prerequest", | |
"script": { | |
"id": "3f9661db-69e6-44c1-8f4f-3df1d64445fa", | |
"exec": [ | |
"let entries = JSON.parse(pm.environment.get(\"ENTRIES\"))", | |
"", | |
"if (entries && entries.length > 0) {", | |
" let entry = entries.shift()", | |
" ", | |
" console.log(entry)", | |
" ", | |
" // Just checking if the payload has the right properties", | |
" if(entry.hasOwnProperty(\"date\") && entry.hasOwnProperty(\"hours\") && entry.hasOwnProperty(\"task_id\")) {", | |
" // Set the payload", | |
" pm.environment.set(\"POST_ENTRY_REQUEST_BODY\", JSON.stringify(entry))", | |
" console.log(\"Entry to be submitted: \", JSON.stringify(entry)) ", | |
" } else {", | |
" throw new Error(\"Something's wrong with the entry...\")", | |
" }", | |
"} else {", | |
" postman.setNextRequest(null)", | |
" pm.environment.unset(\"POST_ENTRY_REQUEST_BODY\")", | |
"}" | |
], | |
"type": "text/javascript" | |
} | |
}, | |
{ | |
"listen": "test", | |
"script": { | |
"id": "25c60777-d18e-413c-aeed-20194e2bc4b1", | |
"exec": [ | |
"postman.setNextRequest(null)", | |
"", | |
"if(pm.response.code == 201) {", | |
" // Remove the submitted payload", | |
" pm.environment.unset(\"POST_ENTRY_REQUEST_BODY\")", | |
" ", | |
" // Update the entries array on success so the same won't be submitted again.", | |
" let entries = pm.environment.get(\"ENTRIES\");", | |
" entries = JSON.parse(entries)", | |
" entries.shift()", | |
" pm.environment.set(\"ENTRIES\", JSON.stringify(entries))", | |
" ", | |
" if(entries.length > 0){", | |
" postman.setNextRequest(\"Add Missing Days\")", | |
" }", | |
" ", | |
" // Call the same request again in case there are more entries to submit.", | |
"} else {", | |
" console.error(\"Server returned an unexpected HTTP code for the request:\", pm.request, pm.response)", | |
"}" | |
], | |
"type": "text/javascript" | |
} | |
} | |
], | |
"request": { | |
"method": "POST", | |
"header": [ | |
{ | |
"key": "User-Agent", | |
"type": "text", | |
"value": "{{USER_AGENT}}" | |
}, | |
{ | |
"key": "Content-Type", | |
"name": "Content-Type", | |
"value": "application/json", | |
"type": "text" | |
} | |
], | |
"body": { | |
"mode": "raw", | |
"raw": "{{POST_ENTRY_REQUEST_BODY}}", | |
"options": { | |
"raw": { | |
"language": "json" | |
} | |
} | |
}, | |
"url": { | |
"raw": "{{API_URL}}/entries.json", | |
"host": [ | |
"{{API_URL}}" | |
], | |
"path": [ | |
"entries.json" | |
] | |
} | |
}, | |
"response": [] | |
} | |
], | |
"auth": { | |
"type": "apikey", | |
"apikey": [ | |
{ | |
"key": "value", | |
"value": "Token {{API_TOKEN}}", | |
"type": "string" | |
}, | |
{ | |
"key": "key", | |
"value": "Authorization", | |
"type": "string" | |
} | |
] | |
}, | |
"event": [ | |
{ | |
"listen": "prerequest", | |
"script": { | |
"id": "28139143-6878-4a0c-94c7-481e7c188213", | |
"type": "text/javascript", | |
"exec": [ | |
"console.log(\"here...\")", | |
"// Check if the variables are defined.", | |
"", | |
"let keys = ['API_TOKEN', 'SUBSCRIPTION_ID', 'USER_AGENT', 'TASK_NAME']", | |
"", | |
"keys.forEach((key) => {", | |
" let value = pm.collectionVariables.get(key)", | |
" console.log(key, value, pm.collectionVariables.toObject())", | |
" ", | |
" if (typeof value == \"undefined\" || value.length === 0) {", | |
" throw new Error(key + \" needs to be defined in collection variables.!\")", | |
" }", | |
" ", | |
" if (key == 'USER_AGENT'){", | |
" pm.expect(value).to.include(value, \"@\", \"The key USER_AGENT must contain your registered email address\")", | |
" }", | |
"})", | |
"", | |
"pm.collectionVariables.set('API_URL', \"https://www.tickspot.com/\" + pm.collectionVariables.get(\"SUBSCRIPTION_ID\") + \"/api/v2\")" | |
] | |
} | |
}, | |
{ | |
"listen": "test", | |
"script": { | |
"id": "3386b66c-341e-44c7-a879-6f7d89e20dce", | |
"type": "text/javascript", | |
"exec": [ | |
"" | |
] | |
} | |
} | |
], | |
"variable": [ | |
{ | |
"id": "1c0fd921-46ed-4fe4-b692-b99c8c807f3e", | |
"key": "API_TOKEN", | |
"value": "", | |
"type": "string" | |
}, | |
{ | |
"id": "efca0652-bf37-4003-8f2e-8d909b0ea756", | |
"key": "SUBSCRIPTION_ID", | |
"value": "", | |
"type": "string" | |
}, | |
{ | |
"id": "6f162bbb-0084-4279-a309-d8665a2416b5", | |
"key": "USER_AGENT", | |
"value": "Tick CLI because I am too lazy to fill this over the app (put_your_registered_email_address_here)", | |
"type": "string" | |
}, | |
{ | |
"id": "1f7c93d5-8541-4276-8f9f-75a9ec6514e6", | |
"key": "TASK_NAME", | |
"value": "", | |
"type": "string" | |
}, | |
{ | |
"id": "389893c2-7112-4bb9-8784-6d819e3bb3de", | |
"key": "API_URL", | |
"value": "", | |
"type": "string" | |
} | |
], | |
"protocolProfileBehavior": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment