-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo-rename.js
48 lines (44 loc) · 1.21 KB
/
repo-rename.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
const shell = require('shelljs');
// 1st part: get id of repo
function getRepoId(owner, name) {
const queryRepoId = (owner, name) => `
query {
repository(owner: "${owner}", name: "${name}") {
id
}
}
`;
let r = shell.exec(`gh api graphql -f query='${queryRepoId(owner, name)}' --jq '.data.repository.id'`,
{silent: true});
if (r.code !== 0) {
console.error(r.stderr);
process.exit(r.code);
}
const Id = r.stdout.replace(/\s+$/g,'');
return Id;
}
// 2nd part: rename repo
function renameRepo(id, newName) {
const queryRenameRepo = (id, newName) => `
mutation {
updateRepository(input: {name: "${newName}", repositoryId: "${id}"}) {
repository {
name
}
}
}
`;
// stdout: '{"data":{"updateRepository":{"repository":{"name":"prueba"}}}}'
r = shell.exec(
`gh api graphql -f query='${queryRenameRepo(id, newName)}' --jq '.data.updateRepository.repository.name'`,
{silent:true})
if (r.code !== 0) {
console.error(r.stderr);
process.exit(r.code);
}
return r.stdout.replace(/\s+$/,'');
}
module.exports = {
getRepoId,
renameRepo
}