Skip to content

Commit 76a5085

Browse files
author
Abhijeet Singh
authored
WIP Adding Tests (abhijeetps#27)
* Updated Package.json * Added all possible values for getDate.test.js * Fixed the return value when the number is an integer * Added tests for getNumDayFromLongDay.js * Added emptyIssue payload * Added tests if there are no issues * Fixed markdownIssues to work when there are no issues * Added tests to check return string there's no PR * Fixed errors if the pullRequests is empty * Fixed stargazers tests and markdown if there are no stargazers * Fixed markdownCommits.js to work if there are no commits * Fixed markdownReleases.js to work if there are no releases * Made common empty payload * Fixed and added more cautions getNumDayFromLongDay * Updated .gitignore * Added package.json * Working with moment * Updated getNumDayFromLongDay * Added newline in markdownContributors if there are no contributors * Added newline in markdownIssues if there are no issues * Added newline in markdownReleases if there are no releases * Added newline in markdownStargazers if there are no stargazers * Added getDate.test.js * Complete coverage of getNumDayFromLongDay.js * 100% line coverage on markdownCommits.js 🎉 * Added issues payload. * Added mockdate * 100% statements coverage of PRs * Added tests for markdownCommits.js * Added tests for stargazers * Added tests for markdownReleases * Added nock * Added getContributors method * var => let or const * Added fixConfig and related tests * Added fixConfig.js * Fixed abhijeetps#29 * Added URL of Duplicate issue
1 parent 8831866 commit 76a5085

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+908
-234
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ npm-debug.log
44
.env
55
package-lock.json
66
.vscode
7-
.vs
7+
.vs
8+
coverage

Diff for: index.js

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
const createScheduler = require('probot-scheduler')
22
const getConfig = require('probot-config')
3-
const createWeeklyDigestLabel = require('./lib/markdown/createWeeklyDigestLabel')
4-
const createConfigYML = require('./lib/markdown/createConfigYML')
5-
const weeklyDigest = require('./lib/weeklyDigest')
6-
const getDate = require('./lib/markdown/getDate')
7-
const defaultConfig = require('./lib/markdown/defaultConfig')
8-
const getNumDayFromLongDay = require('./lib/markdown/getNumDayFromLongDay')
3+
const moment = require('moment')
94

10-
// 1 day
11-
const interval = 24 * 60 * 60 * 1000
5+
const createWeeklyDigestLabel = require('./src/markdown/createWeeklyDigestLabel')
6+
const createConfigYML = require('./src/markdown/createConfigYML')
7+
const weeklyDigest = require('./src/weeklyDigest')
8+
const getDate = require('./src/markdown/getDate')
9+
const defaultConfig = require('./src/markdown/defaultConfig')
10+
const getNumDayFromLongDay = require('./src/markdown/getNumDayFromLongDay')
11+
const fixConfig = require('./src/markdown/fixConfig')
12+
const checkDuplicates = require('./src/markdown/checkDuplicates')
13+
14+
// 12 hours
15+
const interval = 12 * 60 * 60 * 1000
1216

1317
module.exports = (app) => {
14-
app.log('Weekly Digest has started running! :)')
18+
app.log('Weekly Digest app is running successfully.')
1519
app.on('installation.created', async (context) => {
16-
console.log('App has been successfully installed.')
20+
app.log('App has been successfully installed.')
21+
app.log('Local Time: ' + moment().format())
1722
const [owner, repo] = await context.payload.repositories[0].full_name.split('/')
18-
console.error(`repo: ${owner}/${repo}`)
23+
console.log(`Repository: ${owner}/${repo}`)
1924
await createWeeklyDigestLabel(context, {owner, repo})
2025
await createConfigYML(context, {owner, repo})
21-
const headDate = await getDate.headDate()
22-
const tailDate = await getDate.tailDate()
23-
const config = await defaultConfig
26+
const headDate = getDate.headDate()
27+
const tailDate = getDate.tailDate()
28+
const config = defaultConfig
2429
weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
2530
})
2631
createScheduler(app, {interval: interval})
2732
app.on('schedule.repository', async (context) => {
2833
console.log(`App is running as per schedule.repository`)
29-
const headDate = await getDate.headDate()
30-
const tailDate = await getDate.tailDate()
31-
var currentDate = await getDate.getUTCFromISO(headDate)
32-
var config = await getConfig(context, 'weekly-digest.yml')
33-
if (config == null) {
34-
config = defaultConfig
35-
}
36-
console.log(`Publish Day: ${getNumDayFromLongDay(config.publishDay)}, Today: ${currentDate.getDay()}`)
37-
if (currentDate.getDay() === getNumDayFromLongDay(config.publishDay)) {
38-
const { owner, repo } = context.repo()
39-
weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
34+
app.log('Local Time: ' + moment().format())
35+
const { owner, repo } = context.repo()
36+
const headDate = getDate.headDate()
37+
const tailDate = getDate.tailDate()
38+
let { hasDuplicates, url } = await checkDuplicates(context, {owner, repo, headDate})
39+
if (hasDuplicates) {
40+
console.log(`Weekly Digest for this week has already been published for ${owner}/${repo}`)
41+
console.log(`URL: ` + url)
42+
} else {
43+
let config = await getConfig(context, 'weekly-digest.yml')
44+
config = fixConfig(config)
45+
console.log(`Publish Day: ${getNumDayFromLongDay(config.publishDay)}, Today: ${moment().day()}`)
46+
if (moment().day() === getNumDayFromLongDay(config.publishDay)) {
47+
weeklyDigest(context, {owner, repo, headDate, tailDate}, config)
48+
}
4049
}
4150
})
4251
}

Diff for: package.json

+40-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1-
{
2-
"name": "weekly-digest",
3-
"version": "0.0.0-development",
4-
"description": "",
5-
"author": "aps120797 <[email protected]>",
6-
"license": "ISC",
7-
"repository": "https://github.com/aps120797/weekly-digest.git",
8-
"scripts": {
9-
"dev": "nodemon --exec \"npm start\"",
10-
"start": "probot run ./index.js",
11-
"lint": "standard --fix",
12-
"test": "exit 0",
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-
"probot": "^6.0.0",
19-
"probot-config": "^0.1.0",
20-
"probot-scheduler": "^1.1.0"
21-
},
22-
"devDependencies": {
23-
"jest": "^23.1.0",
24-
"nodemon": "^1.17.2",
25-
"smee-client": "^1.0.1",
26-
"standard": "^11.0.1",
27-
"semantic-release": "^15.6.0"
28-
},
29-
"engines": {
30-
"node": ">= 8.3.0"
31-
},
32-
"standard": {
33-
"env": [
34-
"jest"
35-
]
36-
}
37-
}
1+
{
2+
"name": "weekly-digest",
3+
"version": "0.0.0-development",
4+
"description": "",
5+
"author": "aps120797 <[email protected]>",
6+
"license": "ISC",
7+
"repository": "https://github.com/aps120797/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+
}

Diff for: src/bin/getAllIssues.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = async (context, {owner, repo, tailDate}) => {
22
// method returns all the issues and pull requests
33
console.log('In getAllIssues.js...')
4-
var issues = await context.github.issues.getForRepo({
4+
let issues = await context.github.issues.getForRepo({
55
owner,
66
repo,
77
state: 'all',

Diff for: src/bin/getAllPullRequests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = async (context, {owner, repo}) => {
22
// method returns all the pull requests
33
console.log('In getAllPullRequests.js...')
4-
var pullRequests = await context.github.pullRequests.getAll({
4+
let pullRequests = await context.github.pullRequests.getAll({
55
owner,
66
repo,
77
state: 'all'

Diff for: src/bin/getCommits.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = async (context, {owner, repo, tailDate}) => {
22
console.log('In getCommits.js...')
3-
var commits = await context.github.repos.getCommits({
3+
let commits = await context.github.repos.getCommits({
44
owner,
55
repo,
66
since: tailDate

Diff for: src/bin/getContributors.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = async (context, {owner, repo}) => {
2+
console.log('In getContributors.js...')
3+
let contributors = await context.github.repos.getContributors({
4+
owner,
5+
repo
6+
})
7+
return contributors
8+
}

Diff for: src/bin/getReleases.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = async (context, {owner, repo}) => {
22
console.log('In getReleases.js...')
3-
var releases = await context.github.repos.getReleases({
3+
let releases = await context.github.repos.getReleases({
44
owner,
55
repo
66
})

Diff for: src/bin/getSearchIssues.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = async (context, {owner, repo, date, author, type}) => {
2+
console.log('In getSearchIssues.js...')
3+
let searchIssues = await context.github.search.issues({
4+
q: `repo:${owner}/${repo} type:${type} author:${author} created:>=${date}`
5+
})
6+
return searchIssues
7+
}

Diff for: src/markdown/checkDuplicates.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const getSearchIssues = require('./../bin/getSearchIssues')
2+
const getDate = require('./getDate')
3+
4+
module.exports = async (context, {owner, repo, headDate}) => {
5+
console.log('In checkDuplicates.js...')
6+
let author = 'app/weekly-digest'
7+
let type = 'issues'
8+
let date = getDate.getDayBeforeDate(headDate).substr(0, 19)
9+
let issues = await getSearchIssues(context, {owner, repo, date, author, type})
10+
if (issues.data.total_count >= 1) {
11+
return {hasDuplicates: true, url: issues.data.items[0].html_url}
12+
} else {
13+
return {hasDuplicates: false, url: undefined}
14+
}
15+
}

Diff for: src/markdown/createConfigYML.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var defaultConfig = require('./defaultConfig')
2-
var putCreateFile = require('./../bin/putCreateFile')
1+
let defaultConfig = require('./defaultConfig')
2+
let putCreateFile = require('./../bin/putCreateFile')
33

44
module.exports = (context, {owner, repo}) => {
55
console.log('In createConfigYML.js...')
6-
var content = Buffer.from(
6+
let content = Buffer.from(
77
`# Configuration for weekly-digest - https://github.com/apps/weekly-digest
88
publishDay: ${defaultConfig.publishDay}
99
canPublishIssues: ${defaultConfig.canPublishIssues}

Diff for: src/markdown/createWeeklyDigestLabel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var postCreateLabel = require('./../bin/postCreateLabel')
1+
let postCreateLabel = require('./../bin/postCreateLabel')
22
module.exports = (context, {owner, repo}) => {
33
console.log('In createWeeklyDigestLabel.js...')
44
postCreateLabel(context, {

Diff for: src/markdown/fixConfig.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = (config) => {
2+
if (!config.hasOwnProperty('publishDay')) {
3+
config.publishDay = 0
4+
}
5+
if (!config.hasOwnProperty('canPublishIssues')) {
6+
config.canPublishIssues = true
7+
}
8+
if (!config.hasOwnProperty('canPublishPullRequests')) {
9+
config.canPublishPullRequests = true
10+
}
11+
if (!config.hasOwnProperty('canPublishContributors')) {
12+
config.canPublishContributors = true
13+
}
14+
if (!config.hasOwnProperty('canPublishStargazers')) {
15+
config.canPublishStargazers = true
16+
}
17+
if (!config.hasOwnProperty('canPublishCommits')) {
18+
config.canPublishCommits = true
19+
}
20+
if (!config.hasOwnProperty('canPublishReleases')) {
21+
config.canPublishReleases = true
22+
}
23+
return config
24+
}

Diff for: src/markdown/getDate.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1+
const moment = require('moment')
2+
13
module.exports = {
2-
headDate: async () => {
4+
headDate: () => {
35
console.log('In getDate.js headDate...')
4-
var date = new Date()
5-
date.setHours(0, 0, 0, 0)
6-
date = await date.toISOString()
7-
return date
6+
return moment().set({'hour': 0, 'minute': 0, 'second': 0, 'millisecond': 0}).format()
87
},
9-
tailDate: async () => {
8+
tailDate: () => {
109
console.log('In getDate.js tailDate...')
11-
var date = new Date()
12-
date.setHours(0, 0, 0, 0)
13-
date.setDate(date.getDate() - 7)
14-
date = await date.toISOString()
15-
return date
10+
return moment().set({'hour': 0, 'minute': 0, 'second': 0, 'millisecond': 0}).subtract(7, 'days').format()
1611
},
17-
getUTCFromISO: async (ISODate) => {
18-
console.log('In getDate.js getDateStringFromISO...')
19-
var date = ISODate.split(/\D/)
20-
var dateUTC = await new Date(Date.UTC(date[0], --date[1], date[2], date[3] || 0, date[4] || 0, date[5] || 0, date[6] || 0))
21-
return dateUTC
12+
getDayBeforeDate: (date) => {
13+
console.log('In getDate.js getDayBeforeDate...')
14+
return moment(date).subtract(1, 'days').format()
2215
}
2316
}

Diff for: src/markdown/getNumDayFromLongDay.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = (day) => {
2-
if (typeof month === 'number') {
2+
if (typeof day === 'number' && day >= 0 && day < 7) {
33
return day
44
} else {
55
const longDay = [
@@ -11,11 +11,11 @@ module.exports = (day) => {
1111
'friday',
1212
'saturday'
1313
]
14-
for (var i = 0; i < 7; i++) {
14+
for (let i = 0; i < 7; i++) {
1515
if (longDay[i].includes(String(day).toLowerCase())) {
1616
return i
1717
}
1818
}
1919
}
20-
return 0
20+
return undefined
2121
}

Diff for: src/markdown/markdownCommits.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module.exports = (commits) => {
22
console.log('In markdown commits...')
3-
var data = commits.data
4-
var i
5-
var commitsString = '# COMMITS\n'
6-
var commitsCount = 0
7-
for (i = 0; i < data.length; i++) {
8-
if (data[i].author.type !== 'Bot') {
9-
commitsCount = commitsCount + 1
3+
let data = commits.data
4+
let i
5+
let commitsString = '# COMMITS\n'
6+
let commitsCount = 0
7+
if (typeof data !== 'undefined' && data !== null && data.length != null && data.length > 0) {
8+
for (i = 0; i < data.length; i++) {
9+
if (data[i].author.type !== 'Bot') {
10+
commitsCount = commitsCount + 1
11+
} else {
12+
continue
13+
}
1014
}
1115
}
1216
if (commitsCount > 1) {
@@ -15,6 +19,8 @@ module.exports = (commits) => {
1519
for (i = 0; i < data.length; i++) {
1620
if (data[i].author.type !== 'Bot') {
1721
commitsString += `:hammer_and_wrench: [${data[i].commit.message}](${data[i].html_url}) by [${data[i].author.login}](${data[i].author.html_url})\n`
22+
} else {
23+
continue
1824
}
1925
}
2026
} else if (commitsCount === 1) {

Diff for: src/markdown/markdownContributors.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = (issues) => {
22
console.log('In markdownContributors.js...')
3-
var contributorsString = `# CONTRIBUTORS \n`
4-
var contributors = []
3+
let contributorsString = `# CONTRIBUTORS \n`
4+
let contributors = []
55
const data = issues.data
6-
var i
6+
let i
77
// getting all the users
88
for (i = 0; i < data.length; i++) {
99
// checking if it's not a bot
@@ -30,7 +30,7 @@ module.exports = (issues) => {
3030
} else if (contributors.length === 1) {
3131
contributorsString += `This week, [${contributors[0].user}](${contributors[0].html_url}) has contributed in the repository.\n`
3232
} else {
33-
contributorsString += `This week, no user has contributed to this repository. `
33+
contributorsString += `This week, no user has contributed to this repository.\n`
3434
}
3535
return contributorsString
3636
}

0 commit comments

Comments
 (0)