close_unresponsive.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. function labeledEvent(data) {
  2. return data.event === "labeled" && data.label.name === "needs:response";
  3. }
  4. const numberOfDaysLimit = 30;
  5. const close_message = `This has been closed since a request for information has \
  6. not been answered for ${numberOfDaysLimit} days. It can be reopened when the \
  7. requested information is provided.`;
  8. module.exports = async ({ github, context }) => {
  9. const owner = context.repo.owner;
  10. const repo = context.repo.repo;
  11. const issues = await github.rest.issues.listForRepo({
  12. owner: owner,
  13. repo: repo,
  14. labels: "needs:response",
  15. });
  16. const numbers = issues.data.map((e) => e.number);
  17. for (const number of numbers) {
  18. const events = await github.paginate(
  19. github.rest.issues.listEventsForTimeline,
  20. {
  21. owner: owner,
  22. repo: repo,
  23. issue_number: number,
  24. },
  25. (response) => response.data.filter(labeledEvent),
  26. );
  27. const latest_response_label = events[events.length - 1];
  28. const created_at = new Date(latest_response_label.created_at);
  29. const now = new Date();
  30. const diff = now - created_at;
  31. const diffDays = diff / (1000 * 60 * 60 * 24);
  32. if (diffDays > numberOfDaysLimit) {
  33. github.rest.issues.update({
  34. owner: owner,
  35. repo: repo,
  36. issue_number: number,
  37. state_reason: "not_planned",
  38. state: "closed",
  39. });
  40. github.rest.issues.createComment({
  41. owner: owner,
  42. repo: repo,
  43. issue_number: number,
  44. body: close_message,
  45. });
  46. }
  47. }
  48. };