Groovy web console

subscribe to the feed Subscribe
to this
site

hotfix

Published 2 months ago by c3p0
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurperClassic

String jsonKO = '''{"message":"In order to keep the API fast for everyone, pagination is limited for this resource. Check the rel=last link relation in the Link response header to see how far back you can traverse.","documentation_url":"https://docs.github.com/enterprise/2.22/v3/#pagination"}'''

String jsonOK = '''[  {
    "id": "27354",
    "type": "CreateEvent",
    "actor": {
      "id": 90,
      "login": "cedric-dirand",
      "display_login": "cedric-dirand",
      "gravatar_id": "",
      "url": "https://github.maif.local/api/v3/users/cedric-dirand",
      "avatar_url": "https://avatars.github.maif.local/u/90?"
    },
    "repo": {
      "id": 190,
      "name": "accueil-et-distribution/nora-frontend",
      "url": "https://github.maif.local/api/v3/repos/accueil-et-distribution/nora-frontend"
    },
    "payload": {
      "ref": "project/testNewPackage",
      "ref_type": "branch",
      "master_branch": "develop",
      "description": "Frontend de l'application NORA",
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2021-03-19T17:17:16Z",
    "org": {
      "id": 221,
      "login": "accueil-et-distribution",
      "gravatar_id": "",
      "url": "https://github.maif.local/api/v3/orgs/accueil-et-distribution",
      "avatar_url": "https://avatars.github.maif.local/u/221?"
    }
  },
  {
    "id": "27287",
    "type": "DeleteEvent",
    "actor": {
      "id": 217,
      "login": "ibrahim-belmihoub",
      "display_login": "ibrahim-belmihoub",
      "gravatar_id": "",
      "url": "https://github.maif.local/api/v3/users/ibrahim-belmihoub",
      "avatar_url": "https://avatars.github.maif.local/u/217?"
    },
    "repo": {
      "id": 190,
      "name": "accueil-et-distribution/nora-frontend",
      "url": "https://github.maif.local/api/v3/repos/accueil-et-distribution/nora-frontend"
    },
    "payload": {
      "ref": "feature/SCAV402_LivretCloture",
      "ref_type": "branch",
      "pusher_type": "user"
    },
    "public": true,
    "created_at": "2021-03-19T15:29:22Z",
    "org": {
      "id": 221,
      "login": "accueil-et-distribution",
      "gravatar_id": "",
      "url": "https://github.maif.local/api/v3/orgs/accueil-et-distribution",
      "avatar_url": "https://avatars.github.maif.local/u/221?"
    }
  }

]'''

boolean isCollectionOrArray(object) {
  [Collection, Object[]].any { it.isAssignableFrom(object.getClass()) }
}

List<String> branches = []

[jsonKO, jsonOK].each { json ->
  Object response = new JsonSlurperClassic().parseText(json)

  if (response != null && isCollectionOrArray(response) && response.size() > 0) {
    response.findAll { it?.type == 'CreateEvent' && it?.payload?.ref_type == 'branch' }.each {
        branches << it.payload.ref
    }
  }
}

branches