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

upgrade cli package, fix drone 1.x token env variable issue #127

Merged

Conversation

oliver-nyt
Copy link
Contributor

@oliver-nyt oliver-nyt commented Apr 1, 2020

This should fix #119 by picking up both the PLUGIN_TOKEN (drone 1.x) and TOKEN(drone 0.x) env variables (see main.go line 93)
I upgraded the cli package to v2 before I noticed that you can do this in v1 as well. I can undo that change if you prefer so.

Copy link
Contributor

@montmanu montmanu left a comment

Choose a reason for hiding this comment

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

LGTM! Just a couple of "nice to haves" ..

  • Update references to TOKEN in documentation
  • Update references to TOKEN in Makefile
  • Add unit tests for token param precedence? Something like:
func TestTokenParamPrecedence(t *testing.T) {
    // Reset environment
    os.Clearenv()

    // Set both token-related environment variables
    os.Setenv("TOKEN", "from-token-env")
    os.Setenv("PLUGIN_TOKEN", "from-plugin-token-env")

    // Set token CLI argument
    set := flag.NewFlagSet("prefer-arg-over-any-env", 0)
    c := cli.NewContext(nil, set, nil)
    set.String("token", "from-token-arg", "")
    
    // token CLI argument takes precedence over either environment variable
    assert.Equal(t, c.String("token"), "from-token-arg")

    // Reset environment
    os.Clearenv()

    // Set both token-related environment variables
    os.Setenv("TOKEN", "from-token-env")
    os.Setenv("PLUGIN_TOKEN", "from-plugin-token-env")

    // Set CLI arguments without token
    set = flag.NewFlagSet("prefer-plugin-token-env", 0)
    c = cli.NewContext(nil, set, nil)

    // PLUGIN_TOKEN environment variable takes precedence over TOKEN environment variable
    assert.Equal(t, c.String("token"), "from-plugin-token-env")

    // Reset environment
    os.Clearenv()

    // Set only TOKEN environment variable
    os.Setenv("TOKEN", "from-token-env")

    // Set without token CLI argument
    set = flag.NewFlagSet("prefer-token-env", 0)
    c = cli.NewContext(nil, set, nil)

    // TOKEN environment variable supported without token CLI argument or PLUGIN_TOKEN environment variable
    assert.Equal(t, c.String("token"), "from-token-env")

    // ...
}

main.go Outdated Show resolved Hide resolved
@oliver-nyt
Copy link
Contributor Author

Thanks for the comments @montmanu - i updated the docs and Makefile but still trying to get the tests to work, I don't think cli.NewContext() parses and populates the flags but trying to confirm that right now.

main_test.go Outdated
set.String("token", "from-token-arg", "")

// token CLI argument takes precedence over either environment variable
assert.Equal(t, "from-token-arg", c.String("token"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@montmanu - there more I think about these tests, the less they make sense.
This would really just tests that the library is working, none of this invokes code any plugin code
(i also still haven't figured out how to trigger env parsing of flags without using the full cli.App() thing but that's another problem)
Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

i agree that its not testing much of the plugin's code, but its also a somewhat critical part of the plugin's public API, so a bit worried that from a user's e2e POV, we're not well covered on the token parsing / selection logic..

maybe it would be easier for us to figure out a way to cover that via e2e tests? build the plugin, trigger some downstream ci process that would run that version of the plugin against all versions of drone supported by the plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed this is important. Let me try something, i think i have an idea what might work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I found a way to get the tests to do something meaningful. Might actually want to replace some others like TestCheckParams with the same App.Run() pattern to test the real plugin-code.

README.md Show resolved Hide resolved
@oliver-nyt oliver-nyt force-pushed the oh_update_cli_fix_drone_1.0_issue branch from fb233f3 to 9771e44 Compare April 1, 2020 22:47
Copy link
Member

@tonglil tonglil left a comment

Choose a reason for hiding this comment

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

Thanks for the changes!

@tonglil tonglil merged commit f25a5b2 into nytimes:master Apr 6, 2020
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.

Token not being found
3 participants