|
| 1 | +package com.walmartlabs.concord.plugins.git.actions; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ***** |
| 5 | + * Concord |
| 6 | + * ----- |
| 7 | + * Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors |
| 8 | + * ----- |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * ===== |
| 21 | + */ |
| 22 | + |
| 23 | +import com.walmartlabs.concord.common.ConfigurationUtils; |
| 24 | +import com.walmartlabs.concord.plugins.git.GitHubTaskAction; |
| 25 | +import com.walmartlabs.concord.plugins.git.GitHubTaskParams; |
| 26 | +import com.walmartlabs.concord.plugins.git.client.GitHubClient; |
| 27 | +import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.util.*; |
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 33 | +import java.util.concurrent.atomic.AtomicInteger; |
| 34 | +import java.util.regex.Pattern; |
| 35 | + |
| 36 | +public class ListCommitsAction extends GitHubTaskAction<GitHubTaskParams.ListCommits> { |
| 37 | + |
| 38 | + private final static Logger log = LoggerFactory.getLogger(ListCommitsAction.class); |
| 39 | + |
| 40 | + @Override |
| 41 | + public Map<String, Object> execute(UUID txId, GitHubApiInfo apiInfo, GitHubTaskParams.ListCommits input) { |
| 42 | + if (input.toSha().equals(input.fromSha())) { |
| 43 | + return Map.of( |
| 44 | + "commits", List.of(), |
| 45 | + "filterMatches", List.of(), |
| 46 | + "searchDepthReached", false); |
| 47 | + } |
| 48 | + |
| 49 | + var client = new GitHubClient(txId, apiInfo); |
| 50 | + try { |
| 51 | + var parameters = new HashMap<String, String>(); |
| 52 | + if (input.shaOrBranch() != null) { |
| 53 | + parameters.put("sha", input.shaOrBranch()); |
| 54 | + } |
| 55 | + if (input.since() != null) { |
| 56 | + parameters.put("since", input.since()); |
| 57 | + } |
| 58 | + |
| 59 | + var resultCommits = new ArrayList<Map<String, Object>>(); |
| 60 | + var filterMatches = new ArrayList<String>(); |
| 61 | + |
| 62 | + var counted = new AtomicInteger(0); |
| 63 | + var started = new AtomicBoolean(false); |
| 64 | + var shouldStop = new AtomicBoolean(false); |
| 65 | + |
| 66 | + client.forEachPage("/repos/" + input.org() + "/" + input.repo() + "/commits", parameters, input.pageSize(), commits -> { |
| 67 | + for (var commit : commits) { |
| 68 | + var sha = commit.get("sha"); |
| 69 | + |
| 70 | + if (input.toSha().equals(sha)) { |
| 71 | + started.set(true); |
| 72 | + } |
| 73 | + if (input.fromSha().equals(sha) || counted.get() >= input.searchDepth()) { |
| 74 | + shouldStop.set(true); |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + if (started.get()) { |
| 79 | + processCommit(commit, input.filter(), resultCommits, filterMatches); |
| 80 | + } |
| 81 | + counted.incrementAndGet(); |
| 82 | + } |
| 83 | + |
| 84 | + return !shouldStop.get(); |
| 85 | + }); |
| 86 | + |
| 87 | + log.info("✅ Loaded {} commits in '{}/{}' since '{}': ('{}', '{}']", |
| 88 | + resultCommits.size(), input.org(), input.repo(), input.since(), input.fromSha(), input.toSha()); |
| 89 | + |
| 90 | + return Map.of( |
| 91 | + "commits", resultCommits, |
| 92 | + "filterMatches", filterMatches, |
| 93 | + "searchDepthReached", counted.get() >= input.searchDepth()); |
| 94 | + } catch (Exception e) { |
| 95 | + log.error("❌ Failed to list commits '{}/{}' since '{}'", input.org(), input.repo(), input.since(), e); |
| 96 | + throw new RuntimeException(" Failed to list commits: " + e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void processCommit(Map<String, Object> commit, Pattern filter, List<Map<String, Object>> commits, List<String> filterMatches) { |
| 101 | + if (filter == null) { |
| 102 | + commits.add(commit); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + var message = (String)ConfigurationUtils.get(commit, "commit", "message"); |
| 107 | + if (message == null) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + var matcher = filter.matcher(message); |
| 112 | + if (matcher.matches()) { |
| 113 | + commits.add(commit); |
| 114 | + if (matcher.groupCount() > 0) { |
| 115 | + var match = matcher.group(1); |
| 116 | + if (match != null) { |
| 117 | + filterMatches.add(match.trim()); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments