Skip to content
Open
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
56 changes: 56 additions & 0 deletions .github/scripts/backup-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const axios = require('axios');
const fs = require('fs');

async function backupCollection() {
try {
// First, try to find "latest" collection by name (preferred)
// Fall back to COLLECTION_UID if "latest" doesn't exist yet
let collectionUid = process.env.COLLECTION_UID;
let collectionName = 'unknown';

if (process.env.POSTMAN_API_KEY) {
try {
const collectionsResponse = await axios({
method: 'get',
url: 'https://api.getpostman.com/collections',
headers: { 'X-Api-Key': process.env.POSTMAN_API_KEY }
});

const latestCollection = collectionsResponse.data.collections.find(c => c.name === 'latest');
if (latestCollection) {
collectionUid = latestCollection.uid;
collectionName = 'latest';
console.log('Found "latest" collection, using UID:', collectionUid);
} else {
console.log('No "latest" collection found, using COLLECTION_UID:', collectionUid);
}
} catch (error) {
console.log('Could not fetch collections list, using COLLECTION_UID:', collectionUid);
}
}

const url = 'https://api.getpostman.com/collections/' + collectionUid;
console.log('Backup URL:', url);

console.log('Making backup request...');
const config = {
method: 'get',
url,
headers: {
'X-Api-Key': process.env.POSTMAN_API_KEY
}
};

const response = await axios(config);

const backupPath = './postman/backup/collection_' + process.env.TIMESTAMP + '.json';
fs.writeFileSync(backupPath, JSON.stringify(response.data, null, 2));
console.log('Backup created successfully at:', backupPath);
console.log('Backed up collection:', collectionName, '(UID:', collectionUid + ')');
} catch (error) {
console.error('Backup failed:', error.response?.data || error.message);
process.exit(1);
}
}

backupCollection();
93 changes: 93 additions & 0 deletions .github/scripts/update-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const axios = require('axios');
const fs = require('fs');

async function versionCollection() {
try {
console.log('Starting collection versioning process...');

// Get all collections
const collectionsResponse = await axios({
method: 'get',
url: 'https://api.getpostman.com/collections',
headers: { 'X-Api-Key': process.env.POSTMAN_API_KEY }
});

const collections = collectionsResponse.data.collections;
console.log('Found collections:', collections.map(c => c.name));

// Always find the "latest" collection by name, not by UID
// This ensures we always work with the current "latest" regardless of UID changes
let currentCollection = collections.find(c => c.name === 'Pinterest REST API latest');

// Fallback: if no "latest" exists, use COLLECTION_UID (for first-time setup)
if (!currentCollection && process.env.COLLECTION_UID) {
currentCollection = collections.find(c => c.uid === process.env.COLLECTION_UID);
console.log('No "Pinterest REST API latest" collection found, using COLLECTION_UID as starting point');
}

if (!currentCollection) {
throw new Error('Could not find "Pinterest REST API latest" collection or collection with COLLECTION_UID');
}
console.log('Current collection:', currentCollection.name, '(UID:', currentCollection.uid + ')');

// Find highest version number from existing Pinterest REST API collections
let highestVersion = { major: 5, minor: 14, patch: 0 }; // Start from 5.14.0 as example
collections.forEach(collection => {
// Look for "Pinterest REST API X.Y.Z" pattern
const match = collection.name.match(/^Pinterest REST API (\d+)\.(\d+)\.(\d+)$/);
if (match) {
const version = {
major: parseInt(match[1]),
minor: parseInt(match[2]),
patch: parseInt(match[3])
};
if (version.major > highestVersion.major ||
(version.major === highestVersion.major && version.minor > highestVersion.minor)) {
highestVersion = version;
}
}
});

// Calculate next version (increment minor)
const nextVersion = `${highestVersion.major}.${highestVersion.minor + 1}.${highestVersion.patch}`;
const nextVersionName = `Pinterest REST API ${nextVersion}`;
console.log('Next version:', nextVersionName);

// Rename current collection to version number
await axios({
method: 'put',
url: `https://api.getpostman.com/collections/${currentCollection.uid}`,
headers: {
'X-Api-Key': process.env.POSTMAN_API_KEY,
'Content-Type': 'application/json'
},
data: { collection: { info: { name: nextVersionName } } }
});
console.log('Renamed current collection to:', nextVersionName);

// Create new "Pinterest REST API latest" collection
const newCollectionData = JSON.parse(fs.readFileSync('./postman/collection.json'));
newCollectionData.info.name = 'Pinterest REST API latest';

const createResponse = await axios({
method: 'post',
url: 'https://api.getpostman.com/collections',
headers: {
'X-Api-Key': process.env.POSTMAN_API_KEY,
'Content-Type': 'application/json'
},
data: { collection: newCollectionData }
});

console.log('Created new "Pinterest REST API latest" collection');
console.log('New collection UID:', createResponse.data.collection.uid);
console.log('✅ Process complete! The "Pinterest REST API latest" collection is now ready for future updates.');
console.log('Note: COLLECTION_UID secret can remain unchanged - script will always find "Pinterest REST API latest" by name.');

} catch (error) {
console.error('Error:', error.response?.data || error.message);
process.exit(1);
}
}

versionCollection();
54 changes: 54 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Transform OpenAPI to Postman Collection

on:
push:
branches: [ main ]
paths:
- 'v5/openapi.yaml'
- 'v5/openapi.json'

jobs:
sync-to-postman:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Dependencies
run: |
npm install -g openapi-to-postmanv2
npm install axios

- name: Backup Current Postman Collection
env:
POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }}
COLLECTION_UID: ${{ secrets.POSTMAN_COLLECTION_UID }}
TIMESTAMP: $(date +%Y%m%d_%H%M%S)
run: |
mkdir -p ./postman/backup
node .github/scripts/backup-collection.js

- name: Upload Backup as Artifact
uses: actions/upload-artifact@v4
with:
name: postman-collection-backup
path: ./postman/backup/collection_*.json
retention-days: 90

- name: Convert OpenAPI to Postman Collection
run: |
openapi2postmanv2 \
-s ./v5/openapi.yaml \
-o ./postman/collection.json \
-p \
--pretty

- name: Version and Create New Postman Collection
env:
POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }}
COLLECTION_UID: ${{ secrets.POSTMAN_COLLECTION_UID }}
run: node .github/scripts/update-collection.js
Loading