/
ChangelogChecker.ts
181 lines (158 loc) · 5.1 KB
/
ChangelogChecker.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
169
170
171
172
173
174
175
176
177
178
179
180
181
import spawnAsync from '@expo/spawn-async';
import * as fs from 'fs';
import { groupBy } from 'lodash';
import * as path from 'path';
import {
createPullRequestManager,
ChangelogEntry,
ChangelogEntryType,
DEFAULT_CHANGELOG_ENTRY_KEY,
} from './PullRequestManager';
import {
getExpoRepositoryRootDir,
getFileContentAsync,
getPackageChangelogRelativePath,
} from './Utils';
export type PackageChangelogEntry = ChangelogEntry & {
packageName: string;
};
export type FixedChangelogEntry = PackageChangelogEntry & { content: string; diff: string };
// Setup
const pr = danger.github.pr;
const prAuthor = pr.user.login;
const pullRequestManager = createPullRequestManager(danger.github.api, pr);
async function getFileDiffAsync(path): Promise<string> {
const { stdout } = await spawnAsync('git', ['diff', '--', path], {
cwd: getExpoRepositoryRootDir(),
});
return stdout;
}
/**
* @returns `false` if `CHANGELOG.md` doesn't exist in provided package.
*/
function isChangelogModified(packageName: string, modifiedFiles: string[]): boolean {
const changelogPath = getPackageChangelogRelativePath(packageName);
return (
modifiedFiles.includes(changelogPath) ||
!fs.existsSync(path.join(getExpoRepositoryRootDir(), changelogPath))
);
}
function getSuggestedChangelogEntries(packageNames: string[]): PackageChangelogEntry[] {
const {
[DEFAULT_CHANGELOG_ENTRY_KEY]: defaultEntry,
...suggestedEntries
} = pullRequestManager.parseChangelogSuggestionFromDescription();
return packageNames.map(packageName => {
const message = suggestedEntries[packageName]?.message ?? defaultEntry.message;
const type = suggestedEntries[packageName]?.type ?? defaultEntry.type;
return {
packageName,
message,
type,
};
});
}
async function runAddChangelogCommandAsync(
suggestedEntries: PackageChangelogEntry[]
): Promise<FixedChangelogEntry[]> {
for (const entry of suggestedEntries) {
await spawnAsync('et', [
`add-changelog`,
`--package`,
entry.packageName,
`--entry`,
entry.message,
`--author`,
prAuthor,
`--type`,
entryTypeToString(entry.type),
`--pull-request`,
`${pr.number}`,
]);
}
return Promise.all(
suggestedEntries.map(async entry => {
const changelogPath = path.join(
getExpoRepositoryRootDir(),
getPackageChangelogRelativePath(entry.packageName)
);
return {
...entry,
content: await getFileContentAsync(changelogPath),
diff: await getFileDiffAsync(changelogPath),
};
})
);
}
function generateReport(
missingEntries: { packageName: string; diff: string }[],
url?: string | null
) {
const message = missingEntries
.map(
entry =>
`- <code>${danger.github.utils.fileLinks(
[getPackageChangelogRelativePath(entry.packageName)],
false
)}</code>`
)
.join('\n');
const diff = '```diff\n' + missingEntries.map(entry => entry.diff).join('\n') + '```\n';
const pr = url ? `#### or merge this pull request: ${url}` : '';
fail(`
📋 **Missing Changelog**
------
🛠 Add missing entires to:
${message}`);
markdown(
`
### 🛠 Suggested fixes:
<details>
<summary>📋 Missing changelog</summary>
#### Apply suggested changes:
${diff}
${pr}
</details>`
);
}
/**
* This function checks if the changelog was modified, doing the following steps:
* - get packages which were modified but don't have changes in `CHANGELOG.md`
* - parse PR body to get suggested entries for those packages
* - run `et add-changelog` for each package to apply the suggestion
* - create a new PR
* - add a comment to inform about missing changelog
* - fail CI job
*/
export async function checkChangelog(): Promise<void> {
const modifiedPackages = groupBy(
danger.git.modified_files.filter(file => file.startsWith('packages')),
file => file.split(path.sep)[1]
);
const packagesWithoutChangelog = Object.entries(modifiedPackages)
.filter(([packageName, files]) => !isChangelogModified(packageName, files))
.map(([packageName]) => packageName);
if (packagesWithoutChangelog.length === 0) {
message(`✅ **Changelog**`);
return;
}
// gets suggested entries based on pull request
const suggestedEntries = getSuggestedChangelogEntries(packagesWithoutChangelog);
// applies suggested fixes using `et add-changelog` command
const fixedEntries = await runAddChangelogCommandAsync(suggestedEntries);
// creates/updates PR form result of `et` command - it will be merged to the current PR
const { html_url } = (await pullRequestManager.createOrUpdatePRAsync(fixedEntries)) || {};
// generates danger report. It will contain result of `et` command as a git diff and link to created PR
await generateReport(fixedEntries, html_url);
}
function entryTypeToString(type: ChangelogEntryType): string {
switch (type) {
case ChangelogEntryType.BUG_FIXES:
return 'bug-fix';
case ChangelogEntryType.NEW_FEATURES:
return 'new-feature';
case ChangelogEntryType.BREAKING_CHANGES:
return 'breaking-change';
}
throw new Error(`Unknown entry type ${type}.`);
}