This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (102 loc) · 4.06 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { convert, getSHA } = require('./src/util.js')
const defaultConfig = {
sourceBranch: 'develop',
sourceRepo: 'simple-icons',
targetBranch: 'master',
targetRepo: 'simple-icons-pdf'
}
const fileFilter = /icons\/.*\.svg/
module.exports = (robot) => {
robot.on('push', async context => {
robot.log.debug('New push detected')
const config = await context.config('svg-to-pdf.yml', defaultConfig)
let branch = context.payload.ref.replace('refs/heads/', '')
let repo = context.payload.repository.name
if (branch !== config.sourceBranch || repo !== config.sourceRepo) {
robot.log.info({branch, repo}, 'event ignored, branch and/or repo not the correct source')
return
}
let processed = []
for (let commit of context.payload.commits.reverse()) {
let modifiedFiles = commit.modified.filter(file => fileFilter.test(file))
for (let file of modifiedFiles) {
if (processed.includes(file)) {
robot.log.debug({file}, 'skipping file becuase later commits overwrite this change')
continue
}
robot.log.info({file}, '.svg file modification detected')
let pdfFile = file.replace('.svg', '.pdf')
let sha
try {
sha = await getSHA(context, config, pdfFile)
} catch (e) {
robot.log.info(`.pdf version of ${file} not found, adding instead`)
commit.added.push(file)
continue
}
robot.log.info(`Reconverting ${file} into a .pdf file`)
let pdf = await convert(context, file, branch)
robot.log.info(`Preparing commit to update ${pdfFile}`)
let newCommit = context.repo({
branch: config.targetBranch,
content: pdf,
message: `Update ${pdfFile}`,
path: pdfFile,
repo: config.targetRepo,
sha: sha
})
robot.log.info(`Commiting to repository ${config.targetRepo} on branch ${config.targetBranch}`)
if (process.env.dry !== 'true') context.github.repos.updateFile(newCommit)
processed.push(file)
}
let newFiles = commit.added.filter(file => fileFilter.test(file))
for (let file of newFiles) {
if (processed.includes(file)) {
robot.log.debug({file}, 'skipping file becuase later commits overwrite this change')
continue
}
robot.log.info({file}, 'new .svg file detected')
let pdfFile = file.replace('.svg', '.pdf')
robot.log.info(`Converting ${file} into a .pdf file`)
let pdf = await convert(context, file, branch)
robot.log.info(`Preparing commit for the newly created .pdf`)
let newCommit = context.repo({
branch: config.targetBranch,
content: pdf,
message: `Add ${file} as pdf`,
path: pdfFile,
repo: config.targetRepo
})
robot.log.info(`Commiting to repository ${config.targetRepo} on branch ${config.targetBranch}`)
if (process.env.dry !== 'true') context.github.repos.createOrUpdateFile(newCommit)
processed.push(file)
}
let removedFiles = commit.removed.filter(file => fileFilter.test(file))
for (let file of removedFiles) {
if (processed.includes(file)) {
robot.log.debug({file}, 'skipping file becuase later commits overwrite this change')
continue
}
robot.log.info({file}, '.svg file deletion detected')
let pdfFile = file.replace('.svg', '.pdf')
let sha
try {
sha = await getSHA(context, config, pdfFile)
} catch (e) {
continue
}
robot.log.info(`Preparing commit to delete ${pdfFile}`)
let newCommit = context.repo({
branch: config.targetBranch,
message: `Remove ${pdfFile}`,
path: pdfFile,
repo: config.targetRepo,
sha: sha
})
robot.log.info(`Commiting to repository ${config.targetRepo} on branch ${config.targetBranch}`)
if (process.env.dry !== 'true') context.github.repos.deleteFile(newCommit)
processed.push(file)
}
}
})
}