Skip to content

Commit 3d0e6c8

Browse files
committed
feat: add use_badges parameter to build_table for private repo support
- Add use_badges parameter to build_table function - Default (false) generates 3-column table without shields.io badges - When true, generates 4-column table with shields.io status badges - Update existing tests to use new default (no badges) - Add new tests for badges mode
1 parent fdadf62 commit 3d0e6c8

10 files changed

Lines changed: 207 additions & 105 deletions

src/markdown.rs

Lines changed: 162 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn build_table(
88
title: &str,
99
prelude_path: Option<&str>,
1010
repository: &str,
11+
use_badges: bool,
1112
) -> String {
1213
let is_complete = deps
1314
.iter()
@@ -27,89 +28,104 @@ pub fn build_table(
2728
out.push('\n');
2829
}
2930

30-
out.push_str("| PR | Title | Status | Merges Into |\n");
31-
out.push_str("|:--:|:------|:-------|:-------------:|\n");
31+
if use_badges {
32+
out.push_str("| PR | Title | Status | Merges Into |\n");
33+
out.push_str("|:--:|:------|:-------|:-----------:|\n");
34+
} else {
35+
out.push_str("| PR | Title | Merges Into |\n");
36+
out.push_str("|:--:|:------|:-----------:|\n");
37+
}
3238

3339
for (node, parent) in deps {
34-
let review_state = match node.review_state() {
35-
PullRequestReviewState::APPROVED => {
36-
format!(
37-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
38-
repository,
39-
&node.number().to_string(),
40-
"Approved"
41-
)
42-
}
43-
PullRequestReviewState::MERGED => {
44-
format!(
45-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
46-
repository,
47-
&node.number().to_string(),
48-
"%20"
49-
)
50-
}
51-
PullRequestReviewState::PENDING => {
52-
format!(
53-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
54-
repository,
55-
&node.number().to_string(),
56-
"Pending"
57-
)
58-
}
59-
PullRequestReviewState::CHANGES_REQUESTED => {
60-
format!(
61-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
62-
repository,
63-
&node.number().to_string(),
64-
"Changes Requested"
65-
)
66-
}
67-
PullRequestReviewState::DISMISSED => {
68-
format!(
69-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
70-
repository,
71-
&node.number().to_string(),
72-
"Dismissed"
73-
)
74-
}
75-
PullRequestReviewState::COMMENTED => {
40+
let review_state = if use_badges {
41+
let state = match node.review_state() {
42+
PullRequestReviewState::APPROVED => {
43+
format!(
44+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
45+
repository,
46+
&node.number().to_string(),
47+
"Approved"
48+
)
49+
}
50+
PullRequestReviewState::MERGED => {
51+
format!(
52+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
53+
repository,
54+
&node.number().to_string(),
55+
"%20"
56+
)
57+
}
58+
PullRequestReviewState::PENDING => {
59+
format!(
60+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
61+
repository,
62+
&node.number().to_string(),
63+
"Pending"
64+
)
65+
}
66+
PullRequestReviewState::CHANGES_REQUESTED => {
67+
format!(
68+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
69+
repository,
70+
&node.number().to_string(),
71+
"Changes Requested"
72+
)
73+
}
74+
PullRequestReviewState::DISMISSED => {
75+
format!(
76+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
77+
repository,
78+
&node.number().to_string(),
79+
"Dismissed"
80+
)
81+
}
82+
PullRequestReviewState::COMMENTED => {
83+
format!(
84+
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
85+
repository,
86+
&node.number().to_string(),
87+
"Commented"
88+
)
89+
}
90+
};
91+
92+
if node.review_state() != PullRequestReviewState::MERGED
93+
&& *node.state() == PullRequestStatus::Closed
94+
{
7695
format!(
7796
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
7897
repository,
7998
&node.number().to_string(),
80-
"Commented"
99+
"Closed"
81100
)
101+
} else {
102+
state
82103
}
83-
};
84-
85-
let review_state = if node.review_state() != PullRequestReviewState::MERGED
86-
&& *node.state() == PullRequestStatus::Closed
87-
{
88-
format!(
89-
"![](https://img.shields.io/github/pulls/detail/state/{}/{}?label={})",
90-
repository,
91-
&node.number().to_string(),
92-
"Closed"
93-
)
94104
} else {
95-
review_state
105+
String::new()
96106
};
97107

98-
let row = match (node.state(), parent) {
99-
(_, None) => format!(
100-
"|#{}|{}|{}|{}|\n",
101-
node.number(),
102-
node.title(),
103-
review_state,
104-
"-"
105-
),
106-
(_, Some(parent)) => format!(
107-
"|#{}|{}|{}|#{}|\n",
108-
node.number(),
109-
node.title(),
110-
review_state,
111-
parent.number(),
112-
),
108+
let row = if use_badges {
109+
match parent {
110+
None => format!("|#{}|{}|{}|-|\n", node.number(), node.title(), review_state),
111+
Some(parent) => format!(
112+
"|#{}|{}|{}|#{}|\n",
113+
node.number(),
114+
node.title(),
115+
review_state,
116+
parent.number()
117+
),
118+
}
119+
} else {
120+
match parent {
121+
None => format!("|#{}|{}|-|\n", node.number(), node.title()),
122+
Some(parent) => format!(
123+
"|#{}|{}|#{}|\n",
124+
node.number(),
125+
node.title(),
126+
parent.number()
127+
),
128+
}
113129
};
114130

115131
out.push_str(&row);
@@ -158,7 +174,7 @@ mod tests {
158174
);
159175
let deps: FlatDep = vec![(pr, None)];
160176

161-
let table = build_table(&deps, "JIRA-123", None, "user/repo");
177+
let table = build_table(&deps, "JIRA-123", None, "user/repo", false);
162178
insta::assert_snapshot!(table);
163179
}
164180

@@ -198,7 +214,7 @@ mod tests {
198214
(pr3.clone(), Some(pr2.clone())),
199215
];
200216

201-
let table = build_table(&deps, "STACK-456", None, "org/project");
217+
let table = build_table(&deps, "STACK-456", None, "org/project", false);
202218
insta::assert_snapshot!(table);
203219
}
204220

@@ -215,7 +231,7 @@ mod tests {
215231
);
216232
let deps: FlatDep = vec![(pr, None)];
217233

218-
let table = build_table(&deps, "DRAFT-TEST", None, "user/repo");
234+
let table = build_table(&deps, "DRAFT-TEST", None, "user/repo", false);
219235
insta::assert_snapshot!(table);
220236
}
221237

@@ -232,7 +248,7 @@ mod tests {
232248
);
233249
let deps: FlatDep = vec![(pr, None)];
234250

235-
let table = build_table(&deps, "CLOSED-TEST", None, "user/repo");
251+
let table = build_table(&deps, "CLOSED-TEST", None, "user/repo", false);
236252
insta::assert_snapshot!(table);
237253
}
238254

@@ -249,7 +265,7 @@ mod tests {
249265
);
250266
let deps: FlatDep = vec![(pr, None)];
251267

252-
let table = build_table(&deps, "MERGED-TEST", None, "user/repo");
268+
let table = build_table(&deps, "MERGED-TEST", None, "user/repo", false);
253269
insta::assert_snapshot!(table);
254270
}
255271

@@ -276,7 +292,7 @@ mod tests {
276292

277293
let deps: FlatDep = vec![(pr1.clone(), None), (pr2.clone(), Some(pr1.clone()))];
278294

279-
let table = build_table(&deps, "COMPLETE-STACK", None, "user/repo");
295+
let table = build_table(&deps, "COMPLETE-STACK", None, "user/repo", false);
280296
insta::assert_snapshot!(table);
281297
}
282298

@@ -316,7 +332,74 @@ mod tests {
316332
(pr3.clone(), Some(pr2.clone())),
317333
];
318334

319-
let table = build_table(&deps, "MIXED-STACK", None, "org/repo");
335+
let table = build_table(&deps, "MIXED-STACK", None, "org/repo", false);
336+
insta::assert_snapshot!(table);
337+
}
338+
339+
#[test]
340+
fn test_build_table_with_badges() {
341+
let pr1 = make_pr(
342+
1,
343+
"feature-1",
344+
"main",
345+
"Base feature",
346+
PullRequestStatus::Open,
347+
false,
348+
None,
349+
);
350+
let pr2 = make_pr(
351+
2,
352+
"feature-2",
353+
"feature-1",
354+
"Second feature",
355+
PullRequestStatus::Open,
356+
false,
357+
None,
358+
);
359+
360+
let deps: FlatDep = vec![(pr1.clone(), None), (pr2.clone(), Some(pr1.clone()))];
361+
362+
let table = build_table(&deps, "BADGES-TEST", None, "org/project", true);
363+
insta::assert_snapshot!(table);
364+
}
365+
366+
#[test]
367+
fn test_build_table_with_badges_mixed_states() {
368+
let pr1 = make_pr(
369+
1,
370+
"feature-1",
371+
"main",
372+
"Merged base",
373+
PullRequestStatus::Closed,
374+
false,
375+
Some("2024-01-15T10:00:00Z".to_string()),
376+
);
377+
let pr2 = make_pr(
378+
2,
379+
"feature-2",
380+
"feature-1",
381+
"Open follow-up",
382+
PullRequestStatus::Open,
383+
false,
384+
None,
385+
);
386+
let pr3 = make_pr(
387+
3,
388+
"feature-3",
389+
"feature-2",
390+
"Draft WIP",
391+
PullRequestStatus::Open,
392+
true,
393+
None,
394+
);
395+
396+
let deps: FlatDep = vec![
397+
(pr1.clone(), None),
398+
(pr2.clone(), Some(pr1.clone())),
399+
(pr3.clone(), Some(pr2.clone())),
400+
];
401+
402+
let table = build_table(&deps, "BADGES-MIXED", None, "org/repo", true);
320403
insta::assert_snapshot!(table);
321404
}
322405
}

src/snapshots/gh_stack__markdown__tests__build_table_all_closed_shows_checkmark.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source: src/markdown.rs
33
expression: table
44
---
55
### ✅ Stacked PR Chain: COMPLETE-STACK
6-
| PR | Title | Status | Merges Into |
7-
|:--:|:------|:-------|:-------------:|
8-
|#1|~~First~~|![](https://img.shields.io/github/pulls/detail/state/user/repo/1?label=Closed)|-|
9-
|#2|~~Second~~|![](https://img.shields.io/github/pulls/detail/state/user/repo/2?label=Closed)|#1|
6+
| PR | Title | Merges Into |
7+
|:--:|:------|:-----------:|
8+
|#1|~~First~~|-|
9+
|#2|~~Second~~|#1|

src/snapshots/gh_stack__markdown__tests__build_table_linear_stack.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source: src/markdown.rs
33
expression: table
44
---
55
### Stacked PR Chain: STACK-456
6-
| PR | Title | Status | Merges Into |
7-
|:--:|:------|:-------|:-------------:|
8-
|#1|Base feature|![](https://img.shields.io/github/pulls/detail/state/org/project/1?label=Pending)|-|
9-
|#2|Second feature|![](https://img.shields.io/github/pulls/detail/state/org/project/2?label=Pending)|#1|
10-
|#3|Third feature|![](https://img.shields.io/github/pulls/detail/state/org/project/3?label=Pending)|#2|
6+
| PR | Title | Merges Into |
7+
|:--:|:------|:-----------:|
8+
|#1|Base feature|-|
9+
|#2|Second feature|#1|
10+
|#3|Third feature|#2|

src/snapshots/gh_stack__markdown__tests__build_table_mixed_states.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source: src/markdown.rs
33
expression: table
44
---
55
### Stacked PR Chain: MIXED-STACK
6-
| PR | Title | Status | Merges Into |
7-
|:--:|:------|:-------|:-------------:|
8-
|#1|~~Merged base~~|![](https://img.shields.io/github/pulls/detail/state/org/repo/1?label=%20)|-|
9-
|#2|Open follow-up|![](https://img.shields.io/github/pulls/detail/state/org/repo/2?label=Pending)|#1|
10-
|#3|*(Draft) Draft WIP*|![](https://img.shields.io/github/pulls/detail/state/org/repo/3?label=Pending)|#2|
6+
| PR | Title | Merges Into |
7+
|:--:|:------|:-----------:|
8+
|#1|~~Merged base~~|-|
9+
|#2|Open follow-up|#1|
10+
|#3|*(Draft) Draft WIP*|#2|

src/snapshots/gh_stack__markdown__tests__build_table_single_pr.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ source: src/markdown.rs
33
expression: table
44
---
55
### Stacked PR Chain: JIRA-123
6-
| PR | Title | Status | Merges Into |
7-
|:--:|:------|:-------|:-------------:|
8-
|#1|Add new feature|![](https://img.shields.io/github/pulls/detail/state/user/repo/1?label=Pending)|-|
6+
| PR | Title | Merges Into |
7+
|:--:|:------|:-----------:|
8+
|#1|Add new feature|-|
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: src/markdown.rs
3+
expression: table
4+
---
5+
### Stacked PR Chain: BADGES-TEST
6+
| PR | Title | Status | Merges Into |
7+
|:--:|:------|:-------|:-----------:|
8+
|#1|Base feature|![](https://img.shields.io/github/pulls/detail/state/org/project/1?label=Pending)|-|
9+
|#2|Second feature|![](https://img.shields.io/github/pulls/detail/state/org/project/2?label=Pending)|#1|
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: src/markdown.rs
3+
expression: table
4+
---
5+
### Stacked PR Chain: BADGES-MIXED
6+
| PR | Title | Status | Merges Into |
7+
|:--:|:------|:-------|:-----------:|
8+
|#1|~~Merged base~~|![](https://img.shields.io/github/pulls/detail/state/org/repo/1?label=%20)|-|
9+
|#2|Open follow-up|![](https://img.shields.io/github/pulls/detail/state/org/repo/2?label=Pending)|#1|
10+
|#3|*(Draft) Draft WIP*|![](https://img.shields.io/github/pulls/detail/state/org/repo/3?label=Pending)|#2|

src/snapshots/gh_stack__markdown__tests__build_table_with_closed_pr.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ source: src/markdown.rs
33
expression: table
44
---
55
### ✅ Stacked PR Chain: CLOSED-TEST
6-
| PR | Title | Status | Merges Into |
7-
|:--:|:------|:-------|:-------------:|
8-
|#1|~~Completed feature~~|![](https://img.shields.io/github/pulls/detail/state/user/repo/1?label=Closed)|-|
6+
| PR | Title | Merges Into |
7+
|:--:|:------|:-----------:|
8+
|#1|~~Completed feature~~|-|

0 commit comments

Comments
 (0)