Skip to content

Commit e4cfee1

Browse files
committed
use sponsorId instead of sponsorLogin for donate cmd
1 parent c941d9b commit e4cfee1

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

commands/donate/cmd.go

+59-25
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (c *CmdDonate) Run(
6363
amount := githubv4.Int(c.Amount)
6464
isRecurring := githubv4.Boolean(c.IsRecurring)
6565
privacyLevel := githubv4.SponsorshipPrivacy(githubv4.SponsorshipPrivacyPublic)
66+
receiveEmails := githubv4.Boolean(false)
67+
68+
sponsorIds := map[string]string{}
6669

6770
// For each recipient create a GH sponsorship that is:
6871
// - $1
@@ -72,43 +75,74 @@ func (c *CmdDonate) Run(
7275
row := row
7376
logger.Infof("donating %s:%s", row.SponsorID, row.RecipientID)
7477

75-
var m struct {
76-
CreateSponsorship struct {
77-
ClientMutationID string
78-
} `graphql:"createSponsorship(input:$input)"`
78+
failed := false
79+
80+
sid, ok := sponsorIds[row.SponsorID]
81+
if !ok {
82+
var q struct {
83+
RepositoryOwner struct {
84+
ID string
85+
} `graphql:"repositoryOwner(login: $login)"`
86+
}
87+
var vars map[string]any = map[string]any{
88+
"login": githubv4.String(row.SponsorID),
89+
}
90+
91+
err := client.Query(ctx, &q, vars)
92+
if err != nil {
93+
logger.WithError(err).Error("failed to get sponsor id")
94+
failed = true
95+
} else {
96+
sid = q.RepositoryOwner.ID
97+
sponsorIds[row.SponsorID] = sid
98+
}
7999
}
80-
id := githubv4.String(fmt.Sprintf("%s:%s", row.SponsorID, row.RecipientID))
81-
sponsorLogin := githubv4.String(row.SponsorID)
82-
sponsorableLogin := githubv4.String(row.RecipientID)
83-
var input githubv4.Input = githubv4.CreateSponsorshipInput{
84-
ClientMutationID: &id,
85-
IsRecurring: &isRecurring,
86-
Amount: &amount,
87-
SponsorLogin: &sponsorLogin,
88-
SponsorableLogin: &sponsorableLogin,
89-
PrivacyLevel: &privacyLevel,
100+
101+
if !failed {
102+
var m struct {
103+
CreateSponsorship struct {
104+
ClientMutationID string
105+
} `graphql:"createSponsorship(input:$input)"`
106+
}
107+
id := githubv4.String(fmt.Sprintf("%s:%s", row.SponsorID, row.RecipientID))
108+
sponsorId := githubv4.ID(sid)
109+
sponsorableLogin := githubv4.String(row.RecipientID)
110+
var input githubv4.Input = githubv4.CreateSponsorshipInput{
111+
ClientMutationID: &id,
112+
IsRecurring: &isRecurring,
113+
Amount: &amount,
114+
SponsorID: &sponsorId,
115+
SponsorableLogin: &sponsorableLogin,
116+
PrivacyLevel: &privacyLevel,
117+
ReceiveEmails: &receiveEmails,
118+
}
119+
120+
err := client.Mutate(ctx, &m, input, nil)
121+
if err != nil {
122+
logger.WithError(err).Errorf("failed to create sponsorship for %s", row.RecipientID)
123+
failed = true
124+
}
90125
}
91126

92-
err := client.Mutate(ctx, &m, input, nil)
93-
if err != nil {
94-
logger.WithError(err).Error("failed to create sponsorship")
127+
if failed {
95128
/* autoquery name: UpdateDonationDonateAttemptTs :exec
96129
97130
UPDATE donations
98131
SET donate_attempt_ts = UNIXEPOCH()
99132
WHERE id = ?;
100133
*/
101134
_ = db.UpdateDonationDonateAttemptTs(ctx, row.ID)
102-
continue
103-
}
104135

105-
/* autoquery name: UpdateDonationDonateTs :exec
136+
} else {
106137

107-
UPDATE donations
108-
SET donate_ts = UNIXEPOCH()
109-
WHERE id = ?;
110-
*/
111-
_ = db.UpdateDonationDonateTs(ctx, row.ID)
138+
/* autoquery name: UpdateDonationDonateTs :exec
139+
140+
UPDATE donations
141+
SET donate_ts = UNIXEPOCH()
142+
WHERE id = ?;
143+
*/
144+
_ = db.UpdateDonationDonateTs(ctx, row.ID)
145+
}
112146
}
113147

114148
return nil

0 commit comments

Comments
 (0)