-
Notifications
You must be signed in to change notification settings - Fork 0
/
addlabel.js
103 lines (94 loc) · 3.43 KB
/
addlabel.js
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
//const btoa = require('btoa');
//var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
import axios from 'axios';
import fetch from 'node-fetch';
const githubUrl = 'https://api.github.com/repos/SiyaaJhawar/action/commits/7ba17fe7086423a30485d2949cf32255bc2c479d/comments';
const jiraUsername = process.env.JIRA_USERNAME;
const jiraapitoken = process.env.JIRA_API_TOKEN;
const username = process.env.GITHUB_USERNAME;
const password = process.env.GITHUB_API_TOKEN;
const defectRegex = /([A-Z]{1}[A-Z]{2,})-\d+/g;
async function compareCommitCommentWithJiraIssue() {
try {
const encodedCredentials = Buffer.from(`${username}:${password}`).toString('base64');
const commitsResponse = await axios.get(githubUrl, {
headers: {
"Authorization": `Basic ${encodedCredentials}`,
"Accept": "application/json"
}
});
const commentTexts = commitsResponse.data.map(comment => comment.body);
const defectIds = commentTexts.flatMap(text => text.match(defectRegex));
console.log(`Found the following defect IDs in commit comments: ${defectIds}`);
console.log(`Username: ${jiraUsername}`);
console.log(`Apitoken: ${jiraapitoken}`);
fetch('https://swgup.atlassian.net/rest/api/3/search?filter=allissues', {
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from(
`${jiraUsername}:${jiraapitoken}`
).toString('base64')}`,
'Accept': 'application/json'
}
})
.then(response => {
console.log(
`Response: ${response.status} ${response.statusText}`
);
return response.json(); // Parse the response as JSON
})
.then(data => {
if (data.issues && data.issues.length > 0) {
const issueKeys = data.issues.map(issue => issue.key); // Extract the keys of all the issues
console.log(`Found the following issue keys: ${issueKeys.join(', ')}`);
// Check if any of the issue keys match a defect ID
const matchingIssueKeys = issueKeys.filter(issueKey => {
const regex = new RegExp(`(${defectIds.join('|')})`);
return regex.test(issueKey);
});
console.log(`Found matching issue keys: ${matchingIssueKeys.join(', ')}`);
// Add label to the matching issues
matchingIssueKeys.forEach(issueKey => {
fetch(`https://swgup.atlassian.net/rest/api/2/issue/${issueKey}`, {
method: 'PUT',
headers: {
'Authorization': `Basic ${Buffer.from(
`${jiraUsername}:${jiraapitoken}`
).toString('base64')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
"update": {
"labels": [
{
"add": "int-deploy"
}
]
}
})
}) .then(response => {
console.log(
`Response: ${response.status} ${response.statusText}`
);
if (response.ok) {
console.log(`Added label to issue ${issueKey}.`);
} else {
console.log(`Failed to add label to issue ${issueKey}.`);
}
})
.catch(error => {
console.error(`Error adding label to issue ${issueKey}:`, error);
});
});
} else {
console.log('No issues found in response.');
}
})
.catch(error => {
console.error('Error fetching issues:', error);
});
} catch (error) {
console.error(error);
}
}
compareCommitCommentWithJiraIssue();