Skip to content

Commit

Permalink
add CI changeset step to update jira issue with fixVersion (#13214)
Browse files Browse the repository at this point in the history
* add ci changeset step to update jira issue with fixVersion and labels based on tags

* fix with for env

* change core for testing

* add changeset

* add pnpm install and update working directory

* fix

* fix

* fix env

* remove test from core

* update trigger condition

* remove tags for now

* rename

* refactor

* add test to core

* reorder step

* update changeset content

* update log text

* add private:true to package.json
  • Loading branch information
momentmaker committed May 21, 2024
1 parent c14576a commit 921a015
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-seals-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

Add to CI changeset workflow an additional step to update the Jira issue associated with this PR and set the `fixVersions` for the issue with the upcoming core release version. #internal #wip
3 changes: 3 additions & 0 deletions .github/scripts/check-changeset-tags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fi
CHANGESET_FILE_PATH=$1
tags_list=( "#nops" "#added" "#changed" "#removed" "#updated" "#deprecation_notice" "#breaking_change" "#db_update" "#wip" "#bugfix" "#internal" )
has_tags=false
found_tags=()

if [[ ! -f "$CHANGESET_FILE_PATH" ]]; then
echo "Error: File '$CHANGESET_FILE_PATH' does not exist."
Expand All @@ -40,6 +41,7 @@ fi
while IFS= read -r line; do
for tag in "${tags_list[@]}"; do
if [[ "$line" == *"$tag"* ]]; then
found_tags+=("$tag")
echo "Found tag: $tag in $CHANGESET_FILE_PATH"
has_tags=true
fi
Expand All @@ -51,3 +53,4 @@ if [[ "$has_tags" == false ]]; then
fi

echo "has_tags=$has_tags" >> $GITHUB_OUTPUT
echo "found_tags=$(jq -jR 'split(" ") | join(",")' <<< "${found_tags[*]}")" >> $GITHUB_OUTPUT
15 changes: 15 additions & 0 deletions .github/scripts/jira/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "jira",
"version": "0.1.0",
"description": "Updates Jira issue with release information like the version and tags for a PR.",
"main": "update-jira-issue.js",
"type": "module",
"private": true,
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.1",
"node-fetch": "^2.7.0"
}
}
78 changes: 78 additions & 0 deletions .github/scripts/jira/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions .github/scripts/jira/update-jira-issue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env node

import * as core from "@actions/core";
import fetch from "node-fetch";

function parseIssueNumber(prTitle, commitMessage, branchName) {
const jiraIssueRegex = /[A-Z]{2,}-\d+/;
if (!!branchName && jiraIssueRegex.test(branchName.toUpperCase())) {
return branchName.toUpperCase().match(jiraIssueRegex)[0];
} else if (
!!commitMessage &&
jiraIssueRegex.test(commitMessage.toUpperCase())
) {
return commitMessage.toUpperCase().match(jiraIssueRegex)[0];
} else if (!!prTitle && jiraIssueRegex.test(prTitle.toUpperCase())) {
return prTitle.toUpperCase().match(jiraIssueRegex)[0];
} else {
return null;
}
}

function getLabels(tags) {
const labelPrefix = "core-release";
return tags.map((tag) => {
return {
add: `${labelPrefix}/${tag.substring(1)}`,
};
});
}

