forked from opengaming/osgameclones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdangerfile.js
118 lines (105 loc) · 3.71 KB
/
dangerfile.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
106
107
108
109
110
111
112
113
114
115
116
117
118
const {danger, markdown, message, warn} = require('danger')
const http = require('http')
const url = require('url')
const yaml = require('js-yaml')
const fs = require('fs')
markdown("Hey there! Thanks for contributing a PR to osgameclones! 🎉")
let namesAdded = []
let namesChanged = []
let namesRemoved = []
const isGame = game => /^games\/\w+\.yaml$/.test(game)
// -----------
// Game checks
// -----------
// Check that updated is within one day of today
const isDateWithinOneDayToday = d => {
if (!d) {
return false
}
const timeDiff = Math.abs(new Date().getTime() - d.getTime())
return Math.ceil(timeDiff / (1000 * 3600 * 24)) <= 1
}
const checkGameUpdated = game => {
if (!isDateWithinOneDayToday(game.updated)) {
const gameUpdated = game.updated && game.updated.toISOString().slice(0, 10)
const updated = new Date().toISOString().slice(0, 10)
warn(`${game.name}'s "updated" value should be ${updated}; got ${gameUpdated} instead`)
}
}
const checkRepoGoogleCode = game => {
if (game.repo && (game.repo.indexOf('googlecode') >= 0 || game.repo.indexOf('code.google') >= 0)) {
warn(`${game.name}'s repo is Google Code, a dead service. Please check if there is an updated repo elsewhere.`)
}
}
// -----------
const onGameAdded = game => {
namesAdded.push(game.name)
checkGameUpdated(game)
checkRepoGoogleCode(game)
}
const onGameChanged = game => {
namesChanged.push(game.name)
checkGameUpdated(game)
checkRepoGoogleCode(game)
}
const onGameRemoved = game => {
namesRemoved.push(game.name)
}
const getGameChanges = files => {
Promise.all(files.filter(isGame).map(file => danger.git.diffForFile(file)))
.then(diffs => {
diffs.forEach(diff => {
const gamesBefore = yaml.safeLoad(diff.before)
// Compare any changes in games metadata
const stringsBefore = gamesBefore.map(game => JSON.stringify(game))
const gamesAfter = yaml.safeLoad(diff.after)
const namesBefore = gamesBefore.map(game => game.name)
const namesAfter = gamesAfter.map(game => game.name)
gamesBefore.forEach(game => {
if (!namesAfter.includes(game.name)) {
onGameRemoved(game)
}
})
gamesAfter.forEach(game => {
if (!namesBefore.includes(game.name)) {
onGameAdded(game)
} else if (namesBefore.includes(game.name) && !stringsBefore.includes(JSON.stringify(game))) {
onGameChanged(game)
}
})
})
if (namesAdded.length > 0) {
message(`Game(s) added: ${danger.utils.sentence(namesAdded)} 🎊`)
}
if (namesChanged.length > 0) {
message(`Game(s) updated: ${danger.utils.sentence(namesChanged)} 👏`)
}
if (namesRemoved.length > 0) {
message(`Game(s) removed: ${danger.utils.sentence(namesRemoved)} 😿`)
}
})
}
getGameChanges(danger.git.modified_files.concat(danger.git.created_files, danger.git.deleted_files))
// Information summary of files in the PR
// For debug purposes only
if (danger.git.modified_files.length || danger.git.created_files.length || danger.git.deleted_files.length) {
let changes = ""
const getChanges = (title, files) => {
const md = files.map(file => {
if (isGame(file)) {
const games = yaml.safeLoad(fs.readFileSync(file))
const gamesList = games.map(game => `\n - ${game.name}`)
return `\n- 🎮 \`${file}\`${gamesList.join()}`
}
return `\n- \`${file}\``
})
if (md.length > 0) {
return `\n\n${title}:${md}`
}
return ""
}
changes += getChanges("Changed", danger.git.modified_files)
changes += getChanges("Added", danger.git.created_files)
changes += getChanges("Deleted", danger.git.deleted_files)
markdown(`<details><summary>Files in PR...</summary><p>${changes}</p></details>`)
}