-
Notifications
You must be signed in to change notification settings - Fork 50
chore: add release discovery #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c0a8b6
chore: add release discovery
vladanpaunovic 0c49a03
Merge branch 'main' into add-release-mmt
vladanpaunovic e2ca9a1
chore: update release name in gh action
vladanpaunovic 531b81c
Update packages/unplugin/test/getReleaseName.test.ts
vladanpaunovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ on: | |
|
|
||
| env: | ||
| DEFAULT_NODE_VERSION: "16.15.1" | ||
| SENTRY_RELEASE: "default_release_name" | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import * as child_process from "child_process"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
|
|
||
| function isGit(dir: string) { | ||
| return fs.existsSync(path.join(dir, ".git")); | ||
| } | ||
|
|
||
| function getBranchHead() { | ||
| return child_process.execSync("git rev-parse HEAD").toString().trim(); | ||
| } | ||
|
|
||
| export function getReleaseName(releaseName?: string): string { | ||
| if (releaseName) { | ||
| return releaseName; | ||
| } | ||
|
|
||
| const ENV_VARS = [ | ||
| "SENTRY_RELEASE", | ||
| "SOURCE_VERSION", // Heroku #1 https://devcenter.heroku.com/changelog-items/630 | ||
| "HEROKU_SLUG_COMMIT", // Heroku #2: https://docs.sentry.io/product/integrations/deployment/heroku/#configure-releases | ||
| "CODEBUILD_RESOLVED_SOURCE_VERSION", // AWS CodeBuild: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html | ||
| "CIRCLE_SHA1", // CircleCI: https://circleci.com/docs/2.0/env-vars/ | ||
| "VERCEL_GIT_COMMIT_SHA", // Vercel docs: https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables | ||
| ]; | ||
|
|
||
| const releaseFromEnvironmentVar = ENV_VARS.find((key) => Object.keys(process.env).includes(key)); | ||
|
|
||
| if (releaseFromEnvironmentVar) { | ||
| return process.env[releaseFromEnvironmentVar] as string; | ||
| } | ||
|
|
||
| if (isGit(process.cwd())) { | ||
| return getBranchHead(); | ||
| } else { | ||
| throw new Error("Could not return a release name"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { getReleaseName } from "../src/getReleaseName"; | ||
| import * as fs from "fs"; | ||
| import * as child_process from "child_process"; | ||
| jest.mock("fs"); | ||
| jest.mock("child_process"); | ||
|
|
||
| const mockedFs = fs; | ||
| const mockedChildProcess = child_process; | ||
|
|
||
| describe("environmental getReleaseName", () => { | ||
| const OLD_ENV = process.env; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
| process.env = { ...OLD_ENV }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| (mockedChildProcess.execSync as jest.Mock).mockRestore(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env = OLD_ENV; | ||
| }); | ||
|
|
||
| it("adheres to HEAD when git is present", () => { | ||
| (mockedFs.existsSync as jest.Mock).mockReturnValueOnce(true); | ||
| const sha = "c3f235fc86f1c4007e3a218ec82d666586e73cbf"; | ||
| (mockedChildProcess.execSync as jest.Mock).mockReturnValue(sha); | ||
|
|
||
| expect(getReleaseName()).toBe(sha); | ||
| }); | ||
|
|
||
| it("throws an error if no release information could be found", () => { | ||
| (mockedFs.existsSync as jest.Mock).mockReturnValueOnce(false); | ||
|
|
||
| expect(getReleaseName).toThrow("Could not return a release name"); | ||
| }); | ||
|
|
||
| it("adheres to user defined release name", () => { | ||
| const releaseName = "USER_DEFINED_this-is-my-custom-release"; | ||
|
|
||
| expect(getReleaseName(releaseName)).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("adheres to process.env.SENTRY_RELEASE", () => { | ||
| const releaseName = "SENTRY_RELEASE_string"; | ||
| process.env["SENTRY_RELEASE"] = releaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("adheres to Heroku: process.env.SOURCE_VERSION", () => { | ||
| const releaseName = "SOURCE_VERSION_string"; | ||
| process.env["SOURCE_VERSION"] = releaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("adheres to Heroku: process.env.HEROKU_SLUG_COMMIT", () => { | ||
| const releaseName = "HEROKU_SLUG_COMMIT_string"; | ||
| process.env["HEROKU_SLUG_COMMIT"] = releaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("adheres to AWS: process.env.CODEBUILD_RESOLVED_SOURCE_VERSION", () => { | ||
| const releaseName = "CODEBUILD_RESOLVED_SOURCE_VERSION_string"; | ||
| process.env["CODEBUILD_RESOLVED_SOURCE_VERSION"] = releaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("adheres to Vercel: process.env.VERCEL_GIT_COMMIT_SHA", () => { | ||
| const releaseName = "VERCEL_GIT_COMMIT_SHA_string"; | ||
| process.env["VERCEL_GIT_COMMIT_SHA"] = releaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(releaseName); | ||
| }); | ||
|
|
||
| it("allows SENTRY_RELEASE to take precedence over other env vars", () => { | ||
| const vercelReleaseName = "VERCEL_GIT_COMMIT_SHA_string"; | ||
| const sentryReleaseName = "SENTRY_RELEASE_string"; | ||
| process.env["VERCEL_GIT_COMMIT_SHA"] = vercelReleaseName; | ||
| process.env["SENTRY_RELEASE"] = sentryReleaseName; | ||
|
|
||
| expect(getReleaseName()).toBe(sentryReleaseName); | ||
| }); | ||
|
|
||
| it("allows custom release name to take precedence over other env vars", () => { | ||
| const vercelReleaseName = "VERCEL_GIT_COMMIT_SHA_string"; | ||
| const sentryReleaseName = "SENTRY_RELEASE_string"; | ||
| process.env["VERCEL_GIT_COMMIT_SHA"] = vercelReleaseName; | ||
| process.env["SENTRY_RELEASE"] = sentryReleaseName; | ||
|
|
||
| expect(getReleaseName("cutom_release_name")).toBe("cutom_release_name"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the order matter here? If yes, is the order correct?
(Looking at it again, I think it doesn't but I'm not sure about Heroku...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question! It does. This is why
SENTRY_RELEASEis on the top of the list, everything else is carrying equal weight. I was thinking to extractSENTRY_RELEASEfrom the rest of the pack but found it to be overkillThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining. Makes sense! Then, let's make sure to add a comment that
SENTRY_RELEASEshould be the first item in the list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ here https://github.com/getsentry/hackweek-sentry-unplugin/pull/22/files