Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a date range for changelog updates #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 15 additions & 3 deletions changelog-update/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ inputs:
description: 'Target repo. By default, current repo but mandatory in batch mode'
required: false
owner:
description: "Owner of the target repo. By default, current repo owner but mandatory in batch mode"
description: 'Owner of the target repo. By default, current repo owner but mandatory in batch mode'
required: false
start-date:
description: 'The date (YYYY-MM-dd) to start collecting changelog updates from. If empty, updates will only be applied from the day before today. Only has an effect in batch mode, and does not check for duplicate changelog entries.'
required: false
default: ''
end-date:
description: 'The date (YYYY-MM-dd) to collect changelog updates to. Only has an effect when "start-date" is specified.'
required: false
default: ''
dry-run:
description: 'Collect and print the potential changelog update, but do not apply the commit or open a PR.'
required: false
default: false
runs:
using: "node16"
main: "index.js"
using: 'node20'
main: 'index.js'
78 changes: 65 additions & 13 deletions changelog-update/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
//
// This Github action will update the changelog automatically and push the changes on
// the repository afterward.
const core = require('@actions/core');
const github = require('@actions/github');
const { Base64 } = require('js-base64');
import * as core from '@actions/core';
import * as github from '@actions/github';
import { Base64 } from 'js-base64';

// We use octokit directly because the version that @actions/github is too old.
const { Octokit } = require('@octokit/rest');
import { Octokit } from '@octokit/rest';
const auth = core.getInput('github-token');
const octokit = new Octokit({
auth
auth: auth,
log: {
warn: console.warn,
error: console.error
}
});

const onSpotMode = 'on-spot';
Expand Down Expand Up @@ -212,7 +216,7 @@ const fetchPullRequestsOfTheDay = async (owner, repo, offset) => {
// that file.
const getCurrentChangelogText = async (owner, repo) => {
try {
const response = await octokit.repos.getContents({
const response = await octokit.repos.getContent({
owner,
repo,
path: "CHANGELOG.md",
Expand Down Expand Up @@ -256,6 +260,9 @@ async function run() {
const skipped = core.getInput('skipped') || 'false';
const owner = core.getInput("owner") || payload.repository.owner.login;
const repo = core.getInput("repo") || payload.repository.name;
const startDate = core.getInput("start-date") || "";
const endDate = core.getInput("end-date") || "";
const dryRun = core.getInput("dry-run") || false;

core.debug(`Get ${owner}/${repo} default branch...`);
const default_branch = (await octokit.repos.get({
Expand Down Expand Up @@ -313,16 +320,51 @@ async function run() {

base_tree = response.data.tree.sha;
} else if (mode === batchMode) {
let offset = new Date();
offset.setUTCDate(offset.getUTCDate() - 1);
let pulls = [];
core.debug("Running in batch mode");
if (startDate === "") {
core.debug("startDate was not provided. Only dealing with pull requests from the day before today");
let offset = new Date();
offset.setUTCDate(offset.getUTCDate() - 1);

core.debug("Before fetchPullRequestsOfTheDay…");
pulls = await fetchPullRequestsOfTheDay(owner, repo, offset);
core.debug("Completed");
} else {
core.debug("startDate was provided. Parsing start and end dates");
let start = new Date(startDate);
if (isNaN(start.getTime())) {
core.setFailed("startDate input is an invalid date. Please provide the startDate in the format YYYY-MM-dd");
}
core.debug(`start date set to ${start}`);

let end = new Date();
if (endDate !== "") {
end = new Date(endDate);
if (isNaN(end.getTime())) {
core.setFailed("endDate input is an invalid date. Please provide the endDate in the format YYYY-MM-dd");
}
}
core.debug(`end date set to ${end}`);
core.info(`Creating changelog for pull requests from ${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()} to ${end.getUTCFullYear()}-${end.getUTCMonth()+1}-${end.getUTCDate()}`);
let offset = end;
// Gather the pull requests day by day until we reach the start date
while (true) {
core.debug(`Before fetchPullRequestsOfTheDay. Offset is ${offset.getUTCFullYear()}-${offset.getUTCMonth()+1}-${offset.getUTCDate()}`);
pulls = pulls.concat(await fetchPullRequestsOfTheDay(owner, repo, offset));
core.debug("Completed");
offset.setUTCDate(offset.getUTCDate() - 1);
if (offset < start && !belongsToSameDay(start, offset)) {
core.debug("Finished collecting pull requests for the time period");
break;
}
}
}

core.debug("Before fetchPullRequestsOfTheDay…");
let pulls = await fetchPullRequestsOfTheDay(owner, repo, offset);
core.debug("Completed");
core.debug("Gathering pull requests…");
let input = pulls.flatMap(pull => {
core.debug(`>>>Dealing with #${pull.number}`);
core.debug(`${JSON.stringify(pull, null, 4)}`);
// core.debug(`${JSON.stringify(pull, null, 4)}`);
// Because it is possible for a pull request check to be skipped by `pr-check` action (for example, if the PR
// doesn’t change anything under `src` directory), we got to filter those skipped pull requests out in batch
// mode.
Expand Down Expand Up @@ -369,6 +411,15 @@ async function run() {
core.setFailed(`Unsupported mode: ${mode}`);
}

if (dryRun) {
core.info("DryRun was specified. The changelog will not be updated.");
core.info(`Changelog update would be made with base tree: '${base_tree}', parent commit: '${commit_sha}', branch: '${default_branch}'`);
core.info("Changelog update contents: ");
core.info(content);
core.info("Exiting...");
return;
}

core.debug("Create a new git tree…");
const treeResponse = await octokit.git.createTree({
owner,
Expand Down Expand Up @@ -411,4 +462,5 @@ async function run() {
}
}

run();

run();
1 change: 0 additions & 1 deletion changelog-update/node_modules/.bin/semver

This file was deleted.

1 change: 0 additions & 1 deletion changelog-update/node_modules/.bin/which

This file was deleted.

Loading