|
| 1 | +function calcResponseTimeForIssueCreatedAt(createdAt) { |
| 2 | + const issueOpenedDate = new Date(createdAt); |
| 3 | + const issueTriagedDate = new Date(); |
| 4 | + const businessDaysResponseTime = calcBusinessDaysBetweenDates(issueOpenedDate, issueTriagedDate); |
| 5 | + return businessDaysResponseTime; |
| 6 | +} |
| 7 | + |
| 8 | +function calcBusinessDaysBetweenDates(openedDate, triagedDate) { |
| 9 | + let differenceInWeeks, responseTime; |
| 10 | + if (triagedDate < openedDate) |
| 11 | + return -1; // error code if dates transposed |
| 12 | + let openedDay = openedDate.getDay(); // day of week |
| 13 | + let triagedDay = triagedDate.getDay(); |
| 14 | + openedDay = (openedDay == 0) ? 7 : openedDay; // change Sunday from 0 to 7 |
| 15 | + triagedDay = (triagedDay == 0) ? 7 : triagedDay; |
| 16 | + openedDay = (openedDay > 5) ? 5 : openedDay; // only count weekdays |
| 17 | + triagedDay = (triagedDay > 5) ? 5 : triagedDay; |
| 18 | + // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000) |
| 19 | + differenceInWeeks = Math.floor((triagedDate.getTime() - openedDate.getTime()) / 604800000); |
| 20 | + if (openedDay < triagedDay) { //Equal to makes it reduce 5 days |
| 21 | + responseTime = (differenceInWeeks * 5) + (triagedDay - openedDay); |
| 22 | + } |
| 23 | + else if (openedDay == triagedDay) { |
| 24 | + responseTime = differenceInWeeks * 5; |
| 25 | + } |
| 26 | + else { |
| 27 | + responseTime = ((differenceInWeeks + 1) * 5) - (openedDay - triagedDay); |
| 28 | + } |
| 29 | + return (responseTime); |
| 30 | +} |
| 31 | + |
| 32 | +module.exports = async(context, osmetadata) => { |
| 33 | + const foundResponseTime = await osmetadata(context).get('response_time_in_business_days'); |
| 34 | + if (foundResponseTime) { |
| 35 | + const foundString = "already found response time in business days: " + foundResponseTime |
| 36 | + console.log(foundString); |
| 37 | + return foundString; |
| 38 | + } |
| 39 | + if (context.payload.comment && context.payload.comment.author_association != "MEMBER" && context.payload.comment.author_association != "OWNER" && context.payload.comment.author_association != "CONTRIBUTOR") { |
| 40 | + return; |
| 41 | + } |
| 42 | + const businessDaysResponseTime = calcResponseTimeForIssueCreatedAt(context.payload.issue.created_at); |
| 43 | + console.log("response time in business days: " + businessDaysResponseTime); |
| 44 | + const result = osmetadata(context, context.payload.issue).set('response_time_in_business_days', businessDaysResponseTime) |
| 45 | + console.log("osmetadata update result: " + result); |
| 46 | + return "set response time in business days: " + businessDaysResponseTime; |
| 47 | +} |
0 commit comments