|
| 1 | +name: Documentation Update |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + test_repo: |
| 7 | + description: 'Repository URL to test with' |
| 8 | + required: false |
| 9 | + default: 'https://github.com/CodeBoarding/insights-core' |
| 10 | + type: string |
| 11 | + schedule: |
| 12 | + # Run daily at 2 AM UTC |
| 13 | + - cron: '0 2 * * *' |
| 14 | + |
| 15 | +jobs: |
| 16 | + update-docs: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + |
| 28 | + - name: Get repository URL |
| 29 | + id: repo-url |
| 30 | + run: | |
| 31 | + # Use test repository if provided via workflow_dispatch, otherwise use current repo |
| 32 | + if [ "${{ github.event.inputs.test_repo }}" != "" ]; then |
| 33 | + REPO_URL="${{ github.event.inputs.test_repo }}" |
| 34 | + echo "Using test repository: $REPO_URL" |
| 35 | + else |
| 36 | + REPO_URL="${{ github.server_url }}/${{ github.repository }}" |
| 37 | + echo "Using current repository: $REPO_URL" |
| 38 | + fi |
| 39 | + echo "repo_url=$REPO_URL" >> $GITHUB_OUTPUT |
| 40 | +
|
| 41 | + - name: Fetch documentation files |
| 42 | + id: fetch-docs |
| 43 | + run: | |
| 44 | + # Replace this with your actual endpoint URL |
| 45 | + ENDPOINT_URL="${{ vars.CODEBOARDING_ENDPOINT || 'http://0.0.0.0:8000/generate_docs' }}" |
| 46 | + REPO_URL="${{ steps.repo-url.outputs.repo_url }}" |
| 47 | + |
| 48 | + echo "Fetching documentation from: $ENDPOINT_URL?url=$REPO_URL" |
| 49 | + |
| 50 | + # Make the API call and save response |
| 51 | + response=$(curl -s -w "%{http_code}" -o response.json "$ENDPOINT_URL?url=$REPO_URL") |
| 52 | + http_code=${response: -3} |
| 53 | + |
| 54 | + if [ "$http_code" != "200" ]; then |
| 55 | + echo "Error: API call failed with status code $http_code" |
| 56 | + cat response.json |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + # Check if response is valid JSON |
| 61 | + if ! jq empty response.json 2>/dev/null; then |
| 62 | + echo "Error: Invalid JSON response" |
| 63 | + cat response.json |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + echo "API call successful" |
| 68 | + echo "Response JSON contents:" |
| 69 | + cat response.json |
| 70 | +
|
| 71 | + - name: Create .codeboarding directory and files |
| 72 | + run: | |
| 73 | + # Create the .codeboarding directory |
| 74 | + mkdir -p .codeboarding |
| 75 | + |
| 76 | + # Debug: Print response.json structure and keys |
| 77 | + echo "=== JSON DEBUGGING ===" |
| 78 | + echo "Response JSON structure:" |
| 79 | + jq . response.json |
| 80 | + |
| 81 | + echo "Top-level keys in response:" |
| 82 | + jq 'keys' response.json |
| 83 | + |
| 84 | + echo "Checking if 'files' key exists:" |
| 85 | + jq 'has("files")' response.json |
| 86 | + |
| 87 | + echo "If files exists, what type is it:" |
| 88 | + jq 'if has("files") then (.files | type) else "files key not found" end' response.json |
| 89 | + |
| 90 | + echo "If files exists, what are its keys:" |
| 91 | + jq 'if has("files") then (.files | keys) else "files key not found" end' response.json |
| 92 | + echo "=== END JSON DEBUGGING ===" |
| 93 | + |
| 94 | + # Parse JSON response and create files using keys as filenames |
| 95 | + if jq -e '.files' response.json > /dev/null; then |
| 96 | + echo "Files key found, proceeding to create files..." |
| 97 | + |
| 98 | + # Get each key from files object and create a file with that name |
| 99 | + jq -r '.files | keys[]' response.json | while read -r filename; do |
| 100 | + echo "Processing file: $filename" |
| 101 | + |
| 102 | + # Get the content for this filename |
| 103 | + content=$(jq -r ".files[\"$filename\"]" response.json) |
| 104 | + |
| 105 | + # Add .md extension if not present |
| 106 | + if [[ "$filename" != *.md ]]; then |
| 107 | + output_filename="${filename}.md" |
| 108 | + else |
| 109 | + output_filename="$filename" |
| 110 | + fi |
| 111 | + |
| 112 | + # Write content to file |
| 113 | + echo "$content" > ".codeboarding/$output_filename" |
| 114 | + echo "Created file: .codeboarding/$output_filename" |
| 115 | + done |
| 116 | + else |
| 117 | + echo "No 'files' key found in response JSON" |
| 118 | + fi |
| 119 | + |
| 120 | + # List created files |
| 121 | + echo "Files created in .codeboarding:" |
| 122 | + ls -la .codeboarding/ |
| 123 | + |
| 124 | + # Show a sample of file contents for debugging |
| 125 | + echo "Sample file contents:" |
| 126 | + for file in .codeboarding/*.md; do |
| 127 | + if [ -f "$file" ]; then |
| 128 | + echo "=== Contents of $file (first 10 lines) ===" |
| 129 | + head -10 "$file" |
| 130 | + echo "=== End of $file sample ===" |
| 131 | + break |
| 132 | + fi |
| 133 | + done |
| 134 | +
|
| 135 | + - name: Check for changes |
| 136 | + id: changes |
| 137 | + run: | |
| 138 | + # Add .codeboarding to git to check for changes |
| 139 | + git add .codeboarding/ |
| 140 | + |
| 141 | + # Check if there are any changes |
| 142 | + if git diff --cached --quiet; then |
| 143 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 144 | + echo "No changes detected" |
| 145 | + else |
| 146 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 147 | + echo "Changes detected" |
| 148 | + git diff --cached --name-status |
| 149 | + fi |
| 150 | +
|
| 151 | + - name: Create Pull Request |
| 152 | + if: steps.changes.outputs.has_changes == 'true' |
| 153 | + uses: peter-evans/create-pull-request@v5 |
| 154 | + with: |
| 155 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 156 | + commit-message: "docs: update codeboarding documentation" |
| 157 | + title: "doc update" |
| 158 | + body: | |
| 159 | + ## Documentation Update |
| 160 | + |
| 161 | + This PR contains updated documentation files fetched from the codeboarding service. |
| 162 | + |
| 163 | + ### Changes |
| 164 | + - Updated files in `.codeboarding/` directory |
| 165 | + - Files fetched from: ${{ vars.CODEBOARDING_ENDPOINT || 'codeboarding endpoint' }} |
| 166 | + - Repository: ${{ steps.repo-url.outputs.repo_url }} |
| 167 | + |
| 168 | + This PR was automatically generated by the documentation update workflow. |
| 169 | + branch: docs/codeboarding-update |
| 170 | + base: main |
| 171 | + delete-branch: true |
| 172 | + |
| 173 | + # - name: Cleanup |
| 174 | + # if: success() |
| 175 | + # run: | |
| 176 | + # rm -f response.json |
0 commit comments