-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.ts
More file actions
111 lines (100 loc) · 3.97 KB
/
index.ts
File metadata and controls
111 lines (100 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { CommandModule } from "yargs";
import { CliGlobalOptions } from "../../../types";
import { defaultDir } from "../../../params";
import { getGithubContext } from "../../../providers/github/githubActions";
import { buildHandler } from "../../build";
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
//
// For 'push' events ('branch'):
// Does a build test and uploads release to Pinata tagged with branch
// and commit. It will also locate any PRs from that branch and comment
// the resulting hash, so it can be used by testers.
// Another job 'unpin-on-ref-delete' should delete eventually the
// releases generated by this action
//
// For 'push' events ('tags'):
// Skip for now. On 'tag' another action should publish instead of just
// building, maybe it can be done by this job, but consider alternatives
//
// For 'pull_request' events:
// Does a build test but doesn't upload the result anywhere
export const gaBuild: CommandModule<CliGlobalOptions, CliGlobalOptions> = {
command: "build",
describe:
"Build and upload test release and post a comment with install link to the triggering PR",
builder: {},
handler: async (args): Promise<void> => await gaBuildHandler(args)
};
/**
* Common handler for CLI and programatic usage
*/
export async function gaBuildHandler({
dir = defaultDir
}: CliGlobalOptions): Promise<void> {
const { eventName, sha: commitSha, ref: refString } = getGithubContext();
const ref = parseRef(refString);
// Connect to Github Octokit REST API and post or edit a comment on PR
const github = Github.fromDir(dir);
// Clean pins that were added from past runs.
// Doing it here prevents having to add two workflows per repo.
// Also, ensures that pins are deleted eventually, even if this fails sometimes
try {
await cleanPinsFromDeletedBranches({ dir });
} catch (e) {
console.error("Error on cleanPinsFromDeletedBranches", e);
}
if (
eventName === "push" &&
ref.type === "branch" &&
// Do not upload to pinata for branches that are never deleted
ref.branch !== "HEAD" &&
ref.branch !== "master" &&
ref.branch !== "main"
) {
const { releaseMultiHash } = await buildHandler({
provider: "pinata",
upload_to: "ipfs",
require_git_data: true,
delete_old_pins: true,
verbose: true
});
const body = getBuildBotComment({ commitSha, releaseMultiHash });
console.log(`Build bot comment: \n\n${body}`);
const prs = await github.getOpenPrsFromBranch({ branch: ref.branch });
console.log(`PRs: ${prs.map(pr => pr.number).join(", ")}`);
await Promise.all(
prs.map(pr =>
github.commentToPr({ number: pr.number, body, isTargetComment })
)
);
// 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
// Pinata example: 'dappnodesdk.public HEAD 2f149cf'
// See https://github.community/t/github-sha-not-the-same-as-the-triggering-commit/18286
// By default just do a test build and skip_save
await buildHandler({
provider: "dappnode",
upload_to: "ipfs",
skip_save: true,
verbose: true
});
} else if (!eventName) {
throw Error("Not in Github action context");
} else {
throw Error(`Unsupported event ${eventName}`);
}
}