const Gitea = require ( './gitea.js' ) // We'll need some functions to manage our gitea installation. Check labels, post comments etc.
const Deck = require ( './deck.js' ) // Same goes for deck.
const express = require ( 'express' )
const bodyParser = require ( 'body-parser' )
const axios = require ( 'axios' )
const app = express ( )
// Hardcoded for now...
const gitea = new Gitea ( "https://yourgiteainstall.com/api/v1" , 'apikey' )
const deck = new Deck ( "https://yournextcloudinstall.com" , 'nextcloudusername' , 'password or apppassword' , board - integer , deckStack - integer )
// autoparse JSON and allow CORS.
app . use ( bodyParser . json ( ) )
app . use ( function ( req , res , next ) {
res . header ( "Access-Control-Allow-Origin" , "*" )
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept" )
res . header ( 'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE, OPTIONS, HEAD' )
next ( )
} )
// Create label Gitea inside deck, if it doesn't exist that is.
deck . createLabel ( "Gitea" , "609A21" )
// gitea only POSTs and only to a single URL. We'll need to parse json for more info on what's happening
app . post ( "/" , ( req , res ) => {
// Let's make our console a little more readable :)
console . log ( '\n ----- request -----' )
// Just for convenience
let repo = req . body . repository . full _name
let issue = req . body . issue . number
let issueUrl = ` https://git.thijsdevries.net/ ${ repo } /issues/ ${ issue } `
let action = req . body . action
let title = req . body . issue . title
let tracking = gitea . trackingCode ( repo , issue ) // We MD5 all the issue-specific data that won't change during the lifetime of this issue. That generated sum is then used to track the issue on deck.
gitea . createLabel ( 'Deck' , "#0082C9" , repo ) // Double check existace of issue label, if not; create.
console . log ( ` ${ req . body . action } | ${ tracking } ` )
// Checl if somging label-related changed. The existance of the 'Deck' label wil dictate the existance of the Deck card.
if ( action == "label_updated" || action == "opened" || action == "label_cleared" ) {
gitea . labelStatus ( 'Deck' , repo , issue ) . then ( ( set ) => {
if ( set ) {
deck . createCard ( ` [ ${ repo } ] # ${ issue } : ${ title } ` , ` # [Open Issue]( ${ issueUrl } ) ` , tracking )
. then ( ( result ) => {
if ( result ) gitea . comment ( ` I've added a card to Deck, and I'm tracking this issue for changes :-) ` , repo , issue )
} )
. catch ( e => console . log ( e ) )
} else {
deck . deleteCard ( tracking ) . then ( ( result ) => {
if ( result ) gitea . comment ( ` I've removed the corresponding Deck card, and I'll stop tracking this issue. Bye! ` , repo , issue )
} )
}
} )
}
// If closed, Deck card should be archived.
else if ( action == 'closed' ) {
deck . cardSet ( tracking , "archive" , true )
}
else if ( action == "reopened" ) {
deck . cardSet ( tracking , "archive" , false ) . then ( ( result ) => {
if ( result ) if ( result ) {
gitea . comment ( ` I've dug up that archived Deck card for you. You're welcome :) ` , repo , issue )
}
else {
deck . createCard ( ` [ ${ repo } ] # ${ issue } : ${ title } ` , ` # [Open Issue]( ${ issueUrl } ) ` , tracking )
. then ( ( result ) => {
if ( result ) gitea . comment ( ` Seems like you've recycled this Deck card. I'll re-add it. ` , repo , issue )
} )
}
} )
}
else {
gitea . labelStatus ( 'Deck' , repo , issue ) . then ( ( set ) => {
if ( set ) deck . createCard ( ` [ ${ repo } ] # ${ issue } : ${ title } ` , ` # [Open Issue]( ${ issueUrl } ) ` , tracking )
. then ( ( result ) => {
if ( result ) gitea . comment ( ` Looks like someone acidentally removed or archived the corresponding Deck card! If you're done, just close this issue and I'll archive the card. If you don't want it to show up at al, remove the 'Deck' label from this issue and I'll delete it.
I ' ve re - added the card to Deck . ` , repo, issue)
} )
. catch ( e => console . log ( e ) )
} )
}
res . send ( "okay" ) // generate some feedback for gitea
} )
app . listen ( 8080 , ( ) => console . log ( ` Listening on :80 ` ) )