View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.service;
26  
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.regex.Pattern;
30  
31  /**
32   * The Class GitHub.
33   */
34  public class GitHub extends AbstractServiceSetup {
35  
36      /** The Constant GITHUB_PR. */
37      private static final Pattern GITHUB_PR = Pattern.compile("refs/pull/(\\d+)/merge");
38  
39      /** The Constant GITHUB. */
40      public static final String GITHUB = "github";
41  
42      /** The Constant GITHUB_ACTIONS. */
43      public static final String GITHUB_ACTIONS = "GITHUB_ACTIONS";
44  
45      /** The Constant GITHUB_REF_NAME. */
46      public static final String GITHUB_REF_NAME = "GITHUB_REF_NAME";
47  
48      /** The Constant GITHUB_REPOSITORY. */
49      public static final String GITHUB_REPOSITORY = "GITHUB_REPOSITORY";
50  
51      /** The Constant GITHUB_RUN_ID. */
52      public static final String GITHUB_RUN_ID = "GITHUB_RUN_ID";
53  
54      /** The Constant GITHUB_RUN_NUMBER. */
55      public static final String GITHUB_RUN_NUMBER = "GITHUB_RUN_NUMBER";
56  
57      /** The Constant GITHUB_SERVER_URL. */
58      public static final String GITHUB_SERVER_URL = "GITHUB_SERVER_URL";
59  
60      /** The Constant GITHUB_REF. */
61      public static final String GITHUB_REF = "GITHUB_REF";
62  
63      /**
64       * Instantiates a new git hub.
65       *
66       * @param env
67       *            the env
68       */
69      public GitHub(Map<String, String> env) {
70          super(env);
71      }
72  
73      @Override
74      public boolean isSelected() {
75          return Boolean.parseBoolean(this.getProperty(GitHub.GITHUB_ACTIONS));
76      }
77  
78      @Override
79      public String getName() {
80          return GitHub.GITHUB;
81      }
82  
83      @Override
84      public String getJobId() {
85          return this.getProperty(GitHub.GITHUB_RUN_ID);
86      }
87  
88      @Override
89      public String getBuildNumber() {
90          return this.getProperty(GitHub.GITHUB_RUN_NUMBER);
91      }
92  
93      @Override
94      public String getBuildUrl() {
95          return String.format("%s/%s/actions/runs/%s", this.getProperty(GitHub.GITHUB_SERVER_URL),
96                  this.getProperty(GitHub.GITHUB_REPOSITORY), this.getProperty(GitHub.GITHUB_RUN_ID));
97      }
98  
99      @Override
100     public String getPullRequest() {
101         final var ref = this.getProperty(GitHub.GITHUB_REF);
102         if (ref != null) {
103             final var matcher = GitHub.GITHUB_PR.matcher(ref);
104             if (matcher.matches()) {
105                 return matcher.group(1);
106             }
107         }
108         return null;
109     }
110 
111     @Override
112     public String getBranch() {
113         return this.getProperty(GitHub.GITHUB_REF_NAME);
114     }
115 
116     @Override
117     public Properties getEnvironment() {
118         final var environment = new Properties();
119         this.addProperty(environment, "github_run_id", this.getProperty(GitHub.GITHUB_RUN_ID));
120         this.addProperty(environment, "github_run_number", this.getProperty(GitHub.GITHUB_RUN_NUMBER));
121         this.addProperty(environment, "github_repository", this.getProperty(GitHub.GITHUB_REPOSITORY));
122         this.addProperty(environment, "github_ref_name", this.getProperty(GitHub.GITHUB_REF_NAME));
123         this.addProperty(environment, "github_server_url", this.getProperty(GitHub.GITHUB_SERVER_URL));
124         return environment;
125     }
126 
127 }