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

Modified to use Git with PAT #4534

Closed
wants to merge 0 commits into from

Conversation

sZma5a
Copy link
Contributor

@sZma5a sZma5a commented Jul 30, 2023

What this PR does / why we need it: Modified to use Personal Access Token since currently only SSH can control the Git repository.

Which issue(s) this PR fixes:

Fixes #4106

Does this PR introduce a user-facing change?: Yes

  • How are users affected by this change: Be able to use Personal Access Token setting like this:
apiVersion: pipecd.dev/v1beta1
kind: Piped
spec:
  git:
     personalAccessToken:
        userName: <user-name>
        userToken: <user-token>
  • Is this breaking change: No
  • How to migrate (if breaking change):

@sZma5a sZma5a changed the title Modified to use Git with PAT [WIP]Modified to use Git with PAT Jul 30, 2023
@sZma5a
Copy link
Contributor Author

sZma5a commented Jul 30, 2023

Local behavior testing has not yet been completed...
→Done

@hungran
Copy link
Contributor

hungran commented Jul 31, 2023

+1 for this feature

pkg/config/piped.go Outdated Show resolved Hide resolved
pkg/config/piped.go Outdated Show resolved Hide resolved
@sZma5a sZma5a changed the title [WIP]Modified to use Git with PAT Modified to use Git with PAT Aug 1, 2023
@sZma5a sZma5a marked this pull request as ready for review August 1, 2023 14:53
Copy link
Member

@kentakozuka kentakozuka left a comment

Choose a reason for hiding this comment

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

Thank you for your PR!
Left comments. PTAL 👀
Also, please review the test data and make the test pass 👍

@@ -176,6 +179,7 @@ func (s *PipedSpec) Mask() {
s.PipedKeyData = maskString
}
s.Git.Mask()
s.Git.PersonalAccessToken.Mask()
Copy link
Member

Choose a reason for hiding this comment

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

IMHO, it would be better to move this line to s.Git.Mask().

Comment on lines 356 to 358
if g.ShouldConfigureSSHConfig() && g.PersonalAccessToken.ShouldConfigureSSHConfig() {
return errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken")
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Please try not to use ShouldConfigureSSHConfig? This function is supposed to be used in a different context. Validate configs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kentakozuka
Sorry... I forgot to fix name when I copied.
How about "ShouldConfigurePATConfig" because the function is checking to use PAT or not?

UserToken string `json:"userToken,omitempty"`
}

func (p PipedGitPersonalAccessToken) ShouldConfigureSSHConfig() bool {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (p PipedGitPersonalAccessToken) ShouldConfigureSSHConfig() bool {
func (p PipedGitPersonalAccessToken) Validate() bool {

@sZma5a sZma5a force-pushed the feat/add-pat-setting branch 3 times, most recently from 44bfbae to fc0e001 Compare August 2, 2023 13:29
Copy link
Member

@sivchari sivchari left a comment

Choose a reason for hiding this comment

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

some nits.

@@ -1289,3 +1306,46 @@ func TestFindPlatformProvidersByLabel(t *testing.T) {
})
}
}

