本篇要解決的問題
在上一篇〈用 Postman Integrations,自動備份 Postman Collection〉我們已經做到當 Postman 的 Collection 有更新時,自動備份到 GitHub 上,接著這篇是來筆記如何將這些備份檔,定期匯入到另外的 Postman 帳號上,也算是一種二個 Postman 帳號可以彼此同步的方法。
取得 Access Token
GitHub
取得 GitHub Access Token 的步驟在上一篇的筆記文就寫過了,本段不再重覆寫,可見上篇「建立 GitHub 專案及 Access Token」。
GitLab
點擊 GitLab 存取憑證 頁面。
點擊「新增令牌」:
令牌名稱:取一個自己記得住是做什麼用的。
到期時間:刪掉給空值,可以存活約一年。
選取範圍:勾選 api。
選好、勾好後,按下「建立 個人存取令牌」,就會看見成功的訊息:
記得按下複製的按鈕,將令牌存起來,後續的程式碼會用到。
建立 Postman API Key
打開要匯入 Collections 的 Postman,右上角點擊自己的頭像,接著點擊 Settings:
左側選單點擊 API keys,接著點擊右上角的 Generate API Key 的按鈕:
為 Token 取一個自己記得住是做什麼用的名字後,點擊 Generate API Key:
就會看見 Postman 生成了一組 API Key:
一樣按下複製的按鈕然後存起來,下一段寫程式碼會用到。
從 GitHub 上匯入 Postman Collections
這段的 GitHub 專案,跟下一段的 GitLab 專案,都要是從上一篇 Postman Integrations 的方式備份 Collections,才有效。主要是資料夾的位置是用預設值,不然就要自己替換檔案路徑。
在 Google 雲端硬碟上新增一個 Google Apps Scripts 的檔案,取好檔名存檔後,程式碼複製貼上以下:
const postmanApiKey = '請貼上 Postman API Key'; | |
const githubToken = '請貼上 GitHub Access Token'; | |
const repoOwner = '請貼上 GitHub 帳號名稱'; | |
// 以下請貼上要匯進 Postman Collection 的清單 | |
const repoList = [ | |
{ | |
repoName: 'postman-backup-demo', | |
filePath: 'Postman Collections/postman-backup-demo.json' | |
} | |
] | |
// 取 GitHub 備份檔 | |
function getGitHubBackupFile({ repoName, filePath }) { | |
const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filePath}`; | |
const headers = { | |
'Authorization': 'token ' + githubToken, | |
'Accept': 'application/vnd.github.v3.raw' | |
}; | |
const response = UrlFetchApp.fetch(apiUrl, { headers }); | |
const content = response.getContentText(); | |
// Postman 匯入 GitHub 備份檔 | |
importCollection(content) | |
} | |
// Postman 匯入 GitHub 備份檔 | |
function importCollection(content) { | |
let apiUrl = 'https://api.getpostman.com/collections'; | |
const headers = { | |
'Content-Type': 'application/json', | |
'X-Api-Key': postmanApiKey | |
}; | |
const payload = { 'collection': JSON.parse(content) }; | |
const options = { | |
method: 'get', | |
headers | |
}; | |
// 檢查是否有同名的 Collection | |
const collectionName = payload.collection.info.name; | |
const existingCollectionId = getExistingCollectionId(collectionName, postmanApiKey); | |
if (existingCollectionId) { | |
// 有同名,覆蓋 | |
apiUrl = apiUrl + '/' + existingCollectionId; | |
options.method = 'put'; | |
options.payload = JSON.stringify(payload); | |
} else { | |
// 無同名,代表 Collection 不存在,新增 | |
options.method = 'post'; | |
options.payload = JSON.stringify(payload); | |
} | |
const response = UrlFetchApp.fetch(apiUrl, options); | |
const jsonResponse = response.getContentText(); | |
} | |
// 檢查是否有同名的 Collection | |
function getExistingCollectionId(collectionName, apiKey) { | |
const apiUrl = 'https://api.getpostman.com/collections'; | |
const headers = { 'X-Api-Key': apiKey }; | |
const response = UrlFetchApp.fetch(apiUrl, { headers: headers }); | |
const jsonResponse = JSON.parse(response.getContentText()); | |
const collections = jsonResponse.collections; | |
for (let i = 0; i < collections.length; i++) { | |
if (collections[i].name === collectionName) { | |
return collections[i].uid; | |
} | |
} | |
return null; | |
} |
從 GitLab 上匯入 Postman Collections
在 Google 雲端硬碟上新增一個 Google Apps Scripts 的檔案,取好檔名存檔後,程式碼複製貼上以下:
const gitLabToken = '請貼上 GitHub Access Token'; | |
const postmanApiKey = '請貼上 Postman API Key'; | |
// 以下請貼上要匯進 Postman Collection 的清單 | |
const repoList = [ | |
{ | |
projectId: '48419806', | |
filePath: 'Postman_Collections/postman-backup-demo.json' | |
} | |
]; | |
// 排程:更新 Postman Collection | |
function runPostman() { | |
repoList.forEach(repo => getGitLabBackupFile({ projectId: repo.projectId, filePath: repo.filePath })) | |
} | |
// 取 GitLab 備份檔 | |
// ?ref=main,ref 後面填的是主分支的名稱,如果主分支是用 master 的話請改為 master | |
function getGitLabBackupFile({ projectId, filePath }) { | |
const apiUrl = `https://gitlab.com/api/v4/projects/${projectId}/repository/files/${encodeURIComponent(filePath)}?ref=main`; | |
const options = { | |
method: 'get', | |
headers: { 'Authorization': 'Bearer ' + gitLabToken } | |
}; | |
const response = UrlFetchApp.fetch(apiUrl, options); | |
const data = response.getContentText(); | |
const fileContent = JSON.parse(data).content; | |
// 解 base64 值 | |
const decodedBytes = Utilities.base64Decode(fileContent); | |
// 位元組陣列轉為字串 | |
const decodedText = Utilities.newBlob(decodedBytes).getDataAsString(); | |
importCollection(decodedText); | |
} | |
// Postman 匯入 GitLab 備份檔 | |
function importCollection(content) { | |
let apiUrl = 'https://api.getpostman.com/collections'; | |
const headers = { | |
'Content-Type': 'application/json', | |
'X-Api-Key': postmanApiKey | |
}; | |
const payload = { 'collection': JSON.parse(content) }; | |
const options = { | |
method: 'get', | |
headers | |
}; | |
// 檢查是否有同名的 Collection | |
const collectionName = payload.collection.info.name; | |
const existingCollectionId = getExistingCollectionId(collectionName, postmanApiKey); | |
if (existingCollectionId) { | |
// 有同名,覆蓋 | |
apiUrl = apiUrl + '/' + existingCollectionId; | |
options.method = 'put'; | |
options.payload = JSON.stringify(payload); | |
} else { | |
// 無同名,代表 Collection 不存在,新增 | |
options.method = 'post'; | |
options.payload = JSON.stringify(payload); | |
} | |
const response = UrlFetchApp.fetch(apiUrl, options); | |
} | |
// 檢查是否有同名的 Collection GitLab | |
function getExistingCollectionId(collectionName, apiKey) { | |
const apiUrl = 'https://api.getpostman.com/collections'; | |
const headers = { 'X-Api-Key': apiKey }; | |
const response = UrlFetchApp.fetch(apiUrl, { headers }); | |
const jsonResponse = JSON.parse(response.getContentText()); | |
const collections = jsonResponse.collections; | |
for (let i = 0; i < collections.length; i++) { | |
if (collections[i].name === collectionName) { | |
return collections[i].uid; | |
} | |
} | |
return null; | |
} |
設定排程
Google Apps Script 左側選單點擊 觸發條件:
就可以設定要多久時間自動執行一次函式,建議是不要設成每分鐘都執行,因為容易發生卡住的情形,設每 5 分鐘或每 10 分鐘執行一次比較穩定。
Top comments (0)