From fa3839cdab3a7fc7b15c1cd35b4da175d4e28b07 Mon Sep 17 00:00:00 2001
From: Paula Barszcz <paula.anna.barszcz@gmail.com>
Date: Tue, 2 Aug 2022 23:33:13 +0200
Subject: [PATCH] [GitHub] GitHub file size for a specific branch (#8262)

* Modify github-size.service.js

* Add tests for Github Size Service to test the new ref parameter

* Modified request for a file size: ref is now sent only if if was specified
---
 services/github/github-size.service.js | 44 +++++++++++++++++++++-----
 services/github/github-size.tester.js  | 16 ++++++++++
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/services/github/github-size.service.js b/services/github/github-size.service.js
index b932faf17e..3dfa6692b1 100644
--- a/services/github/github-size.service.js
+++ b/services/github/github-size.service.js
@@ -5,6 +5,10 @@ import { NotFound } from '../index.js'
 import { GithubAuthV3Service } from './github-auth-service.js'
 import { documentation, errorMessagesFor } from './github-helpers.js'
 
+const queryParamSchema = Joi.object({
+  branch: Joi.string(),
+}).required()
+
 const schema = Joi.alternatives(
   Joi.object({
     size: nonNegativeInteger,
@@ -18,6 +22,7 @@ export default class GithubSize extends GithubAuthV3Service {
   static route = {
     base: 'github/size',
     pattern: ':user/:repo/:path*',
+    queryParamSchema,
   }
 
   static examples = [
@@ -32,6 +37,20 @@ export default class GithubSize extends GithubAuthV3Service {
       keywords: ['repo'],
       documentation,
     },
+    {
+      title: 'GitHub file size in bytes on a specified ref (branch/commit/tag)',
+      namedParams: {
+        user: 'webcaetano',
+        repo: 'craft',
+        path: 'build/phaser-craft.min.js',
+      },
+      staticPreview: this.render({ size: 9170 }),
+      keywords: ['repo'],
+      documentation,
+      queryParams: {
+        branch: 'master',
+      },
+    },
   ]
 
   static render({ size }) {
@@ -41,16 +60,25 @@ export default class GithubSize extends GithubAuthV3Service {
     }
   }
 
-  async fetch({ user, repo, path }) {
-    return this._requestJson({
-      url: `/repos/${user}/${repo}/contents/${path}`,
-      schema,
-      errorMessages: errorMessagesFor('repo or file not found'),
-    })
+  async fetch({ user, repo, path, branch }) {
+    if (branch) {
+      return this._requestJson({
+        url: `/repos/${user}/${repo}/contents/${path}?ref=${branch}`,
+        schema,
+        errorMessages: errorMessagesFor('repo, branch or file not found'),
+      })
+    } else {
+      return this._requestJson({
+        url: `/repos/${user}/${repo}/contents/${path}`,
+        schema,
+        errorMessages: errorMessagesFor('repo or file not found'),
+      })
+    }
   }
 
-  async handle({ user, repo, path }) {
-    const body = await this.fetch({ user, repo, path })
+  async handle({ user, repo, path }, queryParams) {
+    const branch = queryParams.branch
+    const body = await this.fetch({ user, repo, path, branch })
     if (Array.isArray(body)) {
       throw new NotFound({ prettyMessage: 'not a regular file' })
     }
diff --git a/services/github/github-size.tester.js b/services/github/github-size.tester.js
index db61aa9ac9..644b482dba 100644
--- a/services/github/github-size.tester.js
+++ b/services/github/github-size.tester.js
@@ -10,6 +10,22 @@ t.create('File size 404')
   .get('/webcaetano/craft/build/does-not-exist.min.js.json')
   .expectBadge({ label: 'size', message: 'repo or file not found' })
 
+t.create('File size for nonexisting branch')
+  .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=notARealBranch')
+  .expectBadge({ label: 'size', message: 'repo, branch or file not found' })
+
 t.create('File size for "not a regular file"')
   .get('/webcaetano/craft/build.json')
   .expectBadge({ label: 'size', message: 'not a regular file' })
+
+t.create('File size for a specified branch')
+  .get('/webcaetano/craft/build/craft.min.js.json?branch=version-2')
+  .expectBadge({ label: 'size', message: isFileSize })
+
+t.create('File size for a specified tag')
+  .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=2.1.2')
+  .expectBadge({ label: 'size', message: isFileSize })
+
+t.create('File size for a specified commit')
+  .get('/webcaetano/craft/build/phaser-craft.min.js.json?branch=b848dbb')
+  .expectBadge({ label: 'size', message: isFileSize })
-- 
GitLab