-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgardener.js
111 lines (109 loc) Β· 3.75 KB
/
gardener.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
const debug = require('debug')('gardener');
const _ = require('lodash');
const rp = require('request-promise-native');
class gardener {
constructor({github_token, owner, repo, app_name, messages}) {
this.domain = 'https://api.github.com';
this.owner = owner;
this.repo = repo;
this.messages = messages || [
"π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±π±",
"I need to mow the lawn.",
"What, did I ruin the lawn?",
"He gets busy picking weeds and tending to the lawn.",
"Let's meet at the public lawn tomorrow.",
"After finishing it, I'll start on the lawn.",
"But if I water the lawn, the grass will grow.",
"We hired a man to mow the lawn.",
"The lawn in the front yard is kept very neat.",
"Did you cut my lawn a few weeks ago?",
"Do not put off lawn treatment.",
"Our lawn will be mowed twice a month.",
"I want my lawn mower back.",
"I'm trying to do my lawn!",
"I'm gonna check the lawn again.",
"The lawn was checkered with sunlight and shade.",
"The gardener trim the lawn evenly.",
"Your lawn looks fantastic.",
"Let me know about your lawns."
];
this.headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${github_token}`,
'User-Agent': app_name
}
}
async get_master() {
await rp({
url: `${this.domain}/repos/${this.owner}/${this.repo}/branches/master`,
method: 'get',
headers: this.headers,
json: true,
transform: (body) => {
debug(body);
this.commit_sha = _.get(body, 'commit.sha');
}
})
}
async get_tree() {
await rp({
url: `${this.domain}/repos/${this.owner}/${this.repo}/git/trees/${this.commit_sha}`,
method: 'get',
headers: this.headers,
json: true,
transform: (body) => {
debug(body);
this.tree = _.get(body, 'tree');
}
})
}
async create_tree() {
await rp({
url: `${this.domain}/repos/${this.owner}/${this.repo}/git/trees`,
method: 'post',
headers: this.headers,
json: {
"tree": this.tree
},
transform: (body) => {
debug(body);
this.new_tree_sha = _.get(body, 'sha');
}
})
}
async create_commit() {
const response = await rp({
url: `${this.domain}/repos/${this.owner}/${this.repo}/git/commits`,
method: 'post',
headers: this.headers,
json: {
message: _.sample(this.messages),
tree: this.new_tree_sha
},
transform: (body) => {
debug(body);
this.new_commit_sha = _.get(body, 'sha');
}
})
}
async update_reference() {
const response = await rp({
url: `${this.domain}/repos/${this.owner}/${this.repo}/git/refs/heads/master`,
method: 'patch',
headers: this.headers,
json: {
sha: this.new_commit_sha,
force: true
}
})
debug(response);
}
async run() {
await this.get_master();
await this.get_tree();
await this.create_tree();
await this.create_commit();
await this.update_reference();
}
}
module.exports = gardener;