1 #!/usr/bin/env node
2
3 import { LinearClient } from "@linear/sdk"
4 import { Octokit } from "@octokit/core"
5 import fs from "fs"
6 import path from "path"
7 import { getDirname } from "utils"
8
9 const octokit = new Octokit({
10 auth: process.env.GH_TOKEN,
11 })
12
13 const linearClient = new LinearClient({
14 apiKey: process.env.LINEAR_API_KEY,
15 })
16
17 const __dirname = getDirname(import.meta.url)
18 const monorepoRoot = path.join(__dirname, "..", "..", "..", "..", "..")
19
20 let freshnessCheckLabelId = ""
21 let documentationTeamId = ""
22
23 async function scanDirectory(startPath: string) {
24 const files = fs.readdirSync(path.join(startPath), {
25 withFileTypes: true,
26 })
27
28 for (const file of files) {
29 const filePath = path.join(startPath, file.name)
30 if (file.isDirectory()) {
31 //if it's references directory, skip
32 if (file.name !== "references") {
33 await scanDirectory(filePath)
34 }
35 continue
36 }
37
38 //check that the file is a markdown file
39 if (file.name.indexOf(".md") === -1 && file.name.indexOf(".mdx") === -1) {
40 continue
41 }
42
43 //if it is a file, check its commits in GitHub
44 // retrieve relative path to monorepo root
45 const relativeFilePath = path.relative(monorepoRoot, filePath)
46 const commitResponse = await octokit.request(
47 "GET /repos/{owner}/{repo}/commits",
48 {
49 owner: "medusajs",
50 repo: "medusa",
51 path: relativeFilePath,
52 per_page: 1,
53 }
54 )
55
56 if (
57 !commitResponse.data.length ||
58 !commitResponse.data[0].commit.committer?.date
59 ) {
60 continue
61 }
62
63 const today = new Date()
64 const lastEditedDate = new Date(
65 commitResponse.data[0].commit.committer.date
66 )
67 const monthsSinceEdited = getMonthDifference(lastEditedDate, today)
68 const monthsThreshold = 6
69
70 if (monthsSinceEdited > monthsThreshold) {
71 //file was edited more than 6 months ago.
72 //check if there's an issue created for this file since the commit date
73 const existingIssue = await linearClient.issues({
74 filter: {
75 createdAt: {
76 gte: subtractMonths(monthsSinceEdited - monthsThreshold, today),
77 },
78 title: {
79 containsIgnoreCase: relativeFilePath,
80 },
81 labels: {
82 some: {
83 id: {
84 eq: freshnessCheckLabelId,
85 },
86 },
87 },
88 },
89 first: 1,
90 })
91
92 if (existingIssue.nodes.length) {
93 //an issue has been created for the past 6 months. Don't create an issue for it.
94 continue
95 }
96
97 console.log(`Creating an issue for ${relativeFilePath}...`)
98
99 //there are no issues in the past 6 months. Create an issue
100 await linearClient.createIssue({
101 teamId: documentationTeamId,
102 title: `Freshness check for ${relativeFilePath}`,
103 labelIds: [freshnessCheckLabelId],
104 description: `File \`${relativeFilePath}\` was last edited on ${lastEditedDate.toDateString()}. Please review and update the content if necessary.`,
105 })
106 }
107 }
108 }
109
110 async function main() {
111 //fetch documentation team ID from linear
112 const documentationTeam = await linearClient.teams({
113 filter: {
114 name: {
115 eqIgnoreCase: "DX",
116 },
117 },
118 first: 1,
119 })
120
121 if (!documentationTeam.nodes.length) {
122 console.log("Please add DX team in Linear first then try again")
123 process.exit(1)
124 }
125
126 documentationTeamId = documentationTeam.nodes[0].id
127
128 //fetch freshness check label ID from linear
129 const freshnessCheckLabel = await linearClient.issueLabels({
130 filter: {
131 name: {
132 eqIgnoreCase: "type: freshness-check",
133 },
134 team: {
135 id: {
136 eq: documentationTeamId,
137 },
138 },
139 },
140 })
141
142 if (!freshnessCheckLabel.nodes.length) {
143 console.log(
144 "Please add freshness check label in Linear under the DX team first then try again"
145 )
146 process.exit(1)
147 }
148
149 freshnessCheckLabelId = freshnessCheckLabel.nodes[0].id
150
151 await scanDirectory(path.join(monorepoRoot, "www", "apps", "book", "app"))
152 await scanDirectory(
153 path.join(monorepoRoot, "www", "apps", "resources", "app")
154 )
155 await scanDirectory(
156 path.join(monorepoRoot, "www", "apps", "user-guide", "app")
157 )
158 await scanDirectory(path.join(monorepoRoot, "www", "apps", "ui", "app"))
159 await scanDirectory(path.join(monorepoRoot, "www", "apps", "cloud", "app"))
160 }
161
162 function getMonthDifference(startDate: Date, endDate: Date) {
163 return (
164 endDate.getMonth() -
165 startDate.getMonth() +
166 12 * (endDate.getFullYear() - startDate.getFullYear())
167 )
168 }
169
170 function subtractMonths(numOfMonths: number, date = new Date()) {
171 date.setMonth(date.getMonth() - numOfMonths)
172
173 return date
174 }
175
176 void main()