Skip to content

Commit d732a9d

Browse files
AayushSaini101Axel von Bertoldi
authored andcommitted
chore: update the logic for comparing the urls and tokens
1 parent e7f7421 commit d732a9d

2 files changed

Lines changed: 198 additions & 1 deletion

File tree

common/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,13 @@ func (c *RunnerCredentials) Log() *logrus.Entry {
21322132
}
21332133

21342134
func (c *RunnerCredentials) SameAs(other *RunnerCredentials) bool {
2135-
return c.URL == other.URL && c.Token == other.Token
2135+
if c.Token != other.Token {
2136+
return false
2137+
}
2138+
if wildcardURL(c.URL) || wildcardURL(other.URL) {
2139+
return true
2140+
}
2141+
return c.URL == other.URL
21362142
}
21372143

21382144
func (c *RunnerConfig) String() string {
@@ -2623,3 +2629,13 @@ func parseVariable(text string) (variable spec.Variable, err error) {
26232629
}
26242630
return
26252631
}
2632+
2633+
// wildcardURL checks if the URL is a wildcard URL
2634+
func wildcardURL(url string) bool {
2635+
switch url {
2636+
case "", "*":
2637+
return true
2638+
default:
2639+
return false
2640+
}
2641+
}

common/config_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,3 +3738,184 @@ func TestInvalidParseVariable(t *testing.T) {
37383738
_, err := parseVariable("some_other_key")
37393739
assert.Error(t, err)
37403740
}
3741+
3742+
func TestRunnerCredentials_SameAs(t *testing.T) {
3743+
tests := map[string]struct {
3744+
c *RunnerCredentials
3745+
other *RunnerCredentials
3746+
result bool
3747+
}{
3748+
"same token and same URL": {
3749+
c: &RunnerCredentials{
3750+
URL: "https://gitlab.com",
3751+
Token: "token123",
3752+
},
3753+
other: &RunnerCredentials{
3754+
URL: "https://gitlab.com",
3755+
Token: "token123",
3756+
},
3757+
result: true,
3758+
},
3759+
"same token but different URL": {
3760+
c: &RunnerCredentials{
3761+
URL: "https://gitlab.com",
3762+
Token: "token123",
3763+
},
3764+
other: &RunnerCredentials{
3765+
URL: "https://gitlab.example.com",
3766+
Token: "token123",
3767+
},
3768+
result: false,
3769+
},
3770+
"different token but same URL": {
3771+
c: &RunnerCredentials{
3772+
URL: "https://gitlab.com",
3773+
Token: "token123",
3774+
},
3775+
other: &RunnerCredentials{
3776+
URL: "https://gitlab.com",
3777+
Token: "token456",
3778+
},
3779+
result: false,
3780+
},
3781+
"different token and different URL": {
3782+
c: &RunnerCredentials{
3783+
URL: "https://gitlab.com",
3784+
Token: "token123",
3785+
},
3786+
other: &RunnerCredentials{
3787+
URL: "https://gitlab.example.com",
3788+
Token: "token456",
3789+
},
3790+
result: false,
3791+
},
3792+
"same token, first URL is wildcard *": {
3793+
c: &RunnerCredentials{
3794+
URL: "*",
3795+
Token: "token123",
3796+
},
3797+
other: &RunnerCredentials{
3798+
URL: "https://gitlab.com",
3799+
Token: "token123",
3800+
},
3801+
result: true,
3802+
},
3803+
"same token, second URL is wildcard *": {
3804+
c: &RunnerCredentials{
3805+
URL: "https://gitlab.com",
3806+
Token: "token123",
3807+
},
3808+
other: &RunnerCredentials{
3809+
URL: "*",
3810+
Token: "token123",
3811+
},
3812+
result: true,
3813+
},
3814+
"same token, both URLs are wildcard *": {
3815+
c: &RunnerCredentials{
3816+
URL: "*",
3817+
Token: "token123",
3818+
},
3819+
other: &RunnerCredentials{
3820+
URL: "*",
3821+
Token: "token123",
3822+
},
3823+
result: true,
3824+
},
3825+
"same token, first URL is empty": {
3826+
c: &RunnerCredentials{
3827+
URL: "",
3828+
Token: "token123",
3829+
},
3830+
other: &RunnerCredentials{
3831+
URL: "https://gitlab.com",
3832+
Token: "token123",
3833+
},
3834+
result: true,
3835+
},
3836+
"same token, second URL is empty": {
3837+
c: &RunnerCredentials{
3838+
URL: "https://gitlab.com",
3839+
Token: "token123",
3840+
},
3841+
other: &RunnerCredentials{
3842+
URL: "",
3843+
Token: "token123",
3844+
},
3845+
result: true,
3846+
},
3847+
"same token, both URLs are empty": {
3848+
c: &RunnerCredentials{
3849+
URL: "",
3850+
Token: "token123",
3851+
},
3852+
other: &RunnerCredentials{
3853+
URL: "",
3854+
Token: "token123",
3855+
},
3856+
result: true,
3857+
},
3858+
"same token, empty and wildcard *": {
3859+
c: &RunnerCredentials{
3860+
URL: "",
3861+
Token: "token123",
3862+
},
3863+
other: &RunnerCredentials{
3864+
URL: "*",
3865+
Token: "token123",
3866+
},
3867+
result: true,
3868+
},
3869+
"different token, first URL is wildcard *": {
3870+
c: &RunnerCredentials{
3871+
URL: "*",
3872+
Token: "token123",
3873+
},
3874+
other: &RunnerCredentials{
3875+
URL: "https://gitlab.com",
3876+
Token: "token456",
3877+
},
3878+
result: false,
3879+
},
3880+
"different token, second URL is wildcard *": {
3881+
c: &RunnerCredentials{
3882+
URL: "https://gitlab.com",
3883+
Token: "token123",
3884+
},
3885+
other: &RunnerCredentials{
3886+
URL: "*",
3887+
Token: "token456",
3888+
},
3889+
result: false,
3890+
},
3891+
"same token, URLs differ only by trailing slash": {
3892+
c: &RunnerCredentials{
3893+
URL: "https://gitlab.com",
3894+
Token: "token123",
3895+
},
3896+
other: &RunnerCredentials{
3897+
URL: "https://gitlab.com/",
3898+
Token: "token123",
3899+
},
3900+
result: false,
3901+
},
3902+
"same token, URLs differ by protocol": {
3903+
c: &RunnerCredentials{
3904+
URL: "http://gitlab.com",
3905+
Token: "token123",
3906+
},
3907+
other: &RunnerCredentials{
3908+
URL: "https://gitlab.com",
3909+
Token: "token123",
3910+
},
3911+
result: false,
3912+
},
3913+
}
3914+
3915+
for name, tt := range tests {
3916+
t.Run(name, func(t *testing.T) {
3917+
result := tt.c.SameAs(tt.other)
3918+
assert.Equal(t, tt.result, result, "SameAs should return %v for this case", tt.result)
3919+
})
3920+
}
3921+
}

0 commit comments

Comments
 (0)