-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
76 lines (67 loc) · 2.06 KB
/
utils.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
const gitSync = require('simple-git')
const github = require('octonode')
const rsync = require('rsyncwrapper')
const config = require('./config')
const GithubClient = github.client(config.GITHUB_TOKEN)
// I am using the un-promised version here because 'commit' and 'reset'
// methods are not implemented yet in the promised version of it.
exports.commitAndPushChanges = (directory, filesToAdd) => {
return new Promise((resolve, reject) => {
gitSync(directory)
.addConfig('user.name', config.USER)
.addConfig('user.email', config.EMAIL)
.checkout(config.BRANCH_NAME)
.add(filesToAdd)
.commit(config.COMMIT_MESSAGE)
.push(['--force', 'origin', config.BRANCH_NAME], (err) => {
err ? reject(err) : resolve()
})
})
}
exports.checkoutAndResetHard = (directory) => {
return new Promise((resolve) => {
gitSync(directory)
.checkout(config.BRANCH_NAME)
.fetch(['--all'])
.reset(['--hard', 'origin/master'], (err) => {
err ? reject(err) : resolve()
})
})
}
exports.isPRAlreadyCreated = (repo) => {
return new Promise((resolve, reject) => {
GithubClient.repo(`${config.USER}/${repo}`).prs((err, pullRequests) => {
if (err) {
return reject(err)
}
const prAlreadyCreated = !!pullRequests.find((pr) => pr.title === config.PULL_REQUEST_NAME)
resolve(prAlreadyCreated)
})
})
}
exports.createPullRequest = (repoName) => {
console.log('Creating PR...')
return new Promise((resolve, reject) => {
GithubClient.repo(`${config.USER}/${repoName}`).pr({
title: config.PULL_REQUEST_NAME,
body: config.PULL_REQUEST_BODY,
head: config.BRANCH_NAME,
base: 'master'
}, (err, data) => err ? reject(err) : resolve(data))
})
}
exports.copyWithRsync = ({ src, dest }) => {
return new Promise((resolve, reject) => {
rsync(
{
src,
dest,
recursive: true,
exclude: config.EXCLUDED_FILES
},
(error, stdout, stderr, cmd) => {
error ? reject(error) : resolve({ src, dest })
}
)
})
}