Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Note: The `GH_TOKEN` environment variable is **required** for GitHub API request
| `files` | **YES** | Multi-line string of file paths to be committed, relative to the current workspace.|
| `workspace` | **NO** | Directory containing files to be committed. **DEFAULT:** GitHub workspace directory (root of the repository). |
| `commit-message` | **YES** | Commit message for the file changes. |
| `repository` | **NO** | Repository name including owner (e.g. owner/repo). **DEFAULT:** Workflow triggered repository |
| `branch-name` | **NO** | Branch to commit, it must already exist in the remote. **DEFAULT:** Workflow triggered branch |
| `branch-push-force` | **NO** | `--force` flag when running `git push <branch-name>`. |
| `tag` | **NO** | Push tag for the new/current commit. |
Expand Down
9 changes: 5 additions & 4 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

describe('Git CLI', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
})

describe('git checkout', () => {
Expand Down Expand Up @@ -89,11 +89,12 @@ describe('Git CLI', () => {
})

describe('git push', () => {
beforeEach(() => {
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
})

it('should push new branch', async () => {
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
const getInput = jest
.spyOn(core, 'getBooleanInput')
.mockReturnValue(false)

await pushCurrentBranch()
expect(execMock).toHaveBeenCalledWith(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/github/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('GitHub Client', () => {
let replacedEnv: jest.Replaced<typeof process.env> | undefined

beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
replacedEnv = jest.replaceProperty(
process,
'env',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/github/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {

describe('GitHub API', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
fetchMock.clearHistory()
fetchMock.removeRoutes()
})
Expand Down
8 changes: 4 additions & 4 deletions __tests__/github/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { getContext } from '../../src/github/repo'

describe('getContext', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('extract owner and repo', () => {
jest.restoreAllMocks()
jest
.spyOn(github.context, 'repo', 'get')
.mockReturnValue({ repo: 'my-repo', owner: 'my-user' })
})

it('extract owner and repo', () => {
jest.replaceProperty(github.context, 'ref', 'refs/heads/main')
const context = getContext()
expect(context).toHaveProperty('owner', 'my-user')
Expand Down
67 changes: 55 additions & 12 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import exp from 'constants'

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
jest.spyOn(core, 'debug').mockReturnValue()
jest.spyOn(core, 'info').mockReturnValue()
jest.spyOn(core, 'group').mockImplementation(async (name, fn) => {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('action', () => {

describe('input branch same as current branch', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'main'
return ''
})
Expand All @@ -140,7 +140,9 @@ describe('action', () => {
await main.run()

expect(switchBranchMock).not.toHaveBeenCalled()
expect(setFailedMock).not.toHaveBeenCalled()
expect(setFailedMock).toHaveBeenCalledWith(
'Neither files nor tag input has been configured'
)
})

it('does not push branch', async () => {
Expand All @@ -156,13 +158,15 @@ describe('action', () => {

expect(switchBranchMock).not.toHaveBeenCalled()
expect(pushBranchMock).not.toHaveBeenCalled()
expect(setFailedMock).not.toHaveBeenCalled()
expect(setFailedMock).toHaveBeenCalledWith(
'Neither files nor tag input has been configured'
)
})
})

describe('input branch not the same as current branch', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'another-branch'
return ''
})
Expand Down Expand Up @@ -195,7 +199,45 @@ describe('action', () => {

expect(switchBranchMock).toHaveBeenCalled()
expect(pushBranchMock).toHaveBeenCalled()
expect(setFailedMock).not.toHaveBeenCalled()
expect(setFailedMock).toHaveBeenCalledWith(
'Neither files nor tag input has been configured'
)
})
})

describe('input repository is given', () => {
describe('valid format', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'repository') return 'the-user/the-repo'
return ''
})
})

it('succeed', async () => {
const setFailedMock = jest.spyOn(core, 'setFailed').mockReturnValue()
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(
'Neither files nor tag input has been configured'
)
})
})

describe('invalid format', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'repository') return 'the-user-the-repo'
return ''
})
})

it('fails', async () => {
const setFailedMock = jest.spyOn(core, 'setFailed').mockReturnValue()
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(
'Input <repository> "the-user-the-repo" is invalid'
)
})
})
})

Expand All @@ -210,10 +252,11 @@ describe('action', () => {

describe('exists in remote', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'existing-branch'
return ''
})
jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
})

