Skip to content

Commit

Permalink
Parse metadata deletions in beta-test-env workflow
Browse files Browse the repository at this point in the history
Add functionality to parse metadata deletions from log output and report them.

* **Add `parse_metadata_deletions.py` script:**
  - Create a Python script to parse metadata deletions from log output.
  - Define a function to extract metadata type and name pairs and return them as JSON.

* **Modify `.github/workflows/beta-test-env.yml`:**
  - Add a step to run the `parse_metadata_deletions.py` script after the `Deploy to Packaging Org` step.
  - Capture the output of the `cci flow run ci_master` command and pass it to the script.
  - Add steps to report metadata deletions via a Job Summary Report, JSON artifact, and GitHub Check.
  - Handle the scenario where parsing the log output fails, fail the build, and output details of why the parser failed.

* **Add `create_github_check.py` script:**
  - Create a Python script to create a GitHub Check for metadata deletions.
  - Define a function to create a new Check called `Package Metadata Deletions` with a status of `needs attention` if new metadata deletions were found.

* **Modify `Dockerfile`:**
  - Combine the d2x user creation, chmod, and writing to bashrc files into a single RUN.
  - Add a step to copy the `scripts` directory into the Docker image and make scripts executable.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/muselab-d2x/d2x/tree/cumulusci-next?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
jlantz committed Nov 21, 2024
1 parent 92cc89d commit 25ecd0d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/beta-test-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,30 @@ jobs:
- name: Auth to DevHub
run: /usr/local/bin/devhub.sh
- name: Deploy to Packaging Org
id: deploy_to_packaging_org
run: cci flow run ci_master --org packaging $([[ "${{ inputs.debug }}" == "true" ]] && echo " --debug")
- name: Parse Metadata Deletions
id: parse_metadata_deletions
run: |
python /usr/local/bin/parse_metadata_deletions.py "${{ steps.deploy_to_packaging_org.outputs.stdout }}"
continue-on-error: false
- name: Report Metadata Deletions
if: steps.parse_metadata_deletions.outcome == 'success'
run: |
echo "## Metadata Deletions" >> $GITHUB_STEP_SUMMARY
cat metadata_deletions.json >> $GITHUB_STEP_SUMMARY
shell: bash
- name: Create JSON Artifact
if: steps.parse_metadata_deletions.outcome == 'success'
uses: actions/upload-artifact@v2
with:
name: metadata_deletions
path: metadata_deletions.json
- name: Create GitHub Check
if: steps.parse_metadata_deletions.outcome == 'success' && secrets.github-app-id && secrets.github-app-key
run: |
bash scripts/create_github_check.sh metadata_deletions.json
shell: bash
- name: Build Beta Package
run: cci flow run release_beta --org packaging $([[ "${{ inputs.debug }}" == "true" ]] && echo " --debug")
shell: bash
Expand Down
17 changes: 6 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ RUN pip install --no-cache-dir \
"git+https://github.com/muselab-d2x/CumulusCI@d2x" \
cookiecutter

# Copy devhub auth script and make it executable
COPY devhub.sh /usr/local/bin/devhub.sh
RUN chmod +x /usr/local/bin/devhub.sh
# Copy scripts directory and make scripts executable
COPY scripts /usr/local/bin/
RUN chmod +x /usr/local/bin/devhub.sh /usr/local/bin/parse_metadata_deletions.py /usr/local/bin/create_github_check.py

# Create d2x user
RUN useradd -r -m -s /bin/bash -c "D2X User" d2x

# Setup PATH for root and d2x user
RUN echo '/usr/local/bin/devhub.sh' >> /root/.bashrc && \
# Create d2x user, setup PATH for root and d2x user
RUN useradd -r -m -s /bin/bash -c "D2X User" d2x && \
echo '/usr/local/bin/devhub.sh' >> /root/.bashrc && \
echo '/usr/local/bin/devhub.sh' >> /home/d2x/.bashrc

# ========================
Expand All @@ -73,6 +71,3 @@ USER d2x

# Default command
CMD ["bash"]



37 changes: 37 additions & 0 deletions scripts/create_github_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import requests
import json
import os

def create_github_check(metadata_deletions):
repo = os.getenv('GITHUB_REPOSITORY')
sha = os.getenv('GITHUB_SHA')
app_id = os.getenv('GITHUB_APP_ID')
app_key = os.getenv('GITHUB_APP_KEY')

url = f"https://api.github.com/repos/{repo}/check-runs"
headers = {
"Authorization": f"Bearer {app_key}",
"Accept": "application/vnd.github.v3+json"
}
data = {
"name": "Package Metadata Deletions",
"head_sha": sha,
"status": "completed",
"conclusion": "neutral",
"output": {
"title": "Metadata Deletions",
"summary": "The following metadata deletions were found:",
"text": json.dumps(metadata_deletions, indent=2)
}
}

if metadata_deletions:
data["conclusion"] = "action_required"

response = requests.post(url, headers=headers, json=data)
response.raise_for_status()

if __name__ == "__main__":
with open('metadata_deletions.json') as f:
metadata_deletions = json.load(f)
create_github_check(metadata_deletions)
20 changes: 20 additions & 0 deletions scripts/parse_metadata_deletions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import re
import json

def parse_metadata_deletions(log_content):
deletions = []
deletion_section = False

for line in log_content.splitlines():
if "Deleting metadata:" in line:
deletion_section = True
continue
if deletion_section:
if line.strip() == "":
break
match = re.match(r"\s*(\w+):\s*(\w+)", line)
if match:
metadata_type, metadata_name = match.groups()
deletions.append({"type": metadata_type, "name": metadata_name})

return json.dumps(deletions)
29 changes: 29 additions & 0 deletions scripts/parse_metadata_deletions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

parse_metadata_deletions() {
local log_content="$1"
local deletions=()
local deletion_section=false

while IFS= read -r line; do
if [[ "$line" == *"Deleting metadata:"* ]]; then
deletion_section=true
continue
fi
if $deletion_section; then
if [[ -z "$line" ]]; then
break
fi
if [[ "$line" =~ ^[[:space:]]*([[:alnum:]]+):[[:space:]]*([[:alnum:]]+)$ ]]; then
local metadata_type="${BASH_REMATCH[1]}"
local metadata_name="${BASH_REMATCH[2]}"
deletions+=("{\"type\":\"$metadata_type\",\"name\":\"$metadata_name\"}")
fi
fi
done <<< "$log_content"

echo "[${deletions[*]}]"
}

log_content=$(cat)
parse_metadata_deletions "$log_content"

0 comments on commit 25ecd0d

Please sign in to comment.