@@ -35,17 +35,22 @@ export class GitHubPublisher implements Publisher {
35
35
private readonly token : string
36
36
private readonly policy : PublishPolicy
37
37
38
+ private readonly options : PublishOptions
39
+
38
40
get releasePromise ( ) : Promise < Release | null > {
39
41
return this . _releasePromise
40
42
}
41
43
42
- constructor ( private owner : string , private repo : string , private version : string , private options : PublishOptions , private isPublishOptionGuessed : boolean = false , config ?: GithubOptions | null ) {
43
- if ( isEmptyOrSpaces ( options . githubToken ) ) {
44
- throw new Error ( "GitHub Personal Access Token is not specified" )
44
+ constructor ( private info : GithubOptions , private version : string , options ?: PublishOptions , private isPublishOptionGuessed : boolean = false , config ?: GithubOptions | null ) {
45
+ let token = info . token
46
+ if ( isEmptyOrSpaces ( token ) ) {
47
+ token = process . env . GH_TOKEN
48
+ throw new Error ( `GitHub Personal Access Token is not set, neither programmatically, nor using env "GH_TOKEN"` )
45
49
}
46
50
47
- this . token = options . githubToken !
48
- this . policy = options . publish || "always"
51
+ this . token = token !
52
+ this . options = options || { }
53
+ this . policy = this . options . publish || "always"
49
54
50
55
if ( version . startsWith ( "v" ) ) {
51
56
throw new Error ( `Version must not starts with "v": ${ version } ` )
@@ -58,7 +63,7 @@ export class GitHubPublisher implements Publisher {
58
63
private async init ( ) : Promise < Release | null > {
59
64
const createReleaseIfNotExists = this . policy !== "onTagOrDraft"
60
65
// we don't use "Get a release by tag name" because "tag name" means existing git tag, but we draft release and don't create git tag
61
- const releases = await githubRequest < Array < Release > > ( `/repos/${ this . owner } /${ this . repo } /releases` , this . token )
66
+ const releases = await githubRequest < Array < Release > > ( `/repos/${ this . info . owner } /${ this . info . repo } /releases` , this . token )
62
67
for ( let release of releases ) {
63
68
if ( release . tag_name === this . tag || release . tag_name === this . version ) {
64
69
if ( release . draft ) {
@@ -120,10 +125,10 @@ export class GitHubPublisher implements Publisher {
120
125
if ( e . response . statusCode === 422 && e . description != null && e . description . errors != null && e . description . errors [ 0 ] . code === "already_exists" ) {
121
126
// delete old artifact and re-upload
122
127
log ( `Artifact ${ fileName } already exists, overwrite one` )
123
- const assets = await githubRequest < Array < Asset > > ( `/repos/${ this . owner } /${ this . repo } /releases/${ release . id } /assets` , this . token )
128
+ const assets = await githubRequest < Array < Asset > > ( `/repos/${ this . info . owner } /${ this . info . repo } /releases/${ release . id } /assets` , this . token )
124
129
for ( let asset of assets ) {
125
130
if ( asset ! . name === fileName ) {
126
- await githubRequest < void > ( `/repos/${ this . owner } /${ this . repo } /releases/assets/${ asset ! . id } ` , this . token , null , "DELETE" )
131
+ await githubRequest < void > ( `/repos/${ this . info . owner } /${ this . info . repo } /releases/assets/${ asset ! . id } ` , this . token , null , "DELETE" )
127
132
continue uploadAttempt
128
133
}
129
134
}
@@ -142,7 +147,7 @@ export class GitHubPublisher implements Publisher {
142
147
}
143
148
144
149
private createRelease ( ) {
145
- return githubRequest < Release > ( `/repos/${ this . owner } /${ this . repo } /releases` , this . token , {
150
+ return githubRequest < Release > ( `/repos/${ this . info . owner } /${ this . info . repo } /releases` , this . token , {
146
151
tag_name : this . tag ,
147
152
name : this . version ,
148
153
draft : this . options . draft == null || this . options . draft ,
@@ -153,7 +158,7 @@ export class GitHubPublisher implements Publisher {
153
158
// test only
154
159
//noinspection JSUnusedGlobalSymbols
155
160
async getRelease ( ) : Promise < any > {
156
- return githubRequest < Release > ( `/repos/${ this . owner } /${ this . repo } /releases/${ this . _releasePromise . value ( ) . id } ` , this . token )
161
+ return githubRequest < Release > ( `/repos/${ this . info . owner } /${ this . info . repo } /releases/${ this . _releasePromise . value ( ) . id } ` , this . token )
157
162
}
158
163
159
164
//noinspection JSUnusedGlobalSymbols
@@ -169,7 +174,7 @@ export class GitHubPublisher implements Publisher {
169
174
170
175
for ( let i = 0 ; i < 3 ; i ++ ) {
171
176
try {
172
- return await githubRequest ( `/repos/${ this . owner } /${ this . repo } /releases/${ release . id } ` , this . token , null , "DELETE" )
177
+ return await githubRequest ( `/repos/${ this . info . owner } /${ this . info . repo } /releases/${ release . id } ` , this . token , null , "DELETE" )
173
178
}
174
179
catch ( e ) {
175
180
if ( e instanceof HttpError && ( e . response . statusCode === 405 || e . response . statusCode === 502 ) ) {
0 commit comments