|
| 1 | +name: Sync OSCAL content to CAC content |
| 2 | +permissions: |
| 3 | + contents: write |
| 4 | + pull-requests: read |
| 5 | +on: |
| 6 | + #push: |
| 7 | + # branches: |
| 8 | + # - master |
| 9 | + pull_request: |
| 10 | + types: [opened, synchronize, reopened] |
| 11 | + |
| 12 | +jobs: |
| 13 | + check-oscal-content-update-sync-to-cac: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + # Step 1: Set up Python 3 |
| 17 | + - name: Set up Python 3 |
| 18 | + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 |
| 19 | + with: |
| 20 | + python-version: '3.9' |
| 21 | + # Step 2: Install Git |
| 22 | + - name: Install Git |
| 23 | + run: sudo apt-get update && sudo apt-get install -y git |
| 24 | + # Step 3: Checkout OSCAL repo |
| 25 | + - name: Checkout OSCAL repo |
| 26 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 27 | + with: |
| 28 | + repository: ${{ github.repository }} |
| 29 | + path: oscal-content |
| 30 | + - name: Get the commit message and PR number |
| 31 | + run: | |
| 32 | + # cd oscal-content |
| 33 | + # # Get the latest commit message |
| 34 | + # COMMIT_MSG=$(git log -1 --pretty=%B) |
| 35 | + # # Extract the PR number from the commit message (if it's a merge commit) |
| 36 | + # PR_NUMBER=$(echo "$COMMIT_MSG" | grep -oP '#\K\d+') |
| 37 | + # if [ -n "$PR_NUMBER" ]; then |
| 38 | + # echo "Found PR number: $PR_NUMBER" |
| 39 | + # echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
| 40 | + # echo "SKIP=false" >> $GITHUB_ENV |
| 41 | + # PR_INFO=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}") |
| 42 | + # # Extract PR title from the response |
| 43 | + # PR_TITLE=$(echo "$PR_INFO" | jq -r .title) |
| 44 | + # echo "PR Title: $PR_TITLE" |
| 45 | + # if [[ "$PR_TITLE" == *"Auto-generated PR from CAC"* ]]; then |
| 46 | + # echo "The PR comes from CAC content. The workflow of Sync OSCAL content to CAC will exit." |
| 47 | + # echo "Skipping further checks." |
| 48 | + # echo "SKIP=true" >> $GITHUB_ENV |
| 49 | + # fi |
| 50 | + # fi |
| 51 | + echo "PR_NUMBER=33" >> $GITHUB_ENV |
| 52 | + echo "SKIP=false" >> $GITHUB_ENV |
| 53 | + # Step 4: Get the access token for content write permission to CAC content |
| 54 | + - name: Get GitHub app token |
| 55 | + if: ${{ env.SKIP == 'false' }} |
| 56 | + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 |
| 57 | + id: app-token |
| 58 | + with: |
| 59 | + app-id: ${{ secrets.APP_ID }} |
| 60 | + private-key: ${{ secrets.PRIVATE_KEY }} |
| 61 | + owner: ${{ github.repository_owner }} |
| 62 | + repositories: | |
| 63 | + content |
| 64 | + oscal-content |
| 65 | + # Step 6: Dectect the updates of OSCAL content |
| 66 | + - name: Detect the changed files of the PR |
| 67 | + id: changed-files |
| 68 | + run: | |
| 69 | + repo=${{ github.repository }} |
| 70 | + # Fetch all pages of the files for the pull request |
| 71 | + url="repos/$repo/pulls/${{ env.PR_NUMBER }}/files" |
| 72 | + response=$(gh api "$url" --paginate) |
| 73 | + echo "$response" | jq -r '.[].filename' > filenames.txt |
| 74 | + echo "OUTPUT_SIZE=$(stat --printf="%s" filenames.txt)" >> $GITHUB_OUTPUT |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 77 | + - name: Checkout CAC repo |
| 78 | + if: ${{ steps.changed-files.outputs.OUTPUT_SIZE != '0' }} |
| 79 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 80 | + with: |
| 81 | + repository: ComplianceAsCode/content |
| 82 | + path: cac-content |
| 83 | + token: ${{ steps.app-token.outputs.token }} |
| 84 | + fetch-depth: 0 |
| 85 | + - name: Checkout complyscribe repo |
| 86 | + if: ${{ steps.changed-files.outputs.OUTPUT_SIZE != '0' }} |
| 87 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 88 | + with: |
| 89 | + repository: complytime/complyscribe |
| 90 | + path: complyscribe |
| 91 | + - name: Setup trestlebot |
| 92 | + if: ${{ steps.changed-files.outputs.OUTPUT_SIZE != '0' }} |
| 93 | + run: | |
| 94 | + cd complyscribe && python3 -m venv venv && source venv/bin/activate |
| 95 | + python3 -m pip install --no-cache-dir "poetry==1.7.1" |
| 96 | + poetry install |
| 97 | + # Step 8: Sync OSCAL content to CAC content |
| 98 | + - name: Sync OSCAL content to CAC content |
| 99 | + if: ${{ steps.changed-files.outputs.OUTPUT_SIZE != '0' }} |
| 100 | + run: | |
| 101 | + pr_number="${{ github.event.pull_request.number }}" |
| 102 | + cat filenames.txt |
| 103 | + cd complyscribe && source venv/bin/activate |
| 104 | + while IFS= read -r line; do |
| 105 | + if [[ "$line" == *catalogs* ]]; then |
| 106 | + policy_id=$(echo "$line" | cut -f2 -d"/") |
| 107 | + echo "sync oscal catalogs according to update of $line" |
| 108 | + echo "poetry run complyscribe sync-oscal-content catalog --repo-path ../oscal-content --committer-email "openscap-ci@gmail.com" --committer-name "openscap-ci" --branch "sync_oscal_pr$pr_number" --cac-content-root "$GITHUB_WORKSPACE/cac-content" --cac-policy-id "$policy_id"" |
| 109 | + elif [[ "$line" == "profiles"* ]]; then |
| 110 | + policy_id=$(echo $line | cut -f2 -d"/" | cut -f2 -d"-") |
| 111 | + product=$(echo "$line" | cut -f2 -d"/" | cut -f1 -d"-") |
| 112 | + echo "sync oscal profiles according to update of $line" |
| 113 | + echo "poetry run complyscribe sync-oscal-content profile --repo-path ../oscal-content --committer-email "openscap-ci@gmail.com" --committer-name "openscap-ci" --branch "sync_oscal_pr$pr_number" --cac-content-root "$GITHUB_WORKSPACE/cac-content" --cac-policy-id "$policy_id" --product "$product"" |
| 114 | + elif [[ "$line" == "component-definitions"* ]]; then |
| 115 | + product=$(echo "$line" | cut -f2 -d"/") |
| 116 | + profile=$(echo "$line" | cut -f3 -d"/" | cut -f2 -d"-") |
| 117 | + echo "sync oscal component-definitions according to update of $line" |
| 118 | + echo "poetry run complyscribe sync-oscal-content component-definition --repo-path ../oscal-content --committer-email "openscap-ci@gmail.com" --committer-name "openscap-ci" --branch "sync_oscal_pr$pr_number" --cac-content-root "$GITHUB_WORKSPACE/cac-content" --cac-policy-id "$policy_id" --product "$product" --oscal-profile "$profile"" |
| 119 | + fi |
| 120 | + done < ../filenames.txt |
| 121 | + # Step 9: Create PR to CAC content |
| 122 | + - name: Create a Pull Request to OSCAL content |
| 123 | + if: ${{ steps.ctf.outputs.CTF_OUTPUT_SIZE != '0' }} |
| 124 | + run: | |
| 125 | + cd cac-content |
| 126 | + BRANCH_NAME="sync_oscal_pr${{ env.PR_NUMBER }}" |
| 127 | + OWNER="ComplianceAsCode" |
| 128 | + REPO="content" |
| 129 | + if [[ "$(git branch --show-current)" == "$BRANCH_NAME" ]]; then |
| 130 | + # Check if the PR exists |
| 131 | + PR_EXISTS=$(gh pr list --repo $OWNER/$REPO \ |
| 132 | + --head $BRANCH_NAME --state open --json id \ |
| 133 | + | jq length) |
| 134 | + # Get commits between main and branch |
| 135 | + commits=$(git log master..$BRANCH_NAME --oneline) |
| 136 | + # If the PR does not exist and there are commits in the branch, |
| 137 | + # then create a PR for this branch. |
| 138 | + if [ "$PR_EXISTS" -gt 0 ]; then |
| 139 | + echo "PR $BRANCH_NAME already exists. Skipping PR creation." |
| 140 | + elif [ -z "$commits" ]; then |
| 141 | + echo "No commits between main and $BRANCH_NAME. Skipping PR creation." |
| 142 | + else |
| 143 | + echo "Creating PR for new branch: $BRANCH_NAME" |
| 144 | + gh pr create --repo $OWNER/$REPO \ |
| 145 | + --title "Auto-generated PR from OSCAL ${{ env.PR_NUMBER }}" \ |
| 146 | + --head "$BRANCH_NAME" \ |
| 147 | + --base "master" \ |
| 148 | + --body "This is an auto-generated PR from OSCAL ${{ env.PR_NUMBER }} updates" |
| 149 | + fi |
| 150 | + else |
| 151 | + echo "No branch $BRANCH_NAME. Skipping PR creation." |
| 152 | + fi |
| 153 | + env: |
| 154 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 155 | + |
| 156 | + |
0 commit comments