drone / go-convert

Package convert provides tools for converting pipeline configuration files to the Drone format.
Apache License 2.0
10 stars 8 forks source link

(feat) add condition mapping for when conditions in drone #15

Closed eoinmcafee00 closed 1 year ago

bradrydzewski commented 1 year ago

@eoinmcafee00 I recommend using %q instead of \"%s\". This will not only quote the string, but will escape any special characters inside the string.

bradrydzewski commented 1 year ago

In the new yaml, the stage id and name are optional and may be empty. If the id is provided we can use it as-is. Else we can use the name as the id. Else we can use the type as the id. Looking at my original code, I can see that I did not account for this scenario. However, the identifiers.Generate function actually handles fallback logic by accepting multiple parameters. So I think we could do the following:

        ID: d.identifiers.Generate(
            slug.Create(stage.ID), 
            slug.Create(stage.Name), 
            slug.Generate(stage.Type),
        ),

It should work something like this:

stages:
- type ci # use "ci" as stage id and name
stages:
- type: ci
  id: foo # use "foo" as stage id and name
stages:
- type: ci
  name: bar # use "bar" as stage id and name
stages:
- type: ci
  id: foo # use "foo" as id and "bar" as name
  name: bar
jimsheldon commented 1 year ago

I am testing this today and I think I stumbled on an important detail.

Since Drone is webhook-driven, the migration will need to create triggers in Harness CI for every pipeline. When a pipeline executes from a trigger event in Harness CI, the <+codebase.*> variables are not available, only <+trigger.*> variables are available.

I think this means we can ignore <+codebase.*> variables in our conversions.

See this documentation for available <+trigger.*> variables.

There isn't a 1-1 match in features between Drone and Harness CI, see Drone's documentation.

Harness CI has:

Harness CI does not have:

jimsheldon commented 1 year ago

JEXL supports different operators for pattern matching. I was able to get similar functionality to what Drone gets from doublestar with the =~ operator.

Examples

By Branch

Harness CI yaml

              - step:
                  when:
                    stageStatus: Success
                    condition: <+trigger.branch> =~ "feature-.*"
                  type: Run
                  name: Feature Branch
                  identifier: featurebranch
                  spec:
                    shell: Sh
                    command: |-
                      echo this runs on a feature branch

Drone yaml

steps:
- name: Feature Branch
  commands:
  - echo this runs on a feature branch
  when:
    branch:
    - feature-*

By Repo

Harness CI yaml

              - step:
                  when:
                    stageStatus: Success
                    condition: <+trigger.payload.repository.name> != "my-repo"
                  type: Run
                  name: Not My Repo
                  identifier: notmyrepo
                  spec:
                    shell: Sh
                    command: |-
                      echo do not run for my-repo

Drone yaml

steps:
- name: Not My Repo
  commands:
  - echo do not run for my-repo
  when:
    repo:
      exclude:
      - my-repo