Skip to content

Commit bf0483e

Browse files
committed
Fix allow strings as bool in dotnet runner configs
1 parent 1636dd1 commit bf0483e

2 files changed

Lines changed: 112 additions & 18 deletions

File tree

runnerconfiguration/compat/actions_runner_compat.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"math/big"
99
"strconv"
10+
"strings"
1011

1112
"github.com/ChristopherHX/github-act-runner/common"
1213
"github.com/ChristopherHX/github-act-runner/protocol"
@@ -24,19 +25,30 @@ type DotnetRsaParameters struct {
2425
Q []byte `json:"q"`
2526
}
2627

28+
type DotnetBoolean bool
29+
30+
func (b *DotnetBoolean) UnmarshalJSON(data []byte) error {
31+
var v bool
32+
if err := json.Unmarshal([]byte(strings.Trim(strings.ToLower(string(data)), "\"")), &v); err != nil {
33+
return err
34+
}
35+
*b = DotnetBoolean(v)
36+
return nil
37+
}
38+
2739
type DotnetAgent struct {
28-
AgentID string `json:"AgentId"`
29-
AgentName string `json:"AgentName"`
30-
DisableUpdate string `json:"DisableUpdate"`
31-
Ephemeral string `json:"Ephemeral"`
32-
PoolID string `json:"PoolId"`
33-
PoolName string `json:"PoolName,omitempty"`
34-
ServerURL string `json:"ServerUrl"`
35-
WorkFolder string `json:"WorkFolder"`
36-
GitHubURL string `json:"GitHubUrl"`
37-
UseV2Flow bool `json:"UseV2Flow"`
38-
ServerURLV2 string `json:"ServerUrlV2"`
39-
UseRunnerAdminFlow bool `json:"UseRunnerAdminFlow,omitempty"`
40+
AgentID string `json:"AgentId"`
41+
AgentName string `json:"AgentName"`
42+
DisableUpdate string `json:"DisableUpdate"`
43+
Ephemeral string `json:"Ephemeral"`
44+
PoolID string `json:"PoolId"`
45+
PoolName string `json:"PoolName,omitempty"`
46+
ServerURL string `json:"ServerUrl"`
47+
WorkFolder string `json:"WorkFolder"`
48+
GitHubURL string `json:"GitHubUrl"`
49+
UseV2Flow DotnetBoolean `json:"UseV2Flow"`
50+
ServerURLV2 string `json:"ServerUrlV2"`
51+
UseRunnerAdminFlow DotnetBoolean `json:"UseRunnerAdminFlow,omitempty"`
4052
}
4153

4254
type DotnetCredentials struct {
@@ -45,9 +57,9 @@ type DotnetCredentials struct {
4557
}
4658

4759
type DotnetCredentialsData struct {
48-
ClientID string `json:"ClientId"`
49-
AuthorizationURL string `json:"AuthorizationUrl"`
50-
RequireFipsCryptography bool `json:"RequireFipsCryptography"`
60+
ClientID string `json:"ClientId"`
61+
AuthorizationURL string `json:"AuthorizationUrl"`
62+
RequireFipsCryptography DotnetBoolean `json:"RequireFipsCryptography"`
5163
}
5264

5365
func BytesToBigInt(bytes []byte) *big.Int {
@@ -171,7 +183,7 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
171183
PoolID: poolID,
172184
Auth: &protocol.GitHubAuthResult{
173185
TenantURL: agent.ServerURL,
174-
UseV2FLow: agent.UseRunnerAdminFlow,
186+
UseV2FLow: bool(agent.UseRunnerAdminFlow),
175187
},
176188
PKey: FromRsaParameters(rsaParameters),
177189
Agent: &protocol.TaskAgent{
@@ -208,9 +220,9 @@ func FromRunnerInstance(instance *runnerconfiguration.RunnerInstance, fileAccess
208220
ServerURL: serverURL,
209221
WorkFolder: instance.WorkFolder,
210222
GitHubURL: instance.RegistrationURL,
211-
UseV2Flow: useV2Flow,
223+
UseV2Flow: DotnetBoolean(useV2Flow),
212224
ServerURLV2: serverV2URL,
213-
UseRunnerAdminFlow: instance.Auth.UseV2FLow,
225+
UseRunnerAdminFlow: DotnetBoolean(instance.Auth.UseV2FLow),
214226
}
215227
if agent.WorkFolder == "" {
216228
agent.WorkFolder = "_work"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package compat
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
func TestUnmarshalConfig(t *testing.T) {
9+
table := []struct {
10+
name string
11+
jsonData string
12+
expected DotnetAgent
13+
}{
14+
{
15+
name: "Boolean true without quotes",
16+
jsonData: `{"UseV2Flow": true}`,
17+
expected: DotnetAgent{UseV2Flow: DotnetBoolean(true)},
18+
},
19+
{
20+
name: "Boolean false without quotes",
21+
jsonData: `{"UseV2Flow": false}`,
22+
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
23+
},
24+
{
25+
name: "Boolean false without quotes no space",
26+
jsonData: `{"UseV2Flow":false}`,
27+
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
28+
},
29+
{
30+
name: "Boolean true with quotes",
31+
jsonData: `{"UseV2Flow": "True"}`,
32+
expected: DotnetAgent{UseV2Flow: DotnetBoolean(true)},
33+
},
34+
{
35+
name: "Boolean false with quotes",
36+
jsonData: `{"UseV2Flow": "False"}`,
37+
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
38+
},
39+
}
40+
for _, tt := range table {
41+
t.Run(tt.name, func(t *testing.T) {
42+
var agent DotnetAgent
43+
err := json.Unmarshal([]byte(tt.jsonData), &agent)
44+
if err != nil {
45+
t.Fatalf("Unexpected error during unmarshal: %v", err)
46+
}
47+
if agent.UseV2Flow != tt.expected.UseV2Flow {
48+
t.Errorf("Expected UseV2Flow to be %v, got %v", tt.expected.UseV2Flow, agent.UseV2Flow)
49+
}
50+
})
51+
}
52+
}
53+
54+
func TestMarshalDotnetBoolean(t *testing.T) {
55+
table := []struct {
56+
name string
57+
agent DotnetBoolean
58+
expectedJSON string
59+
}{
60+
{
61+
name: "Boolean true",
62+
agent: DotnetBoolean(true),
63+
expectedJSON: "true",
64+
},
65+
{
66+
name: "Boolean false",
67+
agent: DotnetBoolean(false),
68+
expectedJSON: "false",
69+
},
70+
}
71+
for _, tt := range table {
72+
t.Run(tt.name, func(t *testing.T) {
73+
data, err := json.Marshal(tt.agent)
74+
if err != nil {
75+
t.Fatalf("Unexpected error during marshal: %v", err)
76+
}
77+
if string(data) != tt.expectedJSON {
78+
t.Errorf("Expected JSON to be %s, got %s", tt.expectedJSON, string(data))
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)