Cloud Functions की मदद से Firebase टेस्ट लैब को बढ़ाना


ज़रूरी मॉड्यूल इंपोर्ट करना

शुरू करने के लिए, Firebase Test Lab इवेंट मैनेज करने के लिए ज़रूरी मॉड्यूल इंपोर्ट करें:

Node.jsPython
 // The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onTestMatrixCompleted} = require("firebase-functions/v2/testLab");
const {logger} = require("firebase-functions");
 # The Cloud Functions for Firebase SDK to set up triggers and logging.
from firebase_functions import test_lab_fn, params

# The requests library to send web requests to Slack.
import requests

TestMatrix के पूरा होने पर कोई फ़ंक्शन ट्रिगर करना

Firebase Test Lab फ़ंक्शन को ट्रिगर करने के लिए, टेस्ट मैट्रिक के पूरा होने के इवेंट के लिए कोई हैंडलर तय करें. इस उदाहरण में, फ़ंक्शन टेस्ट पूरा होने पर ट्रिगर होता है. साथ ही, CloudEvent ऑब्जेक्ट से टेस्ट मैट्रिक्स का डेटा वापस लाता है और उससे जुड़े टेस्ट के नतीजे, Slack चैनल पर भेजता है:

Node.jsPython
exports.posttestresultstoslack = onTestMatrixCompleted(
    {secrets: ["SLACK_WEBHOOK_URL"]},
    async (event) => {
    // Obtain Test Matrix properties from the CloudEvent
      const {testMatrixId, state, outcomeSummary} = event.data;

      // Create the title of the message
      const title = `${getSlackmoji(state)} ${getSlackmoji(
          outcomeSummary,
      )} ${testMatrixId}`;

      // Create the details of the message
      const details = `Status: *${state}* ${getSlackmoji(
          state,
      )}\nOutcome: *${outcomeSummary}* ${getSlackmoji(outcomeSummary)}
    `;

      // Post the message to slack
      const slackResponse = await postToSlack(title, details);

      // Log the response
      logger.log(slackResponse);
    });
@test_lab_fn.on_test_matrix_completed(secrets=["SLACK_WEBHOOK_URL"])
def posttestresultstoslack(
        event: test_lab_fn.CloudEvent[test_lab_fn.TestMatrixCompletedData]) -> None:
    """Posts a test matrix result to Slack."""

    # Obtain Test Matrix properties from the CloudEvent
    test_matrix_id = event.data.test_matrix_id
    state = event.data.state
    outcome_summary = event.data.outcome_summary

    # Create the title of the message
    title = f"{slackmoji(state)} {slackmoji(outcome_summary)} {test_matrix_id}"

    # Create the details of the message
    details = (f"Status: *{state}* {slackmoji(state)}\n"
               f"Outcome: *{outcome_summary}* {slackmoji(outcome_summary)}")

    # Post the message to Slack
    response = post_to_slack(title, details)

    # Log the response
    print(response.status_code, response.text)

क्लाइंट की जानकारी ऐक्सेस करना

टेस्ट मैट्रिक को अलग-अलग सोर्स या वर्कफ़्लो से बनाया जा सकता है. इसलिए, अक्सर ऐसे फ़ंक्शन बनाने का सुझाव दिया जाता है जो टेस्ट के सोर्स या अन्य अहम संदर्भ के आधार पर अलग-अलग कार्रवाइयां करते हैं. इसकी मदद से, gcloud की मदद से कोई भी जानकारी पास की जा सकती है. इस जानकारी को बाद में फ़ंक्शन में ऐक्सेस किया जा सकता है. उदाहरण के लिए:

gcloud beta firebase test android run \
    --app=path/to/app.apk \
    --client-details testType=pr,link=<path/to/pull-request>

इसके बाद, अपने फ़ंक्शन में जानकारी ऐक्सेस करने के लिए:

Node.jsPython
const testType = event.data.clientInfo.details.testType;
const link = event.data.clientInfo.details.link;
test_type: str | None = event.data.client_info.details.get("testType")
link: str | None = event.data.client_info.details.get("link")