Просмотр исходного кода

Merge pull request #8868 from weseek/imprv/147138-147139-suppor-tag-for-github

imprv(plugin): Support github tag in githuburl.ts
Yuki Takei 1 год назад
Родитель
Сommit
7f39e6a5cd
1 измененных файлов с 9 добавлено и 17 удалено
  1. 9 17
      apps/app/src/features/growi-plugin/server/models/vo/github-url.ts

+ 9 - 17
apps/app/src/features/growi-plugin/server/models/vo/github-url.ts

@@ -1,13 +1,14 @@
+
 import sanitize from 'sanitize-filename';
 
 // https://regex101.com/r/fK2rV3/1
 const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
-// https://regex101.com/r/CQjSuz/1
-const sanitizeBranchChars = new RegExp(/[^a-zA-Z0-9_.]+/g);
 
-// https://regex101.com/r/f4wj8q/1
+// https://regex101.com/r/CQjSuz/1
+const sanitizeSymbolsChars = new RegExp(/[^a-zA-Z0-9_.]+/g);
+// https://regex101.com/r/ARgXvb/1
 // GitHub will return a zip file with the v removed if the tag or branch name is "v + number"
-const checkVersionName = new RegExp(/^v[\d]/g);
+const sanitizeVersionChars = new RegExp(/^v[\d]/gi);
 
 export class GitHubUrl {
 
@@ -38,23 +39,14 @@ export class GitHubUrl {
   get archiveUrl(): string {
     const encodedBranchName = encodeURIComponent(this.branchName);
     const encodedTagName = encodeURIComponent(this.tagName);
-    if (encodedTagName !== '') {
-      const ghUrl = new URL(`/${this.organizationName}/${this.reposName}/archive/refs/tags/${encodedTagName}.zip`, 'https://github.com');
-      return ghUrl.toString();
-    }
-
-    const ghUrl = new URL(`/${this.organizationName}/${this.reposName}/archive/refs/heads/${encodedBranchName}.zip`, 'https://github.com');
+    const zipUrl = encodedTagName !== '' ? `tags/${encodedTagName}` : `heads/${encodedBranchName}`;
+    const ghUrl = new URL(`/${this.organizationName}/${this.reposName}/archive/refs/${zipUrl}.zip`, 'https://github.com');
     return ghUrl.toString();
-
   }
 
   get extractedArchiveDirName(): string {
-    if (this._tagName !== '') {
-      const tagName = this._tagName?.match(checkVersionName) ? this._tagName.replace('v', '') : this._tagName;
-      return tagName.replaceAll(sanitizeBranchChars, '-');
-    }
-    const branchName = this._branchName?.match(checkVersionName) ? this._branchName.replace('v', '') : this._branchName;
-    return branchName.replaceAll(sanitizeBranchChars, '-');
+    const name = this._tagName !== '' ? this._tagName : this._branchName;
+    return name.replace(sanitizeVersionChars, m => m.substring(1)).replaceAll(sanitizeSymbolsChars, '-');
   }
 
   constructor(url: string, branchName = 'main', tagName = '') {