/
ChangelogChecker.js
136 lines (134 loc) · 5.64 KB
/
ChangelogChecker.js
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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const spawn_async_1 = __importDefault(require("@expo/spawn-async"));
const fs = __importStar(require("fs"));
const lodash_1 = require("lodash");
const path = __importStar(require("path"));
const PullRequestManager_1 = require("./PullRequestManager");
const Utils_1 = require("./Utils");
// Setup
const pr = danger.github.pr;
const prAuthor = pr.user.login;
const pullRequestManager = PullRequestManager_1.createPullRequestManager(danger.github.api, pr);
async function getFileDiffAsync(path) {
const { stdout } = await spawn_async_1.default('git', ['diff', '--', path], {
cwd: Utils_1.getExpoRepositoryRootDir(),
});
return stdout;
}
/**
* @returns `false` if `CHANGELOG.md` doesn't exist in provided package.
*/
function isChangelogModified(packageName, modifiedFiles) {
const changelogPath = Utils_1.getPackageChangelogRelativePath(packageName);
return (modifiedFiles.includes(changelogPath) ||
!fs.existsSync(path.join(Utils_1.getExpoRepositoryRootDir(), changelogPath)));
}
function getSuggestedChangelogEntries(packageNames) {
const { [PullRequestManager_1.DEFAULT_CHANGELOG_ENTRY_KEY]: defaultEntry, ...suggestedEntries } = pullRequestManager.parseChangelogSuggestionFromDescription();
return packageNames.map(packageName => {
var _a, _b, _c, _d;
const message = (_b = (_a = suggestedEntries[packageName]) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : defaultEntry.message;
const type = (_d = (_c = suggestedEntries[packageName]) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : defaultEntry.type;
return {
packageName,
message,
type,
};
});
}
async function runAddChangelogCommandAsync(suggestedEntries) {
for (const entry of suggestedEntries) {
await spawn_async_1.default('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(Utils_1.getExpoRepositoryRootDir(), Utils_1.getPackageChangelogRelativePath(entry.packageName));
return {
...entry,
content: await Utils_1.getFileContentAsync(changelogPath),
diff: await getFileDiffAsync(changelogPath),
};
}));
}
function generateReport(missingEntries, url) {
const message = missingEntries
.map(entry => `- <code>${danger.github.utils.fileLinks([Utils_1.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
*/
async function checkChangelog() {
const modifiedPackages = lodash_1.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);
}
exports.checkChangelog = checkChangelog;
function entryTypeToString(type) {
switch (type) {
case PullRequestManager_1.ChangelogEntryType.BUG_FIXES:
return 'bug-fix';
case PullRequestManager_1.ChangelogEntryType.NEW_FEATURES:
return 'new-feature';
case PullRequestManager_1.ChangelogEntryType.BREAKING_CHANGES:
return 'breaking-change';
}
throw new Error(`Unknown entry type ${type}.`);
}
//# sourceMappingURL=ChangelogChecker.js.map