Skip to content

Commit

Permalink
feat: add logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SayakMukhopadhyay committed Jun 5, 2024
1 parent 817f477 commit 2f748a1
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 177 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
]
},
"dependencies": {
"@actions/core": "^1.10.1"
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand Down
31 changes: 31 additions & 0 deletions src/github-services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { debug } from '@actions/core'
import { getOctokit } from '@actions/github'

export interface IFile {
filename: string
}

export class GitHubService {
private readonly octokit

constructor(gitHubToken: string) {
this.octokit = getOctokit(gitHubToken)
}

async getChangedFiles(repositoryOwner: string, repositoryName: string, commitSHA: string): Promise<IFile[]> {
const responseBody = await this.octokit.request('GET /repos/{owner}/{repo}/commits/{ref}', {
owner: repositoryOwner,
repo: repositoryName,
ref: commitSHA
})

const files =
responseBody.data.files?.map(file => {
return { filename: file.filename }
}) ?? []

debug(`Commit ${commitSHA} includes following files: ${JSON.stringify(files)}`)

return files
}
}
29 changes: 16 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import * as core from '@actions/core'
import { wait } from './wait'
import { debug, getInput, setFailed, setOutput } from '@actions/core'
import { GitHubService, IFile } from './github-services'
import { context } from '@actions/github'

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
const pattern = getInput('pattern', { required: true })

Check failure on line 11 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'pattern' is assigned a value but never used
const githubToken = getInput('token', { required: true })

// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
core.debug(`Waiting ${ms} milliseconds ...`)
debug('Inputs received')

// Log the current timestamp, wait, then log the new timestamp
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
const gitHubService = new GitHubService(githubToken)
const commitSHA = context.sha

// Set outputs for other workflow steps to use
core.setOutput('time', new Date().toTimeString())
const files: IFile[] = await gitHubService.getChangedFiles(context.repo.owner, context.repo.repo, commitSHA)

Check failure on line 19 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'files' is assigned a value but never used

setOutput('time', new Date().toTimeString())
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
if (error instanceof Error) {
setFailed(error.message)
} else {
setFailed('Unknown error occurred')
}
}
}
14 changes: 14 additions & 0 deletions src/pattern-matcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { debug } from '@actions/core'
import { IFile } from './github-services'

export function checkChangedFilesAgainstPattern(files: IFile[], pattern: string): void {
if (!files || files?.length === 0) {
debug(`This commit doesn't contain any files`)
return
}
const regExp = new RegExp(pattern)
if (files.some(file => regExp.test(file.filename))) {
throw new Error(`There is at least one file matching the pattern ${pattern}`)
}
debug(`There isn't any file matching the pattern ${pattern}`)
}
14 changes: 0 additions & 14 deletions src/wait.ts

This file was deleted.

0 comments on commit 2f748a1

Please sign in to comment.