Skip to content

Commit c43d39d

Browse files
Merge pull request #4646 from communitybridge/unicron-add-logs-search-command-line-utility
Update merge queue commit message parsing to get the correct PR number
2 parents a1355e4 + bee1196 commit c43d39d

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

cla-backend/cla/tests/unit/test_utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import cla
88
from cla import utils
99
from cla.models.dynamo_models import Project, Signature, User
10-
from cla.utils import (append_email_help_sign_off_content,
10+
from cla.utils import (append_email_help_sign_off_content, extract_pull_request_number,
1111
append_project_version_to_url, get_email_help_content,
1212
get_email_sign_off_content, get_full_sign_url)
1313

@@ -283,3 +283,32 @@ def test_append_project_version_to_url():
283283

284284
if __name__ == '__main__':
285285
unittest.main()
286+
287+
def test_extract_pull_request_number():
288+
tests = [
289+
["Merge pull request #232 from sun-test-org/thakurveerendras-patch-26#1\n\nUpdate README.md", 232],
290+
["Merge pull request #234 from sun-test-org/thakurveerendras-patch-26\n\nCreate mqfile2#file2", 234],
291+
["Merge pull request #235 from sun-test-org/branch#2341\n\nMQFileBranch#2342", 235],
292+
["Merge pull request #236 from sun-test-org/thakurveerendras-patch-27\n\nUpdate mqfile2#234", 236],
293+
["Merge pull request #237 from sun-test-org/thakurveerendras-patch-28#123\n\nCreate mqfile3#123", 237],
294+
["Merge pull request #235 from sun-test-org/branch#2341\n\nMQFileBranch#2342", 235],
295+
["Merge pull request #238 from sun-test-org/branch#23456\n\nPR#234567", 238],
296+
["Merge pull request #235 from sun-test-org/branch#2341\n\nMQFileBranch#2342", 235],
297+
["merge pull request #235 from sun-test-org/branch#2341\n\nMQFileBranch#2342", 235],
298+
["Hello world\nThis if for PR #123 fixing issue #112", 123],
299+
# ["Hello world\nThis if for Issue #112 - PR #123", 123],
300+
["[mdatagen] Add event type definition (#12822)\n\n#Description\n\nHello, ...", 12822],
301+
["[pt] Update localized content on content/pt/docs/languages/go/exporters.md (#6783)", 6783],
302+
["[chore]: remove testifylint-fix target (#12828)\n\n#### Description\n\ngolangci-lint is now able to apply suggested fixes from testifylint with\ngolangci-lint --fix .\nThis PR removes testifylint-fix target from Makefile.\n\nSigned-off-by: Matthieu MOREL <[email protected]>", 12828],
303+
["[chore] Prepare release 0.125.0 (#933)\n\n* Update version from 0.124.0 to 0.125.0\n\n* update versions in ebpf\n\n---------\n\nCo-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>\nCo-authored-by: Yang Song <[email protected]>", 933],
304+
["Add invoke_agent as a member of gen_ai.operation.name (#2160)", 2160],
305+
["Merge pull request #61 from open-telemetry/renovate/all-patch\n\nfix(deps): update all patch versions", 61],
306+
["Merge pull request #51 from open-telemetry/rollback-deps\n\nchore: roll back major dependency updates", 51],
307+
["fixes #6549 incorrect use of resource constructor (#6707)", 6707],
308+
["", None],
309+
["Add documentation example for xconfmap (#5675) (#12832)\n#### Description\n\nThis PR introduces a simple testable examples to the package\n[confmap](/confmap/xconfmap)", 12832]
310+
]
311+
312+
for i, (message, expected) in enumerate(tests, 1):
313+
result = extract_pull_request_number(message)
314+
assert result == expected

cla-backend/cla/utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,12 +1942,33 @@ def extract_pull_request_number(pull_request_message):
19421942
pull_request_number = None
19431943
try:
19441944
first_line = pull_request_message.splitlines()[0]
1945-
matches = re.findall(r"#(\d+)", first_line)
1945+
cla.log.debug(f"{fn} - checking line '{first_line}")
1946+
# Case 1: "Merge pull request #N"
1947+
matches = re.match(r'^Merge pull request #(\d+)', first_line)
1948+
if matches:
1949+
pull_request_number = int(matches.group(1))
1950+
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data: {pull_request_message} by matching 'Merge pull request #N...'")
1951+
return pull_request_number
1952+
# Case 2: PR number in last (#N) group on first line, like: "Some text (#whatever) (#N)"
1953+
matches = re.findall(r'\(#(\d+)\)', first_line)
1954+
if matches:
1955+
pull_request_number = int(matches[-1]) # last match
1956+
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data: {pull_request_message} by matching '...(#N)'")
1957+
return pull_request_number
1958+
# Case 3: PR number in last #N on first line, like: "Some text #N"
1959+
matches = re.findall(r"\s+#(\d+)", first_line)
19461960
if matches:
19471961
pull_request_number = int(matches[-1]) # last match
1962+
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data: {pull_request_message} by matching '... #N'")
1963+
return pull_request_number
1964+
# Case 4: PR number in first #N in the entire commit message
1965+
matches = re.findall(r"#(\d+)", pull_request_message)
1966+
if matches:
1967+
pull_request_number = int(matches[0]) # first match
1968+
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data: {pull_request_message} by matching first '#N'")
1969+
return pull_request_number
19481970
else:
19491971
cla.log.warning(f"{fn} - error - unable to extract pull request number from message: {pull_request_message}")
19501972
except Exception as e:
19511973
cla.log.warning(f"{fn} - error - unable to extract pull request number from message: {pull_request_message}, error: {e}")
1952-
cla.log.debug(f"{fn} - extracted PR number {pull_request_number} from merge_queue data")
19531974
return pull_request_number

utils/search_aws_logs.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_logs.sh 'cla-backend-dev-githubactivity' 'error'
3+
4+
if [ -z "$STAGE" ]
5+
then
6+
export STAGE=dev
7+
fi
8+
9+
if [ -z "${1}" ]
10+
then
11+
echo "$0: you must specify log group name, for example: 'cla-backend-dev-githubactivity', 'cla-backend-dev-apiv2', 'cla-backend-dev-api-v3-lambda', 'cla-backend-go-api-v4-lambda'"
12+
exit 1
13+
fi
14+
15+
log_group=$(echo "$1" | sed -E "s/\b(dev|prod)\b/${STAGE}/g")
16+
17+
if [ -z "${2}" ]
18+
then
19+
echo "$0: you must specify the search term, for example 'error'"
20+
exit 2
21+
fi
22+
23+
if [ -z "${DTFROM}" ]
24+
then
25+
export DTFROM="$(date -d '3 days ago' +%s)000"
26+
else
27+
export DTFROM="$(date -d "${DTFROM}" +%s)000"
28+
fi
29+
30+
if [ -z "${DTTO}" ]
31+
then
32+
export DTTO="$(date +%s)000"
33+
else
34+
export DTTO="$(date -d "${DTTO}" +%s)000"
35+
fi
36+
37+
if [ ! -z "${DEBUG}" ]
38+
then
39+
echo "aws --profile \"lfproduct-${STAGE}\" logs filter-log-events --log-group-name \"/aws/lambda/${log_group}\" --start-time \"${DTFROM}\" --end-time \"${DTTO}\" --filter-pattern \"${2}\""
40+
fi
41+
42+
aws --profile "lfproduct-${STAGE}" logs filter-log-events --log-group-name "/aws/lambda/${log_group}" --start-time "${DTFROM}" --end-time "${DTTO}" --filter-pattern "\"${2}\""

0 commit comments

Comments
 (0)