Skip to content

Commit b18ce6e

Browse files
authored
github action remove label on commit and add new label for new failures (#32639)
1 parent fd66913 commit b18ce6e

6 files changed

Lines changed: 135 additions & 26 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
name: Remove TeamCity Label on Commit
3+
4+
on:
5+
pull_request:
6+
types: [synchronize]
7+
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
12+
jobs:
13+
apply-outdated-label:
14+
runs-on: ubuntu-latest
15+
env:
16+
TEAMCITY_LABELS: teamcity-passed,teamcity-failed,teamcity-new-failure
17+
OUTDATED_LABEL: teamcity-outdated
18+
PASSED_LABEL: teamcity-passed
19+
steps:
20+
- name: Check for TeamCity labels and apply outdated label
21+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
22+
with:
23+
github-token: ${{ secrets.GITHUB_TOKEN }}
24+
script: |
25+
const teamcityLabels = process.env.TEAMCITY_LABELS.split(',');
26+
const outdatedLabel = process.env.OUTDATED_LABEL;
27+
const passedLabel = process.env.PASSED_LABEL;
28+
29+
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
30+
owner: context.repo.owner,
31+
repo: context.repo.repo,
32+
issue_number: context.payload.pull_request.number
33+
});
34+
35+
const labelNames = labels.map(label => label.name);
36+
const hasTeamCityLabel = teamcityLabels.some(label => labelNames.includes(label));
37+
const hasOutdatedLabel = labelNames.includes(outdatedLabel);
38+
const hasPassedLabel = labelNames.includes(passedLabel);
39+
40+
if (hasTeamCityLabel) {
41+
const foundLabels = teamcityLabels.filter(label => labelNames.includes(label));
42+
core.info(`Found TeamCity labels: ${foundLabels.join(', ')}`);
43+
44+
if (!hasOutdatedLabel) {
45+
core.info(`Applying "${outdatedLabel}" label to PR #${context.payload.pull_request.number}`);
46+
47+
try {
48+
await github.rest.issues.addLabels({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
issue_number: context.payload.pull_request.number,
52+
labels: [outdatedLabel]
53+
});
54+
55+
core.info(`Successfully applied "${outdatedLabel}" label`);
56+
} catch (error) {
57+
core.error(`Failed to apply "${outdatedLabel}" label: ${error.message}`);
58+
throw error;
59+
}
60+
} else {
61+
core.info(`Label "${outdatedLabel}" already exists on PR #${context.payload.pull_request.number}`);
62+
}
63+
64+
// Remove passed label if it exists
65+
if (hasPassedLabel) {
66+
core.info(`Removing "${passedLabel}" label from PR #${context.payload.pull_request.number}`);
67+
68+
try {
69+
await github.rest.issues.removeLabel({
70+
owner: context.repo.owner,
71+
repo: context.repo.repo,
72+
issue_number: context.payload.pull_request.number,
73+
name: passedLabel
74+
});
75+
76+
core.info(`Successfully removed "${passedLabel}" label`);
77+
} catch (error) {
78+
if (error.status === 404) {
79+
core.warning(`Label "${passedLabel}" was not found on PR #${context.payload.pull_request.number}`);
80+
} else {
81+
core.error(`Failed to remove "${passedLabel}" label: ${error.message}`);
82+
throw error;
83+
}
84+
}
85+
}
86+
} else {
87+
core.info(`No TeamCity labels found on PR #${context.payload.pull_request.number}`);
88+
}

.teamcity/components/build_azure.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class ClientConfiguration(var clientId: String,
2121
val betaVersionEnvVar : String,
2222
val labelSuccess : String,
2323
val labelFailure : String,
24+
val labelOutdated : String,
25+
val labelNewFailure : String,
26+
val applyTestingLabelsEnabled : Boolean,
2427
)
2528

2629
class LocationConfiguration(var primary : String, var secondary : String, var tertiary : String, var rotate : Boolean)
@@ -59,4 +62,7 @@ fun ParametrizedWithType.ConfigureAzureSpecificTestParameters(environment: Strin
5962
hiddenVariable("env.TRACKING_ID", "0", "Tracking id for the comments posted by the build")
6063
hiddenVariable("env.LABEL_SUCCESS", config.labelSuccess, "Label applied when teamcity build passed")
6164
hiddenVariable("env.LABEL_FAILURE", config.labelFailure, "Label applied when teamcity build failed")
65+
hiddenVariable("env.LABEL_OUTDATED", config.labelOutdated, "Label applied when teamcity build is outdated")
66+
hiddenVariable("env.LABEL_NEW_FAILURE", config.labelNewFailure, "Label applied when teamcity build has new failures")
67+
hiddenVariable("env.APPLY_TESTING_LABELS_ENABLED", config.applyTestingLabelsEnabled.toString(), "Whether to apply testing labels to PRs")
6268
}

