generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817f477
commit 2f748a1
Showing
7 changed files
with
246 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
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) | ||
|
||
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') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} |
This file was deleted.
Oops, something went wrong.