mirror of
				https://github.com/bcanseco/github-contribution-graph-action.git
				synced 2025-10-09 13:41:58 +03:00 
			
		
		
		
	feat: add RNG for number of commits per day
This commit is contained in:
		
							
								
								
									
										26
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								README.md
									
									
									
									
									
								
							| @@ -59,6 +59,8 @@ If you need help with cron job syntax, [crontab guru](https://crontab.guru/) is | ||||
|  | ||||
| ### Backfill a year of commits whenever you push to master 🍻 | ||||
|  | ||||
| This rolls a pseudorandom number generator between 1 and 5 (inclusive) to determine how many commits to make per-day. | ||||
|  | ||||
| ```yml | ||||
| # .github/workflows/main.yml | ||||
|  | ||||
| @@ -75,6 +77,8 @@ jobs: | ||||
|         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||||
|         GIT_EMAIL: you@youremail.com # replace me | ||||
|         MAX_DAYS: 365 | ||||
|         MIN_COMMITS_PER_DAY: 1 | ||||
|         MAX_COMMITS_PER_DAY: 5 | ||||
| ``` | ||||
|  | ||||
| Keep reading for more cool stuff like: | ||||
| @@ -86,16 +90,18 @@ Keep reading for more cool stuff like: | ||||
|  | ||||
| ## Environment variables 🌳 | ||||
|  | ||||
| | Key                  | Description                                                                                                                   | Default value                                         | Required? | | ||||
| |----------------------|-------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------|-----------| | ||||
| | `GITHUB_TOKEN`       | Allows this GitHub Action to make commits for you. Simply pass in `${{ secrets.GITHUB_TOKEN }}`. [Read more](#github_token-). |                                                       |    🟩    | | ||||
| | `GIT_EMAIL`          | An email address associated with your GitHub account. Without this, contributions won't show up. [Read more](#git_email-).    |                                                       |    🟩    | | ||||
| | `GIT_BRANCH`         | Must either be the default branch (usually `master`) or `gh-pages` for contributions to show up.                              | `master`                                              |           | | ||||
| | `GIT_COMMIT_MESSAGE` | The message to use for commits made by this GitHub Action.                                                                    | `chore(actions): empty commit for contribution graph` |           | | ||||
| | `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                                 |           | | ||||
| | `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`                                                   |           | | ||||
| | `INCLUDE_WEEKDAYS`   | A boolean indicating whether or not to make commits on weekdays.                                                              | `true`                                                |           | | ||||
| | `INCLUDE_WEEKENDS`   | A boolean indicating whether or not to make commits on weekends.                                                              | `true`                                                |           | | ||||
| | Key                   | Description                                                                                                                   | Default value                                         | Required? | | ||||
| |-----------------------|-------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------|-----------| | ||||
| | `GITHUB_TOKEN`        | Allows this GitHub Action to make commits for you. Simply pass in `${{ secrets.GITHUB_TOKEN }}`. [Read more](#github_token-). |                                                       |    🟩    | | ||||
| | `GIT_EMAIL`           | An email address associated with your GitHub account. Without this, contributions won't show up. [Read more](#git_email-).    |                                                       |    🟩    | | ||||
| | `GIT_BRANCH`          | Must either be the default branch (usually `master`) or `gh-pages` for contributions to show up.                              | `master`                                              |           | | ||||
| | `GIT_COMMIT_MESSAGE`  | The message to use for commits made by this GitHub Action.                                                                    | `chore(actions): empty commit for contribution graph` |           | | ||||
| | `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                                 |           | | ||||
| | `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`                                                   |           | | ||||
| | `INCLUDE_WEEKDAYS`    | A boolean indicating whether or not to make commits on weekdays.                                                              | `true`                                                |           | | ||||
| | `INCLUDE_WEEKENDS`    | A boolean indicating whether or not to make commits on weekends.                                                              | `true`                                                |           | | ||||
| | `MIN_COMMITS_PER_DAY` | The minimum integer number of commits to make per day (inclusive). Used by a pseudo-RNG.                                      | `1`                                                   |           | | ||||
| | `MAX_COMMITS_PER_DAY` | The maximum integer number of commits to make per day (inclusive). Used by a pseudo-RNG.                                      | `1`                                                   |           | | ||||
|  | ||||
| ### Advanced environment variables 🧙♂️ | ||||
|  | ||||
|   | ||||
							
								
								
									
										19
									
								
								src/index.js
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/index.js
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ import subDays from 'date-fns/fp/subDays'; | ||||
| import getUnixTime from 'date-fns/fp/getUnixTime'; | ||||
| import fromUnixTime from 'date-fns/fp/fromUnixTime'; | ||||
| import isWeekend from 'date-fns/fp/isWeekend'; | ||||
| import {getRandomInt} from './random'; | ||||
|  | ||||
| const { | ||||
|   GITHUB_ACTOR, | ||||
| @@ -18,6 +19,8 @@ const { | ||||
|   MAX_DAYS = 1, | ||||
|   INCLUDE_WEEKDAYS = true, | ||||
|   INCLUDE_WEEKENDS = true, | ||||
|   MIN_COMMITS_PER_DAY = 1, | ||||
|   MAX_COMMITS_PER_DAY = 1, | ||||
| } = process.env; | ||||
|  | ||||
| const repoPath = `https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${GIT_HOST}/${GITHUB_REPOSITORY}`; | ||||
| @@ -35,13 +38,17 @@ const commitCreators = dayOffsets | ||||
|   .map((dayOffset) => subDays(dayOffset, originDay)) | ||||
|   .filter((day) => !(!JSON.parse(INCLUDE_WEEKENDS) && isWeekend(day))) | ||||
|   .filter((day) => !(!JSON.parse(INCLUDE_WEEKDAYS) && !isWeekend(day))) | ||||
|   .map((day) => async () => { | ||||
|     const {commit: sha} = await git(localPath).commit([GIT_COMMIT_MESSAGE, secondLine], { | ||||
|       '--allow-empty': null, | ||||
|       '--date': `format:iso8601:${day.toISOString()}`, | ||||
|   .map((/** @type {Date} */ day) => { | ||||
|     const commitsToMake = getRandomInt(MIN_COMMITS_PER_DAY, MAX_COMMITS_PER_DAY); | ||||
|     return [...Array(commitsToMake)].map((_, i) => async () => { | ||||
|       const {commit: sha} = await git(localPath).commit([GIT_COMMIT_MESSAGE, secondLine], { | ||||
|         '--allow-empty': null, | ||||
|         '--date': `format:iso8601:${day.toISOString()}`, | ||||
|       }); | ||||
|       console.log(`Successfully committed ${sha} on ${day.toISOString()} (${i + 1} / ${commitsToMake})`); | ||||
|     }); | ||||
|     console.log(`Successfully committed ${sha}`); | ||||
|   }); | ||||
|   }) | ||||
|   .flat(); | ||||
|  | ||||
| await commitCreators.reduce((p, nextPromise) => p.then(nextPromise), Promise.resolve()); | ||||
| await git(localPath).push(repoPath, GIT_BRANCH); | ||||
|   | ||||
							
								
								
									
										16
									
								
								src/random/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/random/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| /** | ||||
|  * Returns a pseudorandom number between two inclusive numbers. | ||||
|  * @param {Number} min The minimum rollable number | ||||
|  * @param {Number} max The maximum rollable number | ||||
|  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values_inclusive | ||||
|  */ | ||||
| export const getRandomInt = (min, max) => { | ||||
|   min = Math.ceil(min); | ||||
|   max = Math.floor(max); | ||||
|  | ||||
|   if (min > max || Math.min(min, max) < 0) { | ||||
|     throw new Error('min and max must be a positive integer range'); | ||||
|   } | ||||
|  | ||||
|   return Math.floor(Math.random() * (max - min + 1)) + min; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Borja Canseco
					Borja Canseco