Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: general formatting #5

Merged
merged 30 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bf5ffd2
fix(discord): replace discord string
ChatDisabled Dec 6, 2023
653b3e1
chore(discord): non capital
ChatDisabled Dec 6, 2023
e38b57f
chore: formatting
ChatDisabled Dec 6, 2023
6192eb3
fix(discord): replace string
ChatDisabled Dec 6, 2023
3dcf1c3
fix(discord): store replaced string
ChatDisabled Dec 6, 2023
bd86ac7
chore: logging
ChatDisabled Dec 6, 2023
3e7cc55
chore(action): use new branch
ChatDisabled Dec 6, 2023
b7f7094
fix(action): always use current branch
ChatDisabled Dec 6, 2023
5fd599d
fix(action): or don't
ChatDisabled Dec 6, 2023
839dc14
chore: testing a very long commit message by me
ChatDisabled Dec 6, 2023
a31e8fd
chore: remove threadId functionality
ChatDisabled Dec 6, 2023
139eb33
chore: testing
ChatDisabled Dec 6, 2023
3ee0cf2
fix(discord): case insensitive replace
ChatDisabled Dec 6, 2023
08a8df5
chore(discord): remove unnecessary logging
ChatDisabled Dec 6, 2023
77d49a1
chore: remove unsused variables
ChatDisabled Dec 6, 2023
c7fad2d
feat: logging
ChatDisabled Dec 6, 2023
a9621ec
fix: logging
ChatDisabled Dec 6, 2023
a2dbd45
fix: logging
ChatDisabled Dec 6, 2023
da09288
chore remove logging
ChatDisabled Dec 6, 2023
65664cb
feat: use pusher instead of latest author
ChatDisabled Dec 6, 2023
7c727b2
test
D4isDAVID Dec 6, 2023
9c1a21f
feat: get workflow pusher
ChatDisabled Dec 6, 2023
b3acb03
Merge branch 'dev' of https://github.com/ChatDisabled/discord-commits…
ChatDisabled Dec 6, 2023
f316543
chore(index): remove empty comment
ChatDisabled Dec 6, 2023
128271c
chore: revert back to old pusher
ChatDisabled Dec 6, 2023
8ad92e0
chore: remove prettierrc
D4isDAVID Dec 6, 2023
d1be5b1
fix(discord): author iconURL
ChatDisabled Dec 6, 2023
9bbe5e5
Merge branch 'dev' of https://github.com/ChatDisabled/discord-commits…
ChatDisabled Dec 6, 2023
7613ea7
chore: remove eslint
ChatDisabled Dec 6, 2023
b5e3cd5
chore: use main action
ChatDisabled Dec 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/discord-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
uses: ChatDisabled/discord-commits@main
with:
id: ${{ secrets.DISCORD_WEBHOOK_ID }}
token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

3 changes: 0 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ inputs:
token:
description: The Discord Webhook Token
required: true
threadId:
description: If you want to send the embed message in a specific thread
required: false

runs:
using: 'node16'
Expand Down
135 changes: 60 additions & 75 deletions src/discord.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,70 @@
const { MessageEmbed, WebhookClient } = require('discord.js')
const MAX_MESSAGE_LENGTH = 40
const MAX_MESSAGE_LENGTH = 72

module.exports.send = (id, token, repo, branch, url, commits, size, threadId) =>
module.exports.send = (id, token, repo, url, commits, size, pusher) =>
new Promise((resolve, reject) => {
let client
console.log('Preparing Webhook...')
try {
client = new WebhookClient({
id: id,
token: token,
})

if (threadId) {
if (isNaN(threadId)) {
throw new Error('threadId is not a number')
}
console.log('Found thread ID')
client
.send({
username: repo,
embeds: [createEmbed(repo, branch, url, commits, size)],
threadId: threadId,
})
.then(() => {
console.log('Successfully sent the message!')
resolve()
}, reject)
} else {
client
.send({
username: repo,
embeds: [createEmbed(repo, branch, url, commits, size)],
})
.then(() => {
console.log('Successfully sent the message!')
resolve()
}, reject)
}
} catch (error) {
console.log('Error creating Webhook')
reject(error.message)
return
}
let client
const username = repo.replace(/(discord)/gi, '******')
console.log('Preparing Webhook...')
try {
client = new WebhookClient({
id: id,
token: token,
})
client
.send({
username: username,
embeds: [createEmbed(url, commits, size, pusher)],
})
.then(() => {
console.log('Successfully sent the message!')
resolve()
}, reject)
} catch (error) {
console.log('Error creating Webhook')
reject(error.message)
return
}
})

