This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommentOnIssue.ts
74 lines (68 loc) · 2.47 KB
/
CommentOnIssue.ts
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
import {
EventFired,
EventHandler,
HandleEvent,
HandlerContext,
HandlerResult,
Secret,
Secrets,
Success,
Tags,
} from "@atomist/automation-client";
import axios from "axios";
@EventHandler("Notify channel on new issue and add comment to issue", `subscription CommentOnIssue
{
Issue {
number
title
openedBy {
login
}
repo {
owner
name
channels {
name
}
}
}
}`)
@Tags("issue", "comment")
export class CommentOnIssue implements HandleEvent<any> {
@Secret(Secrets.OrgToken)
public githubToken: string;
public handle(e: EventFired<any>, ctx: HandlerContext): Promise<HandlerResult> {
const issue = e.data.Issue[0];
const authorizingGithubUser = whoami(this.githubToken);
// To see these messages, configure Atomist in any slack channel: `@atomist-bot repo <repository-name>`
// Then create an issue in <repository-name> to see this notification in that channel!
const postInLinkedChannels =
ctx.messageClient.addressChannels(`Got a new issue \`${issue.number}# ${issue.title}\``,
issue.repo.channels.map(c => c.name));
const commentOnIssue = postInLinkedChannels
.then(() =>
authorizingGithubUser.then(me => {
// Only comment on issues that I created. Take out this condition to comment on all issues!
if (issue.openedBy.login === me) {
// tslint:disable-next-line:max-line-length
return axios.post(`https://api.github.com/repos/${issue.repo.owner}/${issue.repo.name}/issues/${issue.number}/comments`,
{ body: "Hey, I saw your issue!" },
{ headers: { Authorization: `token ${this.githubToken}` } });
} else {
// tslint:disable-next-line:max-line-length
console.log(`This issue was created by ${issue.openedBy.login} and I am ${me} so I will not comment on it.`);
return;
}
}));
return commentOnIssue.then(() => {
return Success;
});
}
}
function whoami(githubToken: string): Promise<string> {
const githubUser = axios.get(
`https://api.github.com/user`,
{ headers: { Authorization: `token ${githubToken}` } })
.then(response => response.data.login);
return githubUser;
}