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

Send job_run_uuid to status report telemetry #1685

Merged
merged 6 commits into from Jun 21, 2023

Conversation

angelapwen
Copy link
Contributor

@angelapwen angelapwen commented May 18, 2023

This change generates a job_run_uuid in the init Action, and then exports it as an environment variable to propagate it across steps. When any status report is created, the value is read from the environment variable and then sent to our internal telemetry API. This will allow us to join status reports from various steps to one another without using multiple fields.

The PR also tests the telemetry fields in statusReportBase are assigned appropriately and the proper type (eg. string for the job_run_uuid).

[GitHub internal only]: I validated against this branch, with a logging statement, on this job. Note that in a single attempt, the job_run_uuid logged is the same across steps, and in each attempt it is unique.

Merge / deployment checklist

  • Confirm this change is backwards compatible with existing workflows.
  • Confirm the readme has been updated if necessary.
  • Confirm the changelog has been updated if necessary.

@angelapwen angelapwen requested a review from a team as a code owner May 18, 2023 20:35
Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I was pleasantly surprised by how small a change this was. Minor suggestions only.

src/init-action.ts Show resolved Hide resolved
@angelapwen angelapwen closed this Jun 15, 2023
@angelapwen angelapwen deleted the job-run-uuid branch June 15, 2023 23:32
@angelapwen angelapwen restored the job-run-uuid branch June 15, 2023 23:33
@angelapwen angelapwen reopened this Jun 15, 2023
t.assert(statusReport.job_name === (process.env["GITHUB_JOB"] || ""));
t.assert(statusReport.analysis_key === "analysis-key");
t.assert(statusReport.commit_oid === process.env["GITHUB_SHA"]);
t.assert(statusReport.ref === process.env["GITHUB_REF"]);

Check warning

Code scanning / CodeQL

Some environment variables may not exist in default setup workflows Warning

The environment variable GITHUB_REF may not exist in default setup workflows. If all uses are safe, add it to the list of environment variables that are known to be safe in 'queries/default-setup-environment-variables.ql'. If this use is safe but others are not, dismiss this alert as a false positive.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed alert because we are only using the environment variable in a test.

@angelapwen
Copy link
Contributor Author

I realized we hadn't gotten back to this PR, and resolved a merge conflict with main. @adityasharad could you please re-review? Thank you!

Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes look fine, some questions around comments and tests.

src/actions-util.ts Outdated Show resolved Hide resolved
Comment on lines +274 to +278
process.env["GITHUB_REF"] = "refs/heads/main";
process.env["GITHUB_SHA"] = "a".repeat(40);
process.env["GITHUB_RUN_ID"] = "100";
process.env["GITHUB_RUN_ATTEMPT"] = "2";
process.env["GITHUB_REPOSITORY"] = "octocat/HelloWorld";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are supposed to be protected environment variables in the Actions runtime.

So two questions:

  • Do these assignments actually affect the environment variable?
  • If you change the assertions below to compare statusReport.ref etc with the constant values, not with process.env["GITHUB_REF"], do the tests still pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! It seems they actually do affect the environment variable. I changed the assertions to t.assert(statusReport.ref === "refs/heads/main"); and t.assert(statusReport.commit_oid === "a".repeat(40)); for the SHA, and the tests still passed.

If I don't populate these variables manually, I get

  Rejected promise returned by test. Reason:

  Error {
    message: 'GITHUB_SHA environment variable must be set',
  }

errors. I assume that these just aren't populated at all in the unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on this after our 1:1: these tests failed locally because I wasn't in the Actions runtime. When run in Actions, the protected Actions environment variables are able to be overridden for the purpose of the checks, but are not populated across steps and tests.

@henrymercer and/or @aeisenberg, does this match your understanding of how this unit test works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my understanding. I would go further and say that we should be running our unit tests starting from an empty environment — our tests shouldn't depend on the environment we run them in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, I'm happy enough with this test checking the status report is populated from process.env, and we can leave further test cleanup for a future PR:

  • starting from clean environment
  • passing locally when not running in an Actions environment

Comment on lines +274 to +278
process.env["GITHUB_REF"] = "refs/heads/main";
process.env["GITHUB_SHA"] = "a".repeat(40);
process.env["GITHUB_RUN_ID"] = "100";
process.env["GITHUB_RUN_ATTEMPT"] = "2";
process.env["GITHUB_REPOSITORY"] = "octocat/HelloWorld";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, I'm happy enough with this test checking the status report is populated from process.env, and we can leave further test cleanup for a future PR:

  • starting from clean environment
  • passing locally when not running in an Actions environment

@angelapwen angelapwen merged commit 4385ad5 into github:main Jun 21, 2023
328 checks passed
@angelapwen angelapwen deleted the job-run-uuid branch June 21, 2023 06:45
@github-actions github-actions bot mentioned this pull request Jun 21, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants