- 
                Notifications
    
You must be signed in to change notification settings  - Fork 30
 
github: getshortsha action #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            31 changes: 31 additions & 0 deletions
          
          31 
        
  tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTaskAction.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
| } | 
        
          
          
            32 changes: 32 additions & 0 deletions
          
          32 
        
  tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTaskParams.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 { | ||
| } | ||
| } | 
        
          
          
            88 changes: 88 additions & 0 deletions
          
          88 
        
  tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/VariablesGithubTaskParams.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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"); | ||
| } | ||
| } | ||
        
          
          
            89 changes: 89 additions & 0 deletions
          
          89 
        
  ...s/git/src/main/java/com/walmartlabs/concord/plugins/git/actions/ShortCommitShaAction.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
| } | ||
| } | ||
| } | 
        
          
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/client/GitHubApiException.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.