xanzy / go-gitlab

GitLab Go SDK
Apache License 2.0
2.33k stars 924 forks source link

Add Users.UploadAvatar() and add Avatar to UserOptions #1965

Closed andreaswwilson closed 1 week ago

andreaswwilson commented 2 weeks ago

First time committing to external github-repo, so I hope this is the correct way. This fixes xanzy/go-gitlab#1871 I have not added feature for downloading avatar since this is not part of the gitlab api.

andreaswwilson commented 2 weeks ago

Thank you for your feedback and for pointing me in the correct direction. I have updated CreateUser and ModifyUser to follow the existing pattern used for groups and projects. Since i am not an administrator for any gitlab instance i have not been able to test the modified methods on a real instance.

svanharmelen commented 1 week ago

While the PR now looks good, I do prefer this is checked/tested before we merge it. I also don't have access to a GitLab instance (believe it or not 😏), but maybe @PatrickRice-KSC or @timofurrer can help out and give it a quick check/test?

PatrickRice-KSC commented 1 week ago

@svanharmelen - Sure, I'll give it a test! Also, do you want me to get you a contributors license for GitLab? I'm happy to help with that 😆

PatrickRice-KSC commented 1 week ago

I created 3 different tests for this PR, testing:

  1. Uploading an avatar to a current user
  2. Modifying an existing user to have a new avatar
  3. Creating a user with an avatar

All 3 tests passed properly :)

Here is the code I used:

func TestUserAvatar(t *testing.T) {
    client, err := NewClient("glpat-ACCTEST1234567890123", WithBaseURL("http://localhost:8085/api/v4"))
    if err != nil {
        t.Fatal(err)
    }

    avatar, err := os.OpenFile("testdata/avatar.png", os.O_RDONLY, 0644)
    if err != nil {
        t.Fatal(err)
    }

    user, resp, err := client.Users.UploadAvatar(avatar, "avatar.png")
    assert.Nil(t, err)
    assert.Equal(t, 200, resp.StatusCode)
    assert.NotNil(t, user.AvatarURL)
}

func TestModifyUserAvatar(t *testing.T) {
    client, err := NewClient("glpat-ACCTEST1234567890123", WithBaseURL("http://localhost:8085/api/v4"))
    if err != nil {
        t.Fatal(err)
    }

    avatar, err := os.OpenFile("testdata/avatar.png", os.O_RDONLY, 0644)
    if err != nil {
        t.Fatal(err)
    }

    userAvatar := &UserAvatar{
        Image:    avatar,
        Filename: "testdata/avatar.png",
    }

    user, resp, err := client.Users.ModifyUser(1, &ModifyUserOptions{
        Avatar: userAvatar,
    })

    assert.Nil(t, err)
    assert.Equal(t, 200, resp.StatusCode)
    assert.NotNil(t, user.AvatarURL)
}

func TestCreateUserAvatar(t *testing.T) {
    client, err := NewClient("glpat-ACCTEST1234567890123", WithBaseURL("http://localhost:8085/api/v4"))
    if err != nil {
        t.Fatal(err)
    }

    avatar, err := os.OpenFile("testdata/avatar.png", os.O_RDONLY, 0644)
    if err != nil {
        t.Fatal(err)
    }

    userAvatar := &UserAvatar{
        Image:    avatar,
        Filename: "testdata/avatar.png",
    }

    user, resp, err := client.Users.CreateUser(&CreateUserOptions{
        Email:               Ptr("example@exampl.com"),
        Name:                Ptr("PetTheKitty"),
        Username:            Ptr("PetTheKitty"),
        ForceRandomPassword: Ptr(true),
        Avatar:              userAvatar,
    })

    assert.Nil(t, err)
    assert.Equal(t, 201, resp.StatusCode)
    assert.NotNil(t, user.AvatarURL)
}
svanharmelen commented 1 week ago

Thanks you so much for your (quick) help @PatrickRice-KSC! Really appreciated it ❤️