-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.js
More file actions
158 lines (132 loc) · 5 KB
/
plugin.js
File metadata and controls
158 lines (132 loc) · 5 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const format = require("util").format;
const inspect = require("util").inspect;
const fetch = require("node-fetch");
fetch.Promise = require("bluebird");
function getIssueSummary (options) {
const user = options.user;
const repo = options.repo;
const issueNumber = options.issue;
return fetch(format("https://api.github.com/repos/%s/%s/issues/%s", user, repo, issueNumber))
.then(function (res) {
return res.json();
})
.then(function (res) {
if (res.message === "Not Found") {
return "Issue does not exist.";
}
const type = res.pull_request === undefined ? "Issue" : "PR";
const status = res.state;
const title = res.title;
const link = res.html_url;
if (type === "PR" && status === "closed") {
return fetch(res.pull_request.url)
.then(function (res) {
return res.json();
})
.then(function (res) {
if (res.message === "Not Found") {
return "PR does not exist.";
}
const type = "PR";
const status = res.merged_at === null ? "closed" : "merged";
const title = res.title;
const link = res.html_url;
return format("[%s %s] <%s> %s <%s>", type, issueNumber, status, title, link);
});
} else {
return format("[%s %s] <%s> %s <%s>", type, issueNumber, status, title, link);
}
});
}
function withIssue (obj, issueNumber) {
obj.issue = issueNumber;
return obj;
}
function stringIsPositiveInteger (string) {
const number = Number(string);
if (isNaN(number)) {
return false;
}
if (number === Infinity) {
return false;
}
if (number < 1) {
return false;
}
if (Math.floor(number) !== number) {
return false;
}
return true;
}
module.exports = {
name: "github",
init: function (client, deps) {
const ghUser = client.config("github-user");
if (!ghUser) { throw new Error("github-user configuration value must be set."); }
const ghRepo = client.config("github-repo");
if (!ghRepo) { throw new Error("github-repo configuration value must be set."); }
/// Takes a string of one of the following formats and turns it into
/// a {user, repo} object defaulting to the client's config values
/// for missing values.
///
/// ""
/// "repo"
/// "user/repo"
/// "user/"
/// "/repo"
/// "/"
function parseUserRepoString (userRepoString) {
if (userRepoString.length === 0) {
return {user: ghUser, repo: ghRepo};
}
if (userRepoString.indexOf("/") === -1) {
return {user: ghUser, repo: userRepoString};
}
const split = userRepoString.split("/");
const user = split[0].length !== 0 ? split[0] : ghUser;
const repo = split[1].length !== 0 ? split[1] : ghRepo;
return {user: user, repo: repo};
}
return {
handlers: {
"!gh": function (command) {
// There's a lot of missing error handling here.
// e.g., the Number testing ignores the possibility of NaN.
if (command.args.length === 0) {
// !gh
return format("https://github.com/%s/%s", ghUser, ghRepo);
}
if (command.args.length === 1) {
const arg = command.args[0];
if (stringIsPositiveInteger(arg)) {
// !gh Number
return getIssueSummary({user: ghUser, repo: ghRepo, issue: arg});
} else {
const userRepo = parseUserRepoString(arg);
return format("https://github.com/%s/%s", userRepo.user, userRepo.repo);
}
}
if (command.args.length === 2) {
if (stringIsPositiveInteger(command.args[1])) {
return getIssueSummary(withIssue(parseUserRepoString(command.args[0]), command.args[1]));
} else {
return format("https://github.com/%s/%s", command.args[0], command.args[1]);
}
}
if (command.args.length === 3) {
return getIssueSummary({
user: command.args[0],
repo: command.args[1],
issue: command.args[2]
});
}
return "!gh [user/repo] issue-number";
}
},
commands: ["gh"],
help: {
"gh": ["!gh [user/repo] issue-number"]
}
};
}
};