forked from withfig/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.ts
168 lines (155 loc) · 3.92 KB
/
dangerfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { danger, markdown, schedule } from "danger";
import * as fs from "fs";
import {
createSourceFile,
forEachChild,
isFunctionExpression,
isPropertyAssignment,
isStringLiteral,
Node,
PropertyAssignment,
ScriptTarget,
SourceFile,
TransformerFactory,
visitEachChild,
visitNode,
} from "typescript";
export const specTransformer: TransformerFactory<SourceFile> = (context) => {
return (sourceFile) => {
const visitor = (node: Node) => {
if (isPropertyAssignment(node)) {
const typedNode = node as PropertyAssignment;
if (typedNode.name.getText() === "script") {
console.log(typedNode.initializer.getText());
}
}
return visitEachChild(node, visitor, context);
};
return visitNode(sourceFile, visitor);
};
};
const getFileContent = (fileContent: Node) => {
const scripts: string[] = [];
const functions: [string, string][] = [];
const pairs: [string, [string, string]][] = [];
let isLastScript = false;
let lastScript: string;
const visitor = (node: Node) => {
// PropertyAssignment === Key-Value pair in object
if (isPropertyAssignment(node)) {
const propertyKey: string = (node.name as any).escapedText;
// Find all scripts
if (propertyKey === "script" && isStringLiteral(node.initializer)) {
scripts.push(node.initializer.text);
lastScript = node.initializer.text;
isLastScript = true;
}
// Find all functions
if (isFunctionExpression(node.initializer)) {
if (isLastScript) {
scripts.pop();
pairs.push([
lastScript,
[
propertyKey,
fileContent
.getFullText()
.slice(node.initializer.pos, node.initializer.end),
],
]);
} else {
functions.push([
propertyKey,
fileContent
.getFullText()
.slice(node.initializer.pos, node.initializer.end),
]);
}
}
}
forEachChild(node, visitor);
};
visitor(fileContent);
return {
scripts,
functions,
pairs,
};
};
schedule(async () => {
const { owner, repo, number } = danger.github.thisPR;
const { data: comments } = await danger.github.api.issues.listComments({
issue_number: number,
repo,
owner,
});
const reviewCommentRef = comments.find((comment) =>
comment.body.includes("id: review-bot")
);
// Get all changed and added files
const updatedFiles = danger.git.modified_files
.concat(danger.git.created_files)
.filter((file) => file.includes("dev/"));
let message = "<!-- id: review-bot --> \n";
let comment = "";
if (updatedFiles.length > 0) {
updatedFiles.forEach((fileName) => {
const content = fs.readFileSync(fileName, { encoding: "utf-8" });
const sourceFile = createSourceFile("temp", content, ScriptTarget.Latest);
const fileContent = getFileContent(sourceFile);
message += `## ${fileName}:
### Info:
${fileContent.pairs
.map(
([scriptName, [key, value]]) => `**Script:**
\`${scriptName}\`
**${key}(function):**
\`\`\`typescript
${value}
\`\`\`
`
)
.join("\n")}
${
fileContent.scripts.length > 0
? `### Single Scripts:
${fileContent.scripts.map((s) => `- \`${s}\``).join("\n")}`
: ""
}
${
fileContent.functions.length > 0
? `### Single Functions:
${fileContent.functions
.map(
([key, value]) => `**${key}:**
\`\`\`typescript
${value}
\`\`\`
`
)
.join("\n")}`
: ""
}
`;
});
comment = `# Overview
${message}`;
} else {
comment = `# No files changed ☑️ ${message}`;
}
if (reviewCommentRef != null) {
await danger.github.api.issues.updateComment({
body: comment,
comment_id: reviewCommentRef.id,
owner,
repo,
});
} else {
await danger.github.api.issues.createComment({
body: comment,
issue_number: number,
owner,
repo,
});
}
});