.teamcity/components/settings.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ var serviceTestConfigurationOverrides = mapOf(
206206
// Currently, we have insufficient quota to actually run these, but there are a few nodes in West Europe, so we'll pin it there for now
207207
"vmware" to testConfiguration(parallelism = 3, locationOverride = LocationConfiguration("westeurope", "westus2", "eastus2", false)),
208208

209-
// In general, Azure Voice Service is available in several different regions, but each subscription will only be allowlisted for specific regions(`westcentralus`, `westcentralus`, `westcentralus`).
210-
// Only the regions (`westcentralus`) is specified since the devtest subscription does not support creating resource group for the other two regions.
211-
"voiceservices" to testConfiguration(parallelism = 3, locationOverride = LocationConfiguration("westcentralus", "westcentralus", "westcentralus", false)),
212-
213209
// Offset start hour to avoid collision with new App Service, reduce frequency of testing days
214210
"web" to testConfiguration(startHour = 3, daysOfWeek = "1,3,5", locationOverride = LocationConfiguration("westeurope", "francecentral", "eastus2", true)),
215211

.teamcity/scripts/post_github_comment.sh

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ BETA_VERSION_ENV_VAR="%env.BETA_VERSION_ENV_VAR%"
1010
TEAMCITY_BUILD_BRANCH="%teamcity.build.branch%"
1111
LABEL_SUCCESS="%env.LABEL_SUCCESS%"
1212
LABEL_FAILURE="%env.LABEL_FAILURE%"
13-
13+
LABEL_OUTDATED="%env.LABEL_OUTDATED%"
14+
LABEL_NEW_FAILURE="%env.LABEL_NEW_FAILURE%"
15+
APPLY_TESTING_LABELS_ENABLED="%env.APPLY_TESTING_LABELS_ENABLED%"
1416

1517
if [ "$POST_GITHUB_COMMENT" != "true" ]; then
1618
echo "GitHub commenting disabled — skipping."
@@ -63,11 +65,20 @@ remove_label() {
6365
set_testing_label() {
6466
local label="$1"
6567
if [ "$label" = "$LABEL_SUCCESS" ]; then
68+
remove_label "$LABEL_OUTDATED"
6669
remove_label "$LABEL_FAILURE"
70+
remove_label "$LABEL_NEW_FAILURE"
6771
apply_label "$LABEL_SUCCESS"
6872
elif [ "$label" = "$LABEL_FAILURE" ]; then
73+
remove_label "$LABEL_OUTDATED"
6974
remove_label "$LABEL_SUCCESS"
75+
remove_label "$LABEL_NEW_FAILURE"
7076
apply_label "$LABEL_FAILURE"
77+
elif [ "$label" = "$LABEL_NEW_FAILURE" ]; then
78+
remove_label "$LABEL_OUTDATED"
79+
remove_label "$LABEL_SUCCESS"
80+
remove_label "$LABEL_FAILURE"
81+
apply_label "$LABEL_NEW_FAILURE"
7182
fi
7283
}
7384

@@ -269,25 +280,24 @@ curl -s -X POST \
269280
"https://api.github.com/repos/$GITHUB_REPO/issues/${PR_NUMBER}/comments" \
270281
-d "{\"body\": $(jq -Rs . <<< "$COMMENT")}"
271282

272-
echo "Applying labels..."
283+
if APPLY_TESTING_LABELS_ENABLED; then
284+
echo "Applying labels..."
273285

274-
# If no failures, apply teamcity-passed label
275-
if [ "$FAIL_COUNT" -eq 0 ]; then
276-
echo "No test failures detected"
277-
set_testing_label "$LABEL_SUCCESS"
278-
exit 0
279-
fi
286+
# If no failures, apply teamcity-passed label
287+
if [ "$FAIL_COUNT" -eq 0 ]; then
288+
echo "No test failures detected"
289+
set_testing_label "$LABEL_SUCCESS"
290+
exit 0
291+
fi
280292

281-
# If there are failures, determine label based on earlier analysis
282-
if [ -z "$MAIN_TEST_RESULTS" ]; then
283-
echo "Could not fetch main branch results - applying '$LABEL_FAILURE' label as precaution..."
284-
set_testing_label "$LABEL_FAILURE"
285-
elif [ -z "$NEW_FAILURES" ]; then
286-
echo "All failed tests also exist in main branch"
287-
set_testing_label "$LABEL_SUCCESS"
288-
else
289-
echo "Found new test failures not present in main branch"
290-
set_testing_label "$LABEL_FAILURE"
291-
fi
293+
# If there are failures, determine label based on earlier analysis
294+
if [ -z "$NEW_FAILURES" ]; then
295+
echo "All failed tests also exist in main branch"
296+
set_testing_label "$LABEL_FAILURE"
297+
else
298+
echo "Found new test failures not present in main branch"
299+
set_testing_label "$LABEL_NEW_FAILURE"
300+
fi
292301

293-
echo "Label application complete"
302+
echo "Label application complete"
303+
fi

.teamcity/settings.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ var teamcityToken = DslContext.getParameter("teamcityToken", "")
2424
var betaVersionEnvVar = DslContext.getParameter("betaVersionEnvVar", "env.ARM_FIVEPOINTZERO_BETA")
2525
var labelSuccess = DslContext.getParameter("labelSuccess", "teamcity-passed")
2626
var labelFailure = DslContext.getParameter("labelFailure", "teamcity-failed")
27+
var labelOutdated = DslContext.getParameter("labelOutdated", "teamcity-outdated")
28+
var labelNewFailure = DslContext.getParameter("labelNewFailure", "teamcity-new-failure")
29+
var applyTestingLabelsEnabled = DslContext.getParameter("applyTestingLabelsEnabled", "true").equals("true", ignoreCase = true)
2730

2831

2932
var clientConfig = ClientConfiguration(
@@ -47,7 +50,10 @@ var clientConfig = ClientConfiguration(
4750
teamcityToken,
4851
betaVersionEnvVar,
4952
labelSuccess,
50-
labelFailure
53+
labelFailure,
54+
labelOutdated,
55+
labelNewFailure,
56+
applyTestingLabelsEnabled
5157
)
5258

5359
project(AzureRM(environment, clientConfig))

.teamcity/tests/helpers.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ fun TestConfiguration() : ClientConfiguration {
2929
"teamcityToken",
3030
"env.ARM_FIVEPOINTZERO_BETA",
3131
"teamcity-passed",
32-
"teamcity-failed"
32+
"teamcity-failed",
33+
"teamcity-outdated",
34+
"teamcity-new-failure",
35+
false
3336
)
3437
}

0 commit comments

Comments
 (0)