1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
33
34 public class GitHub extends AbstractServiceSetup {
35
36
37 private static final Pattern GITHUB_PR = Pattern.compile("refs/pull/(\\d+)/merge");
38
39
40 public static final String GITHUB = "github";
41
42
43 public static final String GITHUB_ACTIONS = "GITHUB_ACTIONS";
44
45
46 public static final String GITHUB_REF_NAME = "GITHUB_REF_NAME";
47
48
49 public static final String GITHUB_REPOSITORY = "GITHUB_REPOSITORY";
50
51
52 public static final String GITHUB_RUN_ID = "GITHUB_RUN_ID";
53
54
55 public static final String GITHUB_RUN_NUMBER = "GITHUB_RUN_NUMBER";
56
57
58 public static final String GITHUB_SERVER_URL = "GITHUB_SERVER_URL";
59
60
61 public static final String GITHUB_REF = "GITHUB_REF";
62
63
64
65
66
67
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 }