Skip to content

Commit

Permalink
feat: set login/email on fixed releaser (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide authored Oct 30, 2022
1 parent 30322db commit d09cf0b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
45 changes: 32 additions & 13 deletions src/application/release-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,9 @@ export class ReleaseEventHandler {
const releaseAuthor = event.payload.sender.login;
console.log(`Release author: ${releaseAuthor}`);

// Use the release author unless a fixed releaser is configured in config.yml.
let releaserUsername = releaseAuthor;
if (rulesetRepo.config.fixedReleaser) {
releaserUsername = rulesetRepo.config.fixedReleaser;
console.log(`Overriding releaser to ${releaserUsername}`);
}

releaser = await this.githubClient.getRepoUser(
releaserUsername,
new Repository(
event.payload.repository.name,
event.payload.repository.owner.login
)
const releaser = await this.determineReleaser(
releaseAuthor,
rulesetRepo
);

console.log(
Expand Down Expand Up @@ -142,6 +132,35 @@ export class ReleaseEventHandler {
}
};

private async determineReleaser(
releaseAuthor: string,
rulesetRepo: RulesetRepository
): Promise<User> {
// Use the GH release author unless a fixedReleaser is configured
let releaserUsername = releaseAuthor;
if (rulesetRepo.config.fixedReleaser) {
releaserUsername = rulesetRepo.config.fixedReleaser.login;
console.log(`Overriding releaser to ${releaserUsername}`);
}

// Fetch the user from GitHub. Note that their email won't be availble
// unless it's publicly listed on their profile.
const fetchedUser = await this.githubClient.getRepoUser(
releaserUsername,
rulesetRepo
);

const releaser: User = {
username: releaserUsername,
name: fetchedUser.name,
email: rulesetRepo.config.fixedReleaser
? rulesetRepo.config.fixedReleaser.email
: fetchedUser.email,
};

return releaser;
}

private async getGitHubWebhookAppAuth(): Promise<GitHubAuth> {
const [githubAppPrivateKey, githubAppClientId, githubAppClientSecret] =
await Promise.all([
Expand Down
7 changes: 6 additions & 1 deletion src/domain/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface Configuration {
readonly fixedReleaser?: string;
readonly fixedReleaser?: FixedReleaser;
}

export interface FixedReleaser {
readonly login: string;
readonly email: string;
}
13 changes: 10 additions & 3 deletions src/domain/ruleset-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
fakeSourceFile,
} from "../test/mock-template-files";
import { expectThrownError } from "../test/util";
import { FixedReleaser } from "./config";

jest.mock("node:fs");
jest.mock("../infrastructure/git");
Expand Down Expand Up @@ -128,9 +129,15 @@ describe("create", () => {
});

test("loads a fixedReleaser", async () => {
mockRulesetFiles({ configExists: true, fixedReleaser: "jbedard" });
mockRulesetFiles({
configExists: true,
fixedReleaser: { login: "jbedard", email: "[email protected]" },
});
const rulesetRepo = await RulesetRepository.create("foo", "bar", "main");
expect(rulesetRepo.config.fixedReleaser).toEqual("jbedard");
expect(rulesetRepo.config.fixedReleaser).toEqual({
login: "jbedard",
email: "[email protected]",
});
});

test("throws on invalid fixedReleaser", async () => {
Expand Down Expand Up @@ -258,7 +265,7 @@ function mockRulesetFiles(
sourceMissingUrl?: boolean;
invalidPresubmit?: boolean;
configExists?: boolean;
fixedReleaser?: string;
fixedReleaser?: FixedReleaser;
invalidFixedReleaser?: boolean;
} = {}
) {
Expand Down
9 changes: 7 additions & 2 deletions src/domain/ruleset-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,19 @@ function loadConfiguration(rulesetRepo: RulesetRepository): Configuration {
return {};
}

let config: Record<string, unknown>;
let config: Record<string, any>;
try {
config = yaml.parse(fs.readFileSync(rulesetRepo.configFilePath, "utf-8"));
} catch (error) {
throw new InvalidConfigFileError(rulesetRepo, "cannot parse file as yaml");
}

if (config.fixedReleaser && typeof config.fixedReleaser !== "string") {
if (
config.fixedReleaser &&
(typeof config.fixedReleaser !== "object" ||
typeof config.fixedReleaser.login !== "string" ||
typeof config.fixedReleaser.email !== "string")
) {
throw new InvalidConfigFileError(
rulesetRepo,
"could not parse 'fixedReleaser'"
Expand Down
14 changes: 11 additions & 3 deletions src/test/mock-template-files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randomUUID } from "crypto";
import { FixedReleaser } from "../domain/config";

export function fakeModuleFile(
overrides: {
Expand Down Expand Up @@ -129,14 +130,21 @@ export function fakeMetadataFile(
}

export function fakeConfigFile(
options: { fixedReleaser?: string; invalidFixedReleaser?: boolean } = {}
options: {
fixedReleaser?: FixedReleaser;
invalidFixedReleaser?: boolean;
} = {}
) {
if (options.invalidFixedReleaser) {
return `\
fixedReleaser: {}
fixedReleaser: foobar
`;
}
return `\
${options.fixedReleaser ? `fixedReleaser: ${options.fixedReleaser}` : ""}
${
options.fixedReleaser
? `fixedReleaser: ${JSON.stringify(options.fixedReleaser)}`
: ""
}
`;
}
10 changes: 6 additions & 4 deletions templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ The `integrity` hash will automatically be filled out by the app.
A configuration file to override default behaviour of the app.

```yaml
fixedReleaser: <GITHUB_USERNAME>
fixedReleaser:
login: <GITHUB_USERNAME>
email: <EMAIL>
```

| Field | Description |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| fixedReleaser | GitHub username (e.g., `kormide`) to author BCR entries. Set this if you want a single user to always be the author of BCR entries regardless of who cut the release. |
| Field | Description |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| fixedReleaser | GitHub username and email to use as the author for BCR commits. Set this if you want a single user to always be the author of BCR entries regardless of who cut the release. |

0 comments on commit d09cf0b

Please sign in to comment.