Skip to content

Commit

Permalink
feat: make reading modified time customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Jul 29, 2024
1 parent 26006aa commit a219dac
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions .knosys/scripts/command/pmc/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { type: getOsType } = require('os');
const { join: joinPath } = require('path');
const { readdirSync, statSync, existsSync } = require('fs');
const { execSync } = require('child_process');
Expand Down Expand Up @@ -41,40 +42,53 @@ function readTaskMetadata() {
return readData(joinPath(pmcDataRoot, 'metadata.json')).task;
}

function resolveTask(taskMetadata, memberDirPath, memberDirName, taskNum) {
function resolveTask({ rewardDeadline, studentRewardPatches, readingModifiedTimeBy }, memberDirPath, memberDirName, taskNum) {
const taskDirName = `task${taskNum}`;
const taskDirPath = joinPath(memberDirPath, taskDirName);
const task = { name: taskDirName, completed: existsSync(taskDirPath), rewardable: false };
const { rewardDeadline, studentRewardPatches } = taskMetadata;

if (task.completed) {
const targetPath = `members/${memberDirName}/${taskDirName}`;
const paths = [`${targetPath}/readme.md`, `${targetPath}/README.md`, targetPath];
let modifiedAt;

for (let i = 0; i < paths.length; i++) {
const modifiedAt = execGit(`git log -1 --follow --pretty=format:"%cd" -- ${paths[i]}`);
if (readingModifiedTimeBy === 'git') {
const targetPath = `members/${memberDirName}/${taskDirName}`;
const paths = [`${targetPath}/readme.md`, `${targetPath}/README.md`, targetPath];

if (modifiedAt) {
task.modifiedAt = dayjs(modifiedAt).format('YYYY-MM-DD HH:mm:ss ZZ');
for (let i = 0; i < paths.length; i++) {
modifiedAt = execGit(`git log -1 --follow --pretty=format:"%cd" -- ${paths[i]}`);

// console.log(`[KNOSYS_INFO] \`${paths[i]}\` full message`, execGit(`git log -1 -- ${paths[i]}`));
console.log(`[KNOSYS_INFO] \`${paths[i]}\` modified at`, modifiedAt, task.modifiedAt);
if (modifiedAt) {
break;
}
}
} else if (readingModifiedTimeBy === 'fs') {
const paths = [joinPath(taskDirPath, 'readme.md'), joinPath(taskDirPath, 'README.md'), taskDirPath];

if (studentRewardPatches[memberDirName] && studentRewardPatches[memberDirName][taskDirName] === true || dayjs(task.modifiedAt).isBefore(dayjs(rewardDeadline))) {
task.rewardable = true;
for (let i = 0; i < paths.length; i++) {
if (existsSync(paths[i])) {
modifiedAt = statSync(paths[i]).mtime;
break;
}
}
}

if (modifiedAt) {
task.modifiedAt = dayjs(modifiedAt).format('YYYY-MM-DD HH:mm:ss ZZ');

console.log(`[KNOSYS_INFO] ${readingModifiedTimeBy} \`members/${memberDirName}/${taskDirName}\` modified at`, task.modifiedAt);

break;
if (studentRewardPatches[memberDirName] && studentRewardPatches[memberDirName][taskDirName] === true || dayjs(task.modifiedAt).isBefore(dayjs(rewardDeadline))) {
task.rewardable = true;
}
}
}

return task;
}

function countStudents() {
function countStudents(readingModifiedTimeBy = getOsType() === 'Linux' ? 'fs' : 'git') {
const MEMBER_ROOT = joinPath(repoRoot, 'members');
const taskMetadata = readTaskMetadata();
const taskMetadata = { ...readTaskMetadata(), readingModifiedTimeBy };

const studentMap = {};
const studentSeq = [];
Expand Down

0 comments on commit a219dac

Please sign in to comment.