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) => api.issues({
  9. filter: {
  10. team: {
  11. key: {
  12. in: ["MAS", "IOS", "AND"],
  13. },
  14. },
  15. labels: {
  16. name: {
  17. eqIgnoreCase: "Public roadmap",
  18. },
  19. },
  20. state: {
  21. type: {
  22. in: [
  23. "backlog",
  24. "unstarted",
  25. "started",
  26. "completed",
  27. ],
  28. },
  29. },
  30. },
  31. })
  32. const processIssues = async (stateMap, issues) => {
  33. for (const issue of issues.nodes) {
  34. const state = await issue.state
  35. const list = (stateMap[state.type] || { items: [] }).items
  36. if (list.find(item => item.id === issue.identifier)) {
  37. continue
  38. }
  39. const parent = await issue.parent
  40. list.push({
  41. id: issue.identifier,
  42. title: issue.title,
  43. priority: issue.priority,
  44. completedAt: issue.completedAt,
  45. parent: parent ? {
  46. id: parent.identifier,
  47. title: parent.title,
  48. } : null,
  49. })
  50. stateMap[state.type] = {
  51. type: state.type,
  52. items: list,
  53. }
  54. }
  55. }
  56. const stateMap = {}
  57. const roadmap = []
  58. let issues = await fetchIssues()
  59. await processIssues(stateMap, issues)
  60. while (issues.pageInfo.hasNextPage) {
  61. issues = await issues.fetchNext()
  62. await processIssues(stateMap, issues)
  63. }
  64. Object.keys(stateMap).forEach(state => {
  65. if (state !== "completed") {
  66. stateMap[state].items.sort((a, b) => a.priority - b.priority)
  67. } else {
  68. stateMap[state].items.sort((a, b) => (new Date(b.completedAt)) - (new Date(a.completedAt)))
  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. )