Implement attachment embeds found in issues and comments.

master
sigonasr2 1 year ago
parent 6b4fcf387f
commit 1140332aed
  1. 130
      server.js

@ -197,20 +197,118 @@ app.post('/addItem',async(req,res)=>{
})
})
function ConvertIssues(str,repoURL){
var split=str.split("Issue #");
for(var i=0;i<split.length-1;i++){
split[i]+="[Issue #"
split[i+1]=split[i+1].at(0)+"]("+repoURL+"/issues/"+split[i+1].at(0)+")"+split[i+1].slice(-1)
}
var split2=split.join("").split("Issue#")
for(var i=0;i<split2.length-1;i++){
split2[i]+="[Issue #"
split2[i+1]=split2[i+1].at(0)+"]("+repoURL+"/issues/"+split2[i+1].at(0)+")"+split2[i+1].slice(-1)
}
return split2.join("");
}
function ParseMentions(str){
//@quapsel: 155789951571591168
//@sigonasr2: 176012829076226048
return str.replaceAll("@Quapsel","<@155789951571591168>").replaceAll("@sigonasr2","<@176012829076226048>")
}
var embedItems=[];
function ParseAttachments(str){
var stage=0;
var backTickRepeatCount=0;
var backTicksEnabled=false;
var isImage=false;
var accumulatedLink="";
for(var i=0;i<str.length;i++){
//If at any time we find ```, we continue until they close.
if(str[i]=='`'){
backTickRepeatCount++;
if(backTickRepeatCount==3){
backTickRepeatCount=0;
backTicksEnabled=!backTicksEnabled;
}
continue;
} else {
backTickRepeatCount=0;
}
if(backTicksEnabled){
continue; //As long as we are in back tick mode, we don't care what is posted.
}
switch(stage){
case 0:{
//In stage 0 we look for [.
if(str[i]=='['){
if(i>0&&str[i-1]=='!'){
isImage=true;
} else {
isImage=false;
}
stage++;
}
}break;
case 1:{
//In stage 1 we look for ].
if(str[i]==']'){
stage++;
}
}break;
case 2:{
//In stage 2 the next character should be (. If it's not, reset.
if(str[i]=='('){
stage++;
} else {
stage=0;
}
}break;
case 3:{
//In stage 3, we accumulate what's inside and look for ).
if(str[i]==')'){
//We're done! We have a new embed.
if(accumulatedLink.startsWith("http")){
if(isImage){
embedItems=[...embedItems,{image:{url:accumulatedLink}}]
} else {
embedItems=[...embedItems,{url:accumulatedLink}]
}
}else{
if(isImage){
embedItems=[...embedItems,{image:{url:"http://sig.projectdivar.com/"+accumulatedLink}}]
} else {
embedItems=[...embedItems,{url:"http://sig.projectdivar.com/"+accumulatedLink}]
}
}
accumulatedLink=""
isImage=false;
stage=0;
break;
} else {
accumulatedLink+=str[i];
}
}break;
}
}
return str
}
app.post("/postUpdate",(req,res)=>{
res.status(200).send("Thank you!")
if(req.body.action){
switch(req.body.action){
case "opened":{
axios.post(process.env.GITEA_WEBHOOK,{embeds:[{
embedItems=[{
author:{
name:"Issue #"+req.body.number+" opened by "+req.body.issue.user.username,
icon_url:req.body.issue.user.avatar_url
},
color:11731199,
description:"**"+req.body.issue.title+":**\n\n"+req.body.issue.body+"\n\n[Link to Issue]("+req.body.issue.html_url+")",
}]
})
description:"**"+req.body.issue.title+":**\n\n"+ParseAttachments(ParseMentions(req.body.issue.body))+"\n\n[Link to Issue]("+req.body.issue.html_url+")",
},...embedItems];
axios.post(process.env.GITEA_WEBHOOK,{embeds:embedItems})
}break;
case "reopened":{
axios.post(process.env.GITEA_WEBHOOK,{embeds:[{
@ -268,20 +366,32 @@ app.post("/postUpdate",(req,res)=>{
icon_url:req.body.comment.user.avatar_url
},
color:11731199,
description:"__**"+req.body.issue.title+"**__\nCommented by **"+req.body.comment.user.username+":**\n"+req.body.comment.body+"\n\n[Link to Comment]("+req.body.comment.html_url+")",
description:"__**"+req.body.issue.title+"**__\nCommented by **"+req.body.comment.user.username+":**\n"+ParseAttachments(ParseMentions(req.body.comment.body))+"\n\n[Link to Comment]("+req.body.comment.html_url+")",
}]
})
}break;
case "edited":{
axios.post(process.env.GITEA_WEBHOOK,{embeds:[{
if(req.body.comment){
embedItems=[{
author:{
name:"Comment edited on Issue #"+req.body.issue.number,
icon_url:req.body.comment.user.avatar_url
},
color:11731199,
description:"__**"+req.body.issue.title+"**__\nCommented by **"+req.body.comment.user.username+":**\n"+req.body.comment.body+"\n\n[Link to Comment]("+req.body.comment.html_url+")",
}]
})
description:"__**"+req.body.issue.title+"**__\nCommented by **"+req.body.comment.user.username+":**\n"+ParseAttachments(ParseMentions(req.body.comment.body))+"\n\n[Link to Comment]("+req.body.comment.html_url+")",
},...embedItems]
axios.post(process.env.GITEA_WEBHOOK,{embeds:embedItems})
} else {
embedItems=[{
author:{
name:"Edit on Issue #"+req.body.issue.number,
icon_url:req.body.issue.user.avatar_url
},
color:11731199,
description:"__**"+req.body.issue.title+"**__\nEdited by **"+req.body.issue.user.username+":**\n"+ParseAttachments(ParseMentions(req.body.issue.body))+"\n\n[Link to Issue]("+req.body.issue.html_url+")",
},...embedItems]
axios.post(process.env.GITEA_WEBHOOK,{embeds:embedItems})
}
}break;
case "deleted":{
axios.post(process.env.GITEA_WEBHOOK,{embeds:[{
@ -321,7 +431,7 @@ app.post("/postUpdate",(req,res)=>{
icon_url:"http://sig.projectdivar.com/assets/update.png"
},
color:17663,
description:"**Commit ID "+commit.id+"**\n\n"+commit.message+"\n"+(filesAdded>0?"\n"+filesAdded+" file"+(filesAdded==1?"":"s")+" added":"")+(filesRemoved>0?"\n"+filesRemoved+" file"+(filesRemoved==1?"":"s")+" removed":"")+(filesModified>0?"\n"+filesModified+" file"+(filesModified==1?"":"s")+" modified":"")+"\n\n[Link to Commit]("+commit.url+")",
description:"**Commit ID "+commit.id+"**\n\n"+ConvertIssues(commit.message,req.body.repository.html_url)+"\n"+(filesAdded>0?"\n"+filesAdded+" file"+(filesAdded==1?"":"s")+" added":"")+(filesRemoved>0?"\n"+filesRemoved+" file"+(filesRemoved==1?"":"s")+" removed":"")+(filesModified>0?"\n"+filesModified+" file"+(filesModified==1?"":"s")+" modified":"")+"\n\n[Link to Commit]("+commit.url+")",
}]
})
}

Loading…
Cancel
Save