-
Notifications
You must be signed in to change notification settings - Fork 2
Sync Postman collection on pr merge #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||
| name: Sync Postman Collection | ||||||
|
|
||||||
| on: | ||||||
| push: | ||||||
| branches: | ||||||
| - main | ||||||
| paths: | ||||||
| - 'StackOne_postman_collection.json' | ||||||
|
|
||||||
| jobs: | ||||||
| sync-postman: | ||||||
| runs-on: ubuntu-latest | ||||||
|
|
||||||
| steps: | ||||||
| - name: Checkout repository | ||||||
| uses: actions/checkout@v4 | ||||||
|
|
||||||
| - name: Setup Node.js | ||||||
| uses: actions/setup-node@v4 | ||||||
| with: | ||||||
| node-version: '20' | ||||||
|
|
||||||
| - name: Install Newman (Postman CLI) | ||||||
| run: npm install -g newman | ||||||
|
|
||||||
| - name: Check if collection exists in Postman | ||||||
| id: check_collection | ||||||
| env: | ||||||
| POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }} | ||||||
| POSTMAN_WORKSPACE_ID: ${{ secrets.POSTMAN_WORKSPACE_ID }} | ||||||
| run: | | ||||||
| # Get all collections in workspace | ||||||
| response=$(curl -s -X GET \ | ||||||
| "https://api.getpostman.com/collections?workspace=$POSTMAN_WORKSPACE_ID" \ | ||||||
| -H "X-Api-Key: $POSTMAN_API_KEY") | ||||||
| # Check if StackOne collection exists | ||||||
| collection_uid=$(echo "$response" | jq -r '.collections[] | select(.name == "StackOne") | .uid' | head -1) | ||||||
|
||||||
| if [ -n "$collection_uid" ]; then | ||||||
| echo "Collection exists with UID: $collection_uid" | ||||||
| echo "collection_exists=true" >> $GITHUB_OUTPUT | ||||||
| echo "collection_uid=$collection_uid" >> $GITHUB_OUTPUT | ||||||
| else | ||||||
| echo "Collection does not exist" | ||||||
| echo "collection_exists=false" >> $GITHUB_OUTPUT | ||||||
| fi | ||||||
| - name: Update existing collection | ||||||
| if: steps.check_collection.outputs.collection_exists == 'true' | ||||||
| env: | ||||||
| POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }} | ||||||
| COLLECTION_UID: ${{ steps.check_collection.outputs.collection_uid }} | ||||||
| run: | | ||||||
| # Read the collection file | ||||||
| collection_json=$(cat StackOne_postman_collection.json) | ||||||
| # Update the existing collection | ||||||
| response=$(curl -s -w "\n%{http_code}" -X PUT \ | ||||||
| "https://api.getpostman.com/collections/$COLLECTION_UID" \ | ||||||
| -H "X-Api-Key: $POSTMAN_API_KEY" \ | ||||||
| -H "Content-Type: application/json" \ | ||||||
| -d "{\"collection\": $collection_json}") | ||||||
| http_code=$(echo "$response" | tail -n1) | ||||||
| body=$(echo "$response" | sed '$d') | ||||||
| if [ "$http_code" -eq 200 ]; then | ||||||
| echo "✅ Successfully updated collection" | ||||||
| echo "$body" | jq '.' | ||||||
| else | ||||||
| echo "❌ Failed to update collection. HTTP Code: $http_code" | ||||||
| echo "Response: $body" | ||||||
| exit 1 | ||||||
| fi | ||||||
| - name: Create new collection | ||||||
| if: steps.check_collection.outputs.collection_exists == 'false' | ||||||
| env: | ||||||
| POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }} | ||||||
| POSTMAN_WORKSPACE_ID: ${{ secrets.POSTMAN_WORKSPACE_ID }} | ||||||
| run: | | ||||||
| # Read the collection file | ||||||
| collection_json=$(cat StackOne_postman_collection.json) | ||||||
| # Create new collection | ||||||
| response=$(curl -s -w "\n%{http_code}" -X POST \ | ||||||
| "https://api.getpostman.com/collections?workspace=$POSTMAN_WORKSPACE_ID" \ | ||||||
| -H "X-Api-Key: $POSTMAN_API_KEY" \ | ||||||
| -H "Content-Type: application/json" \ | ||||||
| -d "{\"collection\": $collection_json}") | ||||||
| http_code=$(echo "$response" | tail -n1) | ||||||
| body=$(echo "$response" | sed '$d') | ||||||
| if [ "$http_code" -eq 200 ]; then | ||||||
|
||||||
| if [ "$http_code" -eq 200 ]; then | |
| if [ "$http_code" -eq 201 ]; then |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,29 @@ To use the Postman collection, follow these steps: | |||||
| - **Collection Name:** StackOne | ||||||
| - **Schema Version:** [Postman Collection Schema v2.1.0](https://schema.getpostman.com/json/collection/v2.1.0/collection.json) | ||||||
|
|
||||||
| ## Automated Sync to Postman | ||||||
|
|
||||||
| This repository includes a GitHub workflow that automatically syncs the Postman collection to your Postman workspace when changes are merged to the main branch. | ||||||
|
|
||||||
| ### Setup Requirements | ||||||
|
|
||||||
| To enable automatic synchronization, you need to configure the following GitHub secrets in your repository: | ||||||
|
|
||||||
| 1. **`POSTMAN_API_KEY`**: Your Postman API key | ||||||
| - Generate from: [Postman Account Settings](https://postman.co/settings/me/api-keys) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Postman settings URL (postman.co). Use the official postman.com web app URL so users can generate an API key successfully. Prompt for AI agents
Suggested change
|
||||||
| - Required permissions: Collection management | ||||||
|
|
||||||
| 2. **`POSTMAN_WORKSPACE_ID`**: Your Postman workspace ID | ||||||
| - Find in Postman: Go to your workspace → Settings → Info → Workspace ID | ||||||
| - Or via API: `GET https://api.getpostman.com/workspaces` | ||||||
|
|
||||||
| ### How It Works | ||||||
|
|
||||||
| - The workflow triggers when changes to `StackOne_postman_collection.json` are pushed to the main branch (including PR merges) | ||||||
| - If a collection named "StackOne" exists in your workspace, it will be updated | ||||||
| - If no collection exists, a new one will be created | ||||||
| - The workflow uses the official Postman API for all operations | ||||||
|
|
||||||
| ## Contributing | ||||||
|
|
||||||
| If you find any issues or have suggestions for improvements, please submit an issue or pull request on this repository. | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newman is installed but never used in the workflow. The sync operations use curl with the Postman API directly. Consider removing this step or use Newman for the API operations if preferred.