Compare commits
23 Commits
2264b76d9c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| d367abe451 | |||
| f5910f763b | |||
| f2da39f0db | |||
| 84f4d2eef8 | |||
| 9297bed50e | |||
| 6660035fa3 | |||
| bd769b0eb0 | |||
| f9185e0f21 | |||
| 3556ac3d76 | |||
| 25ba33d43a | |||
| 896a7413fe | |||
| 1c572a30a0 | |||
| e5cd76c0ba | |||
| 484412b34e | |||
| 9409707329 | |||
| aafc74d876 | |||
| 408411117c | |||
| 22f19a2ced | |||
| ac2174d365 | |||
| 4ef74b38eb | |||
| 214aec2e73 | |||
| f431f1c5dc | |||
| 0550e4a431 |
@@ -9,7 +9,7 @@ pipeline {
|
||||
parameters {
|
||||
string(
|
||||
name: 'JENKINS_BASE_URL',
|
||||
defaultValue: 'https://openbokeron.org/jenkins',
|
||||
defaultValue: 'https://openbokeron.uma.es/jenkins',
|
||||
description: 'Base URL del Jenkins objetivo'
|
||||
)
|
||||
string(
|
||||
@@ -36,32 +36,6 @@ pipeline {
|
||||
|
||||
stages {
|
||||
|
||||
/* =========================
|
||||
TESTS
|
||||
========================= */
|
||||
|
||||
stage('Backend: test (main)') {
|
||||
agent {
|
||||
docker {
|
||||
image 'python:3.11-slim'
|
||||
args '-u root'
|
||||
}
|
||||
}
|
||||
steps {
|
||||
dir('backend') {
|
||||
sh '''
|
||||
set -e
|
||||
python -m venv .venv
|
||||
. .venv/bin/activate
|
||||
pip install --upgrade pip
|
||||
pip install -r requirements-dev.txt
|
||||
pytest
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* =========================
|
||||
DOCKER BUILD
|
||||
========================= */
|
||||
@@ -100,6 +74,22 @@ pipeline {
|
||||
}
|
||||
}
|
||||
|
||||
/* =========================
|
||||
TRIVY
|
||||
========================= */
|
||||
|
||||
stage('Security: Trivy job') {
|
||||
agent any
|
||||
steps {
|
||||
build job: 'Trivy Scanner',
|
||||
parameters: [
|
||||
string(name: 'APP_VERSION', value: "${APP_VERSION}")
|
||||
],
|
||||
propagate: true,
|
||||
wait: true
|
||||
}
|
||||
}
|
||||
|
||||
/* =========================
|
||||
DEPLOY
|
||||
========================= */
|
||||
|
||||
@@ -57,7 +57,7 @@ pipeline {
|
||||
pip install --upgrade pip
|
||||
pip install -r requirements-dev.txt
|
||||
ruff check app tests
|
||||
pytest -o cache_dir="$WORKSPACE/.cache/pytest"
|
||||
pytest -o cache_dir="$WORKSPACE/.cache/pytest" --junitxml=pytest.xml
|
||||
'''
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,7 @@ pipeline {
|
||||
mkdir -p "$NPM_CONFIG_CACHE"
|
||||
npm install --no-progress --no-audit --prefer-offline
|
||||
npm run check
|
||||
mkdir -p test-results
|
||||
npm test
|
||||
npm run build
|
||||
'''
|
||||
@@ -97,9 +98,70 @@ pipeline {
|
||||
always {
|
||||
script {
|
||||
node {
|
||||
junit testResults: 'backend/pytest.xml', allowEmptyResults: true
|
||||
junit testResults: 'frontend/test-results/junit.xml', allowEmptyResults: true
|
||||
|
||||
if (env.CHANGE_ID) {
|
||||
def giteaBase = 'https://openbokeron.uma.es'
|
||||
def owner = 'OpenBokeron'
|
||||
def repo = 'TallerCiCd'
|
||||
def pr = env.CHANGE_ID
|
||||
|
||||
def result = currentBuild.currentResult
|
||||
def msg = ""
|
||||
|
||||
if (result == "SUCCESS") {
|
||||
msg = """
|
||||
✅ Todo en orden, camarada.
|
||||
|
||||
- URL: ${env.BUILD_URL}
|
||||
""".stripIndent().trim()
|
||||
}
|
||||
else if (result == "FAILURE") {
|
||||
msg = """
|
||||
❌ Alto ahí. ¿Qué clase de crímenes de guerra has cometido en esta PR?
|
||||
|
||||
- URL: ${env.BUILD_URL}
|
||||
""".stripIndent().trim()
|
||||
}
|
||||
else {
|
||||
msg = """
|
||||
⚠️ Algo ha pasado, no sé si es culpa tuyao mía. Por probabilidad, pensaré que la has pifiado tú.
|
||||
|
||||
- URL: ${env.BUILD_URL}
|
||||
""".stripIndent().trim()
|
||||
}
|
||||
|
||||
def commentsUrl = "${giteaBase}/gitea/api/v1/repos/${owner}/${repo}/issues/${pr}/comments"
|
||||
|
||||
withCredentials([usernamePassword(credentialsId: 'jenkins-bot-api',
|
||||
usernameVariable: 'GITEA_USER',
|
||||
passwordVariable: 'GITEA_TOKEN')]) {
|
||||
|
||||
def body = msg.replace('\\', '\\\\').replace('"','\\"').replace('\n','\\n')
|
||||
|
||||
// Avoid interpolation of secret variables (https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation)
|
||||
withEnv(["GITEA_COMMENTS_URL=${commentsUrl}", "GITEA_BODY=${body}"]) {
|
||||
sh(label: 'Comentar en PR (Gitea)', script: '''
|
||||
curl -sS -X POST \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
--data-binary @- \
|
||||
"$GITEA_COMMENTS_URL" <<JSON
|
||||
{"body":"$GITEA_BODY"}
|
||||
JSON
|
||||
''')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo "No es build de PR (CHANGE_ID vacío); no comento."
|
||||
}
|
||||
|
||||
cleanWs()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ def health():
|
||||
"commit": settings.git_commit,
|
||||
"build": settings.build_number,
|
||||
"author": settings.commit_author,
|
||||
"uptime_seconds": uptime()
|
||||
"uptime_seconds": uptime(),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import base64
|
||||
from typing import Dict, List
|
||||
import html
|
||||
import re
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
@@ -26,25 +28,74 @@ def _sort_builds(builds: List[Dict]) -> List[Dict]:
|
||||
return sorted(builds, key=lambda build: build.get("number", 0), reverse=True)
|
||||
|
||||
|
||||
def normalize_build(build: Dict) -> Dict:
|
||||
changes = build.get("changeSets", [])
|
||||
def _extract_commits(build: Dict) -> List[Dict]:
|
||||
commits = []
|
||||
|
||||
for cs in changes:
|
||||
for cs in build.get("changeSets", []):
|
||||
for item in cs.get("items", []):
|
||||
commits.append({
|
||||
commits.append(
|
||||
{
|
||||
"commit": item.get("commitId", "")[:7],
|
||||
"message": item.get("msg", ""),
|
||||
"author": item.get("author", {}).get("fullName", "unknown"),
|
||||
})
|
||||
}
|
||||
)
|
||||
return commits
|
||||
|
||||
|
||||
def _extract_trigger(build: Dict) -> str:
|
||||
for action in build.get("actions", []):
|
||||
for cause in action.get("causes", []):
|
||||
description = cause.get("shortDescription")
|
||||
if description:
|
||||
return html.unescape(description).strip()
|
||||
return ""
|
||||
|
||||
|
||||
def _summarize_trigger(trigger: str) -> Tuple[str, str, str]:
|
||||
if not trigger:
|
||||
return "", "", ""
|
||||
|
||||
pr_url = ""
|
||||
for line in trigger.splitlines():
|
||||
if line.strip().lower().startswith("reviewed-on:"):
|
||||
pr_url = line.split(":", 1)[-1].strip()
|
||||
break
|
||||
|
||||
author_match = re.search(r"\(([^)]+)\)\.", trigger)
|
||||
author = author_match.group(1).strip() if author_match else ""
|
||||
|
||||
pr_match = re.search(r"#(\d+)", trigger)
|
||||
title_match = re.search(r"Merge pull request '([^']+)'", trigger)
|
||||
|
||||
if pr_match:
|
||||
pr_number = pr_match.group(1)
|
||||
title = title_match.group(1).strip() if title_match else ""
|
||||
if title:
|
||||
return f"Merge PR #{pr_number} · {title}", pr_url, author
|
||||
return f"Merge PR #{pr_number}", pr_url, author
|
||||
|
||||
return trigger.splitlines()[0].strip(), pr_url, author
|
||||
|
||||
|
||||
def normalize_build(build: Dict) -> Dict:
|
||||
commits = _extract_commits(build)
|
||||
trigger = _extract_trigger(build)
|
||||
trigger_label, trigger_url, trigger_author = _summarize_trigger(trigger)
|
||||
status = (build.get("result") or "RUNNING").lower()
|
||||
if build.get("building"):
|
||||
status = "running"
|
||||
|
||||
return {
|
||||
"number": build.get("number"),
|
||||
"status": (build.get("result") or "RUNNING").lower(),
|
||||
"status": status,
|
||||
"finished_at": build.get("timestamp"),
|
||||
"duration_seconds": build.get("duration", 0) // 1000,
|
||||
"url": build.get("url"),
|
||||
"commits": commits,
|
||||
"trigger": trigger,
|
||||
"trigger_label": trigger_label,
|
||||
"trigger_url": trigger_url,
|
||||
"trigger_author": trigger_author,
|
||||
}
|
||||
|
||||
|
||||
@@ -61,15 +112,17 @@ def fetch_builds(limit: int = 5) -> List[Dict]:
|
||||
raise ValueError("JENKINS_JOB_NAME not configured")
|
||||
url = (
|
||||
f"{settings.jenkins_base_url}/job/{settings.jenkins_job_name}/api/json"
|
||||
"?tree=builds[number,url,result,timestamp,duration,"
|
||||
"changesets[items[commitId,msg,author[fullName]]]]"
|
||||
"?tree=builds[number,url,result,timestamp,duration,building,"
|
||||
"changeSets[items[commitId,msg,author[fullName]]],"
|
||||
"actions[causes[shortDescription]]]"
|
||||
)
|
||||
|
||||
resp = requests.get(url, headers=_auth_header(), timeout=5)
|
||||
resp.raise_for_status()
|
||||
|
||||
builds = resp.json().get("builds", [])
|
||||
return builds[:limit]
|
||||
return _sort_builds(builds)[:limit]
|
||||
|
||||
|
||||
def build_history() -> Dict:
|
||||
"""Return Jenkins build history data."""
|
||||
|
||||
@@ -54,6 +54,7 @@ def _pick_mains(count: int = ITEMS_PER_SECTION) -> List[str]:
|
||||
random.shuffle(mains)
|
||||
return mains
|
||||
|
||||
|
||||
def _pick_garnish() -> List[str]:
|
||||
garnish_options = MENU_SOURCE["mains"]["garnish"]
|
||||
|
||||
@@ -69,6 +70,7 @@ def _build_alternative() -> Dict:
|
||||
"note": alternative.get("note", ""),
|
||||
}
|
||||
|
||||
|
||||
def build_menu(items_per_section: int = ITEMS_PER_SECTION) -> Dict:
|
||||
today = datetime.now()
|
||||
|
||||
|
||||
@@ -24,10 +24,7 @@ class RuntimeConfig:
|
||||
git_commit: str = os.getenv("GIT_COMMIT", "local")
|
||||
build_number: str = os.getenv("BUILD_NUMBER", "-")
|
||||
commit_author: str = os.getenv("COMMIT_AUTHOR", "local")
|
||||
jenkins_base_url: str = os.getenv(
|
||||
"JENKINS_BASE_URL",
|
||||
"http://localhost:8080"
|
||||
).rstrip("/")
|
||||
jenkins_base_url: str = os.getenv("JENKINS_BASE_URL", "http://localhost:8080").rstrip("/")
|
||||
jenkins_job_name: str = os.getenv("JENKINS_JOB_NAME", "")
|
||||
jenkins_user: str = os.getenv("JENKINS_USER", "")
|
||||
jenkins_token: str = os.getenv("JENKINS_TOKEN", "")
|
||||
|
||||
@@ -61,6 +61,15 @@ def test_build_history(monkeypatch):
|
||||
"timestamp": 1719992400000,
|
||||
"duration": 75000,
|
||||
"url": "http://jenkins.local/job/demo/205",
|
||||
"actions": [
|
||||
{
|
||||
"causes": [
|
||||
{
|
||||
"shortDescription": "Triggered by Merge pull request #37",
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"changeSets": [
|
||||
{
|
||||
"items": [
|
||||
@@ -79,6 +88,7 @@ def test_build_history(monkeypatch):
|
||||
"timestamp": 1719988800000,
|
||||
"duration": 1500,
|
||||
"url": "http://jenkins.local/job/demo/204",
|
||||
"actions": [],
|
||||
"changeSets": [],
|
||||
},
|
||||
]
|
||||
@@ -101,12 +111,20 @@ def test_build_history(monkeypatch):
|
||||
assert first["commits"] == [
|
||||
{"commit": "9ac3f91", "message": "Anade la API de Jenkins", "author": "Dev One"}
|
||||
]
|
||||
assert first["trigger"] == "Triggered by Merge pull request #37"
|
||||
assert first["trigger_label"] == "Merge PR #37"
|
||||
assert first["trigger_url"] == ""
|
||||
assert first["trigger_author"] == ""
|
||||
|
||||
second = builds[1]
|
||||
assert second["number"] == 204
|
||||
assert second["status"] == "running"
|
||||
assert second["duration_seconds"] == 1
|
||||
assert second["commits"] == []
|
||||
assert second["trigger"] == ""
|
||||
assert second["trigger_label"] == ""
|
||||
assert second["trigger_url"] == ""
|
||||
assert second["trigger_author"] == ""
|
||||
|
||||
|
||||
def test_build_history_error_returns_empty(monkeypatch):
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"check": "svelte-check",
|
||||
"test": "vitest"
|
||||
"test": "vitest run --reporter=default --reporter=junit --outputFile=./test-results/junit.xml"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sveltejs/vite-plugin-svelte": "^3.0.1",
|
||||
|
||||
@@ -377,12 +377,25 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
<span class={`status-dot ${build.status}`}></span>
|
||||
<span class="summary-title">#{build.number}</span>
|
||||
|
||||
{#if build.commits.length === 0}
|
||||
<span class="muted">Ejecución manual</span>
|
||||
{#if build.trigger_label || build.trigger}
|
||||
<div class="summary-trigger">
|
||||
{#if build.trigger_url}
|
||||
<a href={build.trigger_url} target="_blank" rel="noreferrer">
|
||||
{build.trigger_label || build.trigger}
|
||||
</a>
|
||||
{:else}
|
||||
<span>{build.trigger_label || build.trigger}</span>
|
||||
{/if}
|
||||
{#if build.trigger_author}
|
||||
<span class="trigger-author">{build.trigger_author}</span>
|
||||
{/if}
|
||||
</div>
|
||||
{:else if build.commits.length > 0}
|
||||
<span class="summary-commit">
|
||||
{build.commits[0].commit} · {build.commits[0].author}
|
||||
</span>
|
||||
{:else}
|
||||
<span class="muted">Ejecución manual</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -394,7 +407,36 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
</summary>
|
||||
|
||||
<div class="history-body">
|
||||
{#if build.trigger_label || build.trigger}
|
||||
<p class="history-row">
|
||||
<span>Disparo</span>
|
||||
<span>
|
||||
{#if build.trigger_url}
|
||||
<a
|
||||
class="history-link"
|
||||
href={build.trigger_url}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{build.trigger_label || build.trigger}
|
||||
</a>
|
||||
{:else}
|
||||
{build.trigger_label || build.trigger}
|
||||
{/if}
|
||||
{#if build.trigger_author}
|
||||
<span class="trigger-author">{build.trigger_author}</span>
|
||||
{/if}
|
||||
</span>
|
||||
</p>
|
||||
{/if}
|
||||
|
||||
{#if build.commits.length > 0}
|
||||
<p class="history-row">
|
||||
<span>Commit</span>
|
||||
<span class="mono">
|
||||
{build.commits[0].commit} · {build.commits[0].author}
|
||||
</span>
|
||||
</p>
|
||||
<p class="history-row">
|
||||
<span>Mensaje</span>
|
||||
<span>{build.commits[0].message}</span>
|
||||
@@ -408,6 +450,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
{#if build.status === 'failure'}
|
||||
<p class="history-message danger">Build fallida</p>
|
||||
{:else if build.status === 'running'}
|
||||
<p class="history-message running">En curso</p>
|
||||
{:else}
|
||||
<p class="history-message success">Correcto</p>
|
||||
{/if}
|
||||
|
||||
@@ -697,6 +697,25 @@ li {
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.summary-trigger {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
flex-wrap: wrap;
|
||||
color: #fbbf24;
|
||||
font-weight: 700;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.summary-trigger a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.summary-trigger a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.summary-meta {
|
||||
display: flex;
|
||||
gap: 0.6rem;
|
||||
@@ -723,6 +742,11 @@ li {
|
||||
box-shadow: 0 0 8px rgba(248, 113, 113, 0.5);
|
||||
}
|
||||
|
||||
.status-dot.running {
|
||||
background: #facc15;
|
||||
box-shadow: 0 0 8px rgba(250, 204, 21, 0.5);
|
||||
}
|
||||
|
||||
.history-body {
|
||||
padding: 0.75rem 0.9rem 0.9rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.05);
|
||||
@@ -743,6 +767,21 @@ li {
|
||||
color: #cbd5f5;
|
||||
}
|
||||
|
||||
.history-link {
|
||||
color: #fbbf24;
|
||||
text-decoration: none;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.history-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #94a3b8;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.history-message {
|
||||
margin: 0.2rem 0 0;
|
||||
background: rgba(248, 113, 113, 0.12);
|
||||
@@ -758,6 +797,12 @@ li {
|
||||
color: #bbf7d0;
|
||||
}
|
||||
|
||||
.history-message.running {
|
||||
background: rgba(250, 204, 21, 0.16);
|
||||
border-color: rgba(250, 204, 21, 0.5);
|
||||
color: #fef9c3;
|
||||
}
|
||||
|
||||
.history-item[open] {
|
||||
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
|
||||
}
|
||||
@@ -770,6 +815,10 @@ li {
|
||||
border-color: rgba(248, 113, 113, 0.35);
|
||||
}
|
||||
|
||||
.history-item.running {
|
||||
border-color: rgba(250, 204, 21, 0.35);
|
||||
}
|
||||
|
||||
.chip.danger {
|
||||
background: rgba(248, 113, 113, 0.2);
|
||||
color: #fecdd3;
|
||||
@@ -820,3 +869,15 @@ li {
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.85;
|
||||
}
|
||||
.trigger-author {
|
||||
padding: 0.1rem 0.4rem;
|
||||
border-radius: 999px;
|
||||
background: rgba(148, 163, 184, 0.16);
|
||||
color: #cbd5f5;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.history-row .trigger-author {
|
||||
margin-left: 0.35rem;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user