|
| 1 | +name: Bytebase Masking Policy Update 3 |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [closed] |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + bytebase-masking-3: |
| 11 | + if: github.event.pull_request.merged == true |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + pull-requests: write |
| 15 | + issues: write |
| 16 | + contents: read |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + ref: ${{ github.event.pull_request.head.sha }} |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Login Bytebase |
| 25 | + id: bytebase-login |
| 26 | + |
| 27 | + with: |
| 28 | + bytebase-url: ${{ secrets.BYTEBASE_URL }} |
| 29 | + service-key: ${{ secrets.BYTEBASE_SERVICE_KEY }} |
| 30 | + service-secret: ${{ secrets.BYTEBASE_SERVICE_SECRET }} |
| 31 | + |
| 32 | + - name: Get changed files |
| 33 | + id: changed-files |
| 34 | + uses: tj-actions/changed-files@v42 |
| 35 | + with: |
| 36 | + files: | |
| 37 | + masking/data-classification.json |
| 38 | + masking/global-masking-rule.json |
| 39 | + since_last_remote_commit: true |
| 40 | + fetch_depth: 0 |
| 41 | + include_all_old_new_renamed_files: true |
| 42 | + |
| 43 | + - name: Debug changed files |
| 44 | + run: | |
| 45 | + echo "All changed and added files:" |
| 46 | + echo "Modified files: ${{ steps.changed-files.outputs.modified_files }}" |
| 47 | + echo "Added files: ${{ steps.changed-files.outputs.added_files }}" |
| 48 | + echo "All changes: ${{ steps.changed-files.outputs.all_changed_files }}" |
| 49 | +
|
| 50 | + - name: Debug changed files in detail |
| 51 | + run: | |
| 52 | + echo "All changed files:" |
| 53 | + echo "${{ steps.changed-files.outputs.all_changed_files }}" |
| 54 | + echo "Contains data-classification.json: ${{ contains(steps.changed-files.outputs.all_changed_files, 'data-classification.json') }}" |
| 55 | + echo "Contains global-masking-rule.json: ${{ contains(steps.changed-files.outputs.all_changed_files, 'global-masking-rule.json') }}" |
| 56 | + echo "Raw output:" |
| 57 | + echo "${{ toJSON(steps.changed-files.outputs) }}" |
| 58 | +
|
| 59 | + - name: Apply data classification |
| 60 | + id: apply-data-classification |
| 61 | + if: ${{ steps.changed-files.outputs.any_changed == 'true' && contains(steps.changed-files.outputs.all_changed_files, 'data-classification.json') }} |
| 62 | + run: | |
| 63 | + CHANGED_FILE="masking/data-classification.json" |
| 64 | + echo "Processing: $CHANGED_FILE" |
| 65 | + |
| 66 | + response=$(curl -s -w "\n%{http_code}" --request PATCH "${{ steps.bytebase-login.outputs.api_url }}/settings/bb.workspace.data-classification?allow_missing=true" \ |
| 67 | + --header "Authorization: Bearer ${{ steps.bytebase-login.outputs.token }}" \ |
| 68 | + --header "Content-Type: application/json" \ |
| 69 | + --data @"$CHANGED_FILE") |
| 70 | +
|
| 71 | + # Extract status code and response body |
| 72 | + status_code=$(echo "$response" | tail -n1) |
| 73 | + body=$(echo "$response" | sed '$d') |
| 74 | + |
| 75 | + echo "status_code=${status_code}" >> $GITHUB_OUTPUT |
| 76 | + echo "response_body<<EOF" >> $GITHUB_OUTPUT |
| 77 | + echo "${body}" >> $GITHUB_OUTPUT |
| 78 | + echo "EOF" >> $GITHUB_OUTPUT |
| 79 | + |
| 80 | + if [[ $status_code -lt 200 || $status_code -ge 300 ]]; then |
| 81 | + echo "Failed with status code: $status_code" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Apply semantic type |
| 86 | + id: apply-global-masking-rule |
| 87 | + if: ${{ steps.changed-files.outputs.any_changed == 'true' && contains(steps.changed-files.outputs.all_changed_files, '/global-masking-rule.json') }} |
| 88 | + run: | |
| 89 | + # Process all masking-exception.json files |
| 90 | + echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n' | grep "global-masking-exception.json" | while read -r CHANGED_FILE; do |
| 91 | + echo "Processing: $CHANGED_FILE" |
| 92 | + |
| 93 | + response=$(curl -s -w "\n%{http_code}" --request PATCH "${{ steps.bytebase-login.outputs.api_url }}/settings/bb.workspace.semantic-types?allow_missing=true" \ |
| 94 | + --header "Authorization: Bearer ${{ steps.bytebase-login.outputs.token }}" \ |
| 95 | + --header "Content-Type: application/json" \ |
| 96 | + --data @"$CHANGED_FILE") |
| 97 | + |
| 98 | + # Extract status code and response body |
| 99 | + status_code=$(echo "$response" | tail -n1) |
| 100 | + body=$(echo "$response" | sed '$d') |
| 101 | + |
| 102 | + echo "Status code: $status_code" |
| 103 | + echo "Response body: $body" |
| 104 | + |
| 105 | + # Append to outputs (with unique identifiers) |
| 106 | + echo "${body}" >> $GITHUB_OUTPUT |
| 107 | + echo "EOF" >> $GITHUB_OUTPUT |
| 108 | + |
| 109 | + if [[ $status_code -lt 200 || $status_code -ge 300 ]]; then |
| 110 | + echo "Failed with status code: $status_code" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + done |
| 114 | +
|
| 115 | + - name: Comment on PR |
| 116 | + uses: actions/github-script@v7 |
| 117 | + env: |
| 118 | + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
| 119 | + with: |
| 120 | + script: | |
| 121 | + const changedFiles = process.env.CHANGED_FILES || ''; |
| 122 | + let commentBody = `### Masking Policy Update 2 Summary\n\n`; |
| 123 | + |
| 124 | + // Add status of merge |
| 125 | + commentBody += `✅ **PR Status:** Merged\n\n`; |
| 126 | +
|
| 127 | + // Add changed files section |
| 128 | + commentBody += `📝 **Changed Files:**\n\n`; |
| 129 | + if (changedFiles.trim()) { |
| 130 | + commentBody += changedFiles.split(' ').map(f => `- ${f}`).join('\n'); |
| 131 | + } else { |
| 132 | + commentBody += `None`; |
| 133 | + } |
| 134 | + commentBody += '\n\n'; |
| 135 | + |
| 136 | + // Add API calls summary |
| 137 | + commentBody += `🔄 **API Calls:**\n\n`; |
| 138 | + let apiCallsFound = false; |
| 139 | +
|
| 140 | + if (changedFiles.includes('masking-algorithm.json')) { |
| 141 | + const status = ${{ toJSON(steps.apply-masking-algorithm.outputs) }}.status_code; |
| 142 | + if (status) { |
| 143 | + apiCallsFound = true; |
| 144 | + const success = status >= 200 && status < 300; |
| 145 | + commentBody += `- Column Masking: ${success ? '✅' : '❌'} ${status}\n`; |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + if (changedFiles.includes('semantic-type.json')) { |
| 150 | + const exceptionStatuses = Object.keys(${{ toJSON(steps.apply-semantic-type.outputs) }} || {}) |
| 151 | + .filter(key => key.startsWith('status_code_')) |
| 152 | + .map(key => ({ |
| 153 | + name: key.replace('status_code_', ''), |
| 154 | + status: ${{ toJSON(steps.apply-semantic-type.outputs) }}[key] |
| 155 | + })); |
| 156 | + |
| 157 | + exceptionStatuses.forEach(({name, status}) => { |
| 158 | + apiCallsFound = true; |
| 159 | + const success = status >= 200 && status < 300; |
| 160 | + commentBody += `- Masking Exception (${name}): ${success ? '✅' : '❌'} ${status}\n`; |
| 161 | + }); |
| 162 | + } |
| 163 | +
|
| 164 | + if (!apiCallsFound) { |
| 165 | + commentBody += `None`; |
| 166 | + } |
| 167 | +
|
| 168 | + await github.rest.issues.createComment({ |
| 169 | + ...context.repo, |
| 170 | + issue_number: context.issue.number, |
| 171 | + body: commentBody |
| 172 | + }); |
0 commit comments