Skip to content

Commit

Permalink
Update PR comment with output from migration
Browse files Browse the repository at this point in the history
  • Loading branch information
john-craft committed Jan 26, 2024
1 parent ad23519 commit a283cc7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 125 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/build-and-push.yml

This file was deleted.

125 changes: 66 additions & 59 deletions .github/workflows/test-migration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,35 @@ jobs:
check-migrations:
runs-on: ubuntu-latest
env:
GCP_PROEJCT_ID: "database-migration-with-okteto"
DB_HOST: "users-db"
POSTGRES_PASSWORD: "dMVZFeBWLOzYRV71"
DB_USERNAME: "postgres"
DB_PASSWORD: "dMVZFeBWLOzYRV71"
POSTGRES_DB: "postgres"
DB_HOST: "users-db"
DB_BACKUP_URL: "https://storage.googleapis.com/anonymize-db-backups/us-west1/backup.sql"

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Authenticate with GCP
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
uses: actions/checkout@v4

- name: Use gcloud CLI
run: gcloud info

- name: Configure Docker to authenticate with GCR
run: |
gcloud auth configure-docker
- name: Pull PostgreSQL image from GCP
- name: Build database container
run: |
docker pull gcr.io/$GCP_PROEJCT_ID/$DB_HOST:latest
docker build -t users-db --file db/Dockerfile db
- name: Start PostgreSQL container
run: |
docker run -d --name $DB_HOST \
-e POSTGRES_USER=$DB_USERNAME \
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
-e POSTGRES_PASSWORD=$DB_PASSWORD \
-e POSTGRES_DB=$POSTGRES_DB \
-e RESTORE_FROM_BACKUP=True \
-e DB_BACKUP_URL=$DB_BACKUP_URL \
-p 5432:5432 \
gcr.io/$GCP_PROEJCT_ID/$DB_HOST:latest
users-db:latest
- name: Install Sequelize CLI and dependencies
run: |
cd ./users-api/src
npm install
npm ci
- name: Wait for Postgres to be ready
run: |
Expand All @@ -63,44 +47,67 @@ jobs:
sleep 10
done
- name: Run Migrations
- name: Run Sequelize Migration
id: migration
run: |
cd ./users-api/src
if npx sequelize-cli db:migrate; then
echo "PR_COMMENT=:white_check_mark: Database migration succeeded!" >> $GITHUB_ENV
else
echo "PR_COMMENT=:x: Database migration failed." >> $GITHUB_ENV
{
cd ./users-api/src
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
OUTPUT=$(npx sequelize db:migrate --env ci 2>&1)
EXIT_CODE=$?
echo "MIGRATION_OUTPUT<<$EOF" >> $GITHUB_ENV
echo "$OUTPUT" >> $GITHUB_ENV
echo "$EOF" >> $GITHUB_ENV
echo "EXIT_CODE=$EXIT_CODE" >> $GITHUB_ENV
} || echo "The migration failed, check the output"
if [ $EXIT_CODE -ne 0 ]; then
exit 1
fi
continue-on-error: true
- name: Cleanup
run: docker stop $DB_HOST
- name: Add or Update Comment on PR
uses: actions/github-script@v7
if: always()
with:
script: |
const { MIGRATION_OUTPUT, EXIT_CODE } = process.env
if (!MIGRATION_OUTPUT) return
- name: Check for existing comment
id: check_comment
run: |
comment_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
| jq '.[] | select(.user.login == "github-actions[bot]" and .body | test("Database migration")) | .id')
const migrationMessage = EXIT_CODE == 0 ? 'Migration successful' : `Migration failed with exit code \`${EXIT_CODE}\`:\n\n\`\`\`sh\n${MIGRATION_OUTPUT}\n\`\`\``;
const commentIdentifier = '🔄 Sequelize Migration Result'; // Unique identifier for the comment
const commentBody = `${commentIdentifier}\n\n${migrationMessage}`;
echo "::set-output name=comment_id::$comment_id"
continue-on-error: true
const pr_number = context.payload.pull_request.number;
const { owner, repo } = context.repo;
- name: Post or update PR comment based on migration result
run: |
if [[ -z "${{ steps.check_comment.outputs.comment_id }}" ]]; then
# Create a new comment
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
-d "{\"body\":\"${{ env.PR_COMMENT }}\"}"
else
# Update the existing comment
curl -X PATCH \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/comments/${{ steps.check_comment.outputs.comment_id }} \
-d "{\"body\":\"${{ env.PR_COMMENT }}\"}"
fi
const payload = {
issue_number: pr_number,
owner,
repo
}
// Find existing comment
const comments = await github.rest.issues.listComments(payload);
const migrationComment = comments.data.find(comment => comment.body.startsWith(commentIdentifier));
// Update existing comment or create a new one
if (migrationComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: migrationComment.id,
body: commentBody
});
} else {
await github.rest.issues.createComment({
issue_number: pr_number,
owner,
repo,
body: commentBody
});
}
- name: Cleanup
if: always()
run: docker stop $DB_HOST
2 changes: 1 addition & 1 deletion db/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM postgres:latest
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# Add the loading script to the container
COPY restore-backup.sh /restore-backup.sh
COPY ./restore-backup.sh /restore-backup.sh

# Make the script executable
RUN chmod +x /restore-backup.sh
Expand Down
7 changes: 7 additions & 0 deletions users-api/src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ module.exports = {
dialect: 'postgres',
logging: true
},
ci: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.POSTGRES_DB,
host: 'localhost',
dialect: 'postgres'
}
};
30 changes: 0 additions & 30 deletions users-api/src/migrations/20231024192448-change-dob-type.js

This file was deleted.

11 changes: 0 additions & 11 deletions users-ui/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,6 @@ <h2>List of Users</h2>
console.error('Error fetching users:', error);
});

function getHost() {
const topLevelDomain = ".preview.pvcy.io";
const hostname = window.location.hostname;
const parts = hostname.split('.');
if (parts.length > 0) {
let subdomain = parts[0].replace("ui", "api");
return subdomain + topLevelDomain;
}
return hostname;
}

function getPageParam() {
const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
Expand Down

0 comments on commit a283cc7

Please sign in to comment.