-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprs.ts
61 lines (49 loc) · 2.08 KB
/
prs.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
import { getBranches, loadConfig, getWorkdir } from "./helpers"
import * as shelljs from 'shelljs';
import commandLineArgs, { OptionDefinition } from "command-line-args";
import * as path from 'path';
import { PR, getAll } from "./helpers/bitbucket";
async function prs () {
// config
let cfg = loadConfig()
const workdir = getWorkdir()
const repoSlug:string = path.basename(workdir)
const optionDefinitions: OptionDefinition[] = [
{ name: 'chain', alias: 'c', type: String, defaultValue: "main.prchain" },
{ name: 'branches', alias: 'b', type: String, multiple: true },
]
const options = commandLineArgs(optionDefinitions);
const chainFile = options["chain"]
const branches = getBranches('Enter the branches to fetch PR urls for:', options["branches"], workdir, options["chain"]).filter(b => b != 'master')
// resolve the links for all PRs
let links:string[] = []
for(let i = 0; i < branches.length; i++) {
let branch = branches[i]
let commit = shelljs.exec(`git rev-parse --short ${branch}`, {cwd:workdir, silent: true}).toString()
let msg = shelljs.exec(`git log --format=%B -n 1 ${commit}`, {cwd:workdir, silent: true}).toString()
msg = msg.split("\n")[0]
let prs:PR[] = []
try {
prs = await getAll<PR>(cfg, `https://api.bitbucket.org/2.0/repositories/${cfg.workspace}/${repoSlug}/commit/${commit}/pullrequests`)
if(prs.length == 0) {
throw 'api response values was empty'
}
} catch(e) {
console.log(`Error fetching PR for branch ${branch} - ${e}`)
continue;
}
let url = prs[0].links.html.href;
let link = `${i+1}. ${msg}\n${url}`
console.log(`${msg} - ${url}`)
links.push(link)
}
// copy to the clipboard
let content = links.join("\n")
urlpaste(content)
console.log("copied to clipboard!")
}
function urlpaste(data:string) {
// use special custom hyperlink utility
shelljs.exec(`echo \"${data}\" | urlpaste`, {silent: true})
}
prs()