-
Notifications
You must be signed in to change notification settings - Fork 30
/
createPullRequest.js
120 lines (111 loc) · 3.01 KB
/
createPullRequest.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
import { expandPackageList, getRepoInfo, getReleaseTag } from 'shipjs-lib';
import open from 'open';
import { Octokit } from '@octokit/rest';
import runStep from '../runStep';
import { getPublishCommand, getPackageDirName } from '../../helper';
import { print, run, detectYarn } from '../../util';
export default async ({
baseBranch,
stagingBranch,
currentVersion,
currentTag,
nextVersion,
releaseType,
noBrowse,
config,
dir,
dryRun,
}) =>
await runStep({ title: 'Creating a pull request.' }, async () => {
const {
formatPullRequestTitle,
formatPullRequestMessage,
publishCommand,
draftPullRequest,
pullRequestReviewers,
pullRequestTeamReviewers,
remote,
monorepo,
} = config;
const { url: repoURL, owner, name: repo } = getRepoInfo(remote, dir);
const diffURL = `${repoURL}/compare/${currentTag}...${stagingBranch}`;
const publishCommands = getPublishCommands({
isYarn: detectYarn(dir),
tag: getReleaseTag(nextVersion),
monorepo,
publishCommand,
dir,
});
const title = formatPullRequestTitle({ version: nextVersion, releaseType });
const message = formatPullRequestMessage({
repo,
repoURL,
baseBranch,
stagingBranch,
destinationBranch: baseBranch,
releaseType,
diffURL,
currentVersion,
nextVersion,
publishCommands,
title,
});
run({ command: `git remote prune ${remote}`, dir, dryRun });
if (dryRun) {
print('Creating a pull request with the following:');
print(` - Title: ${title}`);
print(` - Message: ${message}`);
return {};
}
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`,
});
const {
data: { number, html_url: url },
} = await octokit.pulls.create({
owner,
repo,
title,
body: message,
head: stagingBranch,
base: baseBranch,
draft: draftPullRequest,
});
if (
(pullRequestReviewers || []).length > 0 ||
(pullRequestTeamReviewers || []).length > 0
) {
await octokit.pulls.createReviewRequest({
owner,
repo,
pull_number: number, // eslint-disable-line camelcase
reviewers: pullRequestReviewers,
team_reviewers: pullRequestTeamReviewers, // eslint-disable-line camelcase
});
}
if (!noBrowse) {
await open(url);
}
return { pullRequestUrl: url };
});
function getPublishCommands({ isYarn, tag, monorepo, publishCommand, dir }) {
if (monorepo) {
const { packagesToPublish } = monorepo;
const packageList = expandPackageList(packagesToPublish, dir);
return packageList.map((packageDir) => {
const dirName = getPackageDirName(packageDir, dir);
const command = getPublishCommand({
isYarn,
publishCommand,
tag,
dir: packageDir,
});
return {
dirName,
command,
};
});
} else {
return getPublishCommand({ isYarn, publishCommand, tag, dir });
}
}