Skip to content

Reprocess Sensor PR

Reprocess Sensor PR #3

name: Reprocess Sensor PR
on:
workflow_dispatch:
inputs:
pr_number:
description: "PR number to reprocess"
required: true
type: number
jobs:
reprocess_pr:
runs-on: ubuntu-latest
timeout-minutes: 15 # Prevent stuck workflows
steps:
- name: Checkout repository (main branch)
uses: actions/checkout@v4
with:
ref: main
- name: Get PR data
id: pr_data
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ inputs.pr_number }};
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
// Get PR body and branch name
const prBody = pr.data.body;
const branchName = pr.data.head.ref;
// Output the PR body and branch name
core.setOutput('pr_body', prBody);
core.setOutput('branch_name', branchName);
return {prBody, branchName};
- name: Extract details from PR body
id: info
uses: actions/github-script@v7
with:
script: |
const prBody = '${{ steps.pr_data.outputs.pr_body }}';
// More robust pattern matching with regex
const vendorMatch = prBody.match(/### Vendor\s*\n\s*\n\s*(.*?)(\n\s*\n|\n\s*###|$)/s);
const cameraMatch = prBody.match(/### Camera\s*\n\s*\n\s*(.*?)(\n\s*\n|\n\s*###|$)/s);
let vendor = vendorMatch ? vendorMatch[1].trim() : '';
const camera = cameraMatch ? cameraMatch[1].trim() : '';
if (vendor === 'Other') {
const otherVendorMatch = prBody.match(/### Other Vendor\s*\n\s*\n\s*(.*?)(\n\s*\n|\n\s*###|$)/s);
if (otherVendorMatch) vendor = otherVendorMatch[1].trim();
}
core.exportVariable('vendor', vendor);
core.exportVariable('camera', camera);
console.log(`Extracted Vendor: ${vendor}`);
console.log(`Extracted Camera: ${camera}`);
- name: Debug extracted data
run: |
echo "Vendor: ${{ env.vendor }}"
echo "Camera: ${{ env.camera }}"
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"
- name: Process and update JSON with Python
id: python
run: |
pip install -r ./scripts/requirements.txt
python3 ./scripts/update_sensors_and_generate_json.py "${{ steps.pr_data.outputs.pr_body }}" 2> python_error.log || { cat python_error.log; echo "::set-output name=error::$(cat python_error.log)"; exit 1; }
- name: Generate csv
id: csv
run: |
python3 scripts/generate_csv.py 2> csv_error.log || { cat csv_error.log; echo "::set-output name=error::$(cat csv_error.log)"; exit 1; }
- name: Generate yaml
id: yaml
run: |
python3 scripts/generate_yaml.py 2> yaml_error.log || { cat yaml_error.log; echo "::set-output name=error::$(cat yaml_error.log)"; exit 1; }
- name: Generate markdown/documentation
id: markdown
run: |
python3 scripts/generate_markdown.py 2> markdown_error.log || { cat markdown_error.log; echo "::set-output name=error::$(cat markdown_error.log)"; exit 1; }
- name: Configure Git
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Push changes to PR branch
uses: actions/github-script@v7
with:
script: |
const { execSync } = require('child_process');
// Get branch name from PR
const branchName = '${{ steps.pr_data.outputs.branch_name }}';
// Create temporary branch with unique name to avoid conflicts
const timestamp = new Date().getTime();
const tempBranch = `temp_processing_branch_${timestamp}`;
execSync(`git checkout -b ${tempBranch}`);
// Add and commit changes
execSync('git add -A');
try {
execSync('git commit -m "Reprocessed sensor data for ${{ env.camera }} - ${{ env.vendor }}"', { stdio: 'pipe' });
} catch (error) {
// Check if it's a "nothing to commit" message
if (error.message.includes("nothing to commit")) {
console.log("No changes to commit");
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ inputs.pr_number }},
body: "No changes were needed for this PR. All files are already up-to-date."
});
return;
} else {
throw error;
}
}
// Force push to PR branch
try {
execSync(`git push origin ${tempBranch}:${branchName} --force`);
console.log("Successfully pushed changes to PR branch");
} catch (error) {
console.error("Failed to push changes:", error);
process.exit(1);
}
- name: Comment on PR
if: ${{ success() }}
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ inputs.pr_number }},
body: "Reprocessed sensor data successfully using the latest scripts from main branch. All files have been updated in this PR."
});
- name: Comment on PR if script fails
if: ${{ failure() }}
uses: actions/github-script@v7
with:
script: |
const stepOutputs = {
python: `${{ steps.python.outputs.error || 'No specific error message available' }}`,
csv: `${{ steps.csv.outputs.error || 'No specific error message available' }}`,
yaml: `${{ steps.yaml.outputs.error || 'No specific error message available' }}`,
markdown: `${{ steps.markdown.outputs.error || 'No specific error message available' }}`
};
const failedSteps = [];
if ("${{ steps.python.outcome }}" == "failure") {
failedSteps.push(`Error in update_sensors_and_generate_json.py:\n\`\`\`\n${stepOutputs.python}\n\`\`\``);
}
if ("${{ steps.csv.outcome }}" == "failure") {
failedSteps.push(`Error in generate_csv.py:\n\`\`\`\n${stepOutputs.csv}\n\`\`\``);
}
if ("${{ steps.yaml.outcome }}" == "failure") {
failedSteps.push(`Error in generate_yaml.py:\n\`\`\`\n${stepOutputs.yaml}\n\`\`\``);
}
if ("${{ steps.markdown.outcome }}" == "failure") {
failedSteps.push(`Error in generate_markdown.py:\n\`\`\`\n${stepOutputs.markdown}\n\`\`\``);
}
if (failedSteps.length === 0) {
failedSteps.push("An error occurred but specific details could not be determined.");
}
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ inputs.pr_number }},
body: 'There was an error reprocessing the sensor data:\n\n' + failedSteps.join("\n\n")
});