Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit bbe1dca

Browse files
authored
Merge pull request #195 from xjusko/add_label_check
[Issue #191]Added check that adds a label with the name of target bra…
2 parents d1c6c2e + f0d1727 commit bbe1dca

File tree

12 files changed

+392
-7
lines changed

12 files changed

+392
-7
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc, and individual contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.tyr.check;
17+
18+
import org.jboss.tyr.Check;
19+
import org.jboss.tyr.InvalidPayloadException;
20+
import org.jboss.tyr.github.GitHubService;
21+
import org.jboss.tyr.model.Utils;
22+
import org.jboss.tyr.model.yaml.LabelDetails;
23+
24+
import javax.enterprise.context.ApplicationScoped;
25+
import javax.inject.Inject;
26+
import javax.json.JsonArray;
27+
import javax.json.JsonObject;
28+
import javax.json.JsonValue;
29+
30+
@ApplicationScoped
31+
public class AddTargetBranchLabelCheck implements Check {
32+
33+
static final String DEFAULT_DESCRIPTION = "No description";
34+
static final String DEFAULT_WHITE_COLOR = "ffffff";
35+
36+
@Inject
37+
GitHubService gitHubService;
38+
39+
private String description = DEFAULT_DESCRIPTION;
40+
41+
private String color = DEFAULT_WHITE_COLOR;
42+
43+
public void initCheck(LabelDetails labelDetails) {
44+
if (labelDetails.getColor() != null && !labelDetails.getColor().toLowerCase().matches(Utils.COLOR_VERIFICATION)) {
45+
throw new IllegalArgumentException(
46+
"Wrong color format, has to be lower character hexadecimal color code, without the leading #.");
47+
}
48+
if (labelDetails.getDescription() != null) {
49+
this.description = labelDetails.getDescription();
50+
}
51+
if (labelDetails.getColor() != null) {
52+
this.color = labelDetails.getColor().toLowerCase();
53+
}
54+
}
55+
56+
@Override
57+
public String check(JsonObject payload) throws InvalidPayloadException {
58+
String repository = payload.getJsonObject(Utils.REPOSITORY).getString(Utils.FULL_NAME);
59+
int pullRequestNumber = payload.getInt(Utils.NUMBER);
60+
String targetBranch = payload.getJsonObject(Utils.PULL_REQUEST).getJsonObject(Utils.BASE).getString(Utils.REF);
61+
62+
JsonArray labels = payload.getJsonObject(Utils.PULL_REQUEST).getJsonArray(Utils.LABELS);
63+
if (labels != null) {
64+
for (JsonValue label : labels) {
65+
if (label.asJsonObject().getString(Utils.NAME).equals(targetBranch)) {
66+
return null;
67+
}
68+
}
69+
}
70+
71+
gitHubService.addLabelToPullRequest(repository, pullRequestNumber, targetBranch, description, color);
72+
return null;
73+
}
74+
}

tyr-core/src/main/java/org/jboss/tyr/check/TemplateChecker.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public class TemplateChecker {
6060
@New
6161
CommitMessagesCheck commitMessagesCheck;
6262

63+
@Inject
64+
AddTargetBranchLabelCheck addTargetBranchLabelCheck;
65+
6366
@Inject
6467
AdditionalResourcesLoader additionalResourcesLoader;
6568

@@ -122,6 +125,13 @@ public String checkPR(JsonObject payload) throws InvalidPayloadException {
122125
private List<Check> registerChecks(Format format) {
123126
List<Check> checks = new ArrayList<>();
124127

128+
if (format.getLabel() != null) {
129+
if (format.getLabel().getTargetBranch() != null) {
130+
addTargetBranchLabelCheck.initCheck(format.getLabel().getTargetBranch());
131+
}
132+
checks.add(addTargetBranchLabelCheck);
133+
}
134+
125135
if (format.getTitle() != null) {
126136
checks.add(new TitleCheck(format.getTitle()));
127137
}
@@ -131,7 +141,7 @@ private List<Check> registerChecks(Format format) {
131141
format.getDescription().getOptionalRows()));
132142
}
133143

134-
if (!configuration.commitChecksDisabled() && format.getCommit() != null){
144+
if (!configuration.commitChecksDisabled() && format.getCommit() != null) {
135145
commitMessagesCheck.setRegex(format.getCommit());
136146
checks.add(commitMessagesCheck);
137147

tyr-core/src/main/java/org/jboss/tyr/github/GitHubService.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jboss.logging.Logger;
1919
import org.jboss.tyr.InvalidPayloadException;
2020
import org.jboss.tyr.model.CommitStatus;
21+
import org.jboss.tyr.model.json.Label;
2122
import org.jboss.tyr.model.StatusPayload;
2223
import org.jboss.tyr.config.TyrConfiguration;
2324
import org.jboss.tyr.model.Utils;
@@ -39,6 +40,8 @@
3940
import javax.ws.rs.core.UriBuilder;
4041
import java.io.StringReader;
4142
import java.net.URI;
43+
import java.util.Collections;
44+
import java.util.List;
4245

4346
@Named("default")
4447
@ApplicationScoped
@@ -55,14 +58,13 @@ public void updateCommitStatus(String repository, String sha, CommitStatus statu
5558
Client client = ClientBuilder.newClient();
5659
URI statusUri = UriBuilder
5760
.fromUri(Utils.GITHUB_BASE)
58-
.path("/repos")
61+
.path(Utils.REPOS_PATH)
5962
.path("/" + repository)
60-
.path("/statuses")
63+
.path(Utils.STATUSES_PATH)
6164
.path("/" + sha)
6265
.build();
6366

6467
WebTarget target = client.target(statusUri);
65-
6668
Entity<StatusPayload> json = Entity.json(new StatusPayload(status.toString(),
6769
targetUrl, description, context));
6870

@@ -87,6 +89,44 @@ public void updateCommitStatus(String repository, String sha, CommitStatus statu
8789
}
8890
}
8991

92+
public void addLabelToPullRequest
93+
(String repository, int pullRequestNumber, String targetBranch, String description, String color) {
94+
Client client = ClientBuilder.newClient();
95+
URI labelUri = UriBuilder
96+
.fromUri(Utils.GITHUB_BASE)
97+
.path(Utils.REPOS_PATH)
98+
.path("/" + repository)
99+
.path(Utils.ISSUES_PATH)
100+
.path("/" + pullRequestNumber)
101+
.path(Utils.LABELS_PATH)
102+
.build();
103+
104+
WebTarget target = client.target(labelUri);
105+
106+
Entity<List<Label>> json = Entity.json(Collections.singletonList(new Label
107+
(targetBranch, description, color)));
108+
109+
log.debug("Updating label: " + json);
110+
Response response = null;
111+
112+
try {
113+
response = target.request()
114+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
115+
.header(HttpHeaders.AUTHORIZATION, "token " + configuration.oauthToken())
116+
.post(json);
117+
118+
log.debug("Pull request label update: " + response.getStatus());
119+
log.debug("Github response: " + response.readEntity(String.class));
120+
} catch (Throwable e) {
121+
log.error("Cannot add label to pull request", e);
122+
} finally {
123+
if (response != null) {
124+
response.close();
125+
}
126+
client.close();
127+
}
128+
}
129+
90130
public JsonArray getCommitsJSON(JsonObject prPayload) throws InvalidPayloadException {
91131
return getJSONReader(getCommitsUri(prPayload)).readArray();
92132
}

tyr-core/src/main/java/org/jboss/tyr/model/Utils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class Utils {
3737
public static final String REF = "ref";
3838
public static final String BASE = "base";
3939
public static final String COMMITS_URL = "commits_url";
40+
public static final String LABELS = "labels";
41+
public static final String NAME = "name";
42+
43+
//Repository payload
44+
public static final String REPOSITORY = "repository";
45+
public static final String FULL_NAME = "full_name";
4046

4147
//Issue payload
4248
public static final String ISSUE = "issue";
@@ -50,6 +56,15 @@ public class Utils {
5056
public static final String MESSAGE = "message";
5157
public static final String URL = "url";
5258

59+
//GitHub API
60+
public static final String STATUSES_PATH = "/statuses";
61+
public static final String REPOS_PATH = "/repos";
62+
public static final String ISSUES_PATH = "/issues";
63+
public static final String LABELS_PATH = "/labels";
64+
65+
//Hexadecimal color regex
66+
public static final String COLOR_VERIFICATION = "^([a-f0-9]{6})$";
67+
5368
//Repository verification regex
5469
public static final String REPOSITORY_FORMAT_VERIFICATION_REGEX =
5570
"^([a-zA-Z0-9](?:-?[a-zA-Z0-9]){0,38})/[a-zA-Z0-9_.-]{1,100}$";
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc, and individual contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.tyr.model.json;
17+
18+
public class Label {
19+
private String name;
20+
private String description;
21+
private String color;
22+
23+
public Label(String name, String description, String color) {
24+
this.name = name;
25+
this.description = description;
26+
this.color = color;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getDescription() {
38+
return description;
39+
}
40+
41+
public void setDescription(String description) {
42+
this.description = description;
43+
}
44+
45+
public String getColor() {
46+
return color;
47+
}
48+
49+
public void setColor(String color) {
50+
this.color = color;
51+
}
52+
}

tyr-core/src/main/java/org/jboss/tyr/model/yaml/Format.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Format {
2929
private List<String> additionalChecks;
3030
private Map<String, String> commands;
3131
private CommitsQuantity commitsQuantity;
32+
private LabelFormat label;
3233

3334
@JsonProperty("CI")
3435
private List<String> CI;
@@ -96,4 +97,12 @@ public CommitsQuantity getCommitsQuantity() {
9697
public void setCommitsQuantity(CommitsQuantity commitsQuantity) {
9798
this.commitsQuantity = commitsQuantity;
9899
}
100+
101+
public LabelFormat getLabel() {
102+
return label;
103+
}
104+
105+
public void setLabel(LabelFormat label) {
106+
this.label = label;
107+
}
99108
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc, and individual contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.tyr.model.yaml;
17+
18+
public class LabelDetails {
19+
private String description;
20+
private String color;
21+
22+
public String getDescription() {
23+
return description;
24+
}
25+
26+
public void setDescription(String description) {
27+
this.description = description;
28+
}
29+
30+
public String getColor() {
31+
return color;
32+
}
33+
34+
public void setColor(String color) {
35+
this.color = color;
36+
}
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc, and individual contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jboss.tyr.model.yaml;
17+
18+
public class LabelFormat {
19+
private LabelDetails targetBranch;
20+
21+
public LabelDetails getTargetBranch() {
22+
return targetBranch;
23+
}
24+
25+
public void setTargetBranch(LabelDetails targetBranch) {
26+
this.targetBranch = targetBranch;
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
repository: "jboss/test-repo"
2+
statusUrl: "https://github.com/jboss/test-repo/blob/master/.github/pull_request_template.md"
3+
4+
format:
5+
skipPatterns:
6+
title: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
7+
commit: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
8+
description: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
9+
title:
10+
pattern: "\\[WFLY-\\d+\\]\\s+.*|WFLY-\\d+\\s+.*"
11+
message: "Wrong content of the title!"
12+
commit:
13+
pattern: "\\[WFLY-\\d+\\]\\s+.*"
14+
message: "One of the commit messages has wrong format!"
15+
commitsQuantity:
16+
quantity: "1-5"
17+
message: "Too many commits in PR!"
18+
description:
19+
required rows:
20+
- pattern: "JIRA:\\s+https://issues.jboss.org/browse/WFLY-\\d+|https://issues.jboss.org/browse/WFLY-\\d+"
21+
message: "The PR description must contain a link to the JIRA issue"
22+
optional rows:
23+
- precondition: "\\[WFLY-\\s+.*\\]"
24+
pattern: "\\[WFLY-\\d+\\]"
25+
message: "JIRA issues can contain only numbers"
26+
27+
commands:
28+
AddUserCommand: "ok\\s+to\\s+test$"
29+
RetestCommand: "retest\\s+this\\s+please$"
30+
RetestFailedCommand: "retest\\s+failed\\s+please$"
31+
32+
CI:
33+
- TeamCity

0 commit comments

Comments
 (0)