Verified Commit 27f8f943 authored by Zubeen's avatar Zubeen 🤖 Committed by GitLab
Browse files

feat(achievements): support updating the award message of a user achievement

Changelog: Improvements
parent 7f1dadc7
Loading
Loading
Loading
Loading
+6 −0
Changes for achievements.go: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -309,6 +309,9 @@ type UpdateUserAchievementOptions struct {
	// ShowOnProfile indicates whether the awarded achievement is shown on
	// the recipient's profile.
	ShowOnProfile *bool
	// AwardMessage is a message associated with the awarded achievement, up
	// to 200 characters.
	AwardMessage *string
}

// CreateAchievement creates a new achievement in a namespace.
@@ -611,6 +614,9 @@ func (s *AchievementsService) UpdateUserAchievement(userAchievementID int64, opt
		if opt.ShowOnProfile != nil {
			input["showOnProfile"] = opt.ShowOnProfile
		}
		if opt.AwardMessage != nil {
			input["awardMessage"] = opt.AwardMessage
		}
	}

	mutation := GraphQLQuery{
+43 −0
Changes for achievements_test.go: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -687,6 +687,49 @@ func TestAchievementsService_UpdateUserAchievement(t *testing.T) {
	assert.False(t, userAchievement.ShowOnProfile)
}

func TestAchievementsService_UpdateUserAchievement_awardMessage(t *testing.T) {
	t.Parallel()

	// GIVEN a server that captures the mutation variables
	mux, client := setup(t)

	var got struct {
		Variables struct {
			Input map[string]any `json:"input"`
		} `json:"variables"`
	}
	mux.HandleFunc("/api/graphql", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		assert.NoError(t, json.NewDecoder(r.Body).Decode(&got))
		fmt.Fprint(w, `
			{
				"data": {
					"userAchievementsUpdate": {
						"userAchievement": {
							"id": "gid://gitlab/Achievements::UserAchievement/1",
							"showOnProfile": true,
							"awardMessage": "Well done!"
						},
						"errors": []
					}
				}
			}
		`)
	})

	// WHEN UpdateUserAchievement is called with only an award message
	opt := &UpdateUserAchievementOptions{AwardMessage: new("Well done!")}
	userAchievement, _, err := client.Achievements.UpdateUserAchievement(1, opt)

	// THEN awardMessage is sent, showOnProfile is not, and the message is returned
	require.NoError(t, err)
	assert.Equal(t, map[string]any{
		"userAchievementId": "gid://gitlab/Achievements::UserAchievement/1",
		"awardMessage":      "Well done!",
	}, got.Variables.Input)
	assert.Equal(t, new("Well done!"), userAchievement.AwardMessage)
}

func TestAchievementsService_UpdateUserAchievement_errors(t *testing.T) {
	t.Parallel()