Home > front end >  How to generate the report of API changes on the pipeline?
How to generate the report of API changes on the pipeline?

Time:06-24

I have manually generated the report of my API changes using swagger-diff

I can automate it in a local machine using makefile or script but what about if I wanted to implement it in the Gitlab pipeline, how can I generate the report in such a way when someone pushes the changes on the API endpoints

java -jar bin/swagger-diff.jar -old https://url/v1/swagger.json -new https://url2/v2/swagger.json -v 2.0 -output-mode html > changes.html

Note that: All the project code is also being containerized.

CodePudding user response:

Configure a job in the pipeline to run when there are changes to your api routes. Save the output as an artifact. If you also need the diff published, you could either do the publishing in that job or create a dependent job which uses the artifact to publish the diff to a Gitlab page or external provider.

If you have automated the process locally, then most of the work is done already if it is in a shell script or something similar.

Example: This example assumes that your api routes are defined in customer/api/routes/ and internal/api/routes and that you want to generate the diff when a commit or MR is pushed to the dev branch.

ApiDiff:
  stage: build
  image: java:<some-tag>
  script:
    - java -jar bin/swagger-diff.jar -old https://url/v1/swagger.json -new https://url2/v2/swagger.json -v 2.0 -output-mode html > changes.html
  artifacts:
    expire_in: 1 day
    name: api-diff
    when: on_success
    paths: changes.html
  rules:
    - if: "$CI_COMMIT_REF_NAME == 'dev'"
      changes:
        - customer/api/routes/*
        - internal/api/routes/*
    - when: never

And then the job to publish the diff if you want one. This could also be done in the same job that generates the diff.

PublishDiff:
  stage: deploy
  needs: 
    - job: "ApiDiff"
      optional: false
      artifacts: true
  image: someimage:latest
  script:
    - <some script to publish the report>
  rules:
    - if: "$CI_COMMIT_REF_NAME == 'dev'"
      changes:
        - customer/api/routes/*
        - internal/api/routes/*
    - when: never
  • Related