Skip to content

Commit 379e52b

Browse files
authored
feat: history-limit input (#49)
* --wip-- [skip ci] * feat: `history-limit` input
1 parent 5c95b2a commit 379e52b

File tree

7 files changed

+289
-59
lines changed

7 files changed

+289
-59
lines changed

README.md

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ your repository.
5959

6060
### Modify the Pull Request Template
6161

62-
By default, this action will append the visualization to the bottom of the PR description.
62+
By default, the action will append the visualization to the bottom of the PR description.
6363
If you are using a [pull request template](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/creating-a-pull-request-template-for-your-repository),
6464
you can specify the location of the visualization in the template by adding a [HTML comment](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#hiding-content-with-comments)
6565
that contains `branch-stack` inside of it:
@@ -76,7 +76,7 @@ that contains `branch-stack` inside of it:
7676
[ ] Baz
7777
```
7878

79-
This action will look for this comment and insert the visualization underneath the comment
79+
The action will look for this comment and insert the visualization underneath the comment
8080
when it runs. It will also leave behind the comment, so that the next time it runs, it will
8181
be able to use it again to update the visualization:
8282

@@ -98,17 +98,17 @@ be able to use it again to update the visualization:
9898

9999
> [!WARNING]
100100
> Be careful not to add content between the comment and the
101-
> visualization, as this action will replace that content each time it
101+
> visualization, as the action will replace that content each time it
102102
> updates your PR. Adding content above the tag, or below the list is
103103
> safe though!
104104
105-
### Manual Configuration
105+
## Manual Configuration
106106

107-
If you are using Git Town v11 and below, or are setting up this action for a repository
108-
that doesn't have a `.git-branches.toml`, you will need to tell this action what the
107+
If you are using Git Town v11 and below, or are setting up the action for a repository
108+
that doesn't have a `.git-branches.toml`, you will need to tell the action what the
109109
main branch and perennial branches are for your repository.
110110

111-
#### Main Branch
111+
### Main Branch
112112

113113
The main branch is the default parent branch for new feature branches, and can be
114114
specified using the `main-branch` input:
@@ -119,10 +119,10 @@ specified using the `main-branch` input:
119119
main-branch: 'main'
120120
```
121121
122-
This action will default to your repository's default branch, which it fetches via
122+
The action will default to your repository's default branch, which it fetches via
123123
the GitHub REST API.
124124
125-
#### Perennial Branches
125+
### Perennial Branches
126126
127127
Perennial branches are long lived branches and are never shipped.
128128
@@ -139,25 +139,70 @@ be done with the `perennial-branches` and `perennial-regex` inputs respectively:
139139
perennial-regex: '^release-.*$'
140140
```
141141

142-
Both inputs can be used at the same time. This action will merge the perennial
142+
Both inputs can be used at the same time. The action will merge the perennial
143143
branches given into a single, de-duplicated list.
144144

145-
#### Skip Single Stacks
145+
## Customization
146146

147-
If you don't want the stack description to appear on pull requests which are not part of a stack, you can add `skip-single-stacks: true` to the job.
147+
### Skip Single Stacks
148148

149-
This skips all pull requests which point to a main or perennial branch and have no children pull requests pointing to it.
149+
If you don't want the stack visualization to appear on pull requests which are **not** part
150+
of a stack, add `skip-single-stacks: true` to the action's inputs.
151+
152+
A pull request is considered to be **not** a part of a stack if:
153+
- It has no child pull requests.
154+
- It's parent is the main branch or a perennial branch.
150155

151156
```yaml
152157
- uses: git-town/action@v1
153158
with:
154-
perennial-branches: |
155-
dev
156-
staging
157-
prod
158159
skip-single-stacks: true
159160
```
160161

162+
### History Limit
163+
164+
In order to accurately visualize stacked changes, the action needs to fetch _all_ open
165+
and closed pull requests. This can problematic for larger/older repositories that have
166+
a large number of closed pull requests.
167+
168+
The action can be configured to fetch a limited number of closed pull requests. This is
169+
customizable with the `history-limit` input:
170+
171+
```yaml
172+
- uses: git-town/action@v1
173+
with:
174+
history-limit: '500' # Only fetch the latest 500 closed pull requests
175+
```
176+
177+
> [!NOTE]
178+
> This only applies to closed pull requests. Open pull requests will be completely fetched
179+
> regardless of the `history-limit`.
180+
181+
## Reference
182+
183+
```yaml
184+
inputs:
185+
github-token:
186+
required: true
187+
default: ${{ github.token }}
188+
main-branch:
189+
required: false
190+
default: ''
191+
perennial-branches:
192+
required: false
193+
default: ''
194+
perennial-regex:
195+
required: false
196+
default: ''
197+
skip-single-stacks:
198+
required: false
199+
default: false
200+
history-limit:
201+
required: false
202+
default: '0'
203+
```
204+
205+
161206
## License
162207

163208
The scripts and documentation in this project are released under the [MIT License](LICENSE).

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ inputs:
2121
skip-single-stacks:
2222
required: false
2323
default: false
24+
history-limit:
25+
required: false
26+
default: '0'
2427

2528
runs:
2629
using: 'node20'

dist/index.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47076,12 +47076,26 @@ var inputs = {
4707647076
return core2.getInput("github-token", { required: true, trimWhitespace: true });
4707747077
},
4707847078
getSkipSingleStacks() {
47079+
const input = core2.getBooleanInput("skip-single-stacks", {
47080+
required: false,
47081+
trimWhitespace: true
47082+
});
4707947083
core2.startGroup("Inputs: Skip single stacks");
47080-
const input = core2.getBooleanInput("skip-single-stacks", { required: false });
4708147084
core2.info(input.toString());
4708247085
core2.endGroup();
4708347086
return input;
4708447087
},
47088+
getHistoryLimit() {
47089+
const input = core2.getInput("history-limit", {
47090+
required: false,
47091+
trimWhitespace: true
47092+
});
47093+
const historyLimit = Number.parseInt(input, 10);
47094+
core2.startGroup("Inputs: History limit");
47095+
core2.info(input);
47096+
core2.endGroup();
47097+
return historyLimit;
47098+
},
4708547099
async getMainBranch(octokit, config2, context3) {
4708647100
const {
4708747101
data: { default_branch: defaultBranch }
@@ -47155,24 +47169,44 @@ var inputs = {
4715547169
throw error;
4715647170
}
4715747171
},
47158-
async getPullRequests(octokit, context3) {
47159-
const pullRequests = await octokit.paginate(
47160-
"GET /repos/{owner}/{repo}/pulls",
47161-
{
47162-
...context3.repo,
47163-
state: "all",
47164-
per_page: 100
47165-
},
47166-
(response) => response.data.map(
47167-
(item) => ({
47168-
number: item.number,
47169-
base: { ref: item.base.ref },
47170-
head: { ref: item.head.ref },
47171-
body: item.body ?? void 0,
47172-
state: item.state
47173-
})
47172+
async getPullRequests(octokit, context3, historyLimit) {
47173+
function toPullRequest(item) {
47174+
return {
47175+
number: item.number,
47176+
base: { ref: item.base.ref },
47177+
head: { ref: item.head.ref },
47178+
body: item.body ?? void 0,
47179+
state: item.state
47180+
};
47181+
}
47182+
let closedPullRequestCount = 0;
47183+
const [openPullRequests, closedPullRequests] = await Promise.all([
47184+
octokit.paginate(
47185+
"GET /repos/{owner}/{repo}/pulls",
47186+
{
47187+
...context3.repo,
47188+
state: "open",
47189+
per_page: 100
47190+
},
47191+
(response) => response.data.map(toPullRequest)
47192+
),
47193+
octokit.paginate(
47194+
"GET /repos/{owner}/{repo}/pulls",
47195+
{
47196+
...context3.repo,
47197+
state: "closed",
47198+
per_page: 100
47199+
},
47200+
(response, done) => {
47201+
closedPullRequestCount += response.data.length;
47202+
if (historyLimit > 0 && closedPullRequestCount >= historyLimit) {
47203+
done();
47204+
}
47205+
return response.data.map(toPullRequest);
47206+
}
4717447207
)
47175-
);
47208+
]);
47209+
const pullRequests = [...openPullRequests, ...closedPullRequests];
4717647210
pullRequests.sort((a, b) => b.number - a.number);
4717747211
core2.startGroup("Inputs: Pull requests");
4717847212
core2.info(
@@ -47224,10 +47258,11 @@ async function run() {
4722447258
return;
4722547259
}
4722647260
const octokit = github2.getOctokit(inputs.getToken());
47261+
const historyLimit = inputs.getHistoryLimit();
4722747262
const [mainBranch, remoteBranches, pullRequests] = await Promise.all([
4722847263
inputs.getMainBranch(octokit, config, github2.context),
4722947264
inputs.getRemoteBranches(octokit, github2.context),
47230-
inputs.getPullRequests(octokit, github2.context)
47265+
inputs.getPullRequests(octokit, github2.context, historyLimit)
4723147266
]);
4723247267
const perennialBranches = await inputs.getPerennialBranches(config, remoteBranches);
4723347268
const context3 = {

0 commit comments

Comments
 (0)