async function updateJiraIssue(
jiraHost,
jiraUserName,
jiraApiToken,
issueNumber,
tags,
fixVersionName
) {
const token = Buffer.from(`${jiraUserName}:${jiraApiToken}`).toString(
"base64"
);
const bodyData = {
update: {
labels: getLabels(tags),
fixVersions: [{ set: [{ name: fixVersionName }] }],
},
};

fetch(`https://${jiraHost}/rest/api/3/issue/${issueNumber}`, {
method: "PUT",
headers: {
Authorization: `Basic ${token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(bodyData),
})
.then((response) => {
console.log(`Response: ${JSON.stringify(response)}`);
return response.text();
})
.then((text) => console.log(text))
.catch((err) => console.error(err));
}

async function run() {
try {
const jiraHost = process.env.JIRA_HOST;
const jiraUserName = process.env.JIRA_USERNAME;
const jiraApiToken = process.env.JIRA_API_TOKEN;
const chainlinkVersion = process.env.CHAINLINK_VERSION;
const prTitle = process.env.PR_TITLE;
const commitMessage = process.env.COMMIT_MESSAGE;
const branchName = process.env.BRANCH_NAME;
// tags are not getting used at the current moment so will always default to []
const tags = process.env.FOUND_TAGS
? process.env.FOUND_TAGS.split(",")
: [];

// Check for the existence of JIRA_HOST and JIRA_USERNAME and JIRA_API_TOKEN
if (!jiraHost || !jiraUserName || !jiraApiToken) {
core.setFailed(
"Error: Missing required environment variables: JIRA_HOST and JIRA_USERNAME and JIRA_API_TOKEN."
);
return;
}

// Checks for the Jira issue number and exit if it can't find it
const issueNumber = parseIssueNumber(prTitle, commitMessage, branchName);
if (!issueNumber) {
core.info(
"No JIRA issue number found in: PR title, commit message, or branch name. Please include the issue ID in one of these."
);
core.notice(
"No JIRA issue number found in: PR title, commit message, or branch name. Please include the issue ID in one of these."
);
return;
}
const fixVersionName = `chainlink-v${chainlinkVersion}`;
await updateJiraIssue(
jiraHost,
jiraUserName,
jiraApiToken,
issueNumber,
tags,
fixVersionName
);
} catch (error) {
core.setFailed(error.message);
}
}

run();
51 changes: 45 additions & 6 deletions .github/workflows/changeset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ jobs:
contracts-changeset:
- added: 'contracts/.changeset/**'
- name: Check for changeset tags for core
id: changeset-tags
if: ${{ steps.files-changed.outputs.core-changeset == 'true' }}
shell: bash
run: bash ./.github/scripts/check-changeset-tags.sh ${{ steps.files-changed.outputs.core-changeset_files }}

- name: Setup pnpm
uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d # v3.0.0
if: ${{ steps.files-changed.outputs.core == 'true' || steps.files-changed.outputs.shared == 'true' }}
with:
version: ^8.0.0

- name: Setup node
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
if: ${{ steps.files-changed.outputs.core == 'true' || steps.files-changed.outputs.shared == 'true' }}
with:
node-version: 20
cache: pnpm
cache-dependency-path: ./pnpm-lock.yaml

- name: Get next chainlink core version
id: chainlink-version
if: ${{ steps.files-changed.outputs.core == 'true' || steps.files-changed.outputs.shared == 'true' }}
run: |
pnpm install && pnpm changeset version
echo "chainlink_version=$(jq -r '.version' package.json)" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Update Jira ticket for core
if: ${{ steps.files-changed.outputs.core == 'true' || steps.files-changed.outputs.shared == 'true' }}
shell: bash
working-directory: ./.github/scripts/jira
run: |
echo "COMMIT_MESSAGE=$(git log -1 --pretty=format:'%s')" >> $GITHUB_ENV
pnpm install && node update-jira-issue.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
JIRA_HOST: ${{ secrets.JIRA_HOST }}
JIRA_USERNAME: ${{ secrets.JIRA_USERNAME }}
JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
CHAINLINK_VERSION: ${{ steps.chainlink-version.outputs.chainlink_version }}
PR_TITLE: ${{ github.event.pull_request.title }}
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}

- name: Make a comment
uses: thollander/actions-comment-pull-request@fabd468d3a1a0b97feee5f6b9e499eab0dd903f6 # v2.5.0
if: ${{ steps.files-changed.outputs.core == 'true' || steps.files-changed.outputs.shared == 'true' }}
Expand Down Expand Up @@ -99,12 +144,6 @@ jobs:
echo "Please run pnpm changeset to add a changeset for contracts."
exit 1
- name: Check for changeset tags for core
id: changeset-tags
if: ${{ steps.files-changed.outputs.core-changeset == 'true' }}
shell: bash
run: bash ./.github/scripts/check-changeset-tags.sh ${{ steps.files-changed.outputs.core-changeset_files }}

- name: Make a comment
uses: thollander/actions-comment-pull-request@fabd468d3a1a0b97feee5f6b9e499eab0dd903f6 # v2.5.0
if: ${{ steps.files-changed.outputs.core-changeset == 'true' }}
Expand Down

0 comments on commit 921a015

Please sign in to comment.