-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathVariables.cs
More file actions
190 lines (163 loc) · 5.77 KB
/
Variables.cs
File metadata and controls
190 lines (163 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using GitHub.DistributedTask.Logging;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Common;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Sdk;
namespace GitHub.Runner.Worker
{
public sealed class Variables
{
private readonly IHostContext _hostContext;
private readonly ConcurrentDictionary<string, Variable> _variables = new(StringComparer.OrdinalIgnoreCase);
private readonly ISecretMasker _secretMasker;
private readonly object _setLock = new();
private readonly Tracing _trace;
public IEnumerable<Variable> AllVariables
{
get
{
return _variables.Values;
}
}
public Variables(IHostContext hostContext, IDictionary<string, VariableValue> copy)
{
// Store/Validate args.
_hostContext = hostContext;
_secretMasker = _hostContext.SecretMasker;
_trace = _hostContext.GetTrace(nameof(Variables));
ArgUtil.NotNull(hostContext, nameof(hostContext));
// Validate the dictionary, remove any variable with empty variable name.
ArgUtil.NotNull(copy, nameof(copy));
if (copy.Keys.Any(k => string.IsNullOrWhiteSpace(k)))
{
_trace.Info($"Remove {copy.Keys.Count(k => string.IsNullOrWhiteSpace(k))} variables with empty variable name.");
}
// Initialize the variable dictionary.
List<Variable> variables = new();
foreach (var variable in copy)
{
if (!string.IsNullOrWhiteSpace(variable.Key))
{
variables.Add(new Variable(variable.Key, variable.Value.Value, variable.Value.IsSecret));
}
}
foreach (Variable variable in variables)
{
// Store the variable. The initial secret values have already been
// registered by the Worker class.
_variables[variable.Name] = variable;
}
}
// DO NOT add file path variable to here.
// All file path variables needs to be retrive and set through ExecutionContext, so it can handle container file path translation.
public string Build_Number => Get(SdkConstants.Variables.Build.BuildNumber);
#if OS_WINDOWS
public bool Retain_Default_Encoding => false;
#else
public bool Retain_Default_Encoding => true;
#endif
public bool? Step_Debug => GetBoolean(Constants.Variables.Actions.StepDebug);
public string System_PhaseDisplayName => Get(Constants.Variables.System.PhaseDisplayName);
public string Get(string name)
{
Variable variable;
if (_variables.TryGetValue(name, out variable))
{
_trace.Verbose($"Get '{name}': '{variable.Value}'");
return variable.Value;
}
_trace.Verbose($"Get '{name}' (not found)");
return null;
}
public bool? GetBoolean(string name)
{
bool val;
if (bool.TryParse(Get(name), out val))
{
return val;
}
return null;
}
public T? GetEnum<T>(string name) where T : struct
{
return EnumUtil.TryParse<T>(Get(name));
}
public Guid? GetGuid(string name)
{
Guid val;
if (Guid.TryParse(Get(name), out val))
{
return val;
}
return null;
}
public int? GetInt(string name)
{
int val;
if (int.TryParse(Get(name), out val))
{
return val;
}
return null;
}
public long? GetLong(string name)
{
long val;
if (long.TryParse(Get(name), out val))
{
return val;
}
return null;
}
public void Set(string name, string val)
{
ArgUtil.NotNullOrEmpty(name, nameof(name));
_variables[name] = new Variable(name, val, false);
}
public bool TryGetValue(string name, out string val)
{
Variable variable;
if (_variables.TryGetValue(name, out variable))
{
val = variable.Value;
_trace.Verbose($"Get '{name}': '{val}'");
return true;
}
val = null;
_trace.Verbose($"Get '{name}' (not found)");
return false;
}
public DictionaryContextData ToSecretsContext()
{
var result = new DictionaryContextData();
foreach (var variable in _variables.Values)
{
if (variable.Secret &&
!string.Equals(variable.Name, Constants.Variables.System.AccessToken, StringComparison.OrdinalIgnoreCase) &&
!string.Equals(variable.Name, "system.github.token", StringComparison.OrdinalIgnoreCase))
{
result[variable.Name] = new StringContextData(variable.Value);
}
}
return result;
}
}
public sealed class Variable
{
public string Name { get; private set; }
public bool Secret { get; private set; }
public string Value { get; private set; }
public Variable(string name, string value, bool secret)
{
ArgUtil.NotNullOrEmpty(name, nameof(name));
Name = name;
Value = value ?? string.Empty;
Secret = secret;
}
}
}