Sichert den bisher uncommitteten Produktionsstand des Kundenbereich-Servers, damit kuenftige Deploys (git reset --hard) nichts mehr verwerfen. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
124 lines
3.4 KiB
JavaScript
124 lines
3.4 KiB
JavaScript
import { config } from '../config.js'
|
|
import {
|
|
listDocuments,
|
|
getDocument,
|
|
createDocument,
|
|
Query,
|
|
ID,
|
|
} from './appwriteAdmin.js'
|
|
|
|
const WSID_START = 100000
|
|
|
|
function formatToday() {
|
|
const d = new Date()
|
|
const y = d.getFullYear()
|
|
const m = String(d.getMonth() + 1).padStart(2, '0')
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
return `${y}${m}${day}`
|
|
}
|
|
|
|
function formatTimeNow() {
|
|
const d = new Date()
|
|
return `${String(d.getHours()).padStart(2, '0')}${String(d.getMinutes()).padStart(2, '0')}`
|
|
}
|
|
|
|
async function generateNextWsid() {
|
|
const docs = await listDocuments(config.collections.worksheets, [
|
|
Query.orderDesc('wsid'),
|
|
Query.limit(1),
|
|
])
|
|
const highest = docs[0]?.wsid ? parseInt(docs[0].wsid, 10) : WSID_START - 1
|
|
return String(Math.max(WSID_START, highest + 1))
|
|
}
|
|
|
|
async function gitWorksheetExists(woid, commitSha) {
|
|
if (!commitSha) return false
|
|
const docs = await listDocuments(config.collections.worksheets, [
|
|
Query.equal('woid', woid),
|
|
Query.limit(100),
|
|
])
|
|
const marker = `Commit: ${commitSha.slice(0, 7)}`
|
|
return docs.some((doc) => doc.details?.includes(marker) || doc.details?.includes(commitSha))
|
|
}
|
|
|
|
function formatCommitLines(commits, repoFullName) {
|
|
if (!Array.isArray(commits) || commits.length === 0) {
|
|
return '- (keine Commit-Details im Webhook)'
|
|
}
|
|
|
|
return commits
|
|
.map((commit) => {
|
|
const sha = (commit.id || commit.sha || '').slice(0, 7)
|
|
const message = (commit.message || '').split('\n')[0]
|
|
const author = commit.author?.name || commit.committer?.name || 'Unbekannt'
|
|
const url = commit.url || `${config.gitea.baseUrl}/${repoFullName}/commit/${commit.id || commit.sha}`
|
|
return `- [${sha}](${url}): ${message} (${author})`
|
|
})
|
|
.join('\n')
|
|
}
|
|
|
|
export async function createGitPushWorksheet({
|
|
ticketId,
|
|
repoFullName,
|
|
branch,
|
|
commits = [],
|
|
pusher,
|
|
commitSha,
|
|
}) {
|
|
if (!ticketId) return null
|
|
|
|
let workorder
|
|
try {
|
|
workorder = await getDocument(config.collections.workorders, ticketId)
|
|
} catch {
|
|
console.warn('[gitWorksheet] Workorder nicht gefunden:', ticketId)
|
|
return null
|
|
}
|
|
|
|
const woid = workorder.woid
|
|
if (!woid) return null
|
|
|
|
if (await gitWorksheetExists(woid, commitSha)) {
|
|
return { skipped: true, reason: 'duplicate_commit' }
|
|
}
|
|
|
|
const wsid = await generateNextWsid()
|
|
const pusherName = pusher?.full_name || pusher?.username || pusher?.login || 'Gitea'
|
|
const latestCommit = commits[commits.length - 1] || commits[0]
|
|
const details = [
|
|
`Git Push auf \`${repoFullName}\` (Branch: ${branch})`,
|
|
`Commit: ${commitSha?.slice(0, 7) || latestCommit?.id?.slice(0, 7) || '?'}`,
|
|
'',
|
|
formatCommitLines(commits, repoFullName),
|
|
].join('\n')
|
|
|
|
const today = formatToday()
|
|
const worksheet = await createDocument(
|
|
config.collections.worksheets,
|
|
{
|
|
wsid,
|
|
woid: String(woid),
|
|
workorderId: ticketId,
|
|
employeeId: 'gitea',
|
|
employeeName: pusherName,
|
|
employeeShort: 'GIT',
|
|
serviceType: 'GIT',
|
|
oldStatus: workorder.status || '',
|
|
newStatus: workorder.status || '',
|
|
oldResponseLevel: workorder.response || '',
|
|
newResponseLevel: workorder.response || '',
|
|
totalTime: 0,
|
|
startDate: today,
|
|
startTime: formatTimeNow(),
|
|
endDate: today,
|
|
endTime: formatTimeNow(),
|
|
details,
|
|
isComment: true,
|
|
createdAt: new Date().toISOString(),
|
|
},
|
|
ID.unique()
|
|
)
|
|
|
|
return { created: true, wsid, worksheetId: worksheet.$id }
|
|
}
|