diff --git a/go.mod b/go.mod index 60ac1f9b..d1763deb 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/go-git/go-billy/v5 v5.6.2 github.com/go-git/go-git/v5 v5.16.2 github.com/gobwas/glob v0.2.3 + github.com/google/go-cmp v0.7.0 github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/julienschmidt/httprouter v1.3.0 @@ -62,7 +63,6 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect - github.com/google/go-cmp v0.7.0 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect diff --git a/internal/pkg/config/registration.go b/internal/pkg/config/registration.go index be66b4fb..14d592bd 100644 --- a/internal/pkg/config/registration.go +++ b/internal/pkg/config/registration.go @@ -1,3 +1,4 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. // Copyright 2023 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT @@ -6,6 +7,8 @@ package config import ( "encoding/json" "os" + + "github.com/google/go-cmp/cmp" ) const registrationWarning = "This file is automatically generated by act-runner. Do not edit it manually unless you know what you are doing. Removing this file will cause act runner to re-register as a new runner." @@ -34,12 +37,28 @@ func LoadRegistration(file string) (*Registration, error) { return nil, err } - reg.Warning = "" - return ®, nil } +func isEqualRegistration(file string, reg *Registration) (bool, error) { + existing, err := LoadRegistration(file) + if err != nil { + if os.IsNotExist(err) { + return false, nil + } + return false, err + } + return cmp.Equal(*reg, *existing), nil +} + func SaveRegistration(file string, reg *Registration) error { + equal, err := isEqualRegistration(file, reg) + if err != nil { + return err + } + if equal { + return nil + } f, err := os.Create(file) if err != nil { return err diff --git a/internal/pkg/config/registration_test.go b/internal/pkg/config/registration_test.go new file mode 100644 index 00000000..fe89410b --- /dev/null +++ b/internal/pkg/config/registration_test.go @@ -0,0 +1,63 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package config + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestConfig_Registration(t *testing.T) { + reg := Registration{ + Warning: registrationWarning, + ID: 1234, + UUID: "UUID", + Name: "NAME", + Token: "TOKEN", + Address: "ADDRESS", + Labels: []string{"LABEL1", "LABEL2"}, + } + + file := filepath.Join(t.TempDir(), ".runner") + + // when the file does not exist, it is never equal + equal, err := isEqualRegistration(file, ®) + require.NoError(t, err) + assert.False(t, equal) + + require.NoError(t, SaveRegistration(file, ®)) + + regReloaded, err := LoadRegistration(file) + require.NoError(t, err) + assert.Equal(t, reg, *regReloaded) + + equal, err = isEqualRegistration(file, ®) + require.NoError(t, err) + assert.True(t, equal) + + // if the registration is not modified, it is not saved + time.Sleep(2 * time.Second) // file system precision on modification time is one second + before, err := os.Stat(file) + require.NoError(t, err) + require.NoError(t, SaveRegistration(file, ®)) + after, err := os.Stat(file) + require.NoError(t, err) + assert.Equal(t, before.ModTime(), after.ModTime()) + + reg.Labels = []string{"LABEL3"} + equal, err = isEqualRegistration(file, ®) + require.NoError(t, err) + assert.False(t, equal) + + // if the registration is modified, it is saved + require.NoError(t, SaveRegistration(file, ®)) + after, err = os.Stat(file) + require.NoError(t, err) + assert.NotEqual(t, before.ModTime(), after.ModTime()) +}