-
Notifications
You must be signed in to change notification settings - Fork 5
feat: [CI-14828] : Added Error and Output Secret Handling. #43
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 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
06247f4
Added Error and Output Secret Handling.
DevanshMathur19 070cccc
Update harness/variables.go
Ompragash e92ac96
Update harness/variables.go
Ompragash 60cff6e
Update harness/variables.go
Ompragash c73ad8c
Update harness/variables.go
Ompragash 152cb7c
Using CI_ERROR_MESSAGE and CI_ERROR_CODE
DevanshMathur19 d3480b4
Using CI_ERROR_MESSAGE and CI_ERROR_CODE
DevanshMathur19 c3cb2ba
Fixing go.mod
DevanshMathur19 5f6c33d
Update harness/variables.go
Ompragash daa1d98
Update harness/variables.go
Ompragash 49acda8
Update harness/variables.go
Ompragash 108862e
Update harness/variables.go
Ompragash cb2df35
Update harness/variables.go
Ompragash 0594eed
Update harness/variables.go
Ompragash ca32e59
Updating variables and functions.
DevanshMathur19 7fdd7d8
Update harness/variables.go
Ompragash bbf67d8
Adding new functions and updating Update and Delete methods.
DevanshMathur19 85e2820
Optimizing code .
DevanshMathur19 deedf89
Fix staticcheck issue in .drone.yml
Ompragash 3de8df9
Update .drone.yml
Ompragash ad9dad0
Merge pull request #44 from drone-plugins/fix-staticcheck
Ompragash 05a2bbb
Adding UT's.
DevanshMathur19 4891b90
Optimizing Tests.
DevanshMathur19 cc345e6
Fixing lint errors.
DevanshMathur19 d92cff4
Rebasing and fixing.
DevanshMathur19 b8d1f0c
Merge branch 'CI-14828' of https://github.com/DevanshMathur19/drone-p…
DevanshMathur19 ff7bb9a
Changing functionality of ParseKeyValue and adding tests.
DevanshMathur19 4f13a99
Adding multiline tests.
DevanshMathur19 ba76946
Using godotenv for .env files and new tests.
DevanshMathur19 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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package harness | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| // ErrorMessageKey is the key used to retrieve or store the error message content. | ||
| ErrorMessageKey = "ERROR_MESSAGE" | ||
|
|
||
| // ErrorCodeKey is the key used to identify the specific error code associated with an error. | ||
| ErrorCodeKey = "ERROR_CODE" | ||
|
|
||
| // ErrorCategoryKey is the key used to classify the category of the error, which can help in grouping similar types of errors. | ||
| ErrorCategoryKey = "ERROR_CATEGORY" | ||
|
|
||
| // MetadataFile is the key for the file that stores metadata associated with an error, such as details about the error's source or context. | ||
| MetadataFile = "ERROR_METADATA_FILE" | ||
|
|
||
| // DroneOutputFile is the key for the file where output can exported and utilized in the subsequent steps in Harness CI pipeline. | ||
| DroneOutputFile = "DRONE_OUTPUT" | ||
| ) | ||
|
|
||
| // SetSecret sets a new secret by adding it to the DRONE_OUTPUT file | ||
| func SetSecret(name, value string) error { | ||
DevanshMathur19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return WriteEnvToOutputFile(DroneOutputFile, name, value) | ||
| } | ||
|
|
||
| // UpdateSecret updates an existing secret with a new value in the DRONE_OUTPUT file | ||
| func UpdateSecret(name, value string) error { | ||
| return WriteEnvToOutputFile(DroneOutputFile, name, value) | ||
| } | ||
|
|
||
| // DeleteSecret removes a secret by setting it to an empty value in the DRONE_OUTPUT file | ||
| func DeleteSecret(name string) error { | ||
| return WriteEnvToOutputFile(DroneOutputFile, name, "") | ||
DevanshMathur19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // SetError sets the error message and error code, writing them to the CI_ERROR_METADATA file | ||
| // SetError sets the error message, error code, and error category, writing them to the CI_ERROR_METADATA file | ||
| func SetError(message, code, category string) error { | ||
DevanshMathur19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Write the error message | ||
| if err := WriteEnvToOutputFile(MetadataFile, ErrorMessageKey, message); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Write the error code | ||
| if err := WriteEnvToOutputFile(MetadataFile, ErrorCodeKey, code); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Write the error category | ||
| if err := WriteEnvToOutputFile(MetadataFile, ErrorCategoryKey, category); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // WriteEnvToOutputFile writes a key-value pair to the specified file, determined by an environment variable | ||
| func WriteEnvToOutputFile(envVar, key, value string) error { | ||
| // Get the file path from the specified environment variable | ||
| filePath := os.Getenv(envVar) | ||
| if filePath == "" { | ||
| return fmt.Errorf("environment variable %s is not set", envVar) | ||
| } | ||
|
|
||
| // Check the extension of the file (.env or .out) | ||
| ext := strings.ToLower(filepath.Ext(filePath)) | ||
|
|
||
| var content string | ||
| if ext == ".env" { | ||
| // Write in .env format (KEY=VALUE) | ||
| content = fmt.Sprintf("%s=%s\n", key, value) | ||
| } else if ext == ".out" { | ||
| // Write in .out format (export KEY="VALUE") | ||
DevanshMathur19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| content = fmt.Sprintf("%s \"%s\"\n", key, value) | ||
| } else { | ||
| return fmt.Errorf("unsupported file extension: %s", ext) | ||
| } | ||
|
|
||
| return writeToFile(filePath, content) | ||
| } | ||
|
|
||
| // Helper function to append content to the file | ||
| func writeToFile(filename, content string) error { | ||
DevanshMathur19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open file: %w", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| _, err = f.WriteString(content) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to write to file: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.