function createEmbed(repo, branch, url, commits, size) {
console.log('Constructing Embed...')
console.log('Commits :')
console.log(commits)
if (!commits) {
console.log('No commits, skipping...')
return
}
const latest = commits[0]
return new MessageEmbed()
.setColor(0xF1E542)
.setAuthor({
name: `⚡ ${latest.author.username} pushed ${size} commit${size === 1 ? '' : 's'}`,
iconURL: `https://github.com/${latest.author.username}.png?size=32`,
url: url
})
.setDescription(`${getChangeLog(commits, size)}`)
.setTimestamp(Date.parse(latest.timestamp))
function createEmbed(url, commits, size, pusher) {
console.log('Constructing Embed...')
console.log('Commits :')
console.log(commits)
if (!commits) {
console.log('No commits, skipping...')
return
}
const latest = commits[0]
return new MessageEmbed()
.setColor(0xf1e542)
.setAuthor({
name: `⚡ ${pusher} pushed ${size} commit${
size === 1 ? '' : 's'
}`,
iconURL: `https://github.com/${pusher}.png?size=64`,
url: url,
})
.setDescription(`${getChangeLog(commits, size)}`)
.setTimestamp(Date.parse(latest.timestamp))
}

function getChangeLog(commits, size) {
let changelog = ''
for (const i in commits) {
if (i > 7) {
changelog += `+ ${size - i} more...\n`
break
}

const commit = commits[i]
const sha = commit.id.substring(0, 6)
const message =
commit.message.length > MAX_MESSAGE_LENGTH
? commit.message.substring(0, MAX_MESSAGE_LENGTH) + '...'
: commit.message
changelog += `[\`${sha}\`](${commit.url}) — ${message} ([\`${commit.author.username}\`](https://github.com/${commit.author.username}))\n`
let changelog = ''
for (const i in commits) {
if (i > 7) {
changelog += `+ ${size - i} more...\n`
break
}

return changelog
}
const commit = commits[i]
const sha = commit.id.substring(0, 6)
const message =
commit.message.length > MAX_MESSAGE_LENGTH
? commit.message.substring(0, MAX_MESSAGE_LENGTH) + '...'
: commit.message
changelog += `[\`${sha}\`](${commit.url}) — ${message} ([\`${commit.author.username}\`](https://github.com/${commit.author.username}))\n`
}

return changelog
}
57 changes: 31 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
const core = require('@actions/core')
const github = require('@actions/github')

const webhook = require('../src/discord.js')

async function run() {
const payload = github.context.payload
const repository = payload.repository.name
const commits = payload.commits
const size = commits.length
const branch = payload.ref.split('/')[payload.ref.split('/').length - 1]

console.log(`Received payload.`)

console.log(`Received ${commits.length}/${size} commits...`)

if (commits.length === 0) {
console.log(`No commits, skipping...`)
return
}

const id = core.getInput('id')
const token = core.getInput('token')
const threadId = core.getInput('threadId')

webhook
.send(id, token, repository, branch, payload.compare, commits, size, threadId)
.catch((err) => core.setFailed(err.message))
const payload = github.context.payload
const repository = payload.repository.name
const commits = payload.commits
const size = commits.length

console.log(`Received payload.`)

console.log(`Received ${commits.length}/${size} commits...`)

if (commits.length === 0) {
console.log(`No commits, skipping...`)
return
}

const id = core.getInput('id')
const token = core.getInput('token')

webhook
.send(
id,
token,
repository,
payload.compare,
commits,
size,
payload.pusher.name
)
.catch((err) => core.setFailed(err.message))
}

try {
run()
run()
} catch (error) {
core.setFailed(error.message)
}
core.setFailed(error.message)
}
Loading