Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.plugins.git.actions.ShortCommitShaAction;
import com.walmartlabs.concord.plugins.git.model.Auth;
import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo;
import com.walmartlabs.concord.plugins.git.tokens.AccessTokenProvider;
Expand All @@ -43,6 +44,7 @@

import static com.walmartlabs.concord.plugins.git.Utils.getUrl;
import static com.walmartlabs.concord.sdk.MapUtils.*;
import static com.walmartlabs.concord.plugins.git.VariablesGithubTaskParams.Action;

public class GitHubTask {

Expand Down Expand Up @@ -107,13 +109,15 @@ public class GitHubTask {
private static final TypeReference<List<Map<String, Object>>> LIST_OF_OBJECT_TYPE = new TypeReference<>() {
};

private final UUID txId;
private final boolean dryRunMode;

public GitHubTask() {
this(false);
public GitHubTask(UUID txId) {
this(txId, false);
}

public GitHubTask(boolean dryRunMode) {
public GitHubTask(UUID txId, boolean dryRunMode) {
this.txId = txId;
this.dryRunMode = dryRunMode;
}

Expand All @@ -140,6 +144,8 @@ public Map<String, Object> execute(Map<String, Object> in, Map<String, Object> d

log.info("Starting '{}' action on API URL: {}", action, apiInfo.baseUrl());

var input = VariablesGithubTaskParams.merge(defaults, in);

return switch (action) {
case CREATEPR -> createPR(in, apiInfo);
case COMMENTPR -> commentPR(in, apiInfo);
Expand All @@ -166,6 +172,7 @@ public Map<String, Object> execute(Map<String, Object> in, Map<String, Object> d
case CREATEHOOK -> createHook(in, apiInfo);
case GETPRFILES -> getPRFiles(in, apiInfo);
case CREATEAPPTOKEN -> createAppToken(apiInfo);
case GETSHORTSHA -> new ShortCommitShaAction().execute(txId, apiInfo, VariablesGithubTaskParams.getShortSha(input));
};
}

Expand Down Expand Up @@ -1190,32 +1197,4 @@ protected IOException createException(InputStream response, int code, String sta
}
};
}

public enum Action {
CREATEPR,
COMMENTPR,
MERGEPR,
CLOSEPR,
GETPRCOMMITLIST,
MERGE,
CREATEISSUE,
CREATETAG,
CREATEHOOK,
DELETETAG,
DELETEBRANCH,
GETCOMMIT,
ADDSTATUS,
GETSTATUSES,
FORKREPO,
GETBRANCHLIST,
GETPR,
GETPRLIST,
GETPRFILES,
GETTAGLIST,
GETLATESTSHA,
CREATEREPO,
DELETEREPO,
GETCONTENT,
CREATEAPPTOKEN
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.walmartlabs.concord.plugins.git;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo;

import java.util.Map;
import java.util.UUID;

public abstract class GitHubTaskAction<T extends GitHubTaskParams> {

public abstract Map<String, Object> execute(UUID txId, GitHubApiInfo apiInfo, T input) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.walmartlabs.concord.plugins.git;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

public sealed interface GitHubTaskParams {

record GetShortCommitSha(
String org,
String repo,
String sha,
int minLength
) implements GitHubTaskParams {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.walmartlabs.concord.plugins.git;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables;
import com.walmartlabs.concord.runtime.v2.sdk.Variables;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static com.walmartlabs.concord.plugins.git.GitHubTaskParams.*;

public final class VariablesGithubTaskParams {

public enum Action {
CREATEPR,
COMMENTPR,
MERGEPR,
CLOSEPR,
GETPRCOMMITLIST,
MERGE,
CREATEISSUE,
CREATETAG,
CREATEHOOK,
DELETETAG,
DELETEBRANCH,
GETCOMMIT,
ADDSTATUS,
GETSTATUSES,
FORKREPO,
GETBRANCHLIST,
GETPR,
GETPRLIST,
GETPRFILES,
GETTAGLIST,
GETLATESTSHA,
CREATEREPO,
DELETEREPO,
GETCONTENT,
CREATEAPPTOKEN,
GETSHORTSHA
}

public static Variables merge(Map<String, Object> taskDefaults, Map<String, Object> input) {
var merged = new HashMap<String, Object>();
Stream.of(taskDefaults, input)
.filter(Objects::nonNull)
.forEach(merged::putAll);

return new MapBackedVariables(merged);
}

public static GetShortCommitSha getShortSha(Variables variables) {
return new GetShortCommitSha(
assertOrg(variables),
assertRepo(variables),
variables.assertString("sha"),
variables.getInt("minLength", 7));
}

private static String assertOrg(Variables variables) {
return variables.assertString("org");
}

private static String assertRepo(Variables variables) {
return variables.assertString("repo");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.walmartlabs.concord.plugins.git.actions;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.plugins.git.GitHubTaskAction;
import com.walmartlabs.concord.plugins.git.GitHubTaskParams;
import com.walmartlabs.concord.plugins.git.client.GitHubApiException;
import com.walmartlabs.concord.plugins.git.client.GitHubClient;
import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo;
import com.walmartlabs.concord.runtime.v2.sdk.UserDefinedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.UUID;

public class ShortCommitShaAction extends GitHubTaskAction<GitHubTaskParams.GetShortCommitSha> {

private final static Logger log = LoggerFactory.getLogger(ShortCommitShaAction.class);

@Override
public Map<String, Object> execute(UUID txId, GitHubApiInfo apiInfo, GitHubTaskParams.GetShortCommitSha input) {
int minLen = input.minLength() > 0 ? input.minLength() : 7;

var client = new GitHubClient(txId, apiInfo);
try {
var commit = getCommit(client, input.org(), input.repo(), input.sha());
if (commit == null) {
log.error("❌ Commit '{}' not found in '{}/{}'", input.sha(), input.org(), input.repo());
throw new UserDefinedException("Commit not found in '" + input.org() + "/" + input.repo() + "'");
}

for (int len = minLen; len <= 40; len++) {
var prefix = input.sha().substring(0, len);

commit = getCommit(client, input.org(), input.repo(), prefix);
if (commit != null && input.sha().equals(commit.get("sha"))) {

log.info("✅ Short SHA for '{}' commit is '{}' in '{}/{}'",
input.sha(), prefix, input.org(), input.repo());

return Map.of("shortSha", prefix);
}
}

log.error("❌ Could not derive unique short SHA for '{}' commit in '{}/{}'",
input.sha(), input.org(), input.repo());

throw new UserDefinedException("Could not derive unique short SHA for '" + input.sha() + "' commit");
} catch (UserDefinedException e) {
throw e;
} catch (Exception e) {
log.error("❌ Error while getting short SHA for '{}' commit in '{}/{}'",
input.sha(), input.org(), input.repo(), e);

throw new RuntimeException("Failed to get short SHA for '" + input.sha() + "' commit: " + e.getMessage());
}
}

private static Map<String, Object> getCommit(GitHubClient client, String owner, String repo, String sha) throws Exception{
try {
var c = client.singleObjectResult("GET", "/repos/" + owner + "/" + repo + "/commits/" + sha, null);
return c == null || c.isEmpty() ? null : c;
} catch (GitHubApiException e) {
if (e.getStatusCode() == 404 || e.getStatusCode() == 422) {
return null;
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.walmartlabs.concord.plugins.git.client;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import java.io.IOException;

public class GitHubApiException extends IOException {

private final int statusCode;

public GitHubApiException(String message, int statusCode) {
super(message);
this.statusCode = statusCode;
}

public int getStatusCode() {
return statusCode;
}
}
Loading