Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use mergify-cli for upload #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ jobs:
validate_orb:
timeout-minutes: 5
runs-on: ubuntu-22.04
outputs:
job_id: ${{ steps.get-job-id.outputs.job_id }}
steps:
- name: Checkout 🛎️
uses: actions/[email protected]

- name: Setup Python 🔧
uses: actions/[email protected]
with:
python-version: ">=3.12"

- name: Install CLI
run: pip install mergify-cli

- name: Start mockServer with expectation initializer
run: |
docker compose up -d
Expand All @@ -48,23 +58,38 @@ jobs:

- name: Test Orb upload script
id: gigid
run: |
export FAKE_VALID_TOKEN=fake-valid-token
bash src/scripts/upload.sh >> "$GITHUB_OUTPUT"
env:
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
TOKEN: FAKE_VALID_TOKEN
MERGIFY_API_URL: http://localhost:1080
MERGIFY_TOKEN: fake-valid-token
FILES: zfixtures/junit_example.xml
# force fake test environment
CIRCLECI: true
# GITHUB_ACTIONS: false
CIRCLE_JOB: test
CIRCLE_REPOSITORY_URL: ${{ github.event.repository.html_url }}
CIRCLE_SHA1: 948da8c01b17ac2164039f3150221d5cfcae7ecc
FILES: zfixtures/junit_example.xml
MERGIFY_API_SERVER: http://localhost:1080

- name: Check GIGID
env:
CI_ISSUE_GIGID: ${{ steps.gigid.outputs.CI_ISSUE_GIGID }}
run: |
set -e
test "$CI_ISSUE_GIGID" = "1234azertyuiop"
export GITHUB_ACTIONS=false
bash src/scripts/upload.sh

- name: Get job ID
id: get-job-id
uses: actions/github-script@v7
with:
script: |
const { data } = await github.rest.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId
});
// Looks for exact match
let job = data.jobs.find(
(j) => j.name === context.job
)
if (!job) {
core.error("current job not found");
}
core.setOutput("job_id", String(job.id));

- name: Mockserver logs
if: always()
Expand All @@ -77,3 +102,26 @@ jobs:
run: |
circleci orb pack src > orb.yml
circleci orb validate orb.yml

test-annotations:
needs: validate_orb
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Check GIGID
uses: actions/github-script@v7
with:
script: |
const annotations = await github.rest.checks.listAnnotations({
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: ${{ needs.validate_orb.outputs.job_id }},
});
for (const annotation of annotations.data) {
if (annotation.message.trim() === "CI_ISSUE_GIGID=1234azertyuiop") {
core.info(`Annotations found: ${annotation.message}`)
return
}
}
console.log(annotations)
throw new Error("Annotations not found")
35 changes: 22 additions & 13 deletions src/commands/upload.yml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
description: Upload the XML report
description: Upload the Junit XML report

parameters:
token:
type: env_var_name
description: CI Issues Application Key
default: MERGIFY_CI_ISSUES_TOKEN
repository_url:
description: |
URL of the repository (should provide pipeline.trigger_parameters.github_app.repo_url)
type: string
description: Mergify Application Key
default: MERGIFY_TOKEN
report_paths:
description: Paths of the XML files to upload
type: string
mergify_api_server:
mergify_api_url:
description: URL of the Mergify API
type: string
default: https://api.mergify.com/v1
default: https://api.mergify.com

steps:
- run:
name: Install Python
when: always
command: |
sudo apt-get update
sudo apt-get install -y python3

- run:
name: Install mergify-cli
when: always
command: pip install mergify-cli

- run:
name: Uploading the reports to Mergify CI Issues
when: always
environment:
TOKEN: <<parameters.token>>
REPO_URL: <<parameters.repository_url>>
MERGIFY_API_URL: <<parameters.mergify_api_url>>
MERGIFY_TOKEN: <<parameters.token>>
FILES: <<parameters.report_paths>>
MERGIFY_API_SERVER: <<parameters.mergify_api_server>>
when: always
command: <<include(scripts/upload.sh)>>
4 changes: 0 additions & 4 deletions src/examples/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ usage:
mkdir test_results
poetry run pytest -vv --junitxml=test_results/report.xml
- mergifyio/upload:
token: MERGIFY_CI_ISSUES_TOKEN
repository_url: <<pipeline.trigger_parameters.github_app.repo_url>>
report_paths: test_results/report.xml
- store_test_results:
path: test_results

workflows:
continuous_integration:
Expand Down
34 changes: 2 additions & 32 deletions src/scripts/upload.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
#!/bin/bash

set -x
set -e -x

if [[ $REPO_URL =~ ^https:\/\/github\.com\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+)$ ]]; then
REPO_FULL_NAME=${BASH_REMATCH[1]}/${BASH_REMATCH[2]}
else
echo "Invalid repository URL: $REPO_URL"
exit 1
fi

# Check if FILES and TOKEN are set and not empty
if [ -z "${FILES}" ]; then
echo "report_paths is not set or is empty"
exit 1
fi
if [ -z "${!TOKEN}" ]; then
echo "${!TOKEN} is not set or is empty"
exit 1
fi

# TODO: support multiple files
curl -X POST \
-H "Authorization: bearer ${!TOKEN}" \
-F name=${CIRCLE_JOB} \
-F provider=circleci \
-F head_sha=${CIRCLE_SHA1} \
-F files=@${FILES} \
-o result.json \
${MERGIFY_API_SERVER}/repos/${REPO_FULL_NAME}/ci_issues_upload \

GIGID=$(cat result.json | jq -r .gigid)
echo "::notice title=CI Issues report::CI_ISSUE_GIGID=$GIGID"

echo "CI_ISSUE_GIGID=$GIGID"
mergify ci junit-upload "${FILES}"
6 changes: 3 additions & 3 deletions zfixtures/expectationInitializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"httpRequest": {
"method": "POST",
"path": "/repos/Mergifyio/circleci-ci-issues/ci_issues_upload",
"path": "/v1/repos/Mergifyio/circleci-ci-issues/ci_issues_upload",
"headers": {
"Authorization": "bearer fake-valid-token"
"Authorization": "Bearer fake-valid-token"
},
"body": {
"type": "REGEX",
"regex": ".*Content-Disposition: form-data; name=\\\"name\\\"\\r\\n\\r\\ntest\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"provider\\\"\\r\\n\\r\\ncircleci\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"head_sha\\\"\\r\\n\\r\\n948da8c01b17ac2164039f3150221d5cfcae7ecc\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"files\\\"; filename=\\\"junit_example.xml\\\"\\r\\nContent-Type: application/xml\\r\\n\\r\\n.*"
"regex": ".*Content-Disposition: form-data; name=\\\"head_sha\\\"\\r\\n\\r\\n948da8c01b17ac2164039f3150221d5cfcae7ecc\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"name\\\"\\r\\n\\r\\ntest\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"provider\\\"\\r\\n\\r\\ncircleci\\r\\n.*\\r\\nContent-Disposition: form-data; name=\\\"files\\\"; filename=\\\"junit_example.xml\\\"\\r\\nContent-Type: application/xml\\r\\n\\r\\n.*"
}
},
"httpResponse": {
Expand Down
Loading