func TestPipeGitValidate(t *testing.T) {
testcases := []struct {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
testcases := []struct {
t.Parallel()
testcases := []struct {

Comment on lines 1341 to 1430
for _, tc := range testcases {
t.Run(tc.git.SSHKeyData, func(t *testing.T) {
err := tc.git.Validate()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for _, tc := range testcases {
t.Run(tc.git.SSHKeyData, func(t *testing.T) {
err := tc.git.Validate()
for _, tc := range testcases {
tt := tt
t.Run(tc.git.SSHKeyData, func(t *testing.T) {
t.Parallel()
err := tc.git.Validate()

func TestPipeGitValidate(t *testing.T) {
testcases := []struct {
git PipedGit
expectedError error
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expectedError error
err error

Comment on lines 1344 to 1432
if tc.expectedError != nil {
assert.Equal(t, tc.expectedError, err)
} else {
require.NoError(t, err)
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if tc.expectedError != nil {
assert.Equal(t, tc.expectedError, err)
} else {
require.NoError(t, err)
}
})
assert.Equal(t, tc.err, err)
})

Copy link
Member

@kentakozuka kentakozuka left a comment

Choose a reason for hiding this comment

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

Thanks!
Can you run make test/go to make sure all tests pass?
Also, docs needs to be updated.

@@ -345,6 +351,13 @@ func (g PipedGit) LoadSSHKey() ([]byte, error) {
return nil, errors.New("either sshKeyFile or sshKeyData must be set")
}

func (g *PipedGit) Validate() error {
if g.ShouldConfigureSSHConfig() && g.PersonalAccessToken.ShouldConfigurePATConfig() {
Copy link
Member

Choose a reason for hiding this comment

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

ShouldConfigureSSHConfig() is not exactly for validation purpose(called here). Can you validate ssh and pat config without using ShouldConfigureXXX()?

@sZma5a
Copy link
Contributor Author

sZma5a commented Aug 13, 2023

Sorry for the delay.
I have fixed the points you reviewed.

@sZma5a
Copy link
Contributor Author

sZma5a commented Aug 14, 2023

Sorry, I just checked at hand and the test was down, so please hold on a moment for a review.

@codecov
Copy link

codecov bot commented Aug 18, 2023

Codecov Report

Patch coverage: 27.35% and project coverage change: +0.01% 🎉

Comparison is base (41d8b92) 29.98% compared to head (a798b9a) 29.99%.
Report is 17 commits behind head on master.

❗ Current head a798b9a differs from pull request most recent head 01ad346. Consider uploading reports for the commit 01ad346 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4534      +/-   ##
==========================================
+ Coverage   29.98%   29.99%   +0.01%     
==========================================
  Files         220      220              
  Lines       25808    25909     +101     
==========================================
+ Hits         7739     7772      +33     
- Misses      17422    17487      +65     
- Partials      647      650       +3     
Files Changed Coverage Δ
pkg/app/piped/notifier/notifier.go 0.00% <0.00%> (ø)
pkg/app/piped/notifier/slack.go 3.57% <0.00%> (-0.25%) ⬇️
pkg/app/piped/platformprovider/ecs/client.go 0.00% <0.00%> (ø)
pkg/app/piped/platformprovider/ecs/ecs.go 0.00% <ø> (ø)
pkg/app/piped/platformprovider/lambda/client.go 4.73% <0.00%> (-0.21%) ⬇️
pkg/app/piped/platformprovider/lambda/function.go 63.75% <ø> (ø)
pkg/git/client.go 46.87% <42.85%> (-0.80%) ⬇️
pkg/config/piped.go 57.33% <88.46%> (+1.31%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sZma5a
Copy link
Contributor Author

sZma5a commented Aug 24, 2023

I had a lint error in Github Actions, so I tried again in my local enviroment, but it appears that some of these are outside the scope of this PR, how should I handle this?

pkg/config/application.go:255:2: ST1003: struct field Id should be ID (stylecheck)
        Id      string          `json:"id"`
        ^
pkg/config/application.go:443:2: ST1003: struct field Https should be HTTPS (stylecheck)
        Https            []TemplatableAnalysisHTTP    `json:"https"`
        ^
pkg/config/control_plane.go:55:2: ST1003: struct field Id should be ID (stylecheck)
        Id string `json:"id"`

Copy link
Member

@kentakozuka kentakozuka left a comment

Choose a reason for hiding this comment

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

Thanks!
Left some comments.
CI is passing, so ignore the lint error on local for now.
(I also ran make lint/go on local and got a bunch of errors. We should fix them in another PR 😉 )

Comment on lines 384 to 385
func (p PipedGitPersonalAccessToken) Validate() bool {
return p.UserName != "" && p.UserToken != ""
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 make this function return error for consistency?
ref: https://github.com/pipe-cd/pipecd/blob/master/pkg/config/piped.go#L103

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

Comment on lines 354 to 359
func (g *PipedGit) Validate() error {
if g.ShouldConfigureSSHConfig() && g.PersonalAccessToken.Validate() {
return errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken")
}
return nil
}
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 add a unit test for this function?
I don't feel this is not enough. What about a case in which one of ShouldConfigureSSHConfig and PersonalAccessToken.Validate returns false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Corrected missing test case!

## GitPersonalAccessToken
| Field | Type | Description | Required |
|-|-|-|-|
| userName | string | The user name for git used while cloning above Git repository. | No |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
| userName | string | The user name for git used while cloning above Git repository. | No |
| userName | string | The user name for git used while cloning above Git repository. | Yes |

| Field | Type | Description | Required |
|-|-|-|-|
| userName | string | The user name for git used while cloning above Git repository. | No |
| userToken | string | The generated personal access token used while cloning above Git repository. | No |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
| userToken | string | The generated personal access token used while cloning above Git repository. | No |
| userToken | string | The generated personal access token used while cloning above Git repository. | Yes |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the way to access git repository by using Personal Access Token(PAT)
4 participants