linear.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import * as dotenv from "dotenv"
  2. import { LinearClient } from "@linear/sdk"
  3. import fs from "fs"
  4. dotenv.config({ path: ".env.local" })
  5. const api = new LinearClient({
  6. apiKey: process.env.LINEAR_API_KEY,
  7. })
  8. const fetchIssues = async (after) =>
  9. api.issues({
  10. filter: {
  11. team: {
  12. key: {
  13. in: ["MAS", "IOS", "AND"],
  14. },
  15. },
  16. labels: {
  17. name: {
  18. eqIgnoreCase: "Public roadmap",
  19. },
  20. },
  21. state: {
  22. type: {
  23. in: ["backlog", "unstarted", "started", "completed"],
  24. },
  25. },
  26. },
  27. })
  28. const processIssues = async (stateMap, issues) => {
  29. for (const issue of issues.nodes) {
  30. const state = await issue.state
  31. const list = (stateMap[state.type] || { items: [] }).items
  32. if (list.find((item) => item.id === issue.identifier)) {
  33. continue
  34. }
  35. const parent = await issue.parent
  36. list.push({
  37. id: issue.identifier,
  38. title: issue.title,
  39. priority: issue.priority,
  40. completedAt: issue.completedAt,
  41. parent: parent
  42. ? {
  43. id: parent.identifier,
  44. title: parent.title,
  45. }
  46. : null,
  47. })
  48. stateMap[state.type] = {
  49. type: state.type,
  50. items: list,
  51. }
  52. }
  53. }
  54. const stateMap = {}
  55. const roadmap = []
  56. let issues = await fetchIssues()
  57. await processIssues(stateMap, issues)
  58. while (issues.pageInfo.hasNextPage) {
  59. issues = await issues.fetchNext()
  60. await processIssues(stateMap, issues)
  61. }
  62. Object.keys(stateMap).forEach((state) => {
  63. if (state !== "completed") {
  64. stateMap[state].items.sort((a, b) => a.priority - b.priority)
  65. } else {
  66. stateMap[state].items.sort(
  67. (a, b) => new Date(b.completedAt) - new Date(a.completedAt)
  68. )
  69. }
  70. roadmap.push(stateMap[state])
  71. })
  72. const stateTypeToValue = (type) => {
  73. switch (type) {
  74. case "backlog":
  75. return 0
  76. case "unstarted":
  77. return 1
  78. case "started":
  79. return 2
  80. case "completed":
  81. return -1
  82. }
  83. }
  84. roadmap.sort((a, b) => stateTypeToValue(b.type) - stateTypeToValue(a.type))
  85. fs.writeFile(
  86. "./data/linear.json",
  87. JSON.stringify(roadmap, null, " "),
  88. (err) => {
  89. if (err) {
  90. console.error(err)
  91. return
  92. }
  93. console.log("File updated")
  94. }
  95. )