Skip to content

Commit 9436f94

Browse files
authored
Merge of #180
2 parents 356aad2 + 452ad26 commit 9436f94

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/__tests__/mergify.test.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,84 @@ describe("isPullRequestOpen", () => {
106106
document.body.innerHTML = "";
107107
});
108108

109-
it("should get opened pull request status", () => {
109+
it("should detect open PR via legacy span.State (fixture)", () => {
110110
injectFixtureInDOM("github_pr_opened");
111111

112112
const status = isPullRequestOpen();
113113

114114
expect(status).toBe(true);
115115
});
116116

117-
it("should get opened pull request status", () => {
117+
it("should detect merged PR via legacy span.State (fixture)", () => {
118118
injectFixtureInDOM("github_pr_merged");
119119

120120
const status = isPullRequestOpen();
121121

122122
expect(status).toBe(false);
123123
});
124+
125+
it("should detect open PR via data-status=pullOpened attribute", () => {
126+
document.body.innerHTML = '<span data-status="pullOpened">Open</span>';
127+
128+
expect(isPullRequestOpen()).toBe(true);
129+
});
130+
131+
it("should detect draft PR via data-status=draft attribute", () => {
132+
document.body.innerHTML = '<span data-status="draft">Draft</span>';
133+
134+
expect(isPullRequestOpen()).toBe(true);
135+
});
136+
137+
it("should detect closed PR via data-status=pullClosed attribute", () => {
138+
document.body.innerHTML =
139+
'<span data-status="pullClosed">Closed</span>';
140+
141+
expect(isPullRequestOpen()).toBe(false);
142+
});
143+
144+
it("should detect merged PR via data-status=pullMerged attribute", () => {
145+
document.body.innerHTML =
146+
'<span data-status="pullMerged">Merged</span>';
147+
148+
expect(isPullRequestOpen()).toBe(false);
149+
});
150+
151+
it("should prefer data-status over legacy span.State", () => {
152+
document.body.innerHTML =
153+
'<span data-status="pullOpened">Open</span>' +
154+
'<span class="State" title="Status: Closed">Closed</span>';
155+
156+
expect(isPullRequestOpen()).toBe(true);
157+
});
158+
159+
it("should detect closed PR via legacy span.State when no data-status", () => {
160+
document.body.innerHTML =
161+
'<span class="State" title="Status: Closed">Closed</span>';
162+
163+
expect(isPullRequestOpen()).toBe(false);
164+
});
165+
166+
it("should assume open when no status element is found", () => {
167+
document.body.innerHTML = "<div>No status here</div>";
168+
169+
expect(isPullRequestOpen()).toBe(true);
170+
});
171+
172+
it("should assume open when legacy span.State has no parseable title", () => {
173+
const consoleSpy = jest
174+
.spyOn(console, "warn")
175+
.mockImplementation(() => {});
176+
177+
document.body.innerHTML =
178+
'<span class="State" title="Malformed">Badge</span>';
179+
180+
expect(isPullRequestOpen()).toBe(true);
181+
expect(consoleSpy).toHaveBeenCalledWith(
182+
"Can't find pull request status",
183+
);
184+
185+
consoleSpy.mockRestore();
186+
});
124187
});
125188

126189
describe("isMergifyEnabledOnTheRepo caching behavior", () => {

src/mergify.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ function postCommand(command) {
102102
function isPullRequestOpen() {
103103
const opened = document.querySelector("span[data-status=pullOpened]");
104104
const draft = document.querySelector("span[data-status=draft]");
105+
const closed = document.querySelector("span[data-status=pullClosed]");
106+
const merged = document.querySelector("span[data-status=pullMerged]");
105107
if (opened || draft) return true;
108+
if (closed || merged) return false;
106109

107110
const oldStatusBadge = document.querySelector("span.State");
108111
if (oldStatusBadge) {

0 commit comments

Comments
 (0)