Skip to content

Commit 3f29a65

Browse files
authored
Test and Coverage ✨
1 parent 25107ce commit 3f29a65

25 files changed

+615
-192
lines changed

index.js

+29-17
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const createScheduler = require('probot-scheduler')
22
const getConfig = require('probot-config')
33
const moment = require('moment')
44

5-
const createWeeklyDigestLabel = require('./src/markdown/createWeeklyDigestLabel')
6-
const createConfigYML = require('./src/markdown/createConfigYML')
5+
// const createWeeklyDigestLabel = require('./src/markdown/createWeeklyDigestLabel')
6+
const postCreateLabel = require('./src/bin/postCreateLabel')
77
const weeklyDigest = require('./src/weeklyDigest')
88
const getDate = require('./src/markdown/getDate')
99
const defaultConfig = require('./src/markdown/defaultConfig')
@@ -13,40 +13,52 @@ const checkDuplicates = require('./src/markdown/checkDuplicates')
1313

1414
// 12 hours
1515
const interval = 12 * 60 * 60 * 1000
16+
const events = ['installation.created', 'installation_repositories']
1617

1718
module.exports = (app) => {
1819
app.log('Weekly Digest app is running successfully.')
19-
app.on('installation.created', async (context) => {
20+
21+
app.on(events, async (context) => {
2022
app.log('App has been successfully installed.')
23+
app.log('App is running as per ' + context.event)
2124
app.log('Local Time: ' + moment().format())
22-
const [owner, repo] = await context.payload.repositories[0].full_name.split('/')
23-
console.log(`Repository: ${owner}/${repo}`)
24-
await createWeeklyDigestLabel(context, {owner, repo})
25-
await createConfigYML(context, {owner, repo})
2625
const headDate = getDate.headDate()
2726
const tailDate = getDate.tailDate()
2827
const config = defaultConfig
29-
weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
28+
let repoList = []
29+
if (context.event === 'installation' && context.payload.action === 'created') {
30+
repoList = context.payload.repositories
31+
console.log(repoList)
32+
} else if (context.event === 'installation_repositories') {
33+
repoList = context.payload.repositories_added
34+
}
35+
repoList.forEach(async (item) => {
36+
const [owner, repo] = item.full_name.split('/')
37+
app.log(`Repository: ${owner}/${repo}`)
38+
await postCreateLabel(context, {owner, repo, name: 'weekly-digest', color: '9C27B0', description: ''})
39+
await weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
40+
})
3041
})
42+
3143
createScheduler(app, {interval: interval})
3244
app.on('schedule.repository', async (context) => {
33-
console.log(`App is running as per schedule.repository`)
45+
app.log(`App is running as per schedule.repository`)
3446
app.log('Local Time: ' + moment().format())
3547
const { owner, repo } = context.repo()
36-
console.log(`Repository: ${owner}/${repo}`)
48+
app.log(`Repository: ${owner}/${repo}`)
3749
const headDate = getDate.headDate()
3850
const tailDate = getDate.tailDate()
3951
let { hasDuplicates, url } = await checkDuplicates(context, {owner, repo, headDate})
4052
if (hasDuplicates) {
4153
console.log(`Weekly Digest for this week has already been published for ${owner}/${repo}`)
4254
console.log(`URL: ` + url)
43-
} else {
44-
let config = await getConfig(context, 'weekly-digest.yml')
45-
config = fixConfig(config)
46-
console.log(`Publish Day: ${getNumDayFromLongDay(config.publishDay)}, Today: ${moment().day()}`)
47-
if (moment().day() === getNumDayFromLongDay(config.publishDay)) {
48-
weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
49-
}
55+
return
56+
}
57+
let config = await getConfig(context, 'weekly-digest.yml')
58+
config = fixConfig(config)
59+
console.log(`Publish Day: ${getNumDayFromLongDay(config.publishDay)}, Today: ${moment.utc().day()}`)
60+
if (moment.utc().day() === getNumDayFromLongDay(config.publishDay)) {
61+
await weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
5062
}
5163
})
5264
}

index.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { Application } = require('probot')
2+
// Requiring our app implementation
3+
const myProbotApp = require('./')
4+
5+
const installationCreatedPayload = require('./test/payload/issues.opened.json')
6+
7+
describe('My Probot app', () => {
8+
let app, github
9+
10+
beforeEach(() => {
11+
app = new Application()
12+
// Initialize the app based on the code from index.js
13+
app.load(myProbotApp)
14+
// This is an easy way to mock out the GitHub API
15+
github = {
16+
issues: {
17+
createLabel: jest.fn().mockReturnValue(Promise.resolve({}))
18+
}
19+
}
20+
// Passes the mocked out GitHub API into out app instance
21+
app.auth = () => Promise.resolve(github)
22+
})
23+
24+
test.skip('creates a comment when an issue is opened', async () => {
25+
// Simulates delivery of an issues.opened webhook
26+
await app.receive({
27+
event: 'installation.created',
28+
payload: installationCreatedPayload
29+
})
30+
31+
// This test passes if the code in your index.js file calls `context.github.issues.createComment`
32+
expect(github.issues.createLabel).toHaveBeenCalled()
33+
})
34+
})
35+
36+
// For more information about testing with Jest see:
37+
// https://facebook.github.io/jest/

package.json

+49-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
1-
{
2-
"name": "weekly-digest",
3-
"version": "0.0.0-development",
4-
"description": "",
5-
"author": "abhijeetps <[email protected]>",
6-
"license": "ISC",
7-
"repository": "https://github.com/probot/weekly-digest.git",
8-
"scripts": {
9-
"dev": "nodemon --exec \"npm start\"",
10-
"start": "probot run ./index.js",
11-
"lint": "standard --fix",
12-
"test": "standard && jest",
13-
"test:watch": "jest --watch --notify --notifyMode=change --coverage",
14-
"travis-deploy-once": "travis-deploy-once",
15-
"semantic-release": "semantic-release"
16-
},
17-
"dependencies": {
18-
"mockdate": "^2.0.2",
19-
"moment": "^2.22.2",
20-
"nock": "^9.4.1",
21-
"probot": "^7.0.1",
22-
"probot-config": "^0.1.0",
23-
"probot-scheduler": "^1.2.0"
24-
},
25-
"devDependencies": {
26-
"jest": "^23.2.0",
27-
"nodemon": "^1.17.5",
28-
"smee-client": "^1.0.2",
29-
"standard": "^11.0.1",
30-
"semantic-release": "^15.6.0"
31-
},
32-
"engines": {
33-
"node": ">= 8.3.0"
34-
},
35-
"standard": {
36-
"env": [
37-
"jest"
38-
]
39-
}
40-
}
1+
{
2+
"name": "weekly-digest",
3+
"version": "0.0.0-development",
4+
"description": "",
5+
"author": "abhijeetps <[email protected]>",
6+
"license": "ISC",
7+
"repository": "https://github.com/probot/weekly-digest.git",
8+
"scripts": {
9+
"dev": "nodemon --exec \"npm start\"",
10+
"start": "probot run ./index.js",
11+
"lint": "standard --fix",
12+
"test": "standard && jest",
13+
"test:watch": "jest --watch --notify --notifyMode=change --coverage",
14+
"travis-deploy-once": "travis-deploy-once",
15+
"semantic-release": "semantic-release"
16+
},
17+
"dependencies": {
18+
"mockdate": "^2.0.2",
19+
"moment": "^2.22.2",
20+
"nock": "^9.4.4",
21+
"probot": "^7.0.1",
22+
"probot-config": "^0.2.0",
23+
"probot-scheduler": "^1.2.0"
24+
},
25+
"devDependencies": {
26+
"jest": "^23.4.2",
27+
"nodemon": "^1.18.3",
28+
"smee-client": "^1.0.2",
29+
"standard": "^11.0.1",
30+
"semantic-release": "^15.9.3",
31+
"travis-deploy-once": "^5.0.1"
32+
},
33+
"engines": {
34+
"node": ">= 8.3.0"
35+
},
36+
"standard": {
37+
"env": [
38+
"jest"
39+
]
40+
},
41+
"jest": {
42+
"testEnvironment": "node",
43+
"verbose": true,
44+
"testURL": "http://localhost/",
45+
"collectCoverageFrom": [
46+
"src/**/*.js"
47+
]
48+
}
49+
}

src/bin/getSearchIssues.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11

22
module.exports = async (context, {owner, repo, date, author, type}) => {
33
console.log('In getSearchIssues.js...')
4-
let searchIssues = await context.github.paginate(
5-
context.github.search.issues({
6-
q: `repo:${owner}/${repo} type:${type} author:${author} created:>=${date}`,
7-
per_page: 100
8-
}),
9-
res => res.data
10-
)
4+
let searchIssues = await context.github.search.issues({
5+
q: `repo:${owner}/${repo} type:${type} author:${author} created:>=${date}`,
6+
per_page: 100
7+
})
118
return searchIssues
129
}

src/bin/putCreateFile.js

-10
This file was deleted.

src/markdown/checkDuplicates.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = async (context, {owner, repo, headDate}) => {
77
let type = 'issues'
88
let date = getDate.getDayBeforeDate(headDate).substr(0, 19)
99
let issues = await getSearchIssues(context, {owner, repo, date, author, type})
10-
if (issues.total_count >= 1) {
10+
let totalCount = issues.data.total_count
11+
if (totalCount >= 1) {
1112
return {hasDuplicates: true, url: issues.data.items[0].html_url}
1213
} else {
1314
return {hasDuplicates: false, url: undefined}

src/markdown/createConfigYML.js

-22
This file was deleted.

src/markdown/createWeeklyDigestLabel.js

-10
This file was deleted.

src/markdown/markdownBody.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
const markdownIssues = require('./markdownIssues')
3+
const markdownPullRequests = require('./markdownPullRequests')
4+
const markdownContributors = require('./markdownContributors')
5+
const markdownStargazers = require('./markdownStargazers')
6+
const markdownCommits = require('./markdownCommits')
7+
const markdownReleases = require('./markdownReleases')
8+
9+
const getAllIssues = require('./../bin/getAllIssues')
10+
const getAllPullRequests = require('./.././bin/getAllPullRequests')
11+
const getStargazers = require('./../bin/getStargazers')
12+
const getCommits = require('./../bin/getCommits')
13+
const getReleases = require('./../bin/getReleases')
14+
15+
module.exports = async (context, {owner, repo, headDate, tailDate}, config) => {
16+
let body = `Here's the **Weekly Digest** for [*${owner}/${repo}*](https://github.com/${owner}/${repo}):\n`
17+
let issuesString
18+
let contributorsString
19+
let pullRequestsString
20+
let stargazersString
21+
let commitsString
22+
let releasesString
23+
if (config.canPublishIssues) {
24+
const issues = await getAllIssues(context, {owner, repo, tailDate})
25+
issuesString = markdownIssues(issues, headDate, tailDate)
26+
}
27+
if (config.canPublishPullRequests) {
28+
const pullRequests = await getAllPullRequests(context, {owner, repo})
29+
pullRequestsString = markdownPullRequests(pullRequests, headDate, tailDate)
30+
}
31+
if (config.canPublishCommits || config.canPublishContributors) {
32+
const commits = await getCommits(context, {owner, repo, tailDate})
33+
if (config.canPublishCommits) {
34+
commitsString = markdownCommits(commits, headDate, tailDate)
35+
}
36+
if (config.canPublishContributors) {
37+
contributorsString = markdownContributors(commits, headDate, tailDate)
38+
}
39+
}
40+
if (config.canPublishStargazers) {
41+
const stargazers = await getStargazers(context, {owner, repo})
42+
stargazersString = markdownStargazers(stargazers, headDate, tailDate)
43+
}
44+
if (config.canPublishReleases) {
45+
const releases = await getReleases(context, {owner, repo})
46+
releasesString = markdownReleases(releases, headDate, tailDate)
47+
}
48+
if (issuesString) {
49+
body += `\n - - - \n`
50+
body += issuesString
51+
}
52+
if (pullRequestsString) {
53+
body += `\n - - - \n`
54+
body += pullRequestsString
55+
}
56+
if (commitsString) {
57+
body += `\n - - - \n`
58+
body += commitsString
59+
}
60+
if (contributorsString) {
61+
body += `\n - - - \n`
62+
body += contributorsString
63+
}
64+
if (stargazersString) {
65+
body += `\n - - - \n`
66+
body += stargazersString
67+
}
68+
if (releasesString) {
69+
body += `\n - - - \n`
70+
body += releasesString
71+
}
72+
body += `\n - - - \n`
73+
body += '\n'
74+
body += `That's all for last week, please <kbd>:eyes: **Watch**</kbd> and <kbd>:star: **Star**</kbd> the repository [*${owner}/${repo}*](https://github.com/${owner}/${repo}) to receive next weekly updates. :smiley:\n\n`
75+
body += `*You can also [view all Weekly Digests by clicking here](https://github.com/${owner}/${repo}/issues?q=is:open+is:issue+label:weekly-digest).* \n\n`
76+
body += `> Your [**Weekly Digest**](https://github.com/apps/weekly-digest) bot. :calendar:\n`
77+
return body
78+
}

0 commit comments

Comments
 (0)