|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020-present Cloudogu GmbH and Contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.cloudogu.scm |
| 26 | + |
| 27 | +import com.google.common.base.Strings |
| 28 | +import groovy.json.JsonOutput |
| 29 | +import org.eclipse.jetty.client.HttpClient |
| 30 | +import org.eclipse.jetty.client.api.Request |
| 31 | +import org.eclipse.jetty.client.util.StringContentProvider |
| 32 | +import org.eclipse.jetty.http.HttpMethod |
| 33 | +import org.gradle.api.tasks.Input |
| 34 | +import org.gradle.api.tasks.Internal |
| 35 | +import org.gradle.api.tasks.Optional |
| 36 | + |
| 37 | +import java.nio.charset.StandardCharsets |
| 38 | + |
| 39 | +class ScmManagerUploadTask extends UploadTask { |
| 40 | + |
| 41 | + @Input |
| 42 | + @Optional |
| 43 | + String apiToken |
| 44 | + |
| 45 | + @Input |
| 46 | + String server |
| 47 | + |
| 48 | + @Input |
| 49 | + String namespace |
| 50 | + |
| 51 | + @Input |
| 52 | + String repo |
| 53 | + |
| 54 | + @Input |
| 55 | + String path |
| 56 | + |
| 57 | + @Input |
| 58 | + String branch = "master" |
| 59 | + |
| 60 | + @Input |
| 61 | + String message |
| 62 | + |
| 63 | + @Override |
| 64 | + Request createRequest(HttpClient client) { |
| 65 | + String[] pathComponents = path.split("/") |
| 66 | + String filename = pathComponents[pathComponents.length - 1] |
| 67 | + String directory = path.substring(0, path.length() - filename.length() - 1) |
| 68 | + Request request = client.newRequest("${server}/api/v2/edit/${namespace}/${repo}/create/${directory}") |
| 69 | + request.method(HttpMethod.POST) |
| 70 | + request.header("Accept", "application/json") |
| 71 | + request.header("Content-Type", "application/json") |
| 72 | + if (!Strings.isNullOrEmpty(apiToken)) { |
| 73 | + request.header("Authorization","Bearer ${apiToken}") |
| 74 | + } |
| 75 | + |
| 76 | + def body = [ |
| 77 | + commitMessage: message, |
| 78 | + branch: branch, |
| 79 | + fileName: filename, |
| 80 | + fileContent: artifact.text |
| 81 | + ] |
| 82 | + request.content(new StringContentProvider(JsonOutput.toJson(body), StandardCharsets.UTF_8)) |
| 83 | + return request |
| 84 | + } |
| 85 | +} |
0 commit comments