Repository: codecov/uploader
Pull request #823 (2022-07-14 13:50:12)

refactor: exit on invalid slug
If slug is missing a forward slash, or "undefined", log error and exit

codecov[bot]: # [Codecov](https://app.codecov.io/gh/codecov/uploader/compare/823?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) Report
> Merging [#823](https://app.codecov.io/gh/codecov/uploader/compare/823?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (1e96090) into [main](https://codecov.io/gh/codecov/uploader/commit/532ef1b95903cb6c8d0b7e970485c49342c09792?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (532ef1b) will **decrease** coverage by `0.03%`.
> The diff coverage is `66.66%`.

```diff
@@            Coverage Diff             @@
##             main     #823      +/-   ##
==========================================
- Coverage   92.09%   92.05%   -0.04%     
==========================================
  Files          33       34       +1     
  Lines        1164     1171       +7     
  Branches      237      239       +2     
==========================================
+ Hits         1072     1078       +6     
- Misses         61       63       +2     
+ Partials       31       30       -1     
```

| Flag | Coverage Δ | |
|---|---|---|
| alpine | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| alpine-proxy | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| alpine-without-git | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| linux | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| linux-without-git | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| macos | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| macos-without-git | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| windows | `92.05% <66.66%> (-0.04%)` | :arrow_down: |
| windows-without-git | `92.05% <66.66%> (-0.04%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/codecov/uploader/compare/823?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://app.codecov.io/gh/codecov/uploader/compare/823/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#diff-c3JjL2luZGV4LnRz) | `75.16% <40.00%> (-0.18%)` | :arrow_down: |
| [src/helpers/checkSlug.ts](https://app.codecov.io/gh/codecov/uploader/compare/823/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#diff-c3JjL2hlbHBlcnMvY2hlY2tTbHVnLnRz) | `100.00% <100.00%> (ø)` | |

lgtm-com[bot]: This pull request **introduces 1 alert** when merging 629db8b1180638f467d581c858770904e4e6f523 into 9f1f09bf2ef2842ea05275b2b0f9eb82b84e78b4 - [view on LGTM.com](https://lgtm.com/projects/g/codecov/uploader/rev/pr-e7f824e1b389382235f223eec745d804b91fe14b)

**new alerts:**

* 1 for Unused variable, import, function or class
lgtm-com[bot]: This pull request **introduces 1 alert** when merging 5d78f42be1bba8edef9b39d2e5acda07bd19fb79 into 6e53069730a249f900ab85ff2e7629f198d3edb7 - [view on LGTM.com](https://lgtm.com/projects/g/codecov/uploader/rev/pr-7a7b6b1ad040728652d589851156a788176a315a)

**new alerts:**

* 1 for Unused variable, import, function or class

Patch:
diff --git a/src/helpers/checkSlug.ts b/src/helpers/checkSlug.ts
new file mode 100644
index 0000000..dc7ff2a
--- /dev/null
+++ b/src/helpers/checkSlug.ts
@@ -0,0 +1,6 @@
+export function checkSlug(slug: string | undefined): boolean {
+  if (slug !== '' && !slug?.match(/\//)) {
+    return false
+  }
+  return true
+}
diff --git a/src/index.ts b/src/index.ts
index 5d372d8..b89dcee 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -4,7 +4,7 @@ import zlib from 'zlib'
 import { version } from '../package.json'
 import { detectProvider } from './helpers/provider'
 import * as webHelpers from './helpers/web'
-import { info, logError, UploadLogger } from './helpers/logger'
+import { info, UploadLogger } from './helpers/logger'
 import { getToken } from './helpers/token'
 import {
   cleanCoverageFilePaths,
@@ -26,6 +26,7 @@ import { generateCoveragePyFile } from './helpers/coveragepy'
 import { generateGcovCoverageFiles } from './helpers/gcov'
 import { generateXcodeCoverageFiles } from './helpers/xcode'
 import { argAsArray } from './helpers/util'
+import { checkSlug } from './helpers/checkSlug'
 
 /**
  *
@@ -325,8 +326,9 @@ export async function main(
     UploadLogger.verbose(`${parameter}`)
   }
 
-  if (buildParams.slug !== '' && !buildParams.slug?.match(/\//)) {
-    logError(`Slug must follow the format of "<owner>/<repo>" or be blank. We detected "${buildParams.slug}"`)
+  const validSlug =  checkSlug(buildParams.slug)
+  if (!validSlug) {
+    throw new Error(`Slug must follow the format of "<owner>/<repo>" or be blank. We detected "${buildParams.slug}"`)
   }
 
   const query = webHelpers.generateQuery(buildParams)


Test patch:
diff --git a/test/helpers/checkSlug.test.ts b/test/helpers/checkSlug.test.ts
new file mode 100644
index 0000000..875ae15
--- /dev/null
+++ b/test/helpers/checkSlug.test.ts
@@ -0,0 +1,39 @@
+import { checkSlug } from "../../src/helpers/checkSlug";
+
+describe('checkSlug()', () => {
+    it('should return true for a slug with a forward slash', () => {
+        // arrange
+        const inputSlug = "testOrg/testRepo"
+        const expectedResult = true
+
+        // act
+        const result = checkSlug(inputSlug)
+
+        // assert
+        expect(result).toEqual(expectedResult)
+    })
+
+    it('should return false for a slug without a forward slash', () => {
+        // arrange
+        const inputSlug = 'testRepo'
+        const expectedResult = false
+
+        // act
+        const result = checkSlug(inputSlug)
+
+        // assert
+        expect(result).toEqual(expectedResult)
+    })
+
+    it('should return false for a slug that is "undefined"', () => {
+        // arrange
+        const inputSlug = undefined
+        const expectedResult = false
+
+        // act
+        const result = checkSlug(inputSlug)
+
+        // assert
+        expect(result).toEqual(expectedResult)
+    })
+})
\ No newline at end of file