0

I've written a seeder job using the Job DSL Plugin to create folders with a predefined collection of jobs inside. I also use the Ownership Plugin to control which users get access to each folder.

Now I need the user who fired the seeder job to be assigned as the folder's primary owner.

I tried setting "Assign job creators as owners" configuration option, but the result I get is that the created folder's owner is SYSTEM.

enter image description here

Is there a way to set the primary owner of the folder programmatically?

Mig82
  • 4,856
  • 4
  • 40
  • 63

2 Answers2

1

Look at the folder's config XML to see how the information is stored:

<com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.1.2">
    <!-- ... -->
    <properties>
    <!-- ... -->
    <org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipProperty plugin="ownership@0.10.0">
        <ownership>
        <ownershipEnabled>true</ownershipEnabled>
        <primaryOwnerId>admin</primaryOwnerId>
        <coownersIds class="sorted-set">
            <string>daspilker</string>
        </coownersIds>
        </ownership>
    </org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipProperty>
    <!-- ... -->
    </properties>
    <!-- ... -->
</com.cloudbees.hudson.plugins.folder.Folder>

Then you can use a Configure Block to create that config with Job DSL.

Mig82
  • 4,856
  • 4
  • 40
  • 63
daspilker
  • 8,154
  • 1
  • 35
  • 49
0

In the end I was able to do this by getting the name of the current use as the Cause of the currentBuild variable and then assign it to the folder by using the configure step like so:

user = currentBuild.getCause(Cause.UserIdCause)
folder("MyFolder") {
    configure { folder ->
        folder / 'properties' / 'org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipProperty' / 'ownership' {     
            primaryOwnerId(user.getUserId())
            ownershipEnabled('true')
        }
    }
}

If anyone can provide a less verbose way to do this I'll mark that as the accepted answer.

Mig82
  • 4,856
  • 4
  • 40
  • 63