Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54eb1e6

Browse files
SNOW-257781 adding more documentation and files necessary to build custom GH actions (snowflakedb#595)
1 parent 0c3e6c4 commit 54eb1e6

File tree

12 files changed

+8453
-2
lines changed

12 files changed

+8453
-2
lines changed

.github/actions/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/node_modules

.github/actions/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Vendored actions
2+
3+
## Purpose
4+
These modified GitHub actions have been included here because publicly
5+
existing versions of these actions are not quite good enough for us. A good
6+
example of this is our custom Jira fields, so we include a lot of Jira
7+
related actions.
8+
9+
## Updating actions
10+
First please see [the documentation](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions)
11+
for what different files do in a GitHub action.
12+
13+
*Note: The following steps were written for CentOS 7.*
14+
15+
1. Install ``nvm`` with ``sudo yum install nvm``.
16+
2. Install NodeJS 12 with ``nvm install v12``.
17+
3. Activate NodeJS 12 with ``nvm use v12``.
18+
4. Navigate to one of the directories from in this directory.
19+
5. Install the project with ``npm install``.
20+
6. Modify the source code as you see fit.
21+
7. Rebuild the dist folder with ``npm run-script build``.
22+
8. Check in new files.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Close a jira issue from a GH issue
2+
3+
Stolen from [gajira-transition](https://github.com/atlassian/gajira-transition) and modified to support required custom fields in use at snowflake.
4+
5+
Most of the dependencies (gajira-login, etc) have been folded into this action
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const { get } = require('lodash')
2+
3+
const serviceName = 'jira'
4+
const { format } = require('url')
5+
const client = require('./client')(serviceName)
6+
7+
class Jira {
8+
constructor ({ baseUrl, token, email }) {
9+
this.baseUrl = baseUrl
10+
this.token = token
11+
this.email = email
12+
}
13+
14+
async getIssue (issueId, query = {}) {
15+
const { fields = [], expand = [] } = query
16+
17+
try {
18+
return this.fetch('getIssue', {
19+
pathname: `/rest/api/2/issue/${issueId}`,
20+
query: {
21+
fields: fields.join(','),
22+
expand: expand.join(','),
23+
},
24+
})
25+
} catch (error) {
26+
if (get(error, 'res.status') === 404) {
27+
return
28+
}
29+
30+
throw error
31+
}
32+
}
33+
34+
async getIssueTransitions (issueId) {
35+
return this.fetch('getIssueTransitions', {
36+
pathname: `/rest/api/2/issue/${issueId}/transitions`,
37+
}, {
38+
method: 'GET',
39+
})
40+
}
41+
42+
async transitionIssue (issueId, data) {
43+
return this.fetch('transitionIssue', {
44+
pathname: `/rest/api/2/issue/${issueId}/transitions`,
45+
}, {
46+
method: 'POST',
47+
body: data,
48+
})
49+
}
50+
51+
async fetch (apiMethodName,
52+
{ host, pathname, query },
53+
{ method, body, headers = {} } = {}) {
54+
const url = format({
55+
host: host || this.baseUrl,
56+
pathname,
57+
query,
58+
})
59+
60+
if (!method) {
61+
method = 'GET'
62+
}
63+
64+
if (headers['Content-Type'] === undefined) {
65+
headers['Content-Type'] = 'application/json'
66+
}
67+
68+
if (headers.Authorization === undefined) {
69+
headers.Authorization = `Basic ${Buffer.from(`${this.email}:${this.token}`).toString('base64')}`
70+
}
71+
72+
// strong check for undefined
73+
// cause body variable can be 'false' boolean value
74+
if (body && headers['Content-Type'] === 'application/json') {
75+
body = JSON.stringify(body)
76+
}
77+
78+
const state = {
79+
req: {
80+
method,
81+
headers,
82+
body,
83+
url,
84+
},
85+
}
86+
87+
try {
88+
await client(state, `${serviceName}:${apiMethodName}`)
89+
} catch (error) {
90+
const fields = {
91+
originError: error,
92+
source: 'jira',
93+
}
94+
95+
delete state.req.headers
96+
97+
throw Object.assign(
98+
new Error('Jira API error'),
99+
state,
100+
fields
101+
)
102+
}
103+
104+
return state.res.body
105+
}
106+
}
107+
108+
module.exports = Jira
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fetch = require('node-fetch')
2+
// const moment = require('moment')
3+
4+
module.exports = serviceName => async (state, apiMethod = 'unknown') => {
5+
// const startTime = moment.now()
6+
7+
const response = await fetch(state.req.url, state.req)
8+
9+
state.res = {
10+
headers: response.headers.raw(),
11+
status: response.status,
12+
}
13+
14+
// const totalTime = moment.now() - startTime
15+
// const tags = {
16+
// api_method: apiMethod,
17+
// method: state.req.method || 'GET',
18+
// response_code: response.status,
19+
// service: serviceName,
20+
// }
21+
22+
state.res.body = await response.text()
23+
24+
const isJSON = (response.headers.get('content-type') || '').includes('application/json')
25+
26+
if (isJSON && state.res.body) {
27+
state.res.body = JSON.parse(state.res.body)
28+
}
29+
30+
if (!response.ok) {
31+
throw new Error(response.statusText)
32+
}
33+
34+
return state
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs')
2+
const YAML = require('yaml')
3+
const core = require('@actions/core')
4+
5+
const configPath = `${process.env.HOME}/jira/config.yml`
6+
const Action = require('./action')
7+
8+
// eslint-disable-next-line import/no-dynamic-require
9+
const githubEvent = require(process.env.GITHUB_EVENT_PATH)
10+
const config = YAML.parse(fs.readFileSync(configPath, 'utf8'))
11+
12+
async function exec () {
13+
try {
14+
const result = await new Action({
15+
githubEvent,
16+
argv: {issue: core.getInput('issue')},
17+
config,
18+
}).execute()
19+
20+
if (result) {
21+
const extendedConfig = Object.assign({}, config, result)
22+
23+
fs.writeFileSync(configPath, YAML.stringify(extendedConfig))
24+
25+
return
26+
}
27+
28+
console.log('Failed to transition issue.')
29+
process.exit(78)
30+
} catch (error) {
31+
console.error(error)
32+
process.exit(1)
33+
}
34+
}
35+
36+
exec()

0 commit comments

Comments
 (0)