Skip to content

Commit

Permalink
fix: comparing result returns none when length of commits is one
Browse files Browse the repository at this point in the history
  • Loading branch information
piquark6046 committed Dec 10, 2023
1 parent e5aea30 commit 084eeb8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
13 changes: 10 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ExportArgs, IsDebug} from './sources/debug.js'
import {ReplaceStringWithBooleanInObject} from './sources/utility.js'
import {GetLatestWorkflowTime} from './sources/actions.js'
import {ListBranches} from './sources/branches.js'
import {GetChangedFilesFromSHAToHead, GetCommitSHAFromLatestWorkflowTime} from './sources/commits.js'
import {GetChangedFilesFromACommit, GetChangedFilesFromSHAToHead, GetCommitSHAFromLatestWorkflowTime} from './sources/commits.js'
import {PurgeRequestManager} from './sources/requests.js'

const Program = new Commander.Command()
Expand Down Expand Up @@ -38,12 +38,19 @@ const PurgeRequest = new PurgeRequestManager(ProgramOptions)
for (const Branch of Branches) {
// eslint-disable-next-line no-await-in-loop
const CommitSHA = await GetCommitSHAFromLatestWorkflowTime(ProgramOptions, LatestWorkflowRunTime, Branch).then(CommitSHA => CommitSHA)
if (CommitSHA === null) {
var ChangedFiles: string[] = []
if (CommitSHA.length === 0) {
continue
}

if (CommitSHA.length === 1) {
// eslint-disable-next-line no-await-in-loop
ChangedFiles = await GetChangedFilesFromACommit(ProgramOptions, CommitSHA.sha, Branch, Branches[1]).then(ChangedFiles => ChangedFiles)
} else {
// eslint-disable-next-line no-await-in-loop
const ChangedFiles = await GetChangedFilesFromSHAToHead(ProgramOptions, CommitSHA, Branch, Branches[1]).then(ChangedFiles => ChangedFiles)
ChangedFiles = await GetChangedFilesFromSHAToHead(ProgramOptions, CommitSHA.sha, Branch, Branches[1]).then(ChangedFiles => ChangedFiles)
}

PurgeRequest.AddURLs(ChangedFiles, Branch)
}

Expand Down
45 changes: 36 additions & 9 deletions sources/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function CreateGitInstance(BasePath: string): Git.SimpleGit {
* @param {Types.ProgramOptionsType} ProgramOptions The program options.
* @param {number} LatestWorkflowRunTime The latest workflow time in milliseconds.
* @param {string} Branch The branch or tag name.
* @returns {Promise<string>} SHA of the latest commit.
* @returns {Promise<Types.CommitSHA>} SHA of the latest commit.
*/
export async function GetCommitSHAFromLatestWorkflowTime(ProgramOptions: Types.ProgramOptionsType, LatestWorkflowRunTime: number, Branch: string): Promise<string> {
export async function GetCommitSHAFromLatestWorkflowTime(ProgramOptions: Types.ProgramOptionsType, LatestWorkflowRunTime: number, Branch: string): Promise<Types.CommitSHA> {
var MatchedCommitTimeAddress = 0
if (ProgramOptions.shouldUseApi) {
const GitHubInstance = CreateGitHubInstance(ProgramOptions)
Expand All @@ -41,15 +41,15 @@ export async function GetCommitSHAFromLatestWorkflowTime(ProgramOptions: Types.P

// If any commit is pushed after the latest workflow time, skip the branch.
if (MatchedCommitTimeAddress === 0) {
return null
return {sha: '', length: 0}
}

// If the wokflow had not executed before, return SHA of the oldest commit.
if (LatestWorkflowRunTime === 0) {
return GitHubListCommits[GitHubListCommits.length - 1].sha
return {sha: GitHubListCommits[GitHubListCommits.length - 1].sha, length: GitHubListCommits.length}
}

return GitHubListCommits[MatchedCommitTimeAddress].sha
return {sha: GitHubListCommits[MatchedCommitTimeAddress].sha, length: GitHubListCommits.length}
}

if (!ProgramOptions.shouldUseApi) {
Expand All @@ -65,15 +65,15 @@ export async function GetCommitSHAFromLatestWorkflowTime(ProgramOptions: Types.P

// If any commit is pushed after the latest workflow time, skip the branch.
if (MatchedCommitTimeAddress === 0) {
return null
return {sha: '', length: 0}
}

// If the wokflow had not ex>ecuted before, return SHA of the oldest commit.
// If the wokflow had not executed before, return SHA of the oldest commit.
if (LatestWorkflowRunTime === 0) {
return GitLog[GitLog.length - 1].hash
return {sha: GitLog[GitLog.length - 1].hash, length: GitLog.length}
}

return GitLog[MatchedCommitTimeAddress].hash
return {sha: GitLog[MatchedCommitTimeAddress].hash, length: GitLog.length}
}
}

Expand Down Expand Up @@ -103,3 +103,30 @@ export async function GetChangedFilesFromSHAToHead(ProgramOptions: Types.Program
return ChangedFiles[ChangedFiles.length - 1] === '' ? ChangedFiles.slice(0, ChangedFiles.length - 1) : ChangedFiles
}
}

/**
* @name GetChangedFilesFromACommit
* @description Get changed files from a commit.
* @param {Types.ProgramOptionsType} ProgramOptions The program options.
* @param {stirng} CommitSHA The commit SHA.
* @returns {Promise<string[]>} A list of changed files.
*/
export async function GetChangedFilesFromACommit(ProgramOptions: Types.ProgramOptionsType, CommitSHA: string, Branch: string, DefaultBranch: string): Promise<string[]> {
if (ProgramOptions.shouldUseApi) {
const GitHubInstance = CreateGitHubInstance(ProgramOptions)
const GitHubComparingRaw = await GitHubInstance.repos.getCommit({
owner: ProgramOptions.repo.split('/')[0],
repo: ProgramOptions.repo.split('/')[1],
ref: CommitSHA,
}).then(Response => Response.data)
return GitHubComparingRaw.files.map(File => `/gh/${File.filename}`)
}

if (!ProgramOptions.shouldUseApi) {
const GitInstance = CreateGitInstance(ProgramOptions.ciWorkspacePath)
const ChangedFiles = (await GitInstance.show(['--pretty=format:"%f"', '--name-only', CommitSHA])).split('\n')
ChangedFiles.shift() // Remove the commit message.
ChangedFiles.pop()
return ChangedFiles
}
}
5 changes: 5 additions & 0 deletions sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ export type RemainingFilenamesArrayType = {
Filename: string;
BranchOrTag: string;
}

export type CommitSHA = {
sha: string;
length: number;
}

0 comments on commit 084eeb8

Please sign in to comment.