Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger core builds [BLOCKED] #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function cleanPinsFromDeletedBranches({
const manifest = readManifest(dir);

// Connect to Github Octokit REST API to know existing branches
const github = new Github(dir);
const github = Github.fromDir(dir);
const branches = await github.listBranches();

// Fetch Pinata credentials from ENVs
Expand Down
14 changes: 10 additions & 4 deletions src/commands/githubActions/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Github } from "../../../providers/github/Github";
import { parseRef } from "../../../providers/github/utils";
import { getBuildBotComment, isTargetComment } from "./botComment";
import { cleanPinsFromDeletedBranches } from "./cleanPinsFromDeletedBranches";
import { triggerCoreDnpWorkflowMaybe } from "./triggerCoreDnp";

// This action should be run on 'push' and 'pull_request' events
//
Expand Down Expand Up @@ -42,7 +43,7 @@ export async function gaBuildHandler({
const ref = parseRef(refString);

// Connect to Github Octokit REST API and post or edit a comment on PR
const github = new Github(dir);
const github = Github.fromDir(dir);

// Clean pins that were added from past runs.
// Doing it here prevents having to add two workflows per repo.
Expand Down Expand Up @@ -80,10 +81,15 @@ export async function gaBuildHandler({
github.commentToPr({ number: pr.number, body, isTargetComment })
)
);
return; // done
}

if (eventName === "push" || eventName === "pull_request") {
// Trigger CORE package for a possible release
await triggerCoreDnpWorkflowMaybe({
dir,
releaseMultiHash,
branch: ref.branch,
commitSha
});
} else if (eventName === "push" || eventName === "pull_request") {
// Consider that for 'pull_request' commitSha does not represent a known commit
// The incoming branch is merged into the target branch and the resulting
// new commit is tested. gitHead() will return 'HEAD' for branch and a foreign commit
Expand Down
48 changes: 48 additions & 0 deletions src/commands/githubActions/build/triggerCoreDnp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Github } from "../../../providers/github/Github";
import { readManifest } from "../../../utils/manifest";
import {
DNP_CORE_GITHUB_EVENT_TYPE,
DNP_CORE_GITHUB_USER,
DNP_CORE_GITHUB_REPO,
DNP_CORE_DEPENDENCIES
} from "../../../params";

/**
* Send a repository_dispatch event to the DNP_CORE Github repo only if:
* - Current package is a dependency of DNP_CORE
*/
export async function triggerCoreDnpWorkflowMaybe({
dir,
releaseMultiHash,
branch,
commitSha
}: {
dir: string;
releaseMultiHash: string;
branch: string;
commitSha: string;
}): Promise<void> {
const manifest = readManifest(dir);
if (!DNP_CORE_DEPENDENCIES.includes(manifest.name)) {
console.log(`Skipping triggerCoreDnpWorkflow for ${manifest.name}`);
return;
}

const github = new Github({
owner: DNP_CORE_GITHUB_USER,
repo: DNP_CORE_GITHUB_REPO
});

const clientPayload = {
releaseMultiHash,
branch,
commitSha
};

try {
await github.dispatchEvent(DNP_CORE_GITHUB_EVENT_TYPE, clientPayload);
console.log("Triggered DNP_CORE workflow");
} catch (e) {
console.log("Error on triggerCoreDnpWorkflow", e);
}
}
11 changes: 11 additions & 0 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export const UPSTREAM_VERSION_VARNAME = "UPSTREAM_VERSION";
export const upstreamImageLabel = "dappnode.dnp.upstreamImage";
export const PINATA_URL = "https://api.pinata.cloud";

export const DNP_CORE_GITHUB_EVENT_TYPE = "dappnodesdk_build";
export const DNP_CORE_GITHUB_USER = "dappnode";
export const DNP_CORE_GITHUB_REPO = "DNP_CORE";
export const DNP_CORE_DEPENDENCIES = [
"bind.dnp.dappnode.eth",
"ipfs.dnp.dappnode.eth",
"vpn.dnp.dappnode.eth",
"dappmanager.dnp.dappnode.eth",
"wifi.dnp.dappnode.eth"
];

/**
* Plain text file with should contain the IPFS hash of the release
* Necessary for the installer script to fetch the latest content hash
Expand Down
56 changes: 42 additions & 14 deletions src/providers/github/Github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ export class Github {
octokit: Octokit;
owner: string;
repo: string;
repoSlug: string;

constructor(dir: string) {
constructor({ owner, repo }: { owner: string; repo: string }) {
this.owner = owner;
this.repo = repo;

// OAuth2 token from Github
if (!process.env.GITHUB_TOKEN)
throw Error("GITHUB_TOKEN ENV (OAuth2) is required");
this.octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`
});
}

static fromDir(dir: string): Github {
const repoSlug =
getRepoSlugFromManifest(dir) ||
process.env.TRAVIS_REPO_SLUG ||
Expand All @@ -27,27 +38,19 @@ export class Github {
if (!owner) throw Error(`repoSlug "${repoSlug}" hasn't an owner`);
if (!repo) throw Error(`repoSlug "${repoSlug}" hasn't a repo`);

this.owner = owner;
this.repo = repo;
this.repoSlug = repoSlug;

// OAuth2 token from Github
if (!process.env.GITHUB_TOKEN)
throw Error("GITHUB_TOKEN ENV (OAuth2) is required");
this.octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`
});
return new Github({ owner, repo });
}

async assertRepoExists(): Promise<void> {
try {
await this.octokit.repos.get({ owner: this.owner, repo: this.repo });
} catch (e) {
const repoSlug = `${this.owner}/${this.repo}`;
if (e.status === 404)
throw Error(
`Repo does not exist: ${this.repoSlug}. Check the manifest.repository object and correct the repo URL`
`Repo does not exist: ${repoSlug}. Check the manifest.repository object and correct the repo URL`
);
e.message = `Error verifying repo ${this.repoSlug}: ${e.message}`;
e.message = `Error verifying repo ${repoSlug}: ${e.message}`;
throw e;
}
}
Expand Down Expand Up @@ -299,4 +302,29 @@ export class Github {
});
return res.data;
}

/**
* trigger a webhook event called repository_dispatch in a repository
* The consumer must add in workflow.yml
* ```yaml
* on:
* repository_dispatch:
* types: [backend_automation]
* ```
* Example payload on the receiver https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#repository_dispatch
* @param eventType "backend_automation"
* @param clientPayload Arbitrary JSON payload: `{ "extraData": 1234 }`
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async dispatchEvent(
eventType: string,
clientPayload?: { [key: string]: string | number }
) {
await this.octokit.repos.createDispatchEvent({
owner: this.owner,
repo: this.repo,
event_type: eventType,
client_payload: clientPayload
});
}
}
2 changes: 1 addition & 1 deletion src/tasks/createGithubRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createGithubRelease({
if (!process.env.GITHUB_TOKEN)
throw Error("GITHUB_TOKEN ENV (OAuth2) is required");

const github = new Github(dir);
const github = Github.fromDir(dir);

// Gather repo data, repoSlug = "dappnode/DNP_ADMIN"
const repoSlug =
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/createNextBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createNextBranch({
silent
}: CliGlobalOptions): Listr<ListrContextBuildAndPublish> {
// Gather repo data, repoSlug = "dappnode/DNP_ADMIN"
const github = new Github(dir);
const github = Github.fromDir(dir);

return new Listr<ListrContextBuildAndPublish>(
[
Expand Down