-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
105 lines (83 loc) · 2.48 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
const twit = require('twit');
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit()
const AWS = require('aws-sdk')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const bucketName = "helpwantedbot";
const keyName = "helpwantedbot.json";
const bucketParams = {
Bucket: bucketName,
Key: keyName,
}
const twitterConfig = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
}
const s3Config = {
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY
}
const s3 = new AWS.S3(s3Config);
const Twitter = new twit(twitterConfig);
const QUERY = "label:\"help wanted\"+state:open"
/**
*
* @param {*} status string
* @returns Promise
*/
const tweet = async (status) => {
return Twitter.post('statuses/update', {
status
})
}
/**
*
* @param {*} issue github issue object
* @returns string containing the status of the tweet
*/
const buildStatus = async (issue) => {
// TODO: check character count
let tweetString = issue.title + " " + issue.html_url + " ";
const owner_repo = issue.repository_url.split("https://api.github.com/repos/")[1].split('/');
const [owner, repo] = owner_repo
const { data: languages } = await octokit.repos.listLanguages({ owner, repo })
Object.keys(languages).forEach(language => tweetString += "#" + language + " ")
tweetString += "#opensource"
return tweetString
}
const toAwsParams = (data) => ({
Body: JSON.stringify({ id: data.id }),
ContentType: "application/json",
...bucketParams
})
// AWS Lambda handler
exports.handler = async (event, context) => {
let currentId
try {
const current = await s3.getObject(bucketParams).promise()
currentId = parseInt(JSON.parse(current.Body.toString()).id)
} catch (err) {
console.error(err)
}
const { data: issues } = await octokit.search.issuesAndPullRequests({
q: QUERY,
sort: "created"
})
const [issue] = issues.items;
if (currentId !== issue.id) {
const status = await buildStatus(issue)
const tweetResponse = await tweet(status).catch((err) => (err instanceof Error ? err : new Error(JSON.stringify(err))))
if (tweetResponse instanceof Error) throw tweetResponse
try {
return s3.putObject(toAwsParams(issue)).promise()
} catch (err) {
console.error(err)
return err
}
}
return { "info": "no new issues" }
}