(String[] args)
| 33 | } |
| 34 | |
| 35 | private static int execute(String[] args) throws Exception { |
| 36 | Arguments arguments = Arguments.parse(args); |
| 37 | if (arguments == null) { |
| 38 | return 2; |
| 39 | } |
| 40 | marker = arguments.marker != null ? arguments.marker : DEFAULT_MARKER; |
| 41 | logPrefix = arguments.logPrefix != null ? arguments.logPrefix : DEFAULT_LOG_PREFIX; |
| 42 | |
| 43 | Path bodyPath = arguments.body; |
| 44 | if (!Files.isRegularFile(bodyPath)) { |
| 45 | return 0; |
| 46 | } |
| 47 | String rawBody = Files.readString(bodyPath, StandardCharsets.UTF_8); |
| 48 | String body = rawBody.trim(); |
| 49 | if (body.isEmpty()) { |
| 50 | return 0; |
| 51 | } |
| 52 | if (!body.contains(marker)) { |
| 53 | body = body.stripTrailing() + "\n\n" + marker; |
| 54 | } |
| 55 | String bodyWithoutMarker = body.replace(marker, "").trim(); |
| 56 | if (bodyWithoutMarker.isEmpty()) { |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | String eventPathEnv = System.getenv("GITHUB_EVENT_PATH"); |
| 61 | String repo = System.getenv("GITHUB_REPOSITORY"); |
| 62 | String token = System.getenv("GITHUB_TOKEN"); |
| 63 | if (eventPathEnv == null || repo == null || token == null) { |
| 64 | return 0; |
| 65 | } |
| 66 | Path eventPath = Path.of(eventPathEnv); |
| 67 | if (!Files.isRegularFile(eventPath)) { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | Map<String, Object> event = JsonUtil.asObject(JsonUtil.parse(Files.readString(eventPath, StandardCharsets.UTF_8))); |
| 72 | Integer prNumber = findPrNumber(event); |
| 73 | if (prNumber == null) { |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build(); |
| 78 | Map<String, String> headers = Map.of( |
| 79 | "Authorization", "token " + token, |
| 80 | "Accept", "application/vnd.github+json", |
| 81 | "Content-Type", "application/json" |
| 82 | ); |
| 83 | |
| 84 | boolean isForkPr = isForkPullRequest(event); |
| 85 | CommentContext context = locateExistingComment(client, headers, repo, prNumber, body, event); |
| 86 | if (context == null) { |
| 87 | return 1; |
| 88 | } |
| 89 | Long commentId = context.commentId; |
| 90 | boolean createdPlaceholder = context.createdPlaceholder; |
| 91 | |
| 92 | Path previewDir = arguments.previewDir; |
no test coverage detected