Skip to content

Commit 677ac39

Browse files
authored
fix: do not add comments when PR from fork (#100)
* build: fixed errors when PR from fork and updated dependencies * build: just testing the comment from a fork * fix: just adding some comment to test adding comment from a fork * fix: log info in PR to run some tests * fix: more tests * fix: and more tests * fix: test fork and add comments * fix: removed comments and added tests
1 parent 6f14ab1 commit 677ac39

File tree

9 files changed

+1509
-1993
lines changed

9 files changed

+1509
-1993
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
uses: ./
2323
with:
2424
addHoldComment: "true"
25-
status: "${{ job.status }}"
25+
status: "${{ job.status }}"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ semantic-release-dry-run:
4141
install-npm-check-updates:
4242
npm install npm-check-updates
4343

44-
.PHONY: update-denendencies
45-
update-denendencies: install-npm-check-updates
44+
.PHONY: update-dependencies
45+
update-dependencies: install-npm-check-updates
4646
ncu -u
4747
npm install

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ GitHub does not update the status of a commit when running workflow and therefor
4848
* optional
4949
* default: "true"
5050

51-
* addHoldComment: If true the action will add a comment to the pull request. This is useful if you use prow and you get PRs from forks, you can use `/hold` and `/hold cancel` instead of the status check since the token won't have permissions to do that.
51+
* addHoldComment: If true the action will add a comment to the pull request. This is useful if you use prow, since prow won't detect the github actions, so you can use `/hold` and `/hold cancel` to avoid merging the PR before you want. __Important: this will be disabled for forks if `ignoreForks` is set to true, this is because the default github token won't have permissions to add comments if your PR comes from a fork__
5252

5353
* optional
5454
* default: "false"
@@ -82,7 +82,7 @@ jobs:
8282
runs-on: ubuntu-latest
8383
steps:
8484
- uses: actions/checkout@v2
85-
- uses: ouzi-dev/commit-status-updater@v1.0.0
85+
- uses: ouzi-dev/commit-status-updater@v1.0.4
8686
```
8787

8888
### Action sets commit to error status without comment
@@ -97,7 +97,7 @@ jobs:
9797
runs-on: ubuntu-latest
9898
steps:
9999
- uses: actions/checkout@v2
100-
- uses: ouzi-dev/commit-status-updater@v1.0.0
100+
- uses: ouzi-dev/commit-status-updater@v1.0.4
101101
with:
102102
status: "error"
103103
```
@@ -114,11 +114,11 @@ jobs:
114114
runs-on: ubuntu-latest
115115
steps:
116116
- uses: actions/checkout@v2
117-
- uses: ouzi-dev/commit-status-updater@v1.0.0
117+
- uses: ouzi-dev/commit-status-updater@v1.0.4
118118
with:
119119
addHoldComment: "true"
120120
- if: always()
121-
uses: ouzi-dev/commit-status-updater@v1.0.0
121+
uses: ouzi-dev/commit-status-updater@v1.0.4
122122
with:
123123
addHoldComment: "true"
124124
status: "${{ job.status }}"
@@ -136,7 +136,7 @@ jobs:
136136
runs-on: ubuntu-latest
137137
steps:
138138
- uses: actions/checkout@v2
139-
- uses: ouzi-dev/commit-status-updater@v1.0.0
139+
- uses: ouzi-dev/commit-status-updater@v1.0.4
140140
with:
141141
status: "pending"
142142
addHoldComment: "true"
@@ -157,7 +157,7 @@ jobs:
157157
runs-on: ubuntu-latest
158158
steps:
159159
- uses: actions/checkout@v2
160-
- uses: ouzi-dev/commit-status-updater@v1.0.0
160+
- uses: ouzi-dev/commit-status-updater@v1.0.4
161161
with:
162162
status: "error"
163163
url: http://myurl.io/
@@ -177,7 +177,7 @@ jobs:
177177
runs-on: ubuntu-latest
178178
steps:
179179
- uses: actions/checkout@v2
180-
- uses: ouzi-dev/commit-status-updater@v1.0.0
180+
- uses: ouzi-dev/commit-status-updater@v1.0.4
181181
with:
182182
token: "my_custom_token"
183183
ignoreForks: "false"

__test__/runner.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ describe('runner tests', () => {
8686
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
8787
})
8888

89+
it('run does not set status or comment when it is a fork and add comment enabled', async () => {
90+
const params = ({} as unknown) as IParams
91+
params.token = 'bleh'
92+
params.ignoreForks = true
93+
params.addHoldComment = true
94+
params.selectedComment = 'my comment'
95+
mockInputsHelper.getInputs.mockReturnValue(params)
96+
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
97+
mockIGithubHelper.isFork.mockReturnValue(true)
98+
99+
await runner.run()
100+
101+
expect(mockUtils.validateEventType).toHaveBeenCalled()
102+
expect(mockIGithubHelper.setStatus).toHaveBeenCalledTimes(0)
103+
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
104+
})
105+
89106
it('run sets status if ignore fork false', async () => {
90107
const params = ({} as unknown) as IParams
91108
params.token = 'bleh'

dist/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,9 +1445,11 @@ function run() {
14451445
}
14461446
else {
14471447
yield ghHelper.setStatus(params);
1448-
}
1449-
if (params.addHoldComment) {
1450-
yield ghHelper.addComment(params.selectedComment);
1448+
// for now only add comments if it's not a fork or we explicitly say don't ignore forks
1449+
// we should have a token with permissions in the fork for this
1450+
if (params.addHoldComment) {
1451+
yield ghHelper.addComment(params.selectedComment);
1452+
}
14511453
}
14521454
}
14531455
catch (error) {
@@ -3555,9 +3557,9 @@ module.exports = safer
35553557
/***/ }),
35563558

35573559
/***/ 256:
3558-
/***/ (function() {
3560+
/***/ (function(module) {
35593561

3560-
eval("require")("iconv");
3562+
module.exports = eval("require")("iconv");
35613563

35623564

35633565
/***/ }),
@@ -28427,10 +28429,13 @@ class GithubHelper {
2842728429
}
2842828430
addComment(comment) {
2842928431
return __awaiter(this, void 0, void 0, function* () {
28432+
// if we support forks, then we need to use the base, cause head will be the fork
28433+
const baseOwner = this.payload.pull_request.base.repo.owner.login;
28434+
const baseRepo = this.payload.pull_request.base.repo.name;
2843028435
try {
2843128436
yield this.octokit.issues.createComment({
28432-
owner: this.owner,
28433-
repo: this.repo,
28437+
owner: baseOwner,
28438+
repo: baseRepo,
2843428439
issue_number: this.issueNumber,
2843528440
body: comment
2843628441
});

0 commit comments

Comments
 (0)