Commit c6a5bced authored by Grant's avatar Grant
Browse files

[ci] improve mr comment by including dynamic urls

parent 0442d530
Loading
Loading
Loading
Loading
Loading
+26 −11
Original line number Diff line number Diff line
@@ -32,20 +32,35 @@ comment:
  stage: .post
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
      changes:
        paths:
          - entries/**/*.json
  dependencies:
    - build
  needs:
    - build
  before_script:
    - apk add curl jq
    - apk add curl jq git
  script:
    # Here are the comments that will always be added
    # - DOCUMENTATION="Did you update the corresponding documentation?"
    # Here we parse them into URL
    # - DOCUMENTATION_PARSED=$(echo $DOCUMENTATION | sed 's/ /%20/g')
    - ATLAS2024_JSON_URL=$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/refs/merge-requests/$CI_MERGE_REQUEST_IID/head/raw/atlas2024.json?job=build
    - ATLAS2025_JSON_URL=$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/refs/merge-requests/$CI_MERGE_REQUEST_IID/head/raw/atlas2025.json?job=build
    - BODY="You can preview the data [here for 2024](https://2024.canvas-atlas.fediverse.events?atlas=$(jq -rn --arg x "$ATLAS2024_JSON_URL" '$x|@uri')) and [here for 2025](https://2025.canvas-atlas.fediverse.events?atlas=$(jq -rn --arg x "$ATLAS2025_JSON_URL" '$x|@uri'))."
    - echo $BODY
    # And finally we post them on your merge request!
    - 'curl --fail --request POST --header "PRIVATE-TOKEN: ${GITLAB_CI_COMMENT_API_KEY}" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" --data-urlencode "body=$BODY"'
    - |
      # 📝 Build MR Comment Body
      MR_BODY=$(
        echo "Preview links: *(⚠ Links below will load files from this MR, exercise caution ⚠)*"
        git diff --name-only origin/main HEAD -- entries/**/*.json | while read line; do
          YEAR=${line##entries/}
          YEAR=${YEAR%/*}
          ID=$(jq .id "$line")
          NAME=$(jq .name "$line")

          JOB_ARTIFACT="$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/refs/merge-requests/$CI_MERGE_REQUEST_IID/head/raw/atlas$YEAR.json?job=build"
          PREVIEW_URL="https://$YEAR.canvas-atlas.fediverse.events?atlas=$(jq -rn --arg x "$JOB_ARTIFACT" '$x|@uri')#$ID"

          echo "* [$YEAR - $NAME]($PREVIEW_URL)"
        done
      )
    - |
      # 📧 Post comment on MR
      curl --fail-with-body -X POST \
      --header "PRIVATE-TOKEN: ${GITLAB_CI_COMMENT_API_KEY}" \
      --data-urlencode "body=$MR_BODY" \
      "$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes"