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

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

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

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

@@ -2,8 +2,12 @@ import sanitize from 'sanitize-filename';
 
 // https://regex101.com/r/fK2rV3/1
 const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
-// https://regex101.com/r/YhZVsj/1
-const sanitizeChars = new RegExp(/[^a-zA-Z_.]+/g);
+// https://regex101.com/r/CQjSuz/1
+const sanitizeBranchChars = new RegExp(/[^a-zA-Z0-9_.]+/g);
+
+// https://regex101.com/r/f4wj8q/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);
 
 export class GitHubUrl {
 
@@ -13,6 +17,8 @@ export class GitHubUrl {
 
   private _branchName: string;
 
+  private _tagName: string;
+
   get organizationName(): string {
     return this._organizationName;
   }
@@ -25,17 +31,33 @@ export class GitHubUrl {
     return this._branchName;
   }
 
+  get tagName(): string {
+    return this._tagName;
+  }
+
   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');
     return ghUrl.toString();
+
   }
 
   get extractedArchiveDirName(): string {
-    return this._branchName.replaceAll(sanitizeChars, '-');
+    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, '-');
   }
 
-  constructor(url: string, branchName = 'main') {
+  constructor(url: string, branchName = 'main', tagName = '') {
 
     let matched;
     try {
@@ -52,6 +74,7 @@ export class GitHubUrl {
     }
 
     this._branchName = branchName;
+    this._tagName = tagName;
 
     this._organizationName = sanitize(matched[1]);
     this._reposName = sanitize(matched[2]);