Polpetta / jkk

A git-like cli for Jenkins written in Kotlin 🤵 🔥
GNU General Public License v3.0
0 stars 1 forks source link

Config command implementation proposal #20

Open Polpetta opened 4 years ago

Polpetta commented 4 years ago

Before starting implementing commands, we need to find a way to associate the current vcs repository the project is hosted with a corresponding Jenkins Job configuration. This is vital, no command can be done without it.

Branch association with Jenkins jobs

Note that this part is tricky, because it's not always true that a git branch name corresponds to a Jenkins job. In particular, a Job can be a simple pipeline for a desired branch, while another branch can be configured with another Job. Finally, a multipipeline project can wrap up all the repository and have Job per-branch associated.

So, since this association is not straight forward, I'd suggest impementing a sort of "linking" procedure between the git branches and the Jenkins jobs. First of all, I'd introduce a new directory, called .jkk, where all the jkk configurations are stored for that project. In that folder, I'd create a TOML file (something like config) with the following structure:

[association]
[[job]]
folder="/path/to/folder"
name="job_name"
fallback="exact"

[[branch]]
local="master"
remote="something"

[[branch]]
local="devel"
remote="else"

[[job]]
folder="/another/path"
name="another_name"
fallback="abort"

[[branch]]
local="staging"
remote="example"

Where:

Note that this configuration has to exist, since at least one job needs to be specified.

A TOML specification with examples can be found here.

Components to use

Fortunately, Konf works with TOML, and it's already used to parse the login configuration. This means that we'll have to create the relative Objects like for the Auth one: https://github.com/Polpetta/jkk/blob/598084f1360a6d2e8d13022868921999c5a70bcc/src/main/kotlin/it/polpetta/config/Auth.kt#L23-L27

The CLI command

The command itself I think its pretty simple. The command will give the possibility for the user to create a global association between the vcs and the Jenkins Job. Something like:

jkk config [--global, --remove] [ARGS]... 

Where

ARGS here identifies the TOML path to create/edit. For example, if I want to create something like this:

[association]
[[job]]
folder="/path/to/folder"
name="job_name"
fallback="exact"

I would need to type the following commands:

jkk config association.job0.folder=/path/to/folder
jkk config association.job0.name=job_name
jkk config association.job0.fallback="exact"

New invocation of this command with different argument will update the existing one. If a remove (or rm) flag is passed, the values is removed instead i.e.:

jkk config --rm association.job0 # Remove all the Job
jkk config --rm association.job0.fallback # Remove the fallback strategy
jkk config --rm association.job0.branch0 # Remove the branch 0 local/remote association for the job 0