it('succeed', async () => {
Expand Down Expand Up @@ -246,7 +289,7 @@ describe('action', () => {

describe('does not exist in remote', () => {
beforeEach(() => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'new-branch'
return ''
})
Expand Down Expand Up @@ -355,7 +398,7 @@ describe('action', () => {
})

it('commit files and output commit sha', async () => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'custom-branch'
return ''
})
Expand Down Expand Up @@ -404,7 +447,7 @@ describe('action', () => {
})

it('push tag only', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'tag-branch'
if (name == 'tag') return 'fake-tag'
return ''
Expand Down Expand Up @@ -450,7 +493,7 @@ describe('action', () => {
})

it('commit file and push tag', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'file-tag-branch'
if (name == 'tag') return 'fake-file-tag'
return ''
Expand Down Expand Up @@ -507,7 +550,7 @@ describe('action', () => {
})

it('commit file fails woukd skip push tag', async () => {
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
if (name == 'branch-name') return 'file-fail-tag-branch'
if (name == 'tag') return 'unreachable-tag'
return ''
Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/cwd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getCwd, getWorkspace } from '../../src/utils/cwd'

describe('Current Working Directory', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
jest.spyOn(core, 'debug').mockReturnValue()
})

Expand All @@ -26,7 +26,7 @@ describe('Current Working Directory', () => {

describe('Current Workspace', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
jest.spyOn(core, 'debug').mockReturnValue()
})

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ inputs:
description: |
Commit message for the file changes.
required: false
repository:
description: |
Repository name including owner (e.g. owner/repo). Default: Workflow triggered repository.
required: false
default: ''
branch-name:
description: |
Branch to commit to. Default: Workflow triggered branch.
Expand Down
19 changes: 16 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29910,13 +29910,19 @@ function getBlob(filePath) {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.BranchCommitNotFound = exports.BranchNotFound = exports.InputBranchNotFound = exports.NoFileChanges = void 0;
exports.BranchCommitNotFound = exports.BranchNotFound = exports.InputBranchNotFound = exports.InputRepositoryInvalid = exports.NoFileChanges = void 0;
class NoFileChanges extends Error {
constructor() {
super('No files changes');
}
}
exports.NoFileChanges = NoFileChanges;
class InputRepositoryInvalid extends Error {
constructor(repository) {
super(`Input <repository> "${repository}" is invalid`);
}
}
exports.InputRepositoryInvalid = InputRepositoryInvalid;
class InputBranchNotFound extends Error {
constructor(branchName) {
super(`Input <branch-name> "${branchName}" not found`);
Expand Down Expand Up @@ -30388,15 +30394,22 @@ function run() {
var _a, _b, _c, _d, _e, _f;
try {
const { owner, repo, branch } = (0, repo_1.getContext)();
const inputRepository = (0, input_1.getInput)('repository');
const inputBranch = (0, input_1.getInput)('branch-name');
if (inputBranch && inputBranch !== branch) {
yield (0, git_1.switchBranch)(inputBranch);
yield (0, git_1.pushCurrentBranch)();
}
const repositoryParts = inputRepository ? inputRepository.split('/') : [];
if (repositoryParts.length && repositoryParts.length != 2) {
throw new errors_1.InputRepositoryInvalid(inputRepository);
}
const currentOwner = repositoryParts.length ? repositoryParts[0] : owner;
const currentRepository = repositoryParts.length ? repositoryParts[1] : repo;
const currentBranch = inputBranch ? inputBranch : branch;
const repository = yield core.group(`fetching repository info for owner: ${owner}, repo: ${repo}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () {
const repository = yield core.group(`fetching repository info for owner: ${currentOwner}, repo: ${currentRepository}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () {
const startTime = Date.now();
const repositoryData = yield (0, graphql_1.getRepository)(owner, repo, currentBranch);
const repositoryData = yield (0, graphql_1.getRepository)(currentOwner, currentRepository, currentBranch);
const endTime = Date.now();
core.debug(`time taken: ${(endTime - startTime).toString()} ms`);
return repositoryData;
Expand Down
6 changes: 6 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export class NoFileChanges extends Error {
}
}

export class InputRepositoryInvalid extends Error {
constructor(repository: string) {
super(`Input <repository> "${repository}" is invalid`)
}
}

export class InputBranchNotFound extends Error {
constructor(branchName: string) {
super(`Input <branch-name> "${branchName}" not found`)
Expand Down
18 changes: 16 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,37 @@ import {
NoFileChanges,
BranchNotFound,
BranchCommitNotFound,
InputRepositoryInvalid,
InputBranchNotFound,
} from './errors'

export async function run(): Promise<void> {
try {
const { owner, repo, branch } = getContext()
const inputRepository = getInput('repository')
const inputBranch = getInput('branch-name')
if (inputBranch && inputBranch !== branch) {
await switchBranch(inputBranch)
await pushCurrentBranch()
}

const repositoryParts = inputRepository ? inputRepository.split('/') : []
if (repositoryParts.length && repositoryParts.length != 2) {
throw new InputRepositoryInvalid(inputRepository)
}

const currentOwner = repositoryParts.length ? repositoryParts[0] : owner
const currentRepository = repositoryParts.length ? repositoryParts[1] : repo
const currentBranch = inputBranch ? inputBranch : branch
const repository = await core.group(
`fetching repository info for owner: ${owner}, repo: ${repo}, branch: ${currentBranch}`,
`fetching repository info for owner: ${currentOwner}, repo: ${currentRepository}, branch: ${currentBranch}`,
async () => {
const startTime = Date.now()
const repositoryData = await getRepository(owner, repo, currentBranch)
const repositoryData = await getRepository(
currentOwner,
currentRepository,
currentBranch
)
const endTime = Date.now()
core.debug(`time taken: ${(endTime - startTime).toString()} ms`)
return repositoryData
Expand Down
Loading