Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions .github/workflows/bb-masking-semantic-type-global.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
name: Bytebase Masking Policy Update Semantic Type and Global Masking Rule
on:
pull_request:
types: [closed]
branches:
- main
workflow_dispatch:

jobs:
bytebase-masking-semantic-type-global:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Login Bytebase
id: bytebase-login
uses: bytebase/[email protected]
with:
bytebase-url: ${{ secrets.BYTEBASE_URL }}
service-key: ${{ secrets.BYTEBASE_SERVICE_KEY }}
service-secret: ${{ secrets.BYTEBASE_SERVICE_SECRET }}

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v42
with:
files: |
masking/masking-algorithm.json
masking/semantic-type.json
since_last_remote_commit: true
fetch_depth: 0
include_all_old_new_renamed_files: true

- name: Debug changed files
run: |
echo "All changed and added files:"
echo "Modified files: ${{ steps.changed-files.outputs.modified_files }}"
echo "Added files: ${{ steps.changed-files.outputs.added_files }}"
echo "All changes: ${{ steps.changed-files.outputs.all_changed_files }}"

- name: Debug changed files in detail
run: |
echo "All changed files:"
echo "${{ steps.changed-files.outputs.all_changed_files }}"
echo "Contains semantic-type.json: ${{ contains(steps.changed-files.outputs.all_changed_files, 'semantic-type.json') }}"
echo "Contains global-masking-rule.json: ${{ contains(steps.changed-files.outputs.all_changed_files, 'global-masking-rule.json') }}"
echo "Raw output:"
echo "${{ toJSON(steps.changed-files.outputs) }}"

- name: Apply semantic type
id: apply-semantic-type
if: ${{ steps.changed-files.outputs.any_changed == 'true' && contains(steps.changed-files.outputs.all_changed_files, 'semantic-type.json') }}
run: |
CHANGED_FILE="masking/semantic-type.json"
echo "Processing: $CHANGED_FILE"

response=$(curl -s -w "\n%{http_code}" --request PATCH "${{ steps.bytebase-login.outputs.api_url }}/settings/bb.workspace.semantic-types?allow_missing=true" \
--header "Authorization: Bearer ${{ steps.bytebase-login.outputs.token }}" \
--header "Content-Type: application/json" \
--data @"$CHANGED_FILE")

# Extract status code and response body
status_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

echo "status_code=${status_code}" >> $GITHUB_OUTPUT
echo "response_body<<EOF" >> $GITHUB_OUTPUT
echo "${body}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

if [[ $status_code -lt 200 || $status_code -ge 300 ]]; then
echo "Failed with status code: $status_code"
exit 1
fi

- name: Apply global masking rule
id: apply-global-masking-rule
if: ${{ steps.changed-files.outputs.any_changed == 'true' && contains(steps.changed-files.outputs.all_changed_files, '/global-masking-rule.json') }}
run: |
# Process all global-masking-rule.json files
echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n' | grep "global-masking-rule.json" | while read -r CHANGED_FILE; do
echo "Processing: $CHANGED_FILE"

response=$(curl -s -w "\n%{http_code}" --request PATCH "${{ steps.bytebase-login.outputs.api_url }}/policies/masking_rule?allow_missing=true&update_mask=payload" \
--header "Authorization: Bearer ${{ steps.bytebase-login.outputs.token }}" \
--header "Content-Type: application/json" \
--data @"$CHANGED_FILE")

# Extract status code and response body
status_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

echo "Status code: $status_code"
echo "Response body: $body"

# Append to outputs (with unique identifiers)
echo "${body}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

if [[ $status_code -lt 200 || $status_code -ge 300 ]]; then
echo "Failed with status code: $status_code"
exit 1
fi
done

- name: Comment on PR
uses: actions/github-script@v7
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
with:
script: |
const changedFiles = process.env.CHANGED_FILES || '';
let commentBody = `### Update Semantic Type and Global Masking Rule Summary\n\n`;

// Add status of merge
commentBody += `✅ **PR Status:** Merged\n\n`;

// Add changed files section
commentBody += `📝 **Changed Files:**\n\n`;
if (changedFiles.trim()) {
commentBody += changedFiles.split(' ').map(f => `- ${f}`).join('\n');
} else {
commentBody += `None`;
}
commentBody += '\n\n';

// Add API calls summary
commentBody += `🔄 **API Calls:**\n\n`;
let apiCallsFound = false;

if (changedFiles.includes('semantic-type.json')) {
const exceptionStatuses = Object.keys(${{ toJSON(steps.apply-semantic-type.outputs) }} || {})
.filter(key => key.startsWith('status_code_'))
.map(key => ({
name: key.replace('status_code_', ''),
status: ${{ toJSON(steps.apply-semantic-type.outputs) }}[key]
}));

exceptionStatuses.forEach(({name, status}) => {
apiCallsFound = true;
const success = status >= 200 && status < 300;
commentBody += `- Semantic Type (${name}): ${success ? '✅' : '❌'} ${status}\n`;
});
}

if (changedFiles.includes('global-masking-rule.json')) {
const status = ${{ toJSON(steps.apply-global-masking-rule.outputs) }}.status_code;
if (status) {
apiCallsFound = true;
const success = status >= 200 && status < 300;
commentBody += `- Global Masking Rule: ${success ? '✅' : '❌'} ${status}\n`;
}
}
if (!apiCallsFound) {
commentBody += `None`;
}

await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: commentBody
});
4 changes: 2 additions & 2 deletions masking/global-masking-rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"id": "76356d81-6231-4128-9be7-2c549fc505f5",
"condition": {
"expression": "classification_level in [\"2\", \"3\"]",
"title": "",
"title": "x",
"description": ""
},
"semanticType": "bb.default-partial"
Expand All @@ -17,7 +17,7 @@
"id": "1ddc47c9-6ab6-4760-accd-947bc1a5f155",
"condition": {
"expression": "classification_level in [\"4\"]",
"title": "",
"title": "x",
"description": ""
},
"semanticType": "bb.default"
Expand Down
4 changes: 2 additions & 2 deletions masking/semantic-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"id": "e8d3fd41-eba0-4afd-ae0f-32c91a4ee710",
"title": "Personal sensitive data",
"title": "Personal sensitive data x",
"description": "Custom semantic type with full masking for personal sensitive data",
"algorithm": {
"fullMask": {
Expand All @@ -26,7 +26,7 @@
{
"id": "38b31ac4-b90d-44ac-b42a-19d9c76c83ea",
"title": "Location",
"description": "Custom semantic type with range masking for location",
"description": "Custom semantic type with range masking for location x",
"algorithm": {
"rangeMask": {
"slices": [
Expand Down
Loading