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

[ci-visibility] Fix GitHub Metadata Extraction #1771

Merged
merged 2 commits into from
Nov 17, 2021

Conversation

juan-fernandez
Copy link
Contributor

@juan-fernandez juan-fernandez commented Nov 15, 2021

Changes

  • Use GITHUB_SERVER_URL instead of hardcoding github.com.
  • Change pipeline url from "https://github.com/#{env['GITHUB_REPOSITORY']}/commit/#{env['GITHUB_SHA']}/checks" to "#{env['GITHUB_SERVER_URL']}/#{env['GITHUB_REPOSITORY']}/actions/runs/#{env['GITHUB_RUN_ID']}", which is the correct URL for pipelines in GitHub.
  • Update fixtures in github.json.

@juan-fernandez juan-fernandez marked this pull request as ready for review November 15, 2021 14:19
@juan-fernandez juan-fernandez requested a review from a team November 15, 2021 14:19
@codecov-commenter
Copy link

Codecov Report

Merging #1771 (f714410) into master (9e75a24) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1771   +/-   ##
=======================================
  Coverage   98.21%   98.21%           
=======================================
  Files         931      931           
  Lines       44870    44872    +2     
=======================================
+ Hits        44069    44071    +2     
  Misses        801      801           
Impacted Files Coverage Δ
lib/datadog/ci/ext/environment.rb 98.87% <100.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9e75a24...f714410. Read the comment docs.

Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

Left a bunch of tiny questions, but overall it looks reasonable :)

@@ -213,16 +213,19 @@ def extract_github_actions(env)
ref = env['GITHUB_REF'] if ref.nil? || ref.empty?
branch, tag = branch_or_tag(ref)

pipeline_url = "#{env['GITHUB_SERVER_URL']}/#{env['GITHUB_REPOSITORY']}/actions/runs/#{env['GITHUB_RUN_ID']}"
Copy link
Member

Choose a reason for hiding this comment

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

i. Are we guaranteed that GITHUB_SERVER_URL will be present, or should we have a fallback value for it?


ii. Also somewhat related, one thing we don't do in this file at all, but perhaps would be worth considering, is to use Hash#fetch instead of Hash#[] when we're fetching information we consider to be required.

For instance, in this case:

[1] pry(main)> env = {}
=> {}
[2] pry(main)> env['GITHUB_SERVER_URL']
=> nil
[3] pry(main)> env.fetch('GITHUB_SERVER_URL')
KeyError: key not found: "GITHUB_SERVER_URL"
from (pry):3:in `fetch'

of course, this very much depends on what we want out of this process. Perhaps not having this failures so far is intentional, because we may prefer to have an incorrect metadata value, rather than a failure (...?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i. It seems so: https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables. This has consistently worked for us in our tests.

ii. Yeah I'd say it's safer to fallback to nil rather than failing. We follow CI's documentation pages for the name of these variables, but there are cases in which it's difficult to tell, for example, is GITHUB_REF_NAME defined for both PR and merge triggers? Leaving a key empty is safe: we just don't show a piece of metadata in our UI

Copy link
Member

Choose a reason for hiding this comment

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

Note that in some cases, like this one, the key would not be empty, but actually incorrect, e.g. /actions/runs/ if all of the GITHUB_... env vars were missing.

Should we test that they exist before using them for templating?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we work under the assumption that some env vars are always there. For example, if GITHUB_REPOSITORY is missing, we can't associate the test run to a repository, so most of our UI won't work. Test spans will still be in the event platform, with missing or invalid metadata, but I think that's fine, since we don't have any valid value to fallback to.

Comment on lines -18 to +45
"ci.pipeline.url": "https://github.com/ghactions-repo/commit/ghactions-commit/checks",
"ci.pipeline.url": "https://github.com/ghactions-repo/actions/runs/ghactions-pipeline-id/attempts/ghactions-run-attempt",
Copy link
Member

Choose a reason for hiding this comment

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

Can you confirm this is expected?

E.g. the git commit only mentions the introduction of env['GITHUB_SERVER_URL'] but there's a different feature being changed here, which is changing the pipeline url.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah sry about that. I tend to always squash my PRs so I pay more attention to the PR description than the commit messages. This change is reflected in the description but not in the commit message.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right. I totally missed it.

lib/datadog/ci/ext/environment.rb Show resolved Hide resolved
lib/datadog/ci/ext/environment.rb Show resolved Hide resolved
Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@juan-fernandez juan-fernandez merged commit 15f3403 into master Nov 17, 2021
@juan-fernandez juan-fernandez deleted the juan-fernandez/fix-github-pipeline-url branch November 17, 2021 08:02
@github-actions github-actions bot added this to the 0.54.0 milestone Nov 17, 2021
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

3 participants