Skip to content

fix: correct decoding logic for GitLab clientSpec to allow overwriting #5811

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

Merged
merged 5 commits into from
Aug 6, 2025

Conversation

hebestreit
Copy link
Contributor

In this PR I switched the order of decoding to reference the clientSpec to the action's sub spec when decoding.

I was trying to overwrite the username and token inside the GitLab Merge Request action's spec using below config but it was not passed to the clientSpec correctly. That's why the scm credentials were used instead.

actions:
  default:
    kind: gitlab/mergerequest
    scmid: gitlab
    title: "my-title"
    spec:
      removesourcebranch: true
      spec:
        username: mr
        token: gplat-mr-token

scms:
  gitlab:
    kind: gitlab
    spec:
      owner: "owner"
      repository: "my-group/my-repository"
      username: gitlab-ci-token
      token: {{ requiredEnv "CI_JOB_TOKEN" }}

Test

To test this pull request, you can run the following commands:

cd <to_package_directory>
go test

Additional Information

Checklist

  • I have updated the documentation via pull request in website repository.

Tradeoff

Potential improvement

@olblak
Copy link
Member

olblak commented Aug 5, 2025

@hebestreit I tried your example both with your code and the latest Updatecli version and in both cases I have the same result running updatecli manifest show --config /tmp/test.yaml

name: TEST.YAML
pipelineid: 223989165bed2b42fd29f4aca958d868c934ef019a0656abed5ed4fb7c7c4f46
actions:
    default:
        title: my-title
        kind: gitlab/mergerequest
        spec:
            removesourcebranch: true
            spec:
                token: gplat-mr-token
                username: mr
        scmid: gitlab
scms:
    gitlab:
        kind: gitlab
        spec:
            owner: owner
            repository: my-group/my-repository
            token: xxx
            username: gitlab-ci-token
        disabled: false

@hebestreit
Copy link
Contributor Author

@olblak do you mean that you were not able to reproduce it?

I don't know if the output of the updatecli manifest show command is reliable, because when leaving the actions.default.spec.spec empty it's also not outputting the defaults from scm, right?

name: TEST.YAML
pipelineid: 223989165bed2b42fd29f4aca958d868c934ef019a0656abed5ed4fb7c7c4f46
actions:
    default:
        title: my-title
        kind: gitlab/mergerequest
        spec:
            removesourcebranch: true
#            spec:
#                token: gplat-mr-token
#                username: mr
        scmid: gitlab
scms:
    gitlab:
        kind: gitlab
        spec:
            owner: owner
            repository: my-group/my-repository
            token: xxx
            username: gitlab-ci-token
        disabled: false

I was mainly checking the value inside the debugger where the clientSpec was empty with the latest Updatecli version and result into falling back to the scm.Spec.Token.

	// mapstructure.Decode cannot handle embedded fields
	// hence we decode it in two steps
	err := mapstructure.Decode(spec, &clientSpec)
	if err != nil {
		return Gitlab{}, err
	}

	err = mapstructure.Decode(spec, &s)
	if err != nil {
		return Gitlab{}, nil
	}

	if scm != nil {

		if len(clientSpec.Token) == 0 && len(scm.Spec.Token) > 0 {
			clientSpec.Token = scm.Spec.Token
		}
	// ...
Before change image

After changing the order and referencing to s.Spec the value got populated correctly which for me indicates it uses the correct token then.

After change image

@olblak
Copy link
Member

olblak commented Aug 6, 2025

Well, it was a combination of multiple issues here

The Gitlab action spec has an embedded struct for the client configuration.

In the file pkg/core/pipeline/action/main.go I was decoding the action spec.
I don't remember why I did it because it's misleading as we also do it in each action plugin, anyway.
But worth, it also removes the embedded client struct as the library github.com/mitchellh/mapstructure
doesn't support embedded struct

cfr go-viper/mapstructure#81

By the way this current issue also highlighted that I need to migrate from github.com/mitchellh/mapstructure(archived) to github.com/go-viper/mapstructure (maintained)

Then within the gitlab mergerequest I was decoding the wrong empty object...

@olblak olblak added bug Something isn't working action-gitlab labels Aug 6, 2025
@olblak
Copy link
Member

olblak commented Aug 6, 2025

For simplicity, I am also adding the fix for the other action Gitea/Bitbucket/Stash in this PR

@olblak olblak merged commit 70e0dc3 into updatecli:main Aug 6, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action-gitlab bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants