Skip to content

Commit b24e266

Browse files
committed
feat: add RNG for number of commits per day
1 parent 9597516 commit b24e266

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ If you need help with cron job syntax, [crontab guru](https://crontab.guru/) is
5959
6060
### Backfill a year of commits whenever you push to master 🍻
6161
62+
This rolls a pseudorandom number generator between 1 and 5 (inclusive) to determine how many commits to make per-day.
63+
6264
```yml
6365
# .github/workflows/main.yml
6466

@@ -75,6 +77,8 @@ jobs:
7577
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7678
GIT_EMAIL: [email protected] # replace me
7779
MAX_DAYS: 365
80+
MIN_COMMITS_PER_DAY: 1
81+
MAX_COMMITS_PER_DAY: 5
7882
```
7983
8084
Keep reading for more cool stuff like:
@@ -86,16 +90,18 @@ Keep reading for more cool stuff like:
8690
8791
## Environment variables 🌳
8892
89-
| Key | Description | Default value | Required? |
90-
|----------------------|-------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------|-----------|
91-
| `GITHUB_TOKEN` | Allows this GitHub Action to make commits for you. Simply pass in `${{ secrets.GITHUB_TOKEN }}`. [Read more](#github_token-). | | 🟩 |
92-
| `GIT_EMAIL` | An email address associated with your GitHub account. Without this, contributions won't show up. [Read more](#git_email-). | | 🟩 |
93-
| `GIT_BRANCH` | Must either be the default branch (usually `master`) or `gh-pages` for contributions to show up. | `master` | |
94-
| `GIT_COMMIT_MESSAGE` | The message to use for commits made by this GitHub Action. | `chore(actions): empty commit for contribution graph` | |
95-
| `ORIGIN_TIMESTAMP` | The unix timestamp to start commits on. If you set `MAX_DAYS` greater than 1, commits will be made on days prior to this time.| The current timestamp | |
96-
| `MAX_DAYS` | The maximum integer number of days to commit on. If you want to backfill a year of commits, set this to `365`. | `1` | |
97-
| `INCLUDE_WEEKDAYS` | A boolean indicating whether or not to make commits on weekdays. | `true` | |
98-
| `INCLUDE_WEEKENDS` | A boolean indicating whether or not to make commits on weekends. | `true` | |
93+
| Key | Description | Default value | Required? |
94+
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------|-----------|
95+
| `GITHUB_TOKEN` | Allows this GitHub Action to make commits for you. Simply pass in `${{ secrets.GITHUB_TOKEN }}`. [Read more](#github_token-). | | 🟩 |
96+
| `GIT_EMAIL` | An email address associated with your GitHub account. Without this, contributions won't show up. [Read more](#git_email-). | | 🟩 |
97+
| `GIT_BRANCH` | Must either be the default branch (usually `master`) or `gh-pages` for contributions to show up. | `master` | |
98+
| `GIT_COMMIT_MESSAGE` | The message to use for commits made by this GitHub Action. | `chore(actions): empty commit for contribution graph` | |
99+
| `ORIGIN_TIMESTAMP` | The unix timestamp to start commits on. If you set `MAX_DAYS` greater than 1, commits will be made on days prior to this time.| The current timestamp | |
100+
| `MAX_DAYS` | The maximum integer number of days to commit on. If you want to backfill a year of commits, set this to `365`. | `1` | |
101+
| `INCLUDE_WEEKDAYS` | A boolean indicating whether or not to make commits on weekdays. | `true` | |
102+
| `INCLUDE_WEEKENDS` | A boolean indicating whether or not to make commits on weekends. | `true` | |
103+
| `MIN_COMMITS_PER_DAY` | The minimum integer number of commits to make per day (inclusive). Used by a pseudo-RNG. | `1` | |
104+
| `MAX_COMMITS_PER_DAY` | The maximum integer number of commits to make per day (inclusive). Used by a pseudo-RNG. | `1` | |
99105

100106
### Advanced environment variables 🧙‍♂️
101107

src/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import subDays from 'date-fns/fp/subDays';
33
import getUnixTime from 'date-fns/fp/getUnixTime';
44
import fromUnixTime from 'date-fns/fp/fromUnixTime';
55
import isWeekend from 'date-fns/fp/isWeekend';
6+
import {getRandomInt} from './random';
67

78
const {
89
GITHUB_ACTOR,
@@ -18,6 +19,8 @@ const {
1819
MAX_DAYS = 1,
1920
INCLUDE_WEEKDAYS = true,
2021
INCLUDE_WEEKENDS = true,
22+
MIN_COMMITS_PER_DAY = 1,
23+
MAX_COMMITS_PER_DAY = 1,
2124
} = process.env;
2225

2326
const repoPath = `https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${GIT_HOST}/${GITHUB_REPOSITORY}`;
@@ -35,13 +38,17 @@ const commitCreators = dayOffsets
3538
.map((dayOffset) => subDays(dayOffset, originDay))
3639
.filter((day) => !(!JSON.parse(INCLUDE_WEEKENDS) && isWeekend(day)))
3740
.filter((day) => !(!JSON.parse(INCLUDE_WEEKDAYS) && !isWeekend(day)))
38-
.map((day) => async () => {
39-
const {commit: sha} = await git(localPath).commit([GIT_COMMIT_MESSAGE, secondLine], {
40-
'--allow-empty': null,
41-
'--date': `format:iso8601:${day.toISOString()}`,
41+
.map((/** @type {Date} */ day) => {
42+
const commitsToMake = getRandomInt(MIN_COMMITS_PER_DAY, MAX_COMMITS_PER_DAY);
43+
return [...Array(commitsToMake)].map((_, i) => async () => {
44+
const {commit: sha} = await git(localPath).commit([GIT_COMMIT_MESSAGE, secondLine], {
45+
'--allow-empty': null,
46+
'--date': `format:iso8601:${day.toISOString()}`,
47+
});
48+
console.log(`Successfully committed ${sha} on ${day.toISOString()} (${i + 1} / ${commitsToMake})`);
4249
});
43-
console.log(`Successfully committed ${sha}`);
44-
});
50+
})
51+
.flat();
4552

4653
await commitCreators.reduce((p, nextPromise) => p.then(nextPromise), Promise.resolve());
4754
await git(localPath).push(repoPath, GIT_BRANCH);

src/random/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Returns a pseudorandom number between two inclusive numbers.
3+
* @param {Number} min The minimum rollable number
4+
* @param {Number} max The maximum rollable number
5+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values_inclusive
6+
*/
7+
export const getRandomInt = (min, max) => {
8+
min = Math.ceil(min);
9+
max = Math.floor(max);
10+
11+
if (min > max || Math.min(min, max) < 0) {
12+
throw new Error('min and max must be a positive integer range');
13+
}
14+
15+
return Math.floor(Math.random() * (max - min + 1)) + min;
16+
}

0 commit comments

Comments
 (0)