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