Skip to content

Commit 222db38

Browse files
authored
fix: multiline (#31)
1 parent 075cd36 commit 222db38

File tree

15 files changed

+77596
-31
lines changed

15 files changed

+77596
-31
lines changed

.github/workflows/comment-jira.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,57 @@ on:
66
- main
77
pull_request:
88
jobs:
9+
comment-jira-build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
13+
with:
14+
config: ${{ vars.PERMISSIONS_CONFIG }}
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version-file: comment-jira/package.json
20+
- name: install dependencies
21+
working-directory: ./comment-jira
22+
run: |
23+
npm ci
24+
- name: build
25+
working-directory: ./comment-jira
26+
run: |
27+
npm run build
28+
- name: Check for uncommitted files
29+
run: |
30+
export FILES=
31+
FILES=$(git ls-files -o -m --directory --exclude-standard --no-empty-directory)
32+
export LINES=
33+
LINES=$(echo "$FILES" | awk 'NF' | wc -l)
34+
if [ "$LINES" -ne 0 ]; then
35+
echo "Detected files that need to be committed:"
36+
echo "${FILES//^/ }"
37+
echo ""
38+
echo "Try running: make gen-docs"
39+
exit 1
40+
fi
41+
comment-jira-unit-test:
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
45+
with:
46+
config: ${{ vars.PERMISSIONS_CONFIG }}
47+
- name: Checkout repository
48+
uses: actions/checkout@v4
49+
- uses: actions/setup-node@v4
50+
with:
51+
node-version-file: comment-jira/package.json
52+
- name: install dependencies
53+
working-directory: ./comment-jira
54+
run: |
55+
npm ci
56+
- name: Run tests
57+
working-directory: ./comment-jira
58+
run: |
59+
npm run test
960
comment-jira-test:
1061
runs-on: ubuntu-latest
1162
steps:
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import * as core from '@actions/core';
2+
import request from 'request';
3+
import { main } from '../src/lib';
4+
5+
jest.mock('@actions/core');
6+
jest.mock('request');
7+
8+
describe('Comment Jira Issue Action', () => {
9+
beforeEach(() => {
10+
jest.clearAllMocks();
11+
});
12+
13+
it('should comment on a Jira issue successfully', async () => {
14+
const setFailedMock = jest.spyOn(core, 'setFailed');
15+
const getInputMock = jest.spyOn(core, 'getInput');
16+
17+
getInputMock.mockImplementation((name: string) => {
18+
switch (name) {
19+
case 'token':
20+
return 'fake-token';
21+
case 'issue-key':
22+
return 'TEST-123';
23+
case 'comment':
24+
return 'This is a test comment\nwith multiple lines\n';
25+
case 'api-base':
26+
return 'https://jira.example.com';
27+
default:
28+
return '';
29+
}
30+
});
31+
32+
const responseMock = {
33+
statusCode: 200,
34+
statusMessage: 'OK'
35+
};
36+
37+
const responseBody = { id: '10000' };
38+
39+
let requestBody: any = null;
40+
let requestUrl: string = '';
41+
let requestHeaders: any = null;
42+
43+
(request as unknown as jest.Mock).mockImplementation((options, callback) => {
44+
requestUrl = options.url;
45+
requestHeaders = options.headers;
46+
requestBody = options.body;
47+
callback(null, responseMock, responseBody);
48+
});
49+
50+
await main();
51+
52+
expect(requestUrl).toBe('https://jira.example.com/rest/api/2/issue/TEST-123/comment');
53+
expect(requestHeaders).toEqual({
54+
'Authorization': 'Bearer fake-token',
55+
'Content-Type': 'application/json'
56+
});
57+
expect(requestBody).toEqual({
58+
body: 'This is a test comment\r\nwith multiple lines\r\n'
59+
});
60+
expect(setFailedMock).not.toHaveBeenCalled();
61+
});
62+
63+
it('should fail if request returns an error', async () => {
64+
const setFailedMock = jest.spyOn(core, 'setFailed');
65+
const getInputMock = jest.spyOn(core, 'getInput');
66+
67+
getInputMock.mockImplementation((name: string) => {
68+
switch (name) {
69+
case 'token':
70+
return 'fake-token';
71+
case 'issue-key':
72+
return 'TEST-123';
73+
case 'comment':
74+
return 'This is a test comment\nwith multiple lines\n';
75+
case 'api-base':
76+
return 'https://jira.example.com';
77+
default:
78+
return '';
79+
}
80+
});
81+
82+
const err = new Error('Request failed');
83+
84+
(request as unknown as jest.Mock).mockImplementation((options, callback) => {
85+
callback(err, null, null);
86+
});
87+
88+
await main();
89+
90+
expect(setFailedMock).toHaveBeenCalledWith(err);
91+
});
92+
93+
it('should fail if response status code is >= 400', async () => {
94+
const setFailedMock = jest.spyOn(core, 'setFailed');
95+
const getInputMock = jest.spyOn(core, 'getInput');
96+
97+
getInputMock.mockImplementation((name: string) => {
98+
switch (name) {
99+
case 'token':
100+
return 'fake-token';
101+
case 'issue-key':
102+
return 'TEST-123';
103+
case 'comment':
104+
return 'This is a test comment\nwith multiple lines\n';
105+
case 'api-base':
106+
return 'https://jira.example.com';
107+
default:
108+
return '';
109+
}
110+
});
111+
112+
const responseMock = {
113+
statusCode: 400,
114+
statusMessage: 'Bad Request',
115+
};
116+
117+
const responseBody = {
118+
detail: "Bad Request"
119+
};
120+
121+
(request as unknown as jest.Mock).mockImplementation((options, callback) => {
122+
callback(null, responseMock, responseBody);
123+
});
124+
125+
await main();
126+
127+
const err = new Error("400 Bad Request\n{\"detail\":\"Bad Request\"}");
128+
129+
expect(setFailedMock).toHaveBeenCalledWith(err);
130+
});
131+
});

comment-jira/action.yaml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,5 @@ inputs:
1414
description: "Base URL for the Jira API"
1515
default: "https://jira.mongodb.org"
1616
runs:
17-
using: "composite"
18-
steps:
19-
- id: find
20-
shell: bash
21-
env:
22-
JIRA_API_BASE: ${{ inputs.api-base }}
23-
JIRA_API_TOKEN: ${{ inputs.token }}
24-
ISSUE_KEY: ${{ inputs.issue-key }}
25-
COMMENT: ${{ inputs.comment }}
26-
run: |
27-
request=$(jq -n --arg comment "$COMMENT" '{"body":$comment}')
28-
json_response=$(curl --fail --request POST \
29-
--url "${JIRA_API_BASE}/rest/api/2/issue/$ISSUE_KEY/comment" \
30-
--header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \
31-
--header 'Accept: application/json' \
32-
--header 'Content-Type: application/json' \
33-
--data "$request")
17+
using: "node20"
18+
main: "dist/index.js"

0 commit comments

Comments
 (0)