-
Notifications
You must be signed in to change notification settings - Fork 1
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
Use structs as a container for Action inputs #17
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4a520df
refactor: add string constants for supported destinations
timorthi 5802e19
refactor: remove unused const
timorthi d370932
refactor: logically group constants
timorthi 457b7df
feat: return inputs after validation
timorthi 5a0525c
refactor: don't repeat names in key
timorthi 23c7249
refactor: use actionInputs struct to access inputs
timorthi dd7d994
chore: use dockerfile
timorthi df80cfa
refactor: rename to accessToken
timorthi 8ad94f6
refactor: clean up log calls
timorthi aada45b
docs: add note on repoFullName
timorthi 0df8101
test: consolidate tests
timorthi a5ebcad
feat: report all missing inputs
timorthi 7d3372f
docs: annotate input structs
timorthi 9c0883a
Revert "chore: use dockerfile"
timorthi bfebc8f
refactor: id -> ID
timorthi 0b0cf45
feat: sort inputs in error msg
timorthi 54111c9
refactor: use lowercase
timorthi 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 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 |
---|---|---|
|
@@ -8,30 +8,60 @@ import ( | |
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Required Action inputs | ||
var ( | ||
inputRepoToken *string = flag.String(inputKeyRepoToken, "", "GITHUB_TOKEN or a Personal Access Token") | ||
inputWorkflowRunID *int64 = flag.Int64(inputKeyWorkflowRunID, 0, "GitHub Actions Workflow Run ID") | ||
inputDestination *string = flag.String(inputKeyDestination, "", "The service to export workflow logs to") | ||
) | ||
|
||
// Required inputs for S3 | ||
|
||
// Required inputs for S3 | ||
var ( | ||
inputAWSAccessKeyID *string = flag.String(inputKeyAWSAccessKeyID, "", "AWS Access Key ID") | ||
inputAWSSecretAccessKey *string = flag.String(inputKeyAWSSecretAccessKey, "", "AWS Secret Access Key") | ||
inputAWSRegion *string = flag.String(inputKeyAWSRegion, "us-east-1", "AWS Region for the S3 bucket") | ||
inputS3BucketName *string = flag.String(inputKeyS3BucketName, "", "S3 bucket name") | ||
inputS3Key *string = flag.String(inputKeyS3Key, "", "S3 key") | ||
) | ||
|
||
// Required inputs for Azure Blob Storage | ||
|
||
// Required inputs for Azure Blob Storage | ||
var ( | ||
inputAzureStorageAccountName *string = flag.String(inputKeyAzureStorageAccountName, "", "Storage account name") | ||
inputAzureStorageAccountKey *string = flag.String(inputKeyAzureStorageAccountKey, "", "Storage account key") | ||
inputContainerName *string = flag.String(inputKeyContainerName, "", "Azure blob storage container name") | ||
inputBlobName *string = flag.String(inputKeyBlobName, "", "Azure blob name") | ||
) | ||
|
||
// S3ActionInputs contains inputs required for the `s3` destination | ||
type S3ActionInputs struct { | ||
awsAccessKeyID string | ||
awsSecretAccessKey string | ||
awsRegion string | ||
bucketName string | ||
key string | ||
} | ||
|
||
// BlobStorageActionInputs contains inputs required for the `blobstorage` destination | ||
type BlobStorageActionInputs struct { | ||
storageAccountName string | ||
storageAccountKey string | ||
containerName string | ||
blobName string | ||
} | ||
|
||
// ActionInputs contains all the pertinent inputs for this GitHub Action. For a given destination, its corresponding | ||
// struct field (e.g. s3Inputs for the `s3` destination) is assumed to be non-nil. | ||
type ActionInputs struct { | ||
repoToken string | ||
workflowRunID int64 | ||
destination string | ||
s3Inputs *S3ActionInputs | ||
blobStorageInputs *BlobStorageActionInputs | ||
} | ||
|
||
// validateActionInputs validates input combinations that cannot be checked at the action-level. | ||
// In particular, ensures that the destination is valid and any other inputs required for that destination are present. | ||
func validateActionInputs() error { | ||
func validateActionInputs() (ActionInputs, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
var matchedDestination string | ||
for _, destination := range supportedDestinations { | ||
if strings.EqualFold(destination, *inputDestination) { | ||
|
@@ -41,16 +71,26 @@ func validateActionInputs() error { | |
} | ||
} | ||
if matchedDestination == "" { | ||
return fmt.Errorf( | ||
return ActionInputs{}, fmt.Errorf( | ||
"supplied destination %s is invalid. Supported values are: %s", | ||
*inputDestination, | ||
strings.Join(supportedDestinations, ", "), | ||
) | ||
} | ||
|
||
var inputFlagsToAssertNotEmpty map[string]string | ||
if matchedDestination == "s3" { | ||
var s3Inputs *S3ActionInputs | ||
var blobStorageInputs *BlobStorageActionInputs | ||
|
||
if matchedDestination == AmazonS3Destination { | ||
log.Debug().Msg("Validating Action inputs for S3") | ||
s3Inputs = &S3ActionInputs{ | ||
awsAccessKeyID: *inputAWSAccessKeyID, | ||
awsSecretAccessKey: *inputAWSSecretAccessKey, | ||
awsRegion: *inputAWSRegion, | ||
bucketName: *inputS3BucketName, | ||
key: *inputS3Key, | ||
} | ||
inputFlagsToAssertNotEmpty = map[string]string{ | ||
inputKeyAWSAccessKeyID: *inputAWSAccessKeyID, | ||
inputKeyAWSSecretAccessKey: *inputAWSSecretAccessKey, | ||
|
@@ -60,8 +100,14 @@ func validateActionInputs() error { | |
} | ||
} | ||
|
||
if matchedDestination == "blobstorage" { | ||
if matchedDestination == AzureBlobStorageDestination { | ||
log.Debug().Msg("Validating Action inputs for Blob Storage") | ||
blobStorageInputs = &BlobStorageActionInputs{ | ||
storageAccountName: *inputAzureStorageAccountName, | ||
storageAccountKey: *inputAzureStorageAccountKey, | ||
containerName: *inputContainerName, | ||
blobName: *inputBlobName, | ||
} | ||
inputFlagsToAssertNotEmpty = map[string]string{ | ||
inputKeyAzureStorageAccountName: *inputAzureStorageAccountName, | ||
inputKeyAzureStorageAccountKey: *inputAzureStorageAccountKey, | ||
|
@@ -70,12 +116,22 @@ func validateActionInputs() error { | |
} | ||
} | ||
|
||
var emptyInputs []string | ||
for inputName, inputValue := range inputFlagsToAssertNotEmpty { | ||
if len(inputValue) == 0 { | ||
return fmt.Errorf("the input '%s' is required", inputName) | ||
emptyInputs = append(emptyInputs, inputName) | ||
} | ||
} | ||
if len(emptyInputs) > 0 { | ||
return ActionInputs{}, fmt.Errorf("the following inputs are required: %s", strings.Join(emptyInputs, ", ")) | ||
} | ||
|
||
log.Debug().Msg("Action input validation was successful") | ||
return nil | ||
return ActionInputs{ | ||
repoToken: *inputRepoToken, | ||
workflowRunID: *inputWorkflowRunID, | ||
destination: matchedDestination, | ||
s3Inputs: s3Inputs, | ||
blobStorageInputs: blobStorageInputs, | ||
}, nil | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -1,32 +1,46 @@ | ||
package main | ||
|
||
// Environment variables | ||
// GitHub Actions default env vars reference: https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | ||
const ( | ||
// GitHub Actions default env vars reference: https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables | ||
envVarGitHubServerURL string = "GITHUB_SERVER_URL" | ||
envVarRepoOwner string = "GITHUB_REPOSITORY_OWNER" | ||
envVarRepoFullName string = "GITHUB_REPOSITORY" | ||
envVarRunnerDebug string = "RUNNER_DEBUG" | ||
envVarDebug string = "DEBUG" | ||
) | ||
|
||
tempFileName string = "logs.zip" | ||
githubDefaultBaseURL string = "https://github.com" | ||
|
||
// Action inputs | ||
const ( | ||
inputKeyRepoToken string = "repo-token" | ||
inputKeyWorkflowRunID string = "run-id" | ||
inputKeyDestination string = "destination" | ||
) | ||
|
||
// S3 Action Inputs | ||
const ( | ||
inputKeyAWSAccessKeyID string = "aws-access-key-id" | ||
inputKeyAWSSecretAccessKey string = "aws-secret-access-key" | ||
inputKeyAWSRegion string = "aws-region" | ||
inputKeyS3BucketName string = "s3-bucket-name" | ||
inputKeyS3Key string = "s3-key" | ||
) | ||
|
||
// Blob Storage Action Inputs | ||
const ( | ||
inputKeyAzureStorageAccountName string = "azure-storage-account-name" | ||
inputKeyAzureStorageAccountKey string = "azure-storage-account-key" | ||
inputKeyContainerName string = "container-name" | ||
inputKeyBlobName string = "blob-name" | ||
) | ||
|
||
// Misc constants | ||
const ( | ||
AmazonS3Destination string = "s3" | ||
AzureBlobStorageDestination string = "blobstorage" | ||
githubDefaultBaseURL string = "https://github.com" | ||
) | ||
|
||
var ( | ||
supportedDestinations = []string{"s3", "blobstorage"} | ||
supportedDestinations = []string{AmazonS3Destination, AzureBlobStorageDestination} | ||
) |
This file contains 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
Oops, something went wrong.
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.
Function
validateActionInputs
has 67 lines of code (exceeds 50 allowed). Consider refactoring.