Skip to content

Commit 8ace7de

Browse files
authored
Merge branch 'master' into lu_decomposition
2 parents 1d38b7e + 82ff14c commit 8ace7de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4688
-134
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
name: Close stale PRs with failed workflows
2+
3+
on:
4+
schedule:
5+
- cron: '0 3 * * *' # runs daily at 03:00 UTC
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
issues: write
11+
pull-requests: write
12+
13+
jobs:
14+
close-stale:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Close stale PRs
18+
uses: actions/github-script@v8
19+
with:
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
21+
script: |
22+
const mainBranches = ['main', 'master'];
23+
const cutoffDays = 14;
24+
const cutoff = new Date();
25+
cutoff.setDate(cutoff.getDate() - cutoffDays);
26+
27+
console.log(`Checking PRs older than: ${cutoff.toISOString()}`);
28+
29+
try {
30+
const { data: prs } = await github.rest.pulls.list({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
state: 'open',
34+
sort: 'updated',
35+
direction: 'asc',
36+
per_page: 100
37+
});
38+
39+
console.log(`Found ${prs.length} open PRs to check`);
40+
41+
for (const pr of prs) {
42+
try {
43+
const updated = new Date(pr.updated_at);
44+
45+
if (updated > cutoff) {
46+
console.log(`⏩ Skipping PR #${pr.number} - updated recently`);
47+
continue;
48+
}
49+
50+
console.log(`🔍 Checking PR #${pr.number}: "${pr.title}"`);
51+
52+
// Get commits
53+
const commits = await github.paginate(github.rest.pulls.listCommits, {
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
pull_number: pr.number,
57+
per_page: 100
58+
});
59+
60+
const meaningfulCommits = commits.filter(c => {
61+
const msg = c.commit.message.toLowerCase();
62+
const isMergeFromMain = mainBranches.some(branch =>
63+
msg.startsWith(`merge branch '${branch}'`) ||
64+
msg.includes(`merge remote-tracking branch '${branch}'`)
65+
);
66+
return !isMergeFromMain;
67+
});
68+
69+
// Get checks with error handling
70+
let hasFailedChecks = false;
71+
let allChecksCompleted = false;
72+
let hasChecks = false;
73+
74+
try {
75+
const { data: checks } = await github.rest.checks.listForRef({
76+
owner: context.repo.owner,
77+
repo: context.repo.repo,
78+
ref: pr.head.sha
79+
});
80+
81+
hasChecks = checks.check_runs.length > 0;
82+
hasFailedChecks = checks.check_runs.some(c => c.conclusion === 'failure');
83+
allChecksCompleted = checks.check_runs.every(c =>
84+
c.status === 'completed' || c.status === 'skipped'
85+
);
86+
} catch (error) {
87+
console.log(`⚠️ Could not fetch checks for PR #${pr.number}: ${error.message}`);
88+
}
89+
90+
// Get workflow runs with error handling
91+
let hasFailedWorkflows = false;
92+
let allWorkflowsCompleted = false;
93+
let hasWorkflows = false;
94+
95+
try {
96+
const { data: runs } = await github.rest.actions.listWorkflowRuns({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
head_sha: pr.head.sha,
100+
per_page: 50
101+
});
102+
103+
hasWorkflows = runs.workflow_runs.length > 0;
104+
hasFailedWorkflows = runs.workflow_runs.some(r => r.conclusion === 'failure');
105+
allWorkflowsCompleted = runs.workflow_runs.every(r =>
106+
['completed', 'skipped', 'cancelled'].includes(r.status)
107+
);
108+
109+
console.log(`PR #${pr.number}: ${runs.workflow_runs.length} workflow runs found`);
110+
111+
} catch (error) {
112+
console.log(`⚠️ Could not fetch workflow runs for PR #${pr.number}: ${error.message}`);
113+
}
114+
115+
console.log(`PR #${pr.number}: ${meaningfulCommits.length} meaningful commits`);
116+
console.log(`Checks - has: ${hasChecks}, failed: ${hasFailedChecks}, completed: ${allChecksCompleted}`);
117+
console.log(`Workflows - has: ${hasWorkflows}, failed: ${hasFailedWorkflows}, completed: ${allWorkflowsCompleted}`);
118+
119+
// Combine conditions - only consider if we actually have checks/workflows
120+
const hasAnyFailure = (hasChecks && hasFailedChecks) || (hasWorkflows && hasFailedWorkflows);
121+
const allCompleted = (!hasChecks || allChecksCompleted) && (!hasWorkflows || allWorkflowsCompleted);
122+
123+
if (meaningfulCommits.length === 0 && hasAnyFailure && allCompleted) {
124+
console.log(`✅ Closing PR #${pr.number} (${pr.title})`);
125+
126+
await github.rest.issues.createComment({
127+
owner: context.repo.owner,
128+
repo: context.repo.repo,
129+
issue_number: pr.number,
130+
body: `This pull request has been automatically closed because its workflows or checks failed and it has been inactive for more than ${cutoffDays} days. Please fix the workflows and reopen if you'd like to continue. Merging from main/master alone does not count as activity.`
131+
});
132+
133+
await github.rest.pulls.update({
134+
owner: context.repo.owner,
135+
repo: context.repo.repo,
136+
pull_number: pr.number,
137+
state: 'closed'
138+
});
139+
140+
console.log(`✅ Successfully closed PR #${pr.number}`);
141+
} else {
142+
console.log(`⏩ Not closing PR #${pr.number} - conditions not met`);
143+
}
144+
145+
} catch (prError) {
146+
console.error(`❌ Error processing PR #${pr.number}: ${prError.message}`);
147+
continue;
148+
}
149+
}
150+
151+
} catch (error) {
152+
console.error(`❌ Fatal error: ${error.message}`);
153+
throw error;
154+
}

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
- 📄 [FordFulkerson](src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java)
174174
- 📄 [Graphs](src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java)
175175
- 📄 [HamiltonianCycle](src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java)
176+
- 📄 [HierholzerAlgorithm](src/main/java/com/thealgorithms/graph/HierholzerAlgorithm.java)
176177
- 📄 [JohnsonsAlgorithm](src/main/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithm.java)
177178
- 📄 [KahnsAlgorithm](src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java)
178179
- 📄 [Kosaraju](src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java)

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>6.0.0</version>
23+
<version>6.0.1</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -112,14 +112,14 @@
112112
<dependency>
113113
<groupId>com.puppycrawl.tools</groupId>
114114
<artifactId>checkstyle</artifactId>
115-
<version>12.0.1</version>
115+
<version>12.1.1</version>
116116
</dependency>
117117
</dependencies>
118118
</plugin>
119119
<plugin>
120120
<groupId>com.github.spotbugs</groupId>
121121
<artifactId>spotbugs-maven-plugin</artifactId>
122-
<version>4.9.7.0</version>
122+
<version>4.9.8.1</version>
123123
<configuration>
124124
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
125125
<includeTests>true</includeTests>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class for performing circular bit rotations on 32-bit integers.
5+
* Bit rotation is a circular shift operation where bits shifted out on one end
6+
* are reinserted on the opposite end.
7+
*
8+
* <p>This class provides methods for both left and right circular rotations,
9+
* supporting only 32-bit integer operations with proper shift normalization
10+
* and error handling.</p>
11+
*
12+
* @see <a href="https://en.wikipedia.org/wiki/Bit_rotation">Bit Rotation</a>
13+
*/
14+
public final class BitRotate {
15+
16+
/**
17+
* Private constructor to prevent instantiation.
18+
* This is a utility class with only static methods.
19+
*/
20+
private BitRotate() {
21+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
22+
}
23+
24+
/**
25+
* Performs a circular left rotation (left shift) on a 32-bit integer.
26+
* Bits shifted out from the left side are inserted on the right side.
27+
*
28+
* @param value the 32-bit integer value to rotate
29+
* @param shift the number of positions to rotate left (must be non-negative)
30+
* @return the result of left rotating the value by the specified shift amount
31+
* @throws IllegalArgumentException if shift is negative
32+
*
33+
* @example
34+
* // Binary: 10000000 00000000 00000000 00000001
35+
* rotateLeft(0x80000001, 1)
36+
* // Returns: 3 (binary: 00000000 00000000 00000000 00000011)
37+
*/
38+
public static int rotateLeft(int value, int shift) {
39+
if (shift < 0) {
40+
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
41+
}
42+
43+
// Normalize shift to the range [0, 31] using modulo 32
44+
shift = shift % 32;
45+
46+
if (shift == 0) {
47+
return value;
48+
}
49+
50+
// Left rotation: (value << shift) | (value >>> (32 - shift))
51+
return (value << shift) | (value >>> (32 - shift));
52+
}
53+
54+
/**
55+
* Performs a circular right rotation (right shift) on a 32-bit integer.
56+
* Bits shifted out from the right side are inserted on the left side.
57+
*
58+
* @param value the 32-bit integer value to rotate
59+
* @param shift the number of positions to rotate right (must be non-negative)
60+
* @return the result of right rotating the value by the specified shift amount
61+
* @throws IllegalArgumentException if shift is negative
62+
*
63+
* @example
64+
* // Binary: 00000000 00000000 00000000 00000011
65+
* rotateRight(3, 1)
66+
* // Returns: -2147483647 (binary: 10000000 00000000 00000000 00000001)
67+
*/
68+
public static int rotateRight(int value, int shift) {
69+
if (shift < 0) {
70+
throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
71+
}
72+
73+
// Normalize shift to the range [0, 31] using modulo 32
74+
shift = shift % 32;
75+
76+
if (shift == 0) {
77+
return value;
78+
}
79+
80+
// Right rotation: (value >>> shift) | (value << (32 - shift))
81+
return (value >>> shift) | (value << (32 - shift));
82+
}
83+
}

0 commit comments

Comments
 (0)