-
Notifications
You must be signed in to change notification settings - Fork 43
/
CircleCIPipelineTrigger.ts
137 lines (129 loc) · 3.93 KB
/
CircleCIPipelineTrigger.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
getInput,
info,
setFailed,
setOutput,
startGroup,
error as coreError,
endGroup,
} from "@actions/core";
import axios from "axios";
import { Context } from "@actions/github/lib/context";
export class CircleCIPipelineTrigger {
vcs: string;
owner: string;
repo: string;
tag?: string;
branch?: string;
host: string;
context: Context;
url: string;
metaData: string;
parameters: CircleCIPipelineParams;
constructor(context: Context, host = process.env.CCI_HOST || "circleci.com") {
this.context = context;
this.host = host;
const slug = process.env.TARGET_SLUG ?? getInput("target-slug");
const { vcs, owner, repo } = slug
? this.parseSlug(slug)
: { ...context.repo, vcs: "gh" };
this.vcs = vcs;
this.owner = owner;
this.repo = repo;
this.url = `https://${this.host}/api/v2/project/${this.vcs}/${this.owner}/${this.repo}/pipeline`;
this.metaData = getInput("GHA_Meta");
this.tag = this.getTag();
this.branch = this.getBranch();
this.parameters = {
GHA_Actor: context.actor,
GHA_Action: context.action,
GHA_Event: context.eventName,
};
}
parseSlug(slug: string) {
const [vcs, owner, repo] = slug.split("/");
if (!owner || !repo || !vcs) {
throw new Error(`Invalid target-slug: ${slug}`);
}
return { vcs, owner, repo };
}
getTag() {
let tag = process.env.TARGET_TAG ?? getInput("target-tag");
if (!tag) {
const tagRef = "refs/tags/";
if (this.context.ref.startsWith(tagRef)) {
tag = this.context.ref.substring(tagRef.length);
}
}
return tag;
}
getBranch() {
let branch = process.env.TARGET_BRANCH ?? getInput("target-branch");
if (!branch) {
if (this.context.ref.startsWith("refs/heads/")) {
branch = this.context.ref.substring(11);
} else if (this.context.ref.startsWith("refs/pull/")) {
info(`This is a PR. Using head PR branch`);
const pullRequestNumber = (
this.context.ref.match(/refs\/pull\/([0-9]*)\//) as RegExpMatchArray
)[1];
const newref = `pull/${pullRequestNumber}/head`;
branch = newref;
}
}
return branch;
}
triggerPipeline() {
const body: CircleCITriggerPipelineRequest = {
parameters: this.parameters,
};
startGroup("Preparing CircleCI Pipeline Trigger");
info(`Org: ${this.owner}`);
info(`Repo: ${this.repo}`);
if (this.metaData.length > 0) {
this.parameters.GHA_Meta = this.metaData;
}
body[this.tag ? "tag" : "branch"] = this.tag || this.branch;
info(`Triggering CircleCI Pipeline for ${this.owner}/${this.repo}`);
info(` Triggering URL: ${this.url}`);
const trigger = this.tag ? `tag: ${this.tag}` : `branch: ${this.branch}`;
info(` Triggering ${trigger}`);
info(` Parameters:\n${JSON.stringify(this.parameters)}`);
endGroup();
axios
.post(this.url, body, {
headers: {
"content-type": "application/json",
"x-attribution-login": this.context.actor,
"x-attribution-actor-id": this.context.actor,
"Circle-Token": `${process.env.CCI_TOKEN}`,
},
})
.then((response) => {
startGroup("Successfully triggered CircleCI Pipeline");
info(`CircleCI API Response: ${JSON.stringify(response.data)}`);
setOutput("created_at", response.data.created_at);
setOutput("id", response.data.id);
setOutput("number", response.data.number);
setOutput("state", response.data.state);
endGroup();
})
.catch((error) => {
startGroup("Failed to trigger CircleCI Pipeline");
coreError(error);
setFailed(error.message);
endGroup();
});
}
}
type CircleCIPipelineParams = {
GHA_Actor: string;
GHA_Action: string;
GHA_Event: string;
GHA_Meta?: string;
};
type CircleCITriggerPipelineRequest = {
parameters: CircleCIPipelineParams;
branch?: string;
tag?: string;
};