Skip to content

Commit 93d3ac1

Browse files
authored
Merge pull request #10 from rust-lang/set-user-agent
set user agent
2 parents 4a23b48 + 699a040 commit 93d3ac1

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

dist/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27313,6 +27313,18 @@ async function throwHttpErrorMessage(operation, response) {
2731327313
function getTokensEndpoint(registryUrl) {
2731427314
return `${registryUrl}/api/v1/trusted_publishing/tokens`;
2731527315
}
27316+
function userAgentValue() {
27317+
const version = "v1";
27318+
// TODO: read the package name and version from package.json
27319+
return `crates-io-auth-action/${version}`;
27320+
}
27321+
function getUserAgent() {
27322+
const userAgent = userAgentValue();
27323+
return {
27324+
// eslint-disable-next-line @typescript-eslint/naming-convention
27325+
"User-Agent": userAgent,
27326+
};
27327+
}
2731627328
function runAction(fn) {
2731727329
fn().catch((error) => {
2731827330
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -27353,12 +27365,14 @@ async function getJwtToken(audience) {
2735327365
}
2735427366
async function requestTrustedPublishingToken(registryUrl, jwtToken) {
2735527367
const tokenUrl = getTokensEndpoint(registryUrl);
27356-
coreExports.info(`Requesting token from: ${tokenUrl}`);
27368+
const userAgent = getUserAgent();
27369+
coreExports.info(`Requesting token from: ${tokenUrl}. User agent: ${userAgent["User-Agent"]}`);
2735727370
const response = await fetch(tokenUrl, {
2735827371
method: "POST",
2735927372
headers: {
2736027373
/* eslint-disable @typescript-eslint/naming-convention */
2736127374
"Content-Type": "application/json",
27375+
...userAgent,
2736227376
},
2736327377
body: JSON.stringify({ jwt: jwtToken }),
2736427378
});

dist/post.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27296,6 +27296,18 @@ async function throwHttpErrorMessage(operation, response) {
2729627296
function getTokensEndpoint(registryUrl) {
2729727297
return `${registryUrl}/api/v1/trusted_publishing/tokens`;
2729827298
}
27299+
function userAgentValue() {
27300+
const version = "v1";
27301+
// TODO: read the package name and version from package.json
27302+
return `crates-io-auth-action/${version}`;
27303+
}
27304+
function getUserAgent() {
27305+
const userAgent = userAgentValue();
27306+
return {
27307+
// eslint-disable-next-line @typescript-eslint/naming-convention
27308+
"User-Agent": userAgent,
27309+
};
27310+
}
2729927311
function runAction(fn) {
2730027312
fn().catch((error) => {
2730127313
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -27326,6 +27338,7 @@ async function revokeToken(registryUrl, token) {
2732627338
headers: {
2732727339
/* eslint-disable @typescript-eslint/naming-convention */
2732827340
Authorization: `Bearer ${token}`,
27341+
...getUserAgent(),
2732927342
},
2733027343
});
2733127344
if (!response.ok) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "trusted-publishing-action-draft",
3-
"version": "0.0.0",
2+
"name": "crates-io-auth-action",
3+
"version": "1.0.0",
44
"description": "Get a temporary access token that you can use to interact with crates.io.",
55
"private": true,
66
"scripts": {

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
throwHttpErrorMessage,
77
TOKEN_KEY,
88
REGISTRY_URL_KEY,
9+
getUserAgent,
910
} from "./utils.js";
1011

1112
runAction(run);
@@ -61,13 +62,17 @@ async function requestTrustedPublishingToken(
6162
jwtToken: string,
6263
): Promise<string> {
6364
const tokenUrl = getTokensEndpoint(registryUrl);
64-
core.info(`Requesting token from: ${tokenUrl}`);
65+
const userAgent = getUserAgent();
66+
core.info(
67+
`Requesting token from: ${tokenUrl}. User agent: ${userAgent["User-Agent"]}`,
68+
);
6569

6670
const response = await fetch(tokenUrl, {
6771
method: "POST",
6872
headers: {
6973
/* eslint-disable @typescript-eslint/naming-convention */
7074
"Content-Type": "application/json",
75+
...userAgent,
7176
},
7277
body: JSON.stringify({ jwt: jwtToken }),
7378
});

src/post.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
TOKEN_KEY,
66
REGISTRY_URL_KEY,
77
throwHttpErrorMessage,
8+
getUserAgent,
89
} from "./utils.js";
910

1011
runAction(cleanup);
@@ -37,6 +38,7 @@ async function revokeToken(registryUrl: string, token: string): Promise<void> {
3738
headers: {
3839
/* eslint-disable @typescript-eslint/naming-convention */
3940
Authorization: `Bearer ${token}`,
41+
...getUserAgent(),
4042
},
4143
});
4244

src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ interface ErrorResponse {
88
errors: Array<{ detail: string }>;
99
}
1010

11+
type UserAgent = {
12+
// eslint-disable-next-line @typescript-eslint/naming-convention
13+
"User-Agent": string;
14+
};
15+
1116
/** If true, the compiler considers `value` of type ErrorResponse */
1217
function isErrorResponse(value: unknown): value is ErrorResponse {
1318
if (typeof value !== "object" || value === null) {
@@ -71,6 +76,22 @@ export function getTokensEndpoint(registryUrl: string): string {
7176
return `${registryUrl}/api/v1/trusted_publishing/tokens`;
7277
}
7378

79+
function userAgentValue(): string {
80+
const version = "v1";
81+
82+
// TODO: read the package name and version from package.json
83+
84+
return `crates-io-auth-action/${version}`;
85+
}
86+
87+
export function getUserAgent(): UserAgent {
88+
const userAgent = userAgentValue();
89+
return {
90+
// eslint-disable-next-line @typescript-eslint/naming-convention
91+
"User-Agent": userAgent,
92+
};
93+
}
94+
7495
export function runAction(fn: () => Promise<void>): void {
7596
fn().catch((error: unknown) => {
7697
const errorMessage =

0 commit comments

Comments
 (0)