昨天我們調整了使用者面板,讓它對出新使用者友善,今天我們繼續來增加一些小巧思吧!
在不同專案裡面,Merge Request 的通知格式可能會需要不同的內容。例如:
Hello {REVIEWER_NAME},這是最新的 {MR_LINK},再麻煩你,謝謝
Hello 這是最新的 {MR_LINK} by {author}
如果我們只能維持單一模板,就會變成使用者每次複製前都要手動調整,令人非常煩躁。
因此,今天的目標是:
讓 Extension 可以根據「專案 ID 或 GitLab 網域」自動套用不同的模板格式。
判斷目前所在專案
https://gitlab.com/gitlab-org/security-products/demos/experiments/clangsa/highway/-/merge_requests/14
https://gitlab.com/gitlab-org/security-products/demos/experiments/clangsa/cmake/OpenSceneGraph/-/merge_requests/1
那麼 project 就能成為辨識的依據。function getProjectKey() {
const url = window.location.href;
const beforeDash = url.split('/-/')[0];
const parts = beforeDash.split('/');
return parts.pop() || null;
}
儲存多專案對應模板
使用 chrome.storage.sync 來存放:
{
"reviewTemplates": {
"projectA": "${title} by ${author}",
"projectB": "[${jiraId}] ${title}"
}
}
讀取對應模板
chrome.storage.sync.get("reviewTemplates", (data) => {
const projectKey = getProjectKey();
const template = data.reviewTemplates?.[projectKey] || data.reviewTemplates?.default;
console.log("使用模板:", template);
});
以上為大略的解決方向,實作上會遇到一點小問題,明天一起來解決吧!