Skip to content

Commit 5dbfcdc

Browse files
jameslambgithub-actions[bot]StrikerRUS
authored
[ci] use GitHub built-in token, switch comment-triggered jobs to workflow dispatch (fixes #7012) (#7035)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Nikita Titov <[email protected]>
1 parent 2c6b74a commit 5dbfcdc

File tree

11 files changed

+146
-255
lines changed

11 files changed

+146
-255
lines changed

.ci/append-comment.sh

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22
#
33
# [description]
4-
# Update comment appending a given body to the specified original comment.
4+
# Post a comment to a pull request.
55
#
66
# [usage]
7-
# append-comment.sh <COMMENT_ID> <BODY>
7+
# append-comment.sh <PULL_REQUEST_ID> <BODY>
88
#
9-
# COMMENT_ID: ID of comment that should be modified.
9+
# PULL_REQUEST_ID: ID of PR to post the comment on.
1010
#
11-
# BODY: Text that will be appended to the original comment body.
11+
# BODY: Text of the comment to be posted.
1212

1313
set -e -E -u -o pipefail
1414

@@ -18,33 +18,27 @@ if [ -z "$GITHUB_ACTIONS" ]; then
1818
fi
1919

2020
if [ $# -ne 2 ]; then
21-
echo "Usage: $0 <COMMENT_ID> <BODY>"
21+
echo "Usage: $0 <PULL_REQUEST_ID> <BODY>"
2222
exit 1
2323
fi
2424

25-
comment_id=$1
25+
pr_id=$1
2626
body=$2
2727

28-
old_comment_body=$(
29-
curl -sL \
30-
-H "Accept: application/vnd.github.v3+json" \
31-
-H "Authorization: token $SECRETS_WORKFLOW" \
32-
"${GITHUB_API_URL}/repos/microsoft/LightGBM/issues/comments/$comment_id" | \
33-
jq '.body'
34-
)
3528
body=${body/failure/failure ❌}
3629
body=${body/error/failure ❌}
3730
body=${body/cancelled/failure ❌}
3831
body=${body/timed_out/failure ❌}
3932
body=${body/success/success ✔️}
4033
data=$(
4134
jq -n \
42-
--argjson body "${old_comment_body%?}\r\n\r\n$body\"" \
43-
'{"body":$body}'
35+
--argjson body "\"$body\"" \
36+
'{"body": $body}'
4437
)
4538
curl -sL \
46-
-X PATCH \
39+
--fail \
40+
-X POST \
4741
-H "Accept: application/vnd.github.v3+json" \
48-
-H "Authorization: token $SECRETS_WORKFLOW" \
42+
-H "Authorization: token ${GITHUB_TOKEN}" \
4943
-d "$data" \
50-
"${GITHUB_API_URL}/repos/microsoft/LightGBM/issues/comments/$comment_id"
44+
"${GITHUB_API_URL}/repos/microsoft/LightGBM/issues/${pr_id}/comments"

.ci/check-workflow-status.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# [description]
4+
#
5+
# Look for the last run of a given GitHub Actions workflow on a given branch.
6+
# If there's never been one (as might be the case with optional workflows like valgrind),
7+
# exit with 0.
8+
#
9+
# Otherwise, check the status of that latest run.
10+
# If it wasn't successful, exit with a non-0 exit code.
11+
#
12+
# [usage]
13+
#
14+
# check-workflow-status.sh <BRANCH> <WORKFLOW_FILE>
15+
#
16+
# BRANCH: name of a branch involved in a pull request.
17+
#
18+
# WORKFLOW_FILE: filename (e.g. 'r_valgrind.yml') defining the GitHub Actions workflow.
19+
#
20+
21+
set -e -u -o pipefail
22+
23+
BRANCH="${1}"
24+
WORKFLOW_FILE="${2}"
25+
26+
echo "Searching for latest run of '${WORKFLOW_FILE}' on branch '${BRANCH}'"
27+
28+
LATEST_RUN_ID=$(
29+
gh run list \
30+
--repo "microsoft/LightGBM" \
31+
--branch "${BRANCH}" \
32+
--workflow "${WORKFLOW_FILE}" \
33+
--json 'createdAt,databaseId' \
34+
--jq 'sort_by(.createdAt) | reverse | .[0] | .databaseId'
35+
)
36+
37+
if [[ "${LATEST_RUN_ID}" == "" ]]; then
38+
echo "No runs of '${WORKFLOW_FILE}' found on branch '${BRANCH}'"
39+
exit 0
40+
fi
41+
42+
echo "Checking status of workflow run '${LATEST_RUN_ID}'"
43+
gh run view \
44+
--repo "microsoft/LightGBM" \
45+
--exit-status \
46+
"${LATEST_RUN_ID}"

.ci/get-workflow-status.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

.ci/rerun-workflow.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pr_branch=$3
3131
runs=$(
3232
curl -sL \
3333
-H "Accept: application/vnd.github.v3+json" \
34-
-H "Authorization: token $SECRETS_WORKFLOW" \
34+
-H "Authorization: token ${GITHUB_TOKEN}" \
3535
"${GITHUB_API_URL}/repos/microsoft/LightGBM/actions/workflows/${workflow_id}/runs?event=pull_request&branch=${pr_branch}" | \
3636
jq '.workflow_runs'
3737
)
@@ -42,6 +42,6 @@ if [[ $(echo "${runs}" | jq 'length') -gt 0 ]]; then
4242
curl -sL \
4343
-X POST \
4444
-H "Accept: application/vnd.github.v3+json" \
45-
-H "Authorization: token $SECRETS_WORKFLOW" \
45+
-H "Authorization: token ${GITHUB_TOKEN}" \
4646
"${GITHUB_API_URL}/repos/microsoft/LightGBM/actions/runs/$(echo "${runs}" | jq '.[0].id')/rerun"
4747
fi

.ci/set-commit-status.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ data=$(
4646
)
4747

4848
curl -sL \
49+
--fail \
4950
-X POST \
5051
-H "Accept: application/vnd.github.v3+json" \
51-
-H "Authorization: token $SECRETS_WORKFLOW" \
52+
-H "Authorization: token ${GITHUB_TOKEN}" \
5253
-d "$data" \
5354
"${GITHUB_API_URL}/repos/microsoft/LightGBM/statuses/$sha"

.ci/trigger-dispatch-run.sh

Lines changed: 0 additions & 51 deletions
This file was deleted.

.github/workflows/optional_checks.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,23 @@ on:
77

88
jobs:
99
all-optional-checks-successful:
10-
timeout-minutes: 120
10+
timeout-minutes: 30
1111
runs-on: ubuntu-latest
12+
env:
13+
GITHUB_TOKEN: ${{ github.token }}
1214
steps:
1315
- name: Checkout repository
1416
uses: actions/checkout@v5
1517
with:
1618
fetch-depth: 5
1719
persist-credentials: false
1820
submodules: false
19-
- name: Check that all tests succeeded
21+
- name: Check valgrind workflow
2022
shell: bash
2123
run: |
22-
workflows=(
23-
"R valgrind tests;r-valgrind"
24-
)
25-
for i in "${workflows[@]}"; do
26-
workflow_name=${i%;*}
27-
comment="The last reported status from workflow \"$workflow_name\" is failure."
28-
comment="${comment} Commit fixes and rerun the workflow."
29-
trigger_phrase=${i#*;}
30-
python \
31-
"$GITHUB_WORKSPACE/.ci/get-workflow-status.py" \
32-
"$trigger_phrase" \
33-
|| { echo "${comment}"; exit 1; }
34-
done
24+
# ref: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts#github-context
25+
PR_BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
26+
echo "checking status for branch '${PR_BRANCH}'"
27+
./.ci/check-workflow-status.sh \
28+
"${PR_BRANCH}" \
29+
'r_valgrind.yml'

.github/workflows/r_configure.yml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
name: R generate configure
22

33
on:
4-
repository_dispatch:
5-
types: [gha_run_r_configure]
4+
workflow_dispatch:
5+
inputs:
6+
pr-branch:
7+
type: string
8+
description: |
9+
Branch in microsoft/LightGBM to update.
10+
11+
permissions:
12+
actions: none
13+
checks: none
14+
contents: write
15+
deployments: none
16+
discussions: none
17+
id-token: write
18+
issues: none
19+
packages: none
20+
pages: none
21+
pull-requests: read
22+
repository-projects: none
23+
security-events: none
24+
statuses: none
625

726
jobs:
827
r-configure:
@@ -24,18 +43,19 @@ jobs:
2443
uses: actions/checkout@v5
2544
with:
2645
fetch-depth: 5
27-
submodules: true
46+
submodules: false
2847
repository: microsoft/LightGBM
29-
ref: "refs/heads/${{ fromJSON(github.event.client_payload.pr_branch) }}"
30-
token: ${{ secrets.WORKFLOW }}
48+
ref: "refs/heads/${{ inputs.pr-branch }}"
49+
token: ${{ github.token }}
3150
persist-credentials: true
3251
- name: Update configure
3352
shell: bash
3453
run: ./R-package/recreate-configure.sh || exit 1
3554
- name: Push changes
3655
run: |
37-
git config --global user.name "GitHub Actions Bot"
38-
git config --global user.email "[email protected]"
56+
# source for this user and email: https://github.com/orgs/community/discussions/160496
57+
git config --global user.name "github-actions[bot]"
58+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
3959
git add "./R-package/configure"
4060
git commit --allow-empty -m "Auto-update configure"
4161
git push

0 commit comments

Comments
 (0)