add branch validation to queue
This commit is contained in:
@@ -3,6 +3,8 @@ from flask_login import login_required, current_user
|
||||
from app.models import Job
|
||||
from app import db
|
||||
import json
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
jobs_bp = Blueprint('jobs', __name__, url_prefix='/jobs')
|
||||
|
||||
@@ -15,18 +17,75 @@ def submit():
|
||||
@login_required
|
||||
def submit_step1():
|
||||
branch_name = request.form.get('branch_name')
|
||||
# TODO: Implement branch checkout and scenario detection
|
||||
# For now, return mock scenarios
|
||||
scenarios = [
|
||||
'Scenario_1_Basic_Test',
|
||||
'Scenario_2_Advanced_Test',
|
||||
'Scenario_3_Integration_Test',
|
||||
'Scenario_4_Performance_Test',
|
||||
'Scenario_5_Security_Test'
|
||||
]
|
||||
return render_template('jobs/submit_step2.html', branch_name=branch_name, scenarios=scenarios)
|
||||
|
||||
if not branch_name:
|
||||
flash('Branch name is required', 'error')
|
||||
return redirect(url_for('jobs.submit'))
|
||||
|
||||
# Validate branch exists on remote
|
||||
try:
|
||||
# Get SSH password from environment variable
|
||||
ssh_password = os.environ.get('SSH_PASSWORD', 'default_password')
|
||||
ssh_host = os.environ.get('SSH_HOST', 'remote_host')
|
||||
ssh_user = os.environ.get('SSH_USER', 'asf')
|
||||
|
||||
# First, clone the repository
|
||||
clone_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh clone'"
|
||||
clone_result = subprocess.run(clone_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
||||
|
||||
# Then, checkout the branch
|
||||
checkout_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh checkout {branch_name}'"
|
||||
checkout_result = subprocess.run(checkout_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
||||
|
||||
# Check if checkout was successful (no "fatal:" in output)
|
||||
if "fatal:" in checkout_result.stdout or "fatal:" in checkout_result.stderr or checkout_result.returncode != 0:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Branch "{branch_name}" not found on remote. Please push the branch first.',
|
||||
'output': checkout_result.stdout + checkout_result.stderr
|
||||
})
|
||||
|
||||
# If successful, get available scenarios (mock for now)
|
||||
scenarios = [
|
||||
'Scenario_1_Basic_Test',
|
||||
'Scenario_2_Advanced_Test',
|
||||
'Scenario_3_Integration_Test',
|
||||
'Scenario_4_Performance_Test',
|
||||
'Scenario_5_Security_Test'
|
||||
]
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'scenarios': scenarios,
|
||||
'message': f'Branch "{branch_name}" validated successfully'
|
||||
})
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': 'Branch validation timed out. Please try again.',
|
||||
'output': ''
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Error validating branch: {str(e)}',
|
||||
'output': ''
|
||||
})
|
||||
|
||||
@jobs_bp.route('/submit/step2', methods=['POST'])
|
||||
@jobs_bp.route('/submit/step2-validated', methods=['POST'])
|
||||
@login_required
|
||||
def submit_step2_validated():
|
||||
branch_name = request.form.get('branch_name')
|
||||
scenarios_json = request.form.get('scenarios')
|
||||
|
||||
try:
|
||||
scenarios = json.loads(scenarios_json)
|
||||
except:
|
||||
flash('Invalid scenarios data', 'error')
|
||||
return redirect(url_for('jobs.submit'))
|
||||
|
||||
return render_template('jobs/submit_step2.html', branch_name=branch_name, scenarios=scenarios)
|
||||
@login_required
|
||||
def submit_step2():
|
||||
branch_name = request.form.get('branch_name')
|
||||
|
||||
@@ -540,3 +540,87 @@ body {
|
||||
.empty-state h3 {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
/* Context Menu */
|
||||
.context-menu {
|
||||
display: none;
|
||||
position: absolute;
|
||||
background: white;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
|
||||
z-index: 1000;
|
||||
min-width: 150px;
|
||||
}
|
||||
|
||||
.context-menu-item {
|
||||
padding: 12px 16px;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
transition: background-color 0.2s;
|
||||
}
|
||||
|
||||
.context-menu-item:hover {
|
||||
background: var(--light);
|
||||
}
|
||||
|
||||
.context-menu-item:first-child {
|
||||
border-radius: 8px 8px 0 0;
|
||||
}
|
||||
|
||||
.context-menu-item:last-child {
|
||||
border-radius: 0 0 8px 8px;
|
||||
}
|
||||
|
||||
.context-menu-icon {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
/* Branch Validation */
|
||||
.branch-validation {
|
||||
margin-top: 15px;
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.branch-validation.success {
|
||||
background: #d1fae5;
|
||||
color: #065f46;
|
||||
border: 1px solid #10b981;
|
||||
}
|
||||
|
||||
.branch-validation.error {
|
||||
background: #fee2e2;
|
||||
color: #991b1b;
|
||||
border: 1px solid #ef4444;
|
||||
}
|
||||
|
||||
.branch-validation.loading {
|
||||
background: #fef3c7;
|
||||
color: #92400e;
|
||||
border: 1px solid #f59e0b;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 2px solid #f3f3f3;
|
||||
border-top: 2px solid var(--primary);
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
background: #9ca3af;
|
||||
cursor: not-allowed;
|
||||
opacity: 0.6;
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
<div class="job-list">
|
||||
{% if jobs %}
|
||||
{% for job in jobs %}
|
||||
<div class="job-item" data-job-id="{{ job.id }}" onclick="loadJobDetails({{ job.id }})">
|
||||
<div class="job-item" data-job-id="{{ job.id }}" onclick="loadJobDetails({{ job.id }})" oncontextmenu="showContextMenu(event, {{ job.id }}, '{{ job.status }}')">
|
||||
<div class="job-status-icon">{{ job.get_status_icon() }}</div>
|
||||
<div class="job-info">
|
||||
<h4>Job #{{ job.id }} - {{ job.branch_name }}</h4>
|
||||
@@ -44,7 +44,48 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Context Menu -->
|
||||
<div id="contextMenu" class="context-menu">
|
||||
<div class="context-menu-item" onclick="abortJobFromContext()">
|
||||
<span class="context-menu-icon">⚫</span>
|
||||
Abort Job
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let contextJobId = null;
|
||||
|
||||
function showContextMenu(event, jobId, status) {
|
||||
event.preventDefault();
|
||||
|
||||
// Only show context menu for in_progress jobs
|
||||
if (status !== 'in_progress') {
|
||||
return;
|
||||
}
|
||||
|
||||
contextJobId = jobId;
|
||||
const contextMenu = document.getElementById('contextMenu');
|
||||
|
||||
contextMenu.style.display = 'block';
|
||||
contextMenu.style.left = event.pageX + 'px';
|
||||
contextMenu.style.top = event.pageY + 'px';
|
||||
|
||||
// Hide context menu when clicking elsewhere
|
||||
document.addEventListener('click', hideContextMenu);
|
||||
}
|
||||
|
||||
function hideContextMenu() {
|
||||
document.getElementById('contextMenu').style.display = 'none';
|
||||
document.removeEventListener('click', hideContextMenu);
|
||||
contextJobId = null;
|
||||
}
|
||||
|
||||
function abortJobFromContext() {
|
||||
if (contextJobId) {
|
||||
abortJob(contextJobId);
|
||||
}
|
||||
hideContextMenu();
|
||||
}
|
||||
function loadJobDetails(jobId) {
|
||||
// Mark job as active
|
||||
document.querySelectorAll('.job-item').forEach(item => {
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form method="POST" action="{{ url_for('jobs.submit_step1') }}">
|
||||
<form id="branchForm">
|
||||
<div class="form-group">
|
||||
<label for="branch_name">Git Branch Name</label>
|
||||
<input type="text" id="branch_name" name="branch_name" class="form-control"
|
||||
@@ -37,12 +37,132 @@
|
||||
<small style="color: #6b7280; margin-top: 5px; display: block;">
|
||||
Enter the branch name to analyze available test scenarios
|
||||
</small>
|
||||
|
||||
<div id="branchValidation" class="branch-validation">
|
||||
<div class="loading-spinner"></div>
|
||||
<span id="validationMessage">Validating branch...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-actions">
|
||||
<a href="{{ url_for('dashboard.index') }}" class="btn" style="background: #6b7280; color: white;">Cancel</a>
|
||||
<button type="submit" class="btn btn-primary">Next</button>
|
||||
<button type="submit" id="validateBtn" class="btn btn-primary">Validate Branch</button>
|
||||
<button type="button" id="nextBtn" class="btn btn-primary" style="display: none;" onclick="proceedToStep2()">Next</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let validatedBranch = null;
|
||||
let availableScenarios = [];
|
||||
|
||||
document.getElementById('branchForm').addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
validateBranch();
|
||||
});
|
||||
|
||||
function validateBranch() {
|
||||
const branchName = document.getElementById('branch_name').value.trim();
|
||||
|
||||
if (!branchName) {
|
||||
alert('Please enter a branch name');
|
||||
return;
|
||||
}
|
||||
|
||||
// Show loading state
|
||||
const validation = document.getElementById('branchValidation');
|
||||
const message = document.getElementById('validationMessage');
|
||||
const validateBtn = document.getElementById('validateBtn');
|
||||
const nextBtn = document.getElementById('nextBtn');
|
||||
|
||||
validation.className = 'branch-validation loading';
|
||||
validation.style.display = 'block';
|
||||
message.textContent = 'Validating branch...';
|
||||
validateBtn.disabled = true;
|
||||
nextBtn.style.display = 'none';
|
||||
|
||||
// Make AJAX request
|
||||
fetch('/jobs/submit/step1', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: `branch_name=${encodeURIComponent(branchName)}`
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Success
|
||||
validation.className = 'branch-validation success';
|
||||
message.textContent = data.message;
|
||||
validateBtn.style.display = 'none';
|
||||
nextBtn.style.display = 'inline-block';
|
||||
|
||||
validatedBranch = branchName;
|
||||
availableScenarios = data.scenarios;
|
||||
} else {
|
||||
// Error
|
||||
validation.className = 'branch-validation error';
|
||||
message.textContent = data.error;
|
||||
validateBtn.disabled = false;
|
||||
nextBtn.style.display = 'none';
|
||||
|
||||
validatedBranch = null;
|
||||
availableScenarios = [];
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
// Network error
|
||||
validation.className = 'branch-validation error';
|
||||
message.textContent = 'Network error. Please try again.';
|
||||
validateBtn.disabled = false;
|
||||
nextBtn.style.display = 'none';
|
||||
|
||||
validatedBranch = null;
|
||||
availableScenarios = [];
|
||||
});
|
||||
}
|
||||
|
||||
function proceedToStep2() {
|
||||
if (!validatedBranch || !availableScenarios.length) {
|
||||
alert('Please validate the branch first');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create form to submit to step 2
|
||||
const form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = '/jobs/submit/step2-validated';
|
||||
|
||||
const branchInput = document.createElement('input');
|
||||
branchInput.type = 'hidden';
|
||||
branchInput.name = 'branch_name';
|
||||
branchInput.value = validatedBranch;
|
||||
form.appendChild(branchInput);
|
||||
|
||||
const scenariosInput = document.createElement('input');
|
||||
scenariosInput.type = 'hidden';
|
||||
scenariosInput.name = 'scenarios';
|
||||
scenariosInput.value = JSON.stringify(availableScenarios);
|
||||
form.appendChild(scenariosInput);
|
||||
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
}
|
||||
|
||||
// Reset validation when branch name changes
|
||||
document.getElementById('branch_name').addEventListener('input', function() {
|
||||
const validation = document.getElementById('branchValidation');
|
||||
const validateBtn = document.getElementById('validateBtn');
|
||||
const nextBtn = document.getElementById('nextBtn');
|
||||
|
||||
validation.style.display = 'none';
|
||||
validateBtn.style.display = 'inline-block';
|
||||
validateBtn.disabled = false;
|
||||
nextBtn.style.display = 'none';
|
||||
|
||||
validatedBranch = null;
|
||||
availableScenarios = [];
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user