Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (99 loc) · 3.72 KB
/
Copy pathindex.js
File metadata and controls
120 lines (99 loc) · 3.72 KB
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
// Grabbed from https://github.com/Discord-Datamining/comments-action/blob/main/src/index.ts
const core = require("@actions/core");
const { context, getOctokit } = require("@actions/github")
const differ = require("@adryd325/discord-datamining-lang-differ");
const hyttpo = require('hyttpo').default;
const token = process.env.GITHUB_TOKEN
const discordwebhook = process.env.DISCORD_WEBHOOK
const filePathRegex = /\/\d{4}\/(?:\d{4}-\d{2}-\d{2}|\d{2}\/\d{2})\/[a-z0-9]{20,}\.js$/
const currentFilename = "current.js"
async function run() {
try {
if (!token) return core.setFailed("Invalid GITHUB_TOKEN")
const octokit = getOctokit(token)
const { owner, repo } = context.repo
if (context.eventName !== "push") return
const payload = context.payload
const commitSha = payload.after
const commit = await octokit.rest.repos.getCommit({
owner,
repo,
ref: commitSha
})
if (!commit)
return core.setFailed("commit not found")
const commitFile = commit.data.files[0]
if (!commitFile || commitFile.status !== "added")
return core.info("not a build commit")
const { blob_url, sha: fileSha } = commit.data.files[0]
if (!filePathRegex.test(decodeURIComponent(blob_url)))
return core.info("not a build file")
const currentTree = await octokit.rest.git.getTree({
owner,
repo,
tree_sha: payload.before,
})
const currentFileSha = currentTree.data.tree.find(file => file.path === currentFilename).sha
if (!currentFileSha)
return core.info("no current file")
const currentFile = await octokit.rest.git.getBlob({
owner,
repo,
file_sha: currentFileSha
})
const newFile = await octokit.rest.git.getBlob({
owner,
repo,
file_sha: fileSha,
})
const currentContent = Buffer.from(currentFile.data.content, "base64").toString("utf8")
const newContent = Buffer.from(newFile.data.content, "base64").toString("utf8")
if (core.isDebug()) {
core.debug(`${currentContent.length}`)
core.debug(`${newContent.length}`)
}
let diff
try {
diff = differ(
currentContent,
newContent,
"codeblock",
)
} catch (e) {
return core.setFailed(`unable to diff strings: ${e}`)
}
if (!diff) {
await hyttpo.request({
method: 'POST',
url: discordwebhook,
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({
content: `<@&912434591472705567> https://github.com/xHyroM/discord-datamining/commit/${commitSha}`
})
}).catch(e => console.log(e))
return core.info("no strings changed");
}
await octokit.rest.repos.createCommitComment({
owner,
repo,
commit_sha: commitSha,
body: diff
})
await hyttpo.request({
method: &
3C3B
#039;POST',
url: discordwebhook,
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({
content: `<@&912434624108564542> <@&912434591472705567> https://github.com/xHyroM/discord-datamining/commit/${commitSha}`
})
}).catch(e => console.log(e))
return core.info("created commit comment")
} catch (error) {
core.setFailed(core.isDebug() ? error.stack : error.message)
}
}
run()