Skip to content

Commit a82357e

Browse files
authored
Merge pull request #129 from ryancyq/feat/custom-repository
feat: custom repository
2 parents 694d948 + 3fc7938 commit a82357e

11 files changed

Lines changed: 112 additions & 29 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Note: The `GH_TOKEN` environment variable is **required** for GitHub API request
6363
| `files` | **YES** | Multi-line string of file paths to be committed, relative to the current workspace.|
6464
| `workspace` | **NO** | Directory containing files to be committed. **DEFAULT:** GitHub workspace directory (root of the repository). |
6565
| `commit-message` | **YES** | Commit message for the file changes. |
66+
| `repository` | **NO** | Repository name including owner (e.g. owner/repo). **DEFAULT:** Workflow triggered repository |
6667
| `branch-name` | **NO** | Branch to commit, it must already exist in the remote. **DEFAULT:** Workflow triggered branch |
6768
| `branch-push-force` | **NO** | `--force` flag when running `git push <branch-name>`. |
6869
| `tag` | **NO** | Push tag for the new/current commit. |

__tests__/git.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
describe('Git CLI', () => {
1313
beforeEach(() => {
14-
jest.clearAllMocks()
14+
jest.restoreAllMocks()
1515
})
1616

1717
describe('git checkout', () => {
@@ -89,11 +89,12 @@ describe('Git CLI', () => {
8989
})
9090

9191
describe('git push', () => {
92+
beforeEach(() => {
93+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(false)
94+
})
95+
9296
it('should push new branch', async () => {
9397
const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0)
94-
const getInput = jest
95-
.spyOn(core, 'getBooleanInput')
96-
.mockReturnValue(false)
9798

9899
await pushCurrentBranch()
99100
expect(execMock).toHaveBeenCalledWith(

__tests__/github/client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('GitHub Client', () => {
1212
let replacedEnv: jest.Replaced<typeof process.env> | undefined
1313

1414
beforeEach(() => {
15-
jest.clearAllMocks()
15+
jest.restoreAllMocks()
1616
replacedEnv = jest.replaceProperty(
1717
process,
1818
'env',

__tests__/github/graphql.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222

2323
describe('GitHub API', () => {
2424
beforeEach(() => {
25-
jest.clearAllMocks()
25+
jest.restoreAllMocks()
2626
fetchMock.clearHistory()
2727
fetchMock.removeRoutes()
2828
})

__tests__/github/repo.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { getContext } from '../../src/github/repo'
55

66
describe('getContext', () => {
77
beforeEach(() => {
8-
jest.clearAllMocks()
9-
})
10-
11-
it('extract owner and repo', () => {
8+
jest.restoreAllMocks()
129
jest
1310
.spyOn(github.context, 'repo', 'get')
1411
.mockReturnValue({ repo: 'my-repo', owner: 'my-user' })
12+
})
13+
14+
it('extract owner and repo', () => {
1515
jest.replaceProperty(github.context, 'ref', 'refs/heads/main')
1616
const context = getContext()
1717
expect(context).toHaveProperty('owner', 'my-user')

__tests__/main.test.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import exp from 'constants'
1414

1515
describe('action', () => {
1616
beforeEach(() => {
17-
jest.clearAllMocks()
17+
jest.restoreAllMocks()
1818
jest.spyOn(core, 'debug').mockReturnValue()
1919
jest.spyOn(core, 'info').mockReturnValue()
2020
jest.spyOn(core, 'group').mockImplementation(async (name, fn) => {
@@ -125,7 +125,7 @@ describe('action', () => {
125125

126126
describe('input branch same as current branch', () => {
127127
beforeEach(() => {
128-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
128+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
129129
if (name == 'branch-name') return 'main'
130130
return ''
131131
})
@@ -140,7 +140,9 @@ describe('action', () => {
140140
await main.run()
141141

142142
expect(switchBranchMock).not.toHaveBeenCalled()
143-
expect(setFailedMock).not.toHaveBeenCalled()
143+
expect(setFailedMock).toHaveBeenCalledWith(
144+
'Neither files nor tag input has been configured'
145+
)
144146
})
145147

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

157159
expect(switchBranchMock).not.toHaveBeenCalled()
158160
expect(pushBranchMock).not.toHaveBeenCalled()
159-
expect(setFailedMock).not.toHaveBeenCalled()
161+
expect(setFailedMock).toHaveBeenCalledWith(
162+
'Neither files nor tag input has been configured'
163+
)
160164
})
161165
})
162166

163167
describe('input branch not the same as current branch', () => {
164168
beforeEach(() => {
165-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
169+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
166170
if (name == 'branch-name') return 'another-branch'
167171
return ''
168172
})
@@ -195,7 +199,45 @@ describe('action', () => {
195199

196200
expect(switchBranchMock).toHaveBeenCalled()
197201
expect(pushBranchMock).toHaveBeenCalled()
198-
expect(setFailedMock).not.toHaveBeenCalled()
202+
expect(setFailedMock).toHaveBeenCalledWith(
203+
'Neither files nor tag input has been configured'
204+
)
205+
})
206+
})
207+
208+
describe('input repository is given', () => {
209+
describe('valid format', () => {
210+
beforeEach(() => {
211+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
212+
if (name == 'repository') return 'the-user/the-repo'
213+
return ''
214+
})
215+
})
216+
217+
it('succeed', async () => {
218+
const setFailedMock = jest.spyOn(core, 'setFailed').mockReturnValue()
219+
await main.run()
220+
expect(setFailedMock).toHaveBeenCalledWith(
221+
'Neither files nor tag input has been configured'
222+
)
223+
})
224+
})
225+
226+
describe('invalid format', () => {
227+
beforeEach(() => {
228+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
229+
if (name == 'repository') return 'the-user-the-repo'
230+
return ''
231+
})
232+
})
233+
234+
it('fails', async () => {
235+
const setFailedMock = jest.spyOn(core, 'setFailed').mockReturnValue()
236+
await main.run()
237+
expect(setFailedMock).toHaveBeenCalledWith(
238+
'Input <repository> "the-user-the-repo" is invalid'
239+
)
240+
})
199241
})
200242
})
201243

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

211253
describe('exists in remote', () => {
212254
beforeEach(() => {
213-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
255+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
214256
if (name == 'branch-name') return 'existing-branch'
215257
return ''
216258
})
259+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
217260
})
218261

219262
it('succeed', async () => {
@@ -246,7 +289,7 @@ describe('action', () => {
246289

247290
describe('does not exist in remote', () => {
248291
beforeEach(() => {
249-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
292+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
250293
if (name == 'branch-name') return 'new-branch'
251294
return ''
252295
})
@@ -355,7 +398,7 @@ describe('action', () => {
355398
})
356399

357400
it('commit files and output commit sha', async () => {
358-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
401+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
359402
if (name == 'branch-name') return 'custom-branch'
360403
return ''
361404
})
@@ -404,7 +447,7 @@ describe('action', () => {
404447
})
405448

406449
it('push tag only', async () => {
407-
jest.spyOn(core, 'getInput').mockImplementation((name, option) => {
450+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
408451
if (name == 'branch-name') return 'tag-branch'
409452
if (name == 'tag') return 'fake-tag'
410453
return ''
@@ -450,7 +493,7 @@ describe('action', () => {
450493
})
451494

452495
it('commit file and push tag', async () => {
453-
jest.spyOn(core, 'getInput').mockImplementation((name, option) => {
496+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
454497
if (name == 'branch-name') return 'file-tag-branch'
455498
if (name == 'tag') return 'fake-file-tag'
456499
return ''
@@ -507,7 +550,7 @@ describe('action', () => {
507550
})
508551

509552
it('commit file fails woukd skip push tag', async () => {
510-
jest.spyOn(core, 'getInput').mockImplementationOnce((name, option) => {
553+
jest.spyOn(core, 'getInput').mockImplementation((name, _option) => {
511554
if (name == 'branch-name') return 'file-fail-tag-branch'
512555
if (name == 'tag') return 'unreachable-tag'
513556
return ''

__tests__/utils/cwd.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getCwd, getWorkspace } from '../../src/utils/cwd'
1111

1212
describe('Current Working Directory', () => {
1313
beforeEach(() => {
14-
jest.clearAllMocks()
14+
jest.restoreAllMocks()
1515
jest.spyOn(core, 'debug').mockReturnValue()
1616
})
1717

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

2727
describe('Current Workspace', () => {
2828
beforeEach(() => {
29-
jest.clearAllMocks()
29+
jest.restoreAllMocks()
3030
jest.spyOn(core, 'debug').mockReturnValue()
3131
})
3232

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ inputs:
1919
description: |
2020
Commit message for the file changes.
2121
required: false
22+
repository:
23+
description: |
24+
Repository name including owner (e.g. owner/repo). Default: Workflow triggered repository.
25+
required: false
26+
default: ''
2227
branch-name:
2328
description: |
2429
Branch to commit to. Default: Workflow triggered branch.

dist/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29910,13 +29910,19 @@ function getBlob(filePath) {
2991029910

2991129911

2991229912
Object.defineProperty(exports, "__esModule", ({ value: true }));
29913-
exports.BranchCommitNotFound = exports.BranchNotFound = exports.InputBranchNotFound = exports.NoFileChanges = void 0;
29913+
exports.BranchCommitNotFound = exports.BranchNotFound = exports.InputBranchNotFound = exports.InputRepositoryInvalid = exports.NoFileChanges = void 0;
2991429914
class NoFileChanges extends Error {
2991529915
constructor() {
2991629916
super('No files changes');
2991729917
}
2991829918
}
2991929919
exports.NoFileChanges = NoFileChanges;
29920+
class InputRepositoryInvalid extends Error {
29921+
constructor(repository) {
29922+
super(`Input <repository> "${repository}" is invalid`);
29923+
}
29924+
}
29925+
exports.InputRepositoryInvalid = InputRepositoryInvalid;
2992029926
class InputBranchNotFound extends Error {
2992129927
constructor(branchName) {
2992229928
super(`Input <branch-name> "${branchName}" not found`);
@@ -30388,15 +30394,22 @@ function run() {
3038830394
var _a, _b, _c, _d, _e, _f;
3038930395
try {
3039030396
const { owner, repo, branch } = (0, repo_1.getContext)();
30397+
const inputRepository = (0, input_1.getInput)('repository');
3039130398
const inputBranch = (0, input_1.getInput)('branch-name');
3039230399
if (inputBranch && inputBranch !== branch) {
3039330400
yield (0, git_1.switchBranch)(inputBranch);
3039430401
yield (0, git_1.pushCurrentBranch)();
3039530402
}
30403+
const repositoryParts = inputRepository ? inputRepository.split('/') : [];
30404+
if (repositoryParts.length && repositoryParts.length != 2) {
30405+
throw new errors_1.InputRepositoryInvalid(inputRepository);
30406+
}
30407+
const currentOwner = repositoryParts.length ? repositoryParts[0] : owner;
30408+
const currentRepository = repositoryParts.length ? repositoryParts[1] : repo;
3039630409
const currentBranch = inputBranch ? inputBranch : branch;
30397-
const repository = yield core.group(`fetching repository info for owner: ${owner}, repo: ${repo}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () {
30410+
const repository = yield core.group(`fetching repository info for owner: ${currentOwner}, repo: ${currentRepository}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () {
3039830411
const startTime = Date.now();
30399-
const repositoryData = yield (0, graphql_1.getRepository)(owner, repo, currentBranch);
30412+
const repositoryData = yield (0, graphql_1.getRepository)(currentOwner, currentRepository, currentBranch);
3040030413
const endTime = Date.now();
3040130414
core.debug(`time taken: ${(endTime - startTime).toString()} ms`);
3040230415
return repositoryData;

src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ export class NoFileChanges extends Error {
44
}
55
}
66

7+
export class InputRepositoryInvalid extends Error {
8+
constructor(repository: string) {
9+
super(`Input <repository> "${repository}" is invalid`)
10+
}
11+
}
12+
713
export class InputBranchNotFound extends Error {
814
constructor(branchName: string) {
915
super(`Input <branch-name> "${branchName}" not found`)

0 commit comments

Comments
 (0)