-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add workflow input handling and YAML support #37
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
Open
MDrakakis
wants to merge
1
commit into
timheuer:main
Choose a base branch
from
MDrakakis:feature/Handle-inputs-on-runs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+696
−14
Open
Changes from all commits
Commits
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
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,27 @@ | ||
| namespace GitHubActionsVS.Helpers | ||
| { | ||
| internal class StringHelpers | ||
| { | ||
| public static bool IsBase64(string input) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(input) || input.Length % 4 != 0) | ||
| return false; | ||
|
|
||
| foreach (char c in input) | ||
| { | ||
| if (!(char.IsLetterOrDigit(c) || c == '+' || c == '/' || c == '=')) | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| _ = Convert.FromBase64String(input); | ||
| return true; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,58 @@ | ||||||
| using System.Reflection; | ||||||
| using YamlDotNet.RepresentationModel; | ||||||
|
|
||||||
| namespace GitHubActionsVS.Helpers | ||||||
| { | ||||||
| internal static class YamlHelpers | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// Follows YamlDotNet alias nodes (&anchor / *alias) to the real node. | ||||||
| /// Returns the resolved node if an alias chain exists. | ||||||
| /// </summary> | ||||||
| public static YamlNode Unalias(YamlNode node) | ||||||
| { | ||||||
| // Use reflection to check for YamlAliasNode and access RealNode | ||||||
| var aliasType = node.GetType().FullName == "YamlDotNet.RepresentationModel.YamlAliasNode" | ||||||
| ? node.GetType() | ||||||
| : null; | ||||||
|
|
||||||
| while (aliasType != null) | ||||||
| { | ||||||
| var realNodeProp = aliasType.GetProperty("RealNode", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); | ||||||
| if (realNodeProp?.GetValue(node) is not YamlNode realNode) | ||||||
| break; | ||||||
| node = realNode; | ||||||
| aliasType = node.GetType().FullName == "YamlDotNet.RepresentationModel.YamlAliasNode" | ||||||
| ? node.GetType() | ||||||
| : null; | ||||||
| } | ||||||
|
|
||||||
| return node; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Attempts to retrieve a child value from a mapping node by key name. | ||||||
| /// Uses string comparison on scalar node values rather than allocating new nodes. | ||||||
| /// </summary> | ||||||
| /// <param name="map">The mapping node to search.</param> | ||||||
| /// <param name="key">The scalar key string to match.</param> | ||||||
| /// <param name="value">The value node if found.</param> | ||||||
| /// <returns>True if a matching key was found, otherwise false.</returns> | ||||||
| public static bool TryGetScalarKey(YamlMappingNode map, string key, out YamlNode value) | ||||||
| { | ||||||
| foreach (var kv in map.Children) | ||||||
| { | ||||||
| if (kv.Key is YamlScalarNode sk && | ||||||
| string.Equals(sk.Value, key, StringComparison.Ordinal)) | ||||||
| { | ||||||
| value = kv.Value; | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| value = null!; | ||||||
|
||||||
| value = null!; | |
| value = default; |
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,82 @@ | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
|
|
||
| namespace GitHubActionsVS.Models | ||
| { | ||
| public sealed class InputMetadata | ||
| { | ||
| public string Name { get; } | ||
| public string Type { get; } | ||
| public string Description { get; } | ||
| public bool Required { get; } | ||
| public string Default { get; } | ||
| public string[] Options { get; } | ||
|
|
||
| public InputMetadata(string name, string type, string description, bool required, string @default, string[] options) | ||
| { | ||
| Name = name; | ||
| Type = type; | ||
| Description = description; | ||
| Required = required; | ||
| Default = @default; | ||
| Options = options; | ||
| } | ||
|
|
||
| public static List<InputMetadata> ToInputMeta(IReadOnlyDictionary<string, IReadOnlyDictionary<string, object>> inputs) | ||
| { | ||
| var list = new List<InputMetadata>(); | ||
|
|
||
| try | ||
| { | ||
| foreach (var kv in inputs) | ||
| { | ||
| var name = kv.Key; | ||
| var dict = kv.Value; | ||
|
|
||
| dict.TryGetValue("type", out var typeObj); | ||
| dict.TryGetValue("description", out var descObj); | ||
| dict.TryGetValue("required", out var reqObj); | ||
| dict.TryGetValue("default", out var defObj); | ||
| dict.TryGetValue("options", out var optsObj); | ||
|
|
||
| var type = (typeObj?.ToString()?.Trim().ToLowerInvariant()) switch | ||
| { | ||
| "boolean" => "boolean", | ||
| "choice" => "choice", | ||
| "environment" => "environment", | ||
| _ => "string" | ||
| }; | ||
|
|
||
| string[] options = null; | ||
|
|
||
| if (optsObj is IEnumerable<object> seq) | ||
| { | ||
| options = [.. seq.Select(o => o?.ToString() ?? string.Empty)]; | ||
| } | ||
| else if (optsObj is string s) | ||
| { | ||
| options = [s]; | ||
| } | ||
|
|
||
| var required = reqObj is bool b ? b : bool.TryParse(reqObj?.ToString(), out var br) && br; | ||
|
|
||
| list.Add(new InputMetadata( | ||
| name, | ||
| type, | ||
| descObj?.ToString(), | ||
| required, | ||
| defObj?.ToString(), | ||
| options | ||
| )); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Debug.WriteLine($"Failed to create InputMetadata. Ex: {ex.Message}"); | ||
| } | ||
|
|
||
| return list; | ||
| } | ||
| } | ||
| } |
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,22 @@ | ||
| using System.Collections.Generic; | ||
| using YamlDotNet.Serialization; | ||
|
|
||
| namespace GitHubActionsVS.Models | ||
| { | ||
| sealed class WorkflowRoot | ||
| { | ||
| [YamlMember(Alias = "on")] | ||
| public OnSection On { get; init; } | ||
| } | ||
|
|
||
| sealed class OnSection | ||
| { | ||
| [YamlMember(Alias = "workflow_dispatch")] | ||
| public WorkflowDispatch WorkflowDispatch { get; init; } | ||
| } | ||
|
|
||
| sealed class WorkflowDispatch | ||
| { | ||
| public Dictionary<string, Dictionary<string, object>> Inputs { get; init; } | ||
| } | ||
| } |
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
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.
The StringHelpers class should be declared as 'static' since all its methods are static. This prevents instantiation and makes the intent clearer.