-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-pr
executable file
·124 lines (101 loc) · 3.56 KB
/
create-pr
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
#!/usr/bin/env node
'use strict'
const resolve = require('path').resolve
const { execSync } = require('child_process');
const meow = require('meow')
const { Octokit, App } = require('octokit');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const Green = '\u001b[0;32m'
const Yellow = '\u001b[1;33m'
const Red = '\u001b[0;31m'
const NC = '\u001b[0m'
const cli = meow(`
${Yellow}Description${NC}
Create a pull request on the github repository on the requested branch.
Default branch: main
${Yellow}Usage${NC}
./create-pull-request
${Yellow}Options${NC}
${Green}-b, --base${NC} The base branch to merge into for the pull request. ${Yellow}[Default: main]
${Green}-h, --head${NC} The branch to compare against the base branch. ${Yellow}[Default: The current branch]
`, {
booleanDefault: undefined,
flags: {
base: {
alias: 'b',
type: 'string',
default: 'main'
},
head: {
alias: 'h',
type: 'string'
}
}
})
if (!process.env.GITHUB_TOKEN) {
console.error(`${Red}ERROR:${NC} Please provide a personal access token for the ${Yellow}GITHUB_TOKEN${NC} environment variable`)
return
}
const flags = cli.flags;
const base = cli.flags.base;
const head = cli.flags.head || getCurrentBranch();
const changelog = getChangelog(base)
createPr(base, head, changelog)
async function createPr(base, head, changelog) {
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
const {
data: { login },
} = await octokit.rest.users.getAuthenticated();
console.log(`${Green}Logged in as :${NC} %s`, login);
if (!login) {
console.error(`${Red}ERROR:${NC} Could not log in with the personal access token provided`)
return
}
const pullRequestMessage = `Merging ${head} into ${base} \n\n${changelog}`
const question = `${Yellow}Creating a pull request with the current message :${NC} \n\n${pullRequestMessage} \n\nProceed ? ${Yellow}[Y/n]`
rl.question(question, async (answer) => {
if (answer.toLocaleLowerCase() === 'n') {
console.error(`${Red}Aborting pull request...`)
process.exit(0);
return
}
rl.close();
console.log(`${Green}Creating pull request...`)
const response = await octokit.rest.pulls.create({
owner: "charcoalphp",
repo: "charcoal",
title: `Pulling ${head} into ${base}`,
base: base,
head: head,
body: pullRequestMessage
});
if (response.status === 201) {
const {
number,
html_url
} = response.data
console.log(`Pull request ${Green}#${number}${NC} created. Url: ${html_url} `)
} else {
console.error('Failed to create pull request')
}
process.exit(0);
})
}
function getChangelog(base, compare) {
const stdout = execSync(__dirname + `/build/script/create-release-notes -g --from ${base}`)
return `${stdout}`
}
function getCurrentBranch() {
const stdout = execSync('git branch --show-current')
// if (stderr !== undefined) {
// console.log(`stderr: ${stderr}`);
// return 'no branch';
// }
return `${stdout}`.replace(/[\r\n]/gm, '')
}