-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·136 lines (118 loc) · 3.55 KB
/
index.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
#!/usr/bin/env node
let GitHubApi = require('github');
let _ = require('lodash');
if (!process.env.GITHUB_TOKEN) {
console.log('Please provide environment variable `GITHUB_TOKEN`');
process.exit(1);
}
let github = _getGithub();
let argv = require('yargs')
.usage('$0 <cmd> [args]')
.option('dry-run', {
alias: 'd'
})
.boolean('d')
.command('list-projects [--org]', 'list all projects for an organization', function (yargs) {
return yargs
.option('org', {
describe: 'organization name'
})
.demandOption(['org'])
}, getProjects)
.command('list-columns', 'list all columns from one project', function (yargs) {
return yargs
.option('project-id', {
alias: 'p',
describe: 'GitHub Project Id'
})
.demandOption(['project-id'])
}, listColumns)
.command('move-cards [from] [to]', 'move all cards from one column to another', function (yargs) {
return yargs
.option('project-id', {
alias: 'p',
describe: 'GitHub Project Id'
})
.option('from', {
alias: 'f',
describe: 'Column Id where all cards will be moved from'
})
.option('to', {
alias: 't',
describe: 'Column Id where all cards will be moved to'
})
.demandOption(['from', 'to', 'project-id'])
}, moveCards)
.demandCommand(1, 'You need at least one command before moving on')
.help()
.argv;
function getProjects(argv) {
github.projects.getOrgProjects({
org: argv.org
}).then(({data: projects}) => {
_.each(projects, p => {
console.log(`- ${p.name}
id: ${p.id}`);
});
});
}
function listColumns(argv) {
let projectId = argv.projectId;
github.projects.getProjectColumns({
project_id: projectId
}).then(function({data: columns}) {
_.each(columns, c => {
console.log(`- ${c.name}
id: ${c.id}`)
});
});
}
function moveCards(argv) {
github.projects.getProjectCards({
column_id: argv.from
}).then((data) => {
_handleMoveCardsResponse(data, argv);
});
}
function _handleMoveCardsResponse(res, argv) {
let promise = Promise.resolve();
if (argv.dryRun) {
console.log('dry run !')
}
_.each(res.data, card => {
promise = promise.then(() => {
if (!card.content_url) {
console.log(`NO ACCESS FOR CARD ${card.id}`);
return;
}
if (argv.dryRun) {
console.log(`Will move card id ${card.id}`);
} else {
console.log(`Moving card id ${card.id}`);
return github.projects.moveProjectCard({
id: card.id,
position: 'bottom',
column_id: argv.to
});
}
});
});
promise.then(() => {
if (github.hasNextPage(res)) {
if (argv.dryRun) {
github.getNextPage(res).then(res => _handleMoveCardsResponse(res, argv));
} else {
//We don't check next page as we updated the current column so we re-trigger the get cards
moveCards(argv);
}
}
});
}
function _getGithub() {
let github = new GitHubApi();
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
});
return github;
}