seedGogsProjects.groovy 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. def server = GOGS_SERVER
  2. def apikey = GOGS_API_KEY
  3. def api = "${server}/api/v1"
  4. def restExists(url) {
  5. def u = new URL(url)
  6. def req = u.openConnection();
  7. def getRC = req.getResponseCode();
  8. if(getRC.equals(200)) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. def restJSONGet(url) {
  14. def u = new URL(url)
  15. return new groovy.json.JsonSlurper().parse(u.newReader())
  16. }
  17. def projects = restJSONGet("${api}/user/repos?token=${apikey}")
  18. projects.each {
  19. def project = it.full_name
  20. def repo = it.clone_url
  21. def jenkinsFile = restExists("${api}/repos/${project}/raw/master/Jenkinsfile?token=${apikey}")
  22. def jobName = "${JOB_PREFIX}${project}".replaceAll('/','-')
  23. if (jenkinsFile) {
  24. multibranchPipelineJob(jobName) {
  25. description("Pipeline for $repo")
  26. triggers {
  27. cron('@hourly')
  28. }
  29. branchSources {
  30. git {
  31. remote(repo)
  32. credentialsId(GOGS_CREDENTIALS)
  33. }
  34. }
  35. orphanedItemStrategy {
  36. discardOldItems {
  37. numToKeep(10)
  38. }
  39. }
  40. }
  41. }